File: display.go

package info (click to toggle)
tiup 1.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,384 kB
  • sloc: sh: 1,988; makefile: 138; sql: 16
file content (852 lines) | stat: -rw-r--r-- 25,310 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package manager

import (
	"context"
	"crypto/tls"
	"encoding/json"
	"errors"
	"fmt"
	"math"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/fatih/color"
	perrs "github.com/pingcap/errors"
	"github.com/pingcap/tiup/pkg/checkpoint"
	"github.com/pingcap/tiup/pkg/cluster/api"
	"github.com/pingcap/tiup/pkg/cluster/clusterutil"
	"github.com/pingcap/tiup/pkg/cluster/ctxt"
	"github.com/pingcap/tiup/pkg/cluster/executor"
	operator "github.com/pingcap/tiup/pkg/cluster/operation"
	"github.com/pingcap/tiup/pkg/cluster/spec"
	"github.com/pingcap/tiup/pkg/crypto"
	logprinter "github.com/pingcap/tiup/pkg/logger/printer"
	"github.com/pingcap/tiup/pkg/meta"
	"github.com/pingcap/tiup/pkg/set"
	"github.com/pingcap/tiup/pkg/tui"
	"github.com/pingcap/tiup/pkg/utils"
)

// DisplayOption represents option of display command
type DisplayOption struct {
	ClusterName    string
	ShowUptime     bool
	ShowProcess    bool
	ShowManageHost bool
	ShowNuma       bool
	ShowVersions   bool
}

// InstInfo represents an instance info
type InstInfo struct {
	ID          string `json:"id"`
	Role        string `json:"role"`
	Host        string `json:"host"`
	ManageHost  string `json:"manage_host"`
	Ports       string `json:"ports"`
	OsArch      string `json:"os_arch"`
	Status      string `json:"status"`
	Memory      string `json:"memory"`
	MemoryLimit string `json:"memory_limit"`
	CPUquota    string `json:"cpu_quota"`
	Since       string `json:"since"`
	DataDir     string `json:"data_dir"`
	DeployDir   string `json:"deploy_dir"`
	NumaNode    string `json:"numa_node"`
	NumaCores   string `json:"numa_cores"`
	Version     string `json:"version"`

	ComponentName string
	Port          int
}

// LabelInfo represents an instance label info
type LabelInfo struct {
	Machine   string `json:"machine"`
	Port      string `json:"port"`
	Store     string `json:"store"`
	Status    string `json:"status"`
	Leaders   string `json:"leaders"`
	Regions   string `json:"regions"`
	Capacity  string `json:"capacity"`
	Available string `json:"available"`
	Labels    string `json:"labels"`
}

// ClusterMetaInfo hold the structure for the JSON output of the dashboard info
type ClusterMetaInfo struct {
	ClusterType    string   `json:"cluster_type"`
	ClusterName    string   `json:"cluster_name"`
	ClusterVersion string   `json:"cluster_version"`
	DeployUser     string   `json:"deploy_user"`
	SSHType        string   `json:"ssh_type"`
	TLSEnabled     bool     `json:"tls_enabled"`
	TLSCACert      string   `json:"tls_ca_cert,omitempty"`
	TLSClientCert  string   `json:"tls_client_cert,omitempty"`
	TLSClientKey   string   `json:"tls_client_key,omitempty"`
	DashboardURL   string   `json:"dashboard_url,omitempty"`
	DashboardURLS  []string `json:"dashboard_urls,omitempty"`
	GrafanaURLS    []string `json:"grafana_urls,omitempty"`
}

// JSONOutput holds the structure for the JSON output of `tiup cluster display --json`
type JSONOutput struct {
	ClusterMetaInfo ClusterMetaInfo `json:"cluster_meta"`
	InstanceInfos   []InstInfo      `json:"instances,omitempty"`
	LocationLabel   string          `json:"location_label,omitempty"`
	LabelInfos      []api.LabelInfo `json:"labels,omitempty"`
}

