github.com/pingcap/tiup@v1.15.1/components/playground/instance/tidb.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  	"strconv"
    21  	"strings"
    22  
    23  	tiupexec "github.com/pingcap/tiup/pkg/exec"
    24  	"github.com/pingcap/tiup/pkg/utils"
    25  )
    26  
    27  // TiDBInstance represent a running tidb-server
    28  type TiDBInstance struct {
    29  	instance
    30  	pds []*PDInstance
    31  	Process
    32  	tiproxyCertDir string
    33  	enableBinlog   bool
    34  	isCSEMode      bool
    35  }
    36  
    37  // NewTiDBInstance return a TiDBInstance
    38  func NewTiDBInstance(binPath string, dir, host, configPath string, id, port int, pds []*PDInstance, tiproxyCertDir string, enableBinlog bool, isCSEMode bool) *TiDBInstance {
    39  	if port <= 0 {
    40  		port = 4000
    41  	}
    42  	return &TiDBInstance{
    43  		instance: instance{
    44  			BinPath:    binPath,
    45  			ID:         id,
    46  			Dir:        dir,
    47  			Host:       host,
    48  			Port:       utils.MustGetFreePort(host, port),
    49  			StatusPort: utils.MustGetFreePort("0.0.0.0", 10080),
    50  			ConfigPath: configPath,
    51  		},
    52  		tiproxyCertDir: tiproxyCertDir,
    53  		pds:            pds,
    54  		enableBinlog:   enableBinlog,
    55  		isCSEMode:      isCSEMode,
    56  	}
    57  }
    58  
    59  // Start calls set inst.cmd and Start
    60  func (inst *TiDBInstance) Start(ctx context.Context, version utils.Version) error {
    61  	configPath := filepath.Join(inst.Dir, "tidb.toml")
    62  	if err := prepareConfig(
    63  		configPath,
    64  		inst.ConfigPath,
    65  		inst.getConfig(),
    66  	); err != nil {
    67  		return err
    68  	}
    69  
    70  	endpoints := pdEndpoints(inst.pds, false)
    71  
    72  	args := []string{
    73  		"-P", strconv.Itoa(inst.Port),
    74  		"--store=tikv",
    75  		fmt.Sprintf("--host=%s", inst.Host),
    76  		fmt.Sprintf("--status=%d", inst.StatusPort),
    77  		fmt.Sprintf("--path=%s", strings.Join(endpoints, ",")),
    78  		fmt.Sprintf("--log-file=%s", filepath.Join(inst.Dir, "tidb.log")),
    79  		fmt.Sprintf("--config=%s", configPath),
    80  	}
    81  	if inst.enableBinlog {
    82  		args = append(args, "--enable-binlog=true")
    83  	}
    84  
    85  	var err error
    86  	if inst.BinPath, err = tiupexec.PrepareBinary("tidb", version, inst.BinPath); err != nil {
    87  		return err
    88  	}
    89  	inst.Process = &process{cmd: PrepareCommand(ctx, inst.BinPath, args, nil, inst.Dir)}
    90  
    91  	logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
    92  	return inst.Process.Start()
    93  }
    94  
    95  // Component return the component name.
    96  func (inst *TiDBInstance) Component() string {
    97  	return "tidb"
    98  }
    99  
   100  // LogFile return the log file name.
   101  func (inst *TiDBInstance) LogFile() string {
   102  	return filepath.Join(inst.Dir, "tidb.log")
   103  }
   104  
   105  // Addr return the listen address of TiDB
   106  func (inst *TiDBInstance) Addr() string {
   107  	return utils.JoinHostPort(AdvertiseHost(inst.Host), inst.Port)
   108  }