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