// Display cluster meta and topology.
func (m *Manager) Display(dopt DisplayOption, opt operator.Options) error {
	name := dopt.ClusterName
	if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
		return err
	}

	clusterInstInfos, err := m.GetClusterTopology(dopt, opt)
	if err != nil {
		return err
	}

	metadata, _ := m.meta(name)
	topo := metadata.GetTopology()
	base := metadata.GetBaseMeta()
	cyan := color.New(color.FgCyan, color.Bold)

	// check if managehost is set
	if !dopt.ShowManageHost {
		topo.IterInstance(func(inst spec.Instance) {
			if inst.GetHost() != inst.GetManageHost() {
				dopt.ShowManageHost = true
				return
			}
		})
	}

	statusTimeout := time.Duration(opt.APITimeout) * time.Second
	// display cluster meta
	var j *JSONOutput
	if m.logger.GetDisplayMode() == logprinter.DisplayModeJSON {
		j = &JSONOutput{
			ClusterMetaInfo: ClusterMetaInfo{
				m.sysName,
				name,
				base.Version,
				topo.BaseTopo().GlobalOptions.User,
				string(topo.BaseTopo().GlobalOptions.SSHType),
				topo.BaseTopo().GlobalOptions.TLSEnabled,
				"", // CA Cert
				"", // Client Cert
				"", // Client Key
				"",
				nil,
				nil,
			},
			InstanceInfos: clusterInstInfos,
		}

		if topo.BaseTopo().GlobalOptions.TLSEnabled {
			j.ClusterMetaInfo.TLSCACert = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSCACert)
			j.ClusterMetaInfo.TLSClientKey = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientKey)
			j.ClusterMetaInfo.TLSClientCert = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientCert)
		}
	} else {
		fmt.Printf("Cluster type:       %s\n", cyan.Sprint(m.sysName))
		fmt.Printf("Cluster name:       %s\n", cyan.Sprint(name))
		fmt.Printf("Cluster version:    %s\n", cyan.Sprint(base.Version))
		fmt.Printf("Deploy user:        %s\n", cyan.Sprint(topo.BaseTopo().GlobalOptions.User))
		fmt.Printf("SSH type:           %s\n", cyan.Sprint(topo.BaseTopo().GlobalOptions.SSHType))

		// display TLS info
		if topo.BaseTopo().GlobalOptions.TLSEnabled {
			fmt.Printf("TLS encryption:     %s\n", cyan.Sprint("enabled"))
			fmt.Printf("CA certificate:     %s\n", cyan.Sprint(
				m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSCACert),
			))
			fmt.Printf("Client private key: %s\n", cyan.Sprint(
				m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientKey),
			))
			fmt.Printf("Client certificate: %s\n", cyan.Sprint(
				m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientCert),
			))
		}
	}

	// display topology
	var clusterTable [][]string
	rowHead := []string{"ID", "Role", "Host"}

	if dopt.ShowManageHost {
		rowHead = append(rowHead, "Manage Host")
	}

	rowHead = append(rowHead, "Ports", "OS/Arch", "Status")

	if dopt.ShowProcess {
		rowHead = append(rowHead, "Memory", "Memory Limit", "CPU Quota")
	}
	if dopt.ShowUptime {
		rowHead = append(rowHead, "Since")
	}
	if dopt.ShowNuma {
		rowHead = append(rowHead, "Numa Node", "Numa Cores")
	}
	if dopt.ShowVersions {
		rowHead = append(rowHead, "Version")
	}

	rowHead = append(rowHead, "Data Dir", "Deploy Dir")
	clusterTable = append(clusterTable, rowHead)

	masterActive := make([]string, 0)
	for _, v := range clusterInstInfos {
		row := []string{
			color.CyanString(v.ID),
			v.Role,
			v.Host,
		}

		if dopt.ShowManageHost {
			row = append(row, v.ManageHost)
		}

		row = append(row,
			v.Ports,
			v.OsArch,
			formatInstanceStatus(v.Status))

		if dopt.ShowProcess {
			row = append(row, v.Memory, v.MemoryLimit, v.CPUquota)
		}
		if dopt.ShowUptime {
			row = append(row, v.Since)
		}
		if dopt.ShowNuma {
			row = append(row, v.NumaNode, v.NumaCores)
		}
		if dopt.ShowVersions {
			row = append(row, v.Version)
		}

		row = append(row, v.DataDir, v.DeployDir)
		clusterTable = append(clusterTable, row)

		if v.ComponentName != spec.ComponentPD && v.ComponentName != spec.ComponentDMMaster {
			continue
		}
		if strings.HasPrefix(v.Status, "Up") || strings.HasPrefix(v.Status, "Healthy") {
			instAddr := utils.JoinHostPort(v.ManageHost, v.Port)
			masterActive = append(masterActive, instAddr)
		}
	}

	tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
	if err != nil {
		return err
	}

	ctx := ctxt.New(
		context.Background(),
		opt.Concurrency,
		m.logger,
	)
	if t, ok := topo.(*spec.Specification); ok {
		_ = m.displayDashboards(ctx, t, j, statusTimeout, tlsCfg, "", masterActive...)
	}

	if m.logger.GetDisplayMode() == logprinter.DisplayModeJSON {
		grafanaURLs := getGrafanaURL(clusterInstInfos)
		if len(grafanaURLs) != 0 {
			j.ClusterMetaInfo.GrafanaURLS = grafanaURLs
		}
	} else {
		urls, exist := getGrafanaURLStr(clusterInstInfos)
		if exist {
			fmt.Printf("Grafana URL:        %s\n", cyan.Sprintf("%s", urls))
		}
	}

	if m.logger.GetDisplayMode() == logprinter.DisplayModeJSON {
		d, err := json.MarshalIndent(j, "", "  ")
		if err != nil {
			return err
		}
		fmt.Println(string(d))
		return nil
	}

	tui.PrintTable(clusterTable, true)
	fmt.Printf("Total nodes: %d\n", len(clusterTable)-1)

	if t, ok := topo.(*spec.Specification); ok {
		// Check if TiKV's label set correctly
		pdClient := api.NewPDClient(
			context.WithValue(ctx, logprinter.ContextKeyLogger, m.logger),
			masterActive,
			10*time.Second,
			tlsCfg,
		)

		if lbs, placementRule, err := pdClient.GetLocationLabels(); err != nil {
			m.logger.Debugf("get location labels from pd failed: %v", err)
		} else if !placementRule {
			if err := spec.CheckTiKVLabels(lbs, pdClient); err != nil {
				color.Yellow("\nWARN: there is something wrong with TiKV labels, which may cause data losing:\n%v", err)
			}
		}

		// Check if there is some instance in tombstone state
		nodes, _ := operator.DestroyTombstone(ctx, t, true /* returnNodesOnly */, opt, tlsCfg)
		if len(nodes) != 0 {
			color.Green("There are some nodes can be pruned: \n\tNodes: %+v\n\tYou can destroy them with the command: `tiup cluster prune %s`", nodes, name)
		}
	}

	return nil
}

