@@ -28,11 +28,13 @@ import (
2828 "path/filepath"
2929 "sort"
3030 "strings"
31+ "time"
3132
3233 "github.com/fatih/color"
3334 "github.com/joomcode/errorx"
3435 perrs "github.com/pingcap/errors"
3536 "github.com/pingcap/tiup/pkg/cliutil"
37+ "github.com/pingcap/tiup/pkg/cluster/api"
3638 "github.com/pingcap/tiup/pkg/cluster/clusterutil"
3739 "github.com/pingcap/tiup/pkg/cluster/executor"
3840 operator "github.com/pingcap/tiup/pkg/cluster/operation"
@@ -184,7 +186,7 @@ func (m *Manager) StopCluster(clusterName string, options operator.Options) erro
184186
185187 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
186188 if err != nil {
187- return perrs . AddStack ( err )
189+ return err
188190 }
189191
190192 t := task .NewBuilder ().
@@ -221,7 +223,7 @@ func (m *Manager) RestartCluster(clusterName string, options operator.Options) e
221223
222224 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
223225 if err != nil {
224- return perrs . AddStack ( err )
226+ return err
225227 }
226228
227229 t := task .NewBuilder ().
@@ -291,7 +293,7 @@ func (m *Manager) CleanCluster(clusterName string, gOpt operator.Options, cleanO
291293
292294 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
293295 if err != nil {
294- return perrs . AddStack ( err )
296+ return err
295297 }
296298
297299 if ! skipConfirm {
@@ -354,7 +356,7 @@ func (m *Manager) DestroyCluster(clusterName string, gOpt operator.Options, dest
354356
355357 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
356358 if err != nil {
357- return perrs . AddStack ( err )
359+ return err
358360 }
359361
360362 if ! skipConfirm {
@@ -531,6 +533,10 @@ func (m *Manager) Display(clusterName string, opt operator.Options) error {
531533 filterRoles := set .NewStringSet (opt .Roles ... )
532534 filterNodes := set .NewStringSet (opt .Nodes ... )
533535 pdList := topo .BaseTopo ().MasterList
536+ tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
537+ if err != nil {
538+ return err
539+ }
534540 for _ , comp := range topo .ComponentsByStartOrder () {
535541 for _ , ins := range comp .Instances () {
536542 // apply role filter
@@ -549,10 +555,6 @@ func (m *Manager) Display(clusterName string, opt operator.Options) error {
549555 dataDir = insDirs [1 ]
550556 }
551557
552- tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
553- if err != nil {
554- return perrs .AddStack (err )
555- }
556558 status := ins .Status (tlsCfg , pdList ... )
557559 // Query the service status
558560 if status == "-" {
@@ -597,6 +599,17 @@ func (m *Manager) Display(clusterName string, opt operator.Options) error {
597599 cliutil .PrintTable (clusterTable , true )
598600 fmt .Printf ("Total nodes: %d\n " , len (clusterTable )- 1 )
599601
602+ if topo , ok := topo .(* spec.Specification ); ok {
603+ // Check if TiKV's label set correctly
604+ kvs := topo .TiKVServers
605+ pdClient := api .NewPDClient (pdList , 10 * time .Second , tlsCfg )
606+ if lbs , err := pdClient .GetLocationLabels (); err != nil {
607+ color .Yellow ("\n WARN: get location labels from pd failed: %v" , err )
608+ } else if err := spec .CheckTiKVLocationLabels (lbs , kvs ); err != nil {
609+ color .Yellow ("\n WARN: there is something wrong with TiKV labels, which may cause data losing:\n %v" , err )
610+ }
611+ }
612+
600613 return nil
601614}
602615
@@ -751,7 +764,7 @@ func (m *Manager) Reload(clusterName string, opt operator.Options, skipRestart b
751764
752765 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
753766 if err != nil {
754- return perrs . AddStack ( err )
767+ return err
755768 }
756769 if ! skipRestart {
757770 tb = tb .Func ("UpgradeCluster" , func (ctx * task.Context ) error {
@@ -896,7 +909,7 @@ func (m *Manager) Upgrade(clusterName string, clusterVersion string, opt operato
896909
897910 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
898911 if err != nil {
899- return perrs . AddStack ( err )
912+ return err
900913 }
901914 t := task .NewBuilder ().
902915 SSHKeySet (
@@ -966,7 +979,7 @@ func (m *Manager) Patch(clusterName string, packagePath string, opt operator.Opt
966979
967980 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
968981 if err != nil {
969- return perrs . AddStack ( err )
982+ return err
970983 }
971984 t := task .NewBuilder ().
972985 SSHKeySet (
@@ -1002,6 +1015,7 @@ type ScaleOutOptions struct {
10021015 SkipCreateUser bool // don't create user
10031016 IdentityFile string // path to the private key file
10041017 UsePassword bool // use password instead of identity file for ssh connection
1018+ NoLabels bool // don't check labels for TiKV instance
10051019}
10061020
10071021// DeployOptions contains the options for scale out.
@@ -1012,6 +1026,7 @@ type DeployOptions struct {
10121026 IdentityFile string // path to the private key file
10131027 UsePassword bool // use password instead of identity file for ssh connection
10141028 IgnoreConfigCheck bool // ignore config check result
1029+ NoLabels bool // don't check labels for TiKV instance
10151030}
10161031
10171032// DeployerInstance is a instance can deploy to a target deploy directory.
@@ -1059,6 +1074,18 @@ func (m *Manager) Deploy(
10591074 base .GlobalOptions .SSHType = sshType
10601075 }
10611076
1077+ if topo , ok := topo .(* spec.Specification ); ok && ! opt .NoLabels {
1078+ // Check if TiKV's label set correctly
1079+ lbs , err := topo .LocationLabels ()
1080+ if err != nil {
1081+ return err
1082+ }
1083+ kvs := topo .TiKVServers
1084+ if err := spec .CheckTiKVLocationLabels (lbs , kvs ); err != nil {
1085+ return perrs .Errorf ("check TiKV label failed, please fix that before continue:\n %s" , err )
1086+ }
1087+ }
1088+
10621089 clusterList , err := m .specManager .GetAllClusters ()
10631090 if err != nil {
10641091 return err
@@ -1406,7 +1433,7 @@ func (m *Manager) ScaleIn(
14061433
14071434 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
14081435 if err != nil {
1409- return perrs . AddStack ( err )
1436+ return err
14101437 }
14111438
14121439 b := task .NewBuilder ().
@@ -1480,6 +1507,24 @@ func (m *Manager) ScaleOut(
14801507 return err
14811508 }
14821509
1510+ if topo , ok := topo .(* spec.Specification ); ok && ! opt .NoLabels {
1511+ // Check if TiKV's label set correctly
1512+ pdList := topo .BaseTopo ().MasterList
1513+ tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
1514+ if err != nil {
1515+ return err
1516+ }
1517+ pdClient := api .NewPDClient (pdList , 10 * time .Second , tlsCfg )
1518+ lbs , err := pdClient .GetLocationLabels ()
1519+ if err != nil {
1520+ return err
1521+ }
1522+ kvs := mergedTopo .(* spec.Specification ).TiKVServers
1523+ if err := spec .CheckTiKVLocationLabels (lbs , kvs ); err != nil {
1524+ return perrs .Errorf ("check TiKV label failed, please fix that before continue:\n %s" , err )
1525+ }
1526+ }
1527+
14831528 clusterList , err := m .specManager .GetAllClusters ()
14841529 if err != nil {
14851530 return err
@@ -1874,7 +1919,7 @@ func buildScaleOutTask(
18741919
18751920 tlsCfg , err := topo .TLSConfig (m .specManager .Path (clusterName , spec .TLSCertKeyDir ))
18761921 if err != nil {
1877- return nil , perrs . AddStack ( err )
1922+ return nil , err
18781923 }
18791924
18801925 // Initialize the environments
0 commit comments