github.com/pingcap/tiup@v1.15.1/components/playground/instance/tikv_cdc.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package instance
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"path/filepath"
    20  	"strings"
    21  
    22  	tiupexec "github.com/pingcap/tiup/pkg/exec"
    23  	"github.com/pingcap/tiup/pkg/utils"
    24  )
    25  
    26  // TiKVCDC represent a TiKV-CDC instance.
    27  type TiKVCDC struct {
    28  	instance
    29  	pds []*PDInstance
    30  	Process
    31  }
    32  
    33  var _ Instance = &TiKVCDC{}
    34  
    35  // NewTiKVCDC create a TiKVCDC instance.
    36  func NewTiKVCDC(binPath string, dir, host, configPath string, id int, pds []*PDInstance) *TiKVCDC {
    37  	tikvCdc := &TiKVCDC{
    38  		instance: instance{
    39  			BinPath:    binPath,
    40  			ID:         id,
    41  			Dir:        dir,
    42  			Host:       host,
    43  			Port:       utils.MustGetFreePort(host, 8600),
    44  			ConfigPath: configPath,
    45  		},
    46  		pds: pds,
    47  	}
    48  	tikvCdc.StatusPort = tikvCdc.Port
    49  	return tikvCdc
    50  }
    51  
    52  // Start implements Instance interface.
    53  func (c *TiKVCDC) Start(ctx context.Context, version utils.Version) error {
    54  	endpoints := pdEndpoints(c.pds, true)
    55  
    56  	args := []string{
    57  		"server",
    58  		fmt.Sprintf("--addr=%s", utils.JoinHostPort(c.Host, c.Port)),
    59  		fmt.Sprintf("--advertise-addr=%s", utils.JoinHostPort(AdvertiseHost(c.Host), c.Port)),
    60  		fmt.Sprintf("--pd=%s", strings.Join(endpoints, ",")),
    61  		fmt.Sprintf("--log-file=%s", c.LogFile()),
    62  		fmt.Sprintf("--data-dir=%s", filepath.Join(c.Dir, "data")),
    63  	}
    64  	if c.ConfigPath != "" {
    65  		args = append(args, fmt.Sprintf("--config=%s", c.ConfigPath))
    66  	}
    67  
    68  	var err error
    69  	if c.BinPath, err = tiupexec.PrepareBinary("tikv-cdc", version, c.BinPath); err != nil {
    70  		return err
    71  	}
    72  	c.Process = &process{cmd: PrepareCommand(ctx, c.BinPath, args, nil, c.Dir)}
    73  
    74  	logIfErr(c.Process.SetOutputFile(c.LogFile()))
    75  	return c.Process.Start()
    76  }
    77  
    78  // Component return component name.
    79  func (c *TiKVCDC) Component() string {
    80  	return "tikv-cdc"
    81  }
    82  
    83  // LogFile return the log file.
    84  func (c *TiKVCDC) LogFile() string {
    85  	return filepath.Join(c.Dir, "tikv_cdc.log")
    86  }