func getGrafanaURL(clusterInstInfos []InstInfo) (result []string) {
	var grafanaURLs []string
	for _, instance := range clusterInstInfos {
		if instance.Role == "grafana" || instance.Role == "grafana (patched)" {
			grafanaURLs = append(grafanaURLs, "http://"+utils.JoinHostPort(instance.Host, instance.Port))
		}
	}
	return grafanaURLs
}

func getGrafanaURLStr(clusterInstInfos []InstInfo) (result string, exist bool) {
	grafanaURLs := getGrafanaURL(clusterInstInfos)
	if len(grafanaURLs) == 0 {
		return "", false
	}
	return strings.Join(grafanaURLs, ","), true
}

// DisplayTiKVLabels display cluster tikv labels
func (m *Manager) DisplayTiKVLabels(dopt DisplayOption, opt operator.Options) error {
	name := dopt.ClusterName
	if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
		return err
	}

	clusterInstInfos, err := m.GetClusterTopology(dopt, opt)
	if err != nil {
		return err
	}

	metadata, _ := m.meta(name)
	topo := metadata.GetTopology()
	base := metadata.GetBaseMeta()
	statusTimeout := time.Duration(opt.APITimeout) * time.Second
	// display cluster meta
	cyan := color.New(color.FgCyan, color.Bold)

	var j *JSONOutput
	if strings.ToLower(opt.DisplayMode) == "json" {
		j = &JSONOutput{
			ClusterMetaInfo: ClusterMetaInfo{
				m.sysName,
				name,
				base.Version,
				topo.BaseTopo().GlobalOptions.User,
				string(topo.BaseTopo().GlobalOptions.SSHType),
				topo.BaseTopo().GlobalOptions.TLSEnabled,
				"", // CA Cert
				"", // Client Cert
				"", // Client Key
				"",
				nil,
				nil,
			},
		}

		if topo.BaseTopo().GlobalOptions.TLSEnabled {
			j.ClusterMetaInfo.TLSCACert = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSCACert)
			j.ClusterMetaInfo.TLSClientKey = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientKey)
			j.ClusterMetaInfo.TLSClientCert = m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientCert)
		}
	} else {
		fmt.Printf("Cluster type:       %s\n", cyan.Sprint(m.sysName))
		fmt.Printf("Cluster name:       %s\n", cyan.Sprint(name))
		fmt.Printf("Cluster version:    %s\n", cyan.Sprint(base.Version))
		fmt.Printf("SSH type:           %s\n", cyan.Sprint(topo.BaseTopo().GlobalOptions.SSHType))
		fmt.Printf("Component name:     %s\n", cyan.Sprint("TiKV"))

		// display TLS info
		if topo.BaseTopo().GlobalOptions.TLSEnabled {
			fmt.Printf("TLS encryption:  	%s\n", cyan.Sprint("enabled"))
			fmt.Printf("CA certificate:     %s\n", cyan.Sprint(
				m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSCACert),
			))
			fmt.Printf("Client private key: %s\n", cyan.Sprint(
				m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientKey),
			))
			fmt.Printf("Client certificate: %s\n", cyan.Sprint(
				m.specManager.Path(name, spec.TLSCertKeyDir, spec.TLSClientCert),
			))
		}
	}

	// display topology
	var clusterTable [][]string
	clusterTable = append(clusterTable, []string{"Machine", "Port", "Store", "Status", "Leaders", "Regions", "Capacity", "Available", "Labels"})

	masterActive := make([]string, 0)
	tikvStoreIP := make(map[string]struct{})
	for _, v := range clusterInstInfos {
		if v.ComponentName == spec.ComponentTiKV {
			tikvStoreIP[v.Host] = struct{}{}
		}
	}

	ctx := ctxt.New(
		context.Background(),
		opt.Concurrency,
		m.logger,
	)

	masterList := topo.BaseTopo().MasterList
	tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
	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 := utils.JoinHostPort(ins.GetManageHost(), ins.GetPort())
				mu.Lock()
				masterActive = append(masterActive, instAddr)
				mu.Unlock()
			}
		}
	}, opt.Concurrency)

	var (
		labelInfoArr  []api.LabelInfo
		locationLabel []string
	)

	if _, ok := topo.(*spec.Specification); ok {
		// Check if TiKV's label set correctly
		pdClient := api.NewPDClient(ctx, masterActive, 10*time.Second, tlsCfg)
		// No
		locationLabel, _, err = pdClient.GetLocationLabels()
		if err != nil {
			m.logger.Debugf("get location labels from pd failed: %v", err)
		}

		_, storeInfos, err := pdClient.GetTiKVLabels()
		if err != nil {
			m.logger.Debugf("get tikv state and labels from pd failed: %v", err)
		}

		for storeIP := range tikvStoreIP {
			row := []string{
				color.CyanString(storeIP),
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
			}
			clusterTable = append(clusterTable, row)

			for _, val := range storeInfos {
				if store, ok := val[storeIP]; ok {
					row := []string{
						"",
						store.Port,
						strconv.FormatUint(store.Store, 10),
						color.CyanString(store.Status),
						fmt.Sprintf("%v", store.Leaders),
						fmt.Sprintf("%v", store.Regions),
						store.Capacity,
						store.Available,
						store.Labels,
					}
					clusterTable = append(clusterTable, row)

					labelInfoArr = append(labelInfoArr, store)
				}
			}
		}
	}

	if strings.ToLower(opt.DisplayMode) == "json" {
		j.LocationLabel = strings.Join(locationLabel, ",")
		j.LabelInfos = labelInfoArr
		d, err := json.MarshalIndent(j, "", "  ")
		if err != nil {
			return err
		}
		fmt.Println(string(d))
		return nil
	}
	fmt.Printf("Location labels:    %s\n", cyan.Sprint(strings.Join(locationLabel, ",")))
	tui.PrintTable(clusterTable, true)
	fmt.Printf("Total nodes: %d\n", len(clusterTable)-1)

	return nil
}

