diff --git a/pkg/cluster/spec/validate.go b/pkg/cluster/spec/validate.go index fdc1461fe5..476db90bbc 100644 --- a/pkg/cluster/spec/validate.go +++ b/pkg/cluster/spec/validate.go @@ -794,6 +794,22 @@ func (s *Specification) validateTLSEnabled() error { return nil } +func (s *Specification) validatePDNames() error { + // check pdserver name + pdNames := set.NewStringSet() + for _, pd := range s.PDServers { + if pd.Name == "" { + continue + } + + if pdNames.Exist(pd.Name) { + return errors.Errorf("component pd_servers.name is not supported duplicated, the name %s is duplicated", pd.Name) + } + pdNames.Insert(pd.Name) + } + return nil +} + // Validate validates the topology specification and produce error if the // specification invalid (e.g: port conflicts or directory conflicts) func (s *Specification) Validate() error { @@ -817,6 +833,10 @@ func (s *Specification) Validate() error { return err } + if err := s.validatePDNames(); err != nil { + return err + } + return RelativePathDetect(s, isSkipField) } diff --git a/pkg/cluster/spec/validate_test.go b/pkg/cluster/spec/validate_test.go index 717d0a5ef9..634cf02fa6 100644 --- a/pkg/cluster/spec/validate_test.go +++ b/pkg/cluster/spec/validate_test.go @@ -782,3 +782,31 @@ pd_servers: c.Assert(err, NotNil) c.Assert(err.Error(), Equals, "directory conflict for '/test-1' between 'tiflash_servers:172.16.5.138.data_dir' and 'tiflash_servers:172.16.5.138.data_dir'") } + +func (s *metaSuiteTopo) TestPdServerWithSameName(c *C) { + topo := Specification{} + err := yaml.Unmarshal([]byte(` +pd_servers: + - host: 172.16.5.138 + peer_port: 1234 + name: name1 + - host: 172.16.5.139 + perr_port: 1234 + name: name2 +`), &topo) + c.Assert(err, IsNil) + + topo = Specification{} + err = yaml.Unmarshal([]byte(` + pd_servers: + - host: 172.16.5.138 + peer_port: 1234 + name: name1 + - host: 172.16.5.139 + perr_port: 1234 + name: name1 +`), &topo) + c.Assert(err, NotNil) + c.Assert(err.Error(), Equals, "component pd_servers.name is not supported duplicated, the name name1 is duplicated") + +}