diff --git a/pkg/cluster/manager/display.go b/pkg/cluster/manager/display.go index 1a042ce327..e487137761 100644 --- a/pkg/cluster/manager/display.go +++ b/pkg/cluster/manager/display.go @@ -24,6 +24,7 @@ import ( "sort" "strconv" "strings" + "sync" "time" "github.com/fatih/color" @@ -342,12 +343,16 @@ func (m *Manager) DisplayTiKVLabels(name string, opt operator.Options) error { if err != nil { return err } + + var mu sync.Mutex topo.IterInstance(func(ins spec.Instance) { if ins.ComponentName() == spec.ComponentPD { status := ins.Status(ctx, statusTimeout, tlsCfg, masterList...) if strings.HasPrefix(status, "Up") || strings.HasPrefix(status, "Healthy") { instAddr := fmt.Sprintf("%s:%d", ins.GetHost(), ins.GetPort()) + mu.Lock() masterActive = append(masterActive, instAddr) + mu.Unlock() } } }, opt.Concurrency) @@ -462,6 +467,7 @@ func (m *Manager) GetClusterTopology(name string, opt operator.Options) ([]InstI masterActive := make([]string, 0) masterStatus := make(map[string]string) + var mu sync.Mutex topo.IterInstance(func(ins spec.Instance) { if ins.ComponentName() != spec.ComponentPD && ins.ComponentName() != spec.ComponentDMMaster { return @@ -470,7 +476,9 @@ func (m *Manager) GetClusterTopology(name string, opt operator.Options) ([]InstI status := ins.Status(ctx, statusTimeout, tlsCfg, masterList...) if strings.HasPrefix(status, "Up") || strings.HasPrefix(status, "Healthy") { instAddr := fmt.Sprintf("%s:%d", ins.GetHost(), ins.GetPort()) + mu.Lock() masterActive = append(masterActive, instAddr) + mu.Unlock() } masterStatus[ins.ID()] = status }, opt.Concurrency) @@ -544,6 +552,7 @@ func (m *Manager) GetClusterTopology(name string, opt operator.Options) ([]InstI if ins.IsPatched() { roleName += " (patched)" } + mu.Lock() clusterInstInfos = append(clusterInstInfos, InstInfo{ ID: ins.ID(), Role: roleName, @@ -557,6 +566,7 @@ func (m *Manager) GetClusterTopology(name string, opt operator.Options) ([]InstI Port: ins.GetPort(), Since: since, }) + mu.Unlock() }, opt.Concurrency) // Sort by role,host,ports