// GetClusterTopology get the topology of the cluster.
func (m *Manager) GetClusterTopology(dopt DisplayOption, opt operator.Options) ([]InstInfo, error) {
	ctx := ctxt.New(
		context.Background(),
		opt.Concurrency,
		m.logger,
	)
	name := dopt.ClusterName
	metadata, err := m.meta(name)
	if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) &&
		!errors.Is(perrs.Cause(err), spec.ErrNoTiSparkMaster) {
		return nil, err
	}

	topo := metadata.GetTopology()
	base := metadata.GetBaseMeta()

	statusTimeout := time.Duration(opt.APITimeout) * time.Second

	err = SetSSHKeySet(ctx, m.specManager.Path(name, "ssh", "id_rsa"), m.specManager.Path(name, "ssh", "id_rsa.pub"))
	if err != nil {
		return nil, err
	}

	err = SetClusterSSH(ctx, topo, base.User, opt.SSHTimeout, opt.SSHType, topo.BaseTopo().GlobalOptions.SSHType)
	if err != nil {
		return nil, err
	}

	filterRoles := set.NewStringSet(opt.Roles...)
	filterNodes := set.NewStringSet(opt.Nodes...)
	masterList := topo.BaseTopo().MasterList
	tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
	if err != nil {
		return nil, err
	}

	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
		}

		status := ins.Status(ctx, statusTimeout, tlsCfg, masterList...)
		mu.Lock()
		if strings.HasPrefix(status, "Up") || strings.HasPrefix(status, "Healthy") {
			instAddr := utils.JoinHostPort(ins.GetManageHost(), ins.GetPort())
			masterActive = append(masterActive, instAddr)
		}
		masterStatus[ins.ID()] = status
		mu.Unlock()
	}, opt.Concurrency)

	var dashboardAddr string
	if t, ok := topo.(*spec.Specification); ok {
		dashboardAddr, _ = t.GetPDDashboardAddress(ctx, tlsCfg, statusTimeout, masterActive...)
	}

	clusterInstInfos := []InstInfo{}
	systemdMode := string(topo.BaseTopo().GlobalOptions.SystemdMode)
	topo.IterInstance(func(ins spec.Instance) {
		// apply role filter
		if len(filterRoles) > 0 && !filterRoles.Exist(ins.Role()) {
			return
		}
		// apply node filter
		if len(filterNodes) > 0 && !filterNodes.Exist(ins.ID()) {
			return
		}

		dataDir := "-"
		insDirs := ins.UsedDirs()
		deployDir := insDirs[0]
		if len(insDirs) > 1 {
			dataDir = insDirs[1]
		}

		var status, memory string
		switch ins.ComponentName() {
		case spec.ComponentPD:
			status = masterStatus[ins.ID()]
			instAddr := utils.JoinHostPort(ins.GetManageHost(), ins.GetPort())
			if dashboardAddr == instAddr {
				status += "|UI"
			}
		case spec.ComponentDMMaster:
			status = masterStatus[ins.ID()]
		default:
			status = ins.Status(ctx, statusTimeout, tlsCfg, masterActive...)
		}

		since := "-"
		if dopt.ShowUptime {
			since = formatInstanceSince(ins.Uptime(ctx, statusTimeout, tlsCfg))
		}

		// Query the service status and uptime
		if status == "-" || (dopt.ShowUptime && since == "-") || dopt.ShowProcess {
			e, found := ctxt.GetInner(ctx).GetExecutor(ins.GetManageHost())
			if found {
				var active string
				var systemdSince time.Duration
				nctx := checkpoint.NewContext(ctx)
				active, memory, systemdSince, _ = operator.GetServiceStatus(nctx, e, ins.ServiceName(), systemdMode, systemdMode)
				if status == "-" {
					if active == "active" {
						status = "Up"
					} else {
						status = active
					}
				}
				if dopt.ShowUptime && since == "-" {
					since = formatInstanceSince(systemdSince)
				}
			}
		}

		// check if the role is patched
		roleName := ins.Role()
		// get extended name for TiFlash to distinguish disaggregated mode.
		if ins.ComponentName() == spec.ComponentTiFlash {
			tiflashInstance := ins.(*spec.TiFlashInstance)
			tiflashSpec := tiflashInstance.InstanceSpec.(*spec.TiFlashSpec)
			roleName += tiflashSpec.GetExtendedRole(ctx, tlsCfg, masterActive...)
		}
		if ins.IsPatched() {
			roleName += " (patched)"
		}
		rc := ins.ResourceControl()
		mu.Lock()
		clusterInstInfos = append(clusterInstInfos, InstInfo{
			ID:            ins.ID(),
			Role:          roleName,
			Host:          ins.GetHost(),
			ManageHost:    ins.GetManageHost(),
			Ports:         utils.JoinInt(ins.UsedPorts(), "/"),
			OsArch:        tui.OsArch(ins.OS(), ins.Arch()),
			Status:        status,
			Memory:        utils.Ternary(memory == "", "-", memory).(string),
			MemoryLimit:   utils.Ternary(rc.MemoryLimit == "", "-", rc.MemoryLimit).(string),
			CPUquota:      utils.Ternary(rc.CPUQuota == "", "-", rc.CPUQuota).(string),
			DataDir:       dataDir,
			DeployDir:     deployDir,
			ComponentName: ins.ComponentName(),
			Port:          ins.GetPort(),
			Since:         since,
			NumaNode:      utils.Ternary(ins.GetNumaNode() == "", "-", ins.GetNumaNode()).(string),
			NumaCores:     utils.Ternary(ins.GetNumaCores() == "", "-", ins.GetNumaCores()).(string),
			Version:       ins.CalculateVersion(base.Version),
		})
		mu.Unlock()
	}, opt.Concurrency)

	// Sort by role,host,ports
	sort.Slice(clusterInstInfos, func(i, j int) bool {
		lhs, rhs := clusterInstInfos[i], clusterInstInfos[j]
		if lhs.Role != rhs.Role {
			return lhs.Role < rhs.Role
		}
		if lhs.Host != rhs.Host {
			return lhs.Host < rhs.Host
		}
		return lhs.Ports < rhs.Ports
	})

	return clusterInstInfos, nil
}

