From b3f4565aa41135144bca50678f53cac5b85c9493 Mon Sep 17 00:00:00 2001 From: Jiahao Huang Date: Thu, 3 Sep 2020 13:29:53 +0800 Subject: [PATCH 1/2] Dump dns & port file at data dir Playground duump dsn&port file at wd. After we changed not to set wd as instance data dir, playground dump this file at wd but not instance data dir(not same dir like before). This broke playground subcommand and "tiup client", which search this files at ~/.tiup/data/../ --- components/playground/main.go | 17 +++++++++++------ components/playground/playground.go | 18 ++++++++---------- tests/tiup-playground/test_playground.sh | 4 ---- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/components/playground/main.go b/components/playground/main.go index 87df748b30..ed30625421 100644 --- a/components/playground/main.go +++ b/components/playground/main.go @@ -115,12 +115,17 @@ Examples: opt.version = args[0] } + dataDir := os.Getenv(localdata.EnvNameInstanceDataDir) + if dataDir == "" { + return errors.Errorf("cannot read environment variable %s", localdata.EnvNameInstanceDataDir) + } + port, err := utils.GetFreePort("0.0.0.0", 9527) if err != nil { return errors.AddStack(err) } - err = dumpPort(port) - p := NewPlayground(port) + err = dumpPort(filepath.Join(dataDir, "port"), port) + p := NewPlayground(dataDir, port) if err != nil { return errors.AddStack(err) } @@ -321,8 +326,8 @@ func getAbsolutePath(path string) (string, error) { return absPath, nil } -func dumpPort(port int) error { - return ioutil.WriteFile("port", []byte(strconv.Itoa(port)), 0644) +func dumpPort(fname string, port int) error { + return ioutil.WriteFile(fname, []byte(strconv.Itoa(port)), 0644) } func loadPort(dir string) (port int, err error) { @@ -335,12 +340,12 @@ func loadPort(dir string) (port int, err error) { return } -func dumpDSN(dbs []*instance.TiDBInstance) { +func dumpDSN(fname string, dbs []*instance.TiDBInstance) { var dsn []string for _, db := range dbs { dsn = append(dsn, fmt.Sprintf("mysql://root@%s", db.Addr())) } - _ = ioutil.WriteFile("dsn", []byte(strings.Join(dsn, "\n")), 0644) + _ = ioutil.WriteFile(fname, []byte(strings.Join(dsn, "\n")), 0644) } func newEtcdClient(endpoint string) (*clientv3.Client, error) { diff --git a/components/playground/playground.go b/components/playground/playground.go index cb34cd7b83..0a49701d11 100755 --- a/components/playground/playground.go +++ b/components/playground/playground.go @@ -38,7 +38,6 @@ import ( "github.com/pingcap/tiup/pkg/cliutil/progress" "github.com/pingcap/tiup/pkg/cluster/api" "github.com/pingcap/tiup/pkg/environment" - "github.com/pingcap/tiup/pkg/localdata" "github.com/pingcap/tiup/pkg/repository/v0manifest" "github.com/pingcap/tiup/pkg/utils" "golang.org/x/mod/semver" @@ -50,7 +49,8 @@ const forceKillAfterDuration = time.Second * 10 // Playground represent the playground of a cluster. type Playground struct { - booted bool + dataDir string + booted bool // the latest receive signal curSig int32 bootOptions *bootOptions @@ -82,8 +82,9 @@ type MonitorInfo struct { } // NewPlayground create a Playground instance. -func NewPlayground(port int) *Playground { +func NewPlayground(dataDir string, port int) *Playground { return &Playground{ + dataDir: dataDir, port: port, idAlloc: make(map[string]int), } @@ -582,10 +583,7 @@ func (p *Playground) addInstance(componentID string, cfg instance.Config) (ins i } } - dataDir := os.Getenv(localdata.EnvNameInstanceDataDir) - if dataDir == "" { - return nil, fmt.Errorf("cannot read environment variable %s", localdata.EnvNameInstanceDataDir) - } + dataDir := p.dataDir id := p.allocID(componentID) dir := filepath.Join(dataDir, fmt.Sprintf("%s-%d", componentID, id)) @@ -873,7 +871,7 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme } } - dumpDSN(p.tidbs) + dumpDSN(filepath.Join(p.dataDir, "dsn"), p.tidbs) go func() { // fmt.Printf("serve at :%d\n", p.port) @@ -980,7 +978,7 @@ func (p *Playground) bootMonitor(ctx context.Context, env *environment.Environme options := p.bootOptions monitorInfo := &MonitorInfo{} - dataDir := os.Getenv(localdata.EnvNameInstanceDataDir) + dataDir := p.dataDir promDir := filepath.Join(dataDir, "prometheus") monitor, err := newMonitor(ctx, options.version, options.host, promDir) @@ -1021,7 +1019,7 @@ func (p *Playground) bootGrafana(ctx context.Context, env *environment.Environme return nil, errors.AddStack(err) } - dataDir := os.Getenv(localdata.EnvNameInstanceDataDir) + dataDir := p.dataDir grafanaDir := filepath.Join(dataDir, "grafana") cmd := exec.Command("cp", "-r", installPath, grafanaDir) diff --git a/tests/tiup-playground/test_playground.sh b/tests/tiup-playground/test_playground.sh index 74340027bc..38a840d5d2 100755 --- a/tests/tiup-playground/test_playground.sh +++ b/tests/tiup-playground/test_playground.sh @@ -56,10 +56,6 @@ trap "kill_all > /dev/null 2>&1" EXIT # wait start cluster successfully timeout 300 grep -q "CLUSTER START SUCCESSFULLY" <(tail -f $outfile) -# playground dump this at the wd -sleep 3 -mv ./dsn $TIUP_INSTANCE_DATA_DIR/ -mv ./port $TIUP_INSTANCE_DATA_DIR/ tiup-playground display | grep -qv "exit" tiup-playground scale-out --db 2 From e0f479526f797d2247fe83b4bb8c4a92e3364353 Mon Sep 17 00:00:00 2001 From: Jiahao Huang Date: Thu, 3 Sep 2020 13:29:53 +0800 Subject: [PATCH 2/2] Prepare instacne dir in test --- tests/tiup-playground/test_playground.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/tiup-playground/test_playground.sh b/tests/tiup-playground/test_playground.sh index 38a840d5d2..d22500f98d 100755 --- a/tests/tiup-playground/test_playground.sh +++ b/tests/tiup-playground/test_playground.sh @@ -15,6 +15,7 @@ curl https://tiup-mirrors.pingcap.com/root.json -o $TMP_DIR/home/bin/root.json rm -rf $TIUP_HOME/data mkdir -p $TIUP_HOME/data export TIUP_INSTANCE_DATA_DIR=$TIUP_HOME/data/test_play +mkdir $TIUP_INSTANCE_DATA_DIR mkdir -p $TEST_DIR/cover