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

     1  // Copyright 2020 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/tidbver"
    24  	"github.com/pingcap/tiup/pkg/utils"
    25  )
    26  
    27  // TiCDC represent a ticdc instance.
    28  type TiCDC struct {
    29  	instance
    30  	pds []*PDInstance
    31  	Process
    32  }
    33  
    34  var _ Instance = &TiCDC{}
    35  
    36  // NewTiCDC create a TiCDC instance.
    37  func NewTiCDC(binPath string, dir, host, configPath string, id int, port int, pds []*PDInstance) *TiCDC {
    38  	if port <= 0 {
    39  		port = 8300
    40  	}
    41  	ticdc := &TiCDC{
    42  		instance: instance{
    43  			BinPath:    binPath,
    44  			ID:         id,
    45  			Dir:        dir,
    46  			Host:       host,
    47  			Port:       utils.MustGetFreePort(host, port),
    48  			ConfigPath: configPath,
    49  		},
    50  		pds: pds,
    51  	}
    52  	ticdc.StatusPort = ticdc.Port
    53  	return ticdc
    54  }
    55  
    56  // Start implements Instance interface.
    57  func (c *TiCDC) Start(ctx context.Context, version utils.Version) error {
    58  	endpoints := pdEndpoints(c.pds, true)
    59  
    60  	args := []string{
    61  		"server",
    62  		fmt.Sprintf("--addr=%s", utils.JoinHostPort(c.Host, c.Port)),
    63  		fmt.Sprintf("--advertise-addr=%s", utils.JoinHostPort(AdvertiseHost(c.Host), c.Port)),
    64  		fmt.Sprintf("--pd=%s", strings.Join(endpoints, ",")),
    65  		fmt.Sprintf("--log-file=%s", c.LogFile()),
    66  	}
    67  	clusterVersion := string(version)
    68  	if tidbver.TiCDCSupportConfigFile(clusterVersion) {
    69  		if c.ConfigPath != "" {
    70  			args = append(args, fmt.Sprintf("--config=%s", c.ConfigPath))
    71  		}
    72  		if tidbver.TiCDCSupportDataDir(clusterVersion) {
    73  			args = append(args, fmt.Sprintf("--data-dir=%s", filepath.Join(c.Dir, "data")))
    74  		} else {
    75  			args = append(args, fmt.Sprintf("--sort-dir=%s/tmp/sorter", filepath.Join(c.Dir, "data")))
    76  		}
    77  	}
    78  
    79  	var err error
    80  	if c.BinPath, err = tiupexec.PrepareBinary("cdc", version, c.BinPath); err != nil {
    81  		return err
    82  	}
    83  	c.Process = &process{cmd: PrepareCommand(ctx, c.BinPath, args, nil, c.Dir)}
    84  
    85  	logIfErr(c.Process.SetOutputFile(c.LogFile()))
    86  	return c.Process.Start()
    87  }
    88  
    89  // Component return component name.
    90  func (c *TiCDC) Component() string {
    91  	return "cdc"
    92  }
    93  
    94  // LogFile return the log file.
    95  func (c *TiCDC) LogFile() string {
    96  	return filepath.Join(c.Dir, "ticdc.log")
    97  }