func formatInstanceStatus(status string) string {
	lowercaseStatus := strings.ToLower(status)

	startsWith := func(prefixs ...string) bool {
		for _, prefix := range prefixs {
			if strings.HasPrefix(lowercaseStatus, prefix) {
				return true
			}
		}
		return false
	}

	switch {
	case startsWith("up|l", "healthy|l"): // up|l, up|l|ui, healthy|l
		return color.HiGreenString(status)
	case startsWith("up", "healthy", "free"):
		return color.GreenString(status)
	case startsWith("down", "err", "inactive"): // down, down|ui
		return color.RedString(status)
	case startsWith("tombstone", "disconnected", "n/a"), strings.Contains(strings.ToLower(status), "offline"):
		return color.YellowString(status)
	default:
		return status
	}
}

func formatInstanceSince(uptime time.Duration) string {
	if uptime == 0 {
		return "-"
	}

	d := int64(uptime.Hours() / 24)
	h := int64(math.Mod(uptime.Hours(), 24))
	m := int64(math.Mod(uptime.Minutes(), 60))
	s := int64(math.Mod(uptime.Seconds(), 60))

	chunks := []struct {
		unit  string
		value int64
	}{
		{"d", d},
		{"h", h},
		{"m", m},
		{"s", s},
	}

	parts := []string{}

	for _, chunk := range chunks {
		switch chunk.value {
		case 0:
			continue
		default:
			parts = append(parts, fmt.Sprintf("%d%s", chunk.value, chunk.unit))
		}
	}

	return strings.Join(parts, "")
}

// SetSSHKeySet set ssh key set.
func SetSSHKeySet(ctx context.Context, privateKeyPath string, publicKeyPath string) error {
	ctxt.GetInner(ctx).PrivateKeyPath = privateKeyPath
	ctxt.GetInner(ctx).PublicKeyPath = publicKeyPath
	return nil
}

// SetClusterSSH set cluster user ssh executor in context.
func SetClusterSSH(ctx context.Context, topo spec.Topology, deployUser string, sshTimeout uint64, sshType, defaultSSHType executor.SSHType) error {
	if sshType == "" {
		sshType = defaultSSHType
	}
	if len(ctxt.GetInner(ctx).PrivateKeyPath) == 0 {
		return perrs.Errorf("context has no PrivateKeyPath")
	}

	for _, com := range topo.ComponentsByStartOrder() {
		for _, in := range com.Instances() {
			cf := executor.SSHConfig{
				Host:    in.GetManageHost(),
				Port:    in.GetSSHPort(),
				KeyFile: ctxt.GetInner(ctx).PrivateKeyPath,
				User:    deployUser,
				Timeout: time.Second * time.Duration(sshTimeout),
			}

			e, err := executor.New(sshType, false, cf)
			if err != nil {
				return err
			}
			ctxt.GetInner(ctx).SetExecutor(in.GetManageHost(), e)
		}
	}

	return nil
}

// DisplayDashboardInfo prints the dashboard address of cluster
func (m *Manager) DisplayDashboardInfo(clusterName string, timeout time.Duration, tlsCfg *tls.Config) error {
	metadata, err := spec.ClusterMetadata(clusterName)
	if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) &&
		!errors.Is(perrs.Cause(err), spec.ErrNoTiSparkMaster) {
		return err
	}

	ctx := context.WithValue(context.Background(), logprinter.ContextKeyLogger, m.logger)
	return m.displayDashboards(ctx, metadata.Topology, nil, timeout, tlsCfg, clusterName, metadata.Topology.GetPDListWithManageHost()...)
}

func (m *Manager) displayDashboards(ctx context.Context, t *spec.Specification, j *JSONOutput, timeout time.Duration, tlsCfg *tls.Config, clusterName string, pdList ...string) error {
	dashboardAddrs := []string{}
	t.IterInstance(func(ins spec.Instance) {
		if ins.Role() != spec.ComponentDashboard {
			return
		}
		dashboardAddrs = append(dashboardAddrs, utils.JoinHostPort(ins.GetManageHost(), ins.GetPort()))
	})

	pdDashboardAddr, err := t.GetPDDashboardAddress(ctx, tlsCfg, timeout, pdList...)
	if err == nil && !set.NewStringSet("", "auto", "none").Exist(pdDashboardAddr) {
		dashboardAddrs = append(dashboardAddrs, pdDashboardAddr)
	}

	if len(dashboardAddrs) == 0 {
		return fmt.Errorf("TiDB Dashboard is missing, try again later")
	}

	if clusterName != "" && tlsCfg != nil {
		fmt.Println(
			"Client certificate:",
			color.CyanString(m.specManager.Path(clusterName, spec.TLSCertKeyDir, spec.PFXClientCert)),
		)
		fmt.Println(
			"Certificate password:",
			color.CyanString(crypto.PKCS12Password),
		)
	}

	for i, addr := range dashboardAddrs {
		scheme := "http"

		// show the original info
		if addr == pdDashboardAddr {
			if tlsCfg != nil {
				scheme = "https"
			}
			if m.logger.GetDisplayMode() == logprinter.DisplayModeJSON && j != nil {
				j.ClusterMetaInfo.DashboardURL = fmt.Sprintf("%s://%s/dashboard", scheme, addr)
			} else {
				fmt.Printf("Dashboard URL:      %s\n", color.CyanString("%s://%s/dashboard", scheme, addr))
			}
		}

		if m.logger.GetDisplayMode() == logprinter.DisplayModeJSON && j != nil {
			j.ClusterMetaInfo.DashboardURLS = append(j.ClusterMetaInfo.DashboardURLS, fmt.Sprintf("%s://%s/dashboard", scheme, addr))
		} else {
			dashboardAddrs[i] = color.CyanString("%s://%s/dashboard", scheme, addr)
		}
	}

	if m.logger.GetDisplayMode() != logprinter.DisplayModeJSON || j == nil {
		fmt.Printf("Dashboard URLs:     %s\n", strings.Join(dashboardAddrs, ","))
	}

	return nil
}