github.com/pingcap/tiup@v1.15.1/components/playground/instance/tiflash_pre7.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 "bytes" 18 "context" 19 "encoding/json" 20 "fmt" 21 "path" 22 "path/filepath" 23 "time" 24 25 "github.com/BurntSushi/toml" 26 "github.com/pingcap/errors" 27 "github.com/pingcap/tiup/pkg/cluster/api" 28 "github.com/pingcap/tiup/pkg/cluster/spec" 29 tiupexec "github.com/pingcap/tiup/pkg/exec" 30 "github.com/pingcap/tiup/pkg/utils" 31 ) 32 33 func getFlashClusterPath(dir string) string { 34 return fmt.Sprintf("%s/flash_cluster_manager", dir) 35 } 36 37 type scheduleConfig struct { 38 LowSpaceRatio float64 `json:"low-space-ratio"` 39 } 40 41 type replicateMaxReplicaConfig struct { 42 MaxReplicas int `json:"max-replicas"` 43 } 44 45 type replicateEnablePlacementRulesConfig struct { 46 EnablePlacementRules string `json:"enable-placement-rules"` 47 } 48 49 // startOld is for < 7.1.0. Not maintained any more. Do not introduce new features. 50 func (inst *TiFlashInstance) startOld(ctx context.Context, version utils.Version) error { 51 endpoints := pdEndpoints(inst.pds, false) 52 53 tidbStatusAddrs := make([]string, 0, len(inst.dbs)) 54 for _, db := range inst.dbs { 55 tidbStatusAddrs = append(tidbStatusAddrs, utils.JoinHostPort(AdvertiseHost(db.Host), db.StatusPort)) 56 } 57 wd, err := filepath.Abs(inst.Dir) 58 if err != nil { 59 return err 60 } 61 62 // Wait for PD 63 pdClient := api.NewPDClient(ctx, endpoints, 10*time.Second, nil) 64 // set low-space-ratio to 1 to avoid low disk space 65 lowSpaceRatio, err := json.Marshal(scheduleConfig{ 66 LowSpaceRatio: 0.99, 67 }) 68 if err != nil { 69 return err 70 } 71 if err = pdClient.UpdateScheduleConfig(bytes.NewBuffer(lowSpaceRatio)); err != nil { 72 return err 73 } 74 // Update maxReplicas before placement rules so that it would not be overwritten 75 maxReplicas, err := json.Marshal(replicateMaxReplicaConfig{ 76 MaxReplicas: 1, 77 }) 78 if err != nil { 79 return err 80 } 81 if err = pdClient.UpdateReplicateConfig(bytes.NewBuffer(maxReplicas)); err != nil { 82 return err 83 } 84 // Set enable-placement-rules to allow TiFlash work properly 85 enablePlacementRules, err := json.Marshal(replicateEnablePlacementRulesConfig{ 86 EnablePlacementRules: "true", 87 }) 88 if err != nil { 89 return err 90 } 91 if err = pdClient.UpdateReplicateConfig(bytes.NewBuffer(enablePlacementRules)); err != nil { 92 return err 93 } 94 95 if inst.BinPath, err = tiupexec.PrepareBinary("tiflash", version, inst.BinPath); err != nil { 96 return err 97 } 98 99 dirPath := filepath.Dir(inst.BinPath) 100 clusterManagerPath := getFlashClusterPath(dirPath) 101 if err = inst.checkConfigOld(wd, clusterManagerPath, version, tidbStatusAddrs, endpoints); err != nil { 102 return err 103 } 104 105 args := []string{ 106 "server", 107 fmt.Sprintf("--config-file=%s", inst.ConfigPath), 108 } 109 envs := []string{ 110 fmt.Sprintf("LD_LIBRARY_PATH=%s:$LD_LIBRARY_PATH", dirPath), 111 } 112 inst.Process = &process{cmd: PrepareCommand(ctx, inst.BinPath, args, envs, inst.Dir)} 113 114 logIfErr(inst.Process.SetOutputFile(inst.LogFile())) 115 return inst.Process.Start() 116 } 117 118 // checkConfigOld is for < 7.1.0. Not maintained any more. Do not introduce new features. 119 func (inst *TiFlashInstance) checkConfigOld(deployDir, clusterManagerPath string, 120 version utils.Version, tidbStatusAddrs, endpoints []string) (err error) { 121 if err := utils.MkdirAll(inst.Dir, 0755); err != nil { 122 return errors.Trace(err) 123 } 124 125 var ( 126 flashBuf = new(bytes.Buffer) 127 proxyBuf = new(bytes.Buffer) 128 129 flashCfgPath = path.Join(inst.Dir, "tiflash.toml") 130 proxyCfgPath = path.Join(inst.Dir, "tiflash-learner.toml") 131 ) 132 133 defer func() { 134 if err != nil { 135 return 136 } 137 if err = utils.WriteFile(flashCfgPath, flashBuf.Bytes(), 0644); err != nil { 138 return 139 } 140 141 if err = utils.WriteFile(proxyCfgPath, proxyBuf.Bytes(), 0644); err != nil { 142 return 143 } 144 145 inst.ConfigPath = flashCfgPath 146 }() 147 148 // Write default config to buffer 149 if err := writeTiFlashConfigOld(flashBuf, version, inst.TCPPort, inst.Port, inst.ServicePort, inst.StatusPort, 150 inst.Host, deployDir, clusterManagerPath, tidbStatusAddrs, endpoints); err != nil { 151 return errors.Trace(err) 152 } 153 if err := writeTiFlashProxyConfigOld(proxyBuf, version, inst.Host, deployDir, 154 inst.ServicePort, inst.ProxyPort, inst.ProxyStatusPort); err != nil { 155 return errors.Trace(err) 156 } 157 158 if inst.ConfigPath == "" { 159 return 160 } 161 162 cfg, err := unmarshalConfig(inst.ConfigPath) 163 if err != nil { 164 return errors.Trace(err) 165 } 166 proxyPath := getTiFlashProxyConfigPathOld(cfg) 167 if proxyPath != "" { 168 proxyCfg, err := unmarshalConfig(proxyPath) 169 if err != nil { 170 return errors.Trace(err) 171 } 172 err = overwriteBuf(proxyBuf, proxyCfg) 173 if err != nil { 174 return errors.Trace(err) 175 } 176 } 177 178 // Always use the tiflash proxy config file in the instance directory 179 setTiFlashProxyConfigPathOld(cfg, proxyCfgPath) 180 return errors.Trace(overwriteBuf(flashBuf, cfg)) 181 } 182 183 func overwriteBuf(buf *bytes.Buffer, overwrite map[string]any) (err error) { 184 cfg := make(map[string]any) 185 if err = toml.Unmarshal(buf.Bytes(), &cfg); err != nil { 186 return 187 } 188 buf.Reset() 189 return toml.NewEncoder(buf).Encode(spec.MergeConfig(cfg, overwrite)) 190 }