github.com/pingcap/tiup@v1.15.1/components/playground/instance/tikv.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  	"time"
    22  
    23  	"github.com/pingcap/tiup/pkg/cluster/api"
    24  	tiupexec "github.com/pingcap/tiup/pkg/exec"
    25  	"github.com/pingcap/tiup/pkg/utils"
    26  )
    27  
    28  // TiKVInstance represent a running tikv-server
    29  type TiKVInstance struct {
    30  	instance
    31  	pds  []*PDInstance
    32  	tsos []*PDInstance
    33  	Process
    34  	isCSEMode  bool
    35  	cseOpts    CSEOptions
    36  	isPDMSMode bool
    37  }
    38  
    39  // NewTiKVInstance return a TiKVInstance
    40  func NewTiKVInstance(binPath string, dir, host, configPath string, id int, port int, pds []*PDInstance, tsos []*PDInstance, isCSEMode bool, cseOptions CSEOptions, isPDMSMode bool) *TiKVInstance {
    41  	if port <= 0 {
    42  		port = 20160
    43  	}
    44  	return &TiKVInstance{
    45  		instance: instance{
    46  			BinPath:    binPath,
    47  			ID:         id,
    48  			Dir:        dir,
    49  			Host:       host,
    50  			Port:       utils.MustGetFreePort(host, port),
    51  			StatusPort: utils.MustGetFreePort(host, 20180),
    52  			ConfigPath: configPath,
    53  		},
    54  		pds:        pds,
    55  		tsos:       tsos,
    56  		isCSEMode:  isCSEMode,
    57  		cseOpts:    cseOptions,
    58  		isPDMSMode: isPDMSMode,
    59  	}
    60  }
    61  
    62  // Addr return the address of tikv.
    63  func (inst *TiKVInstance) Addr() string {
    64  	return utils.JoinHostPort(inst.Host, inst.Port)
    65  }
    66  
    67  // Start calls set inst.cmd and Start
    68  func (inst *TiKVInstance) Start(ctx context.Context, version utils.Version) error {
    69  	configPath := filepath.Join(inst.Dir, "tikv.toml")
    70  	if err := prepareConfig(
    71  		configPath,
    72  		inst.ConfigPath,
    73  		inst.getConfig(),
    74  	); err != nil {
    75  		return err
    76  	}
    77  
    78  	// Need to check tso status
    79  	if inst.isPDMSMode {
    80  		var tsoEnds []string
    81  		for _, pd := range inst.tsos {
    82  			tsoEnds = append(tsoEnds, fmt.Sprintf("%s:%d", AdvertiseHost(pd.Host), pd.StatusPort))
    83  		}
    84  		pdcli := api.NewPDClient(ctx,
    85  			tsoEnds, 10*time.Second, nil,
    86  		)
    87  		if err := pdcli.CheckTSOHealth(&utils.RetryOption{
    88  			Delay:   time.Second * 5,
    89  			Timeout: time.Second * 300,
    90  		}); err != nil {
    91  			return err
    92  		}
    93  	}
    94  
    95  	endpoints := pdEndpoints(inst.pds, true)
    96  	args := []string{
    97  		fmt.Sprintf("--addr=%s", utils.JoinHostPort(inst.Host, inst.Port)),
    98  		fmt.Sprintf("--advertise-addr=%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.Port)),
    99  		fmt.Sprintf("--status-addr=%s", utils.JoinHostPort(inst.Host, inst.StatusPort)),
   100  		fmt.Sprintf("--pd-endpoints=%s", strings.Join(endpoints, ",")),
   101  		fmt.Sprintf("--config=%s", configPath),
   102  		fmt.Sprintf("--data-dir=%s", filepath.Join(inst.Dir, "data")),
   103  		fmt.Sprintf("--log-file=%s", inst.LogFile()),
   104  	}
   105  
   106  	envs := []string{"MALLOC_CONF=prof:true,prof_active:false"}
   107  	var err error
   108  	if inst.BinPath, err = tiupexec.PrepareBinary("tikv", version, inst.BinPath); err != nil {
   109  		return err
   110  	}
   111  	inst.Process = &process{cmd: PrepareCommand(ctx, inst.BinPath, args, envs, inst.Dir)}
   112  
   113  	logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
   114  	return inst.Process.Start()
   115  }
   116  
   117  // Component return the component name.
   118  func (inst *TiKVInstance) Component() string {
   119  	return "tikv"
   120  }
   121  
   122  // LogFile return the log file name.
   123  func (inst *TiKVInstance) LogFile() string {
   124  	return filepath.Join(inst.Dir, "tikv.log")
   125  }
   126  
   127  // StoreAddr return the store address of TiKV
   128  func (inst *TiKVInstance) StoreAddr() string {
   129  	return utils.JoinHostPort(AdvertiseHost(inst.Host), inst.Port)
   130  }