From 291bce6d2b33a8133db026e8a6e34bf52a9971d3 Mon Sep 17 00:00:00 2001 From: disksing Date: Tue, 28 Jul 2020 19:13:45 +0800 Subject: [PATCH 1/3] config: add initial-cluster-token config Signed-off-by: disksing --- conf/config.toml | 3 +++ server/config/config.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/conf/config.toml b/conf/config.toml index 713a42bd4f6..150d8d54e42 100644 --- a/conf/config.toml +++ b/conf/config.toml @@ -14,6 +14,9 @@ advertise-peer-urls = "" initial-cluster = "pd=http://127.0.0.1:2380" initial-cluster-state = "new" +## set different tokens to prevent communication between PDs in different clusters. +# initial-cluster-token = "pd-cluster" + lease = 3 tso-save-interval = "3s" diff --git a/server/config/config.go b/server/config/config.go index 3ad9593a248..521f96bc5ca 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -64,6 +64,7 @@ type Config struct { InitialCluster string `toml:"initial-cluster" json:"initial-cluster"` InitialClusterState string `toml:"initial-cluster-state" json:"initial-cluster-state"` + InitialClusterToken string `toml:"initial-cluster-token" json:"initial-cluster-token"` // Join to an existing pd cluster, a string of endpoints. Join string `toml:"join" json:"join"` @@ -187,6 +188,7 @@ const ( defaultClientUrls = "http://127.0.0.1:2379" defaultPeerUrls = "http://127.0.0.1:2380" defaultInitialClusterState = embed.ClusterStateFlagNew + defaultInitialClusterToken = "pd-cluster" // etcd use 100ms for heartbeat and 1s for election timeout. // We can enlarge both a little to reduce the network aggression. @@ -479,6 +481,7 @@ func (c *Config) Adjust(meta *toml.MetaData) error { } adjustString(&c.InitialClusterState, defaultInitialClusterState) + adjustString(&c.InitialClusterToken, defaultInitialClusterToken) if len(c.Join) > 0 { if _, err := url.Parse(c.Join); err != nil { @@ -1171,6 +1174,7 @@ func (c *Config) GenEmbedEtcdConfig() (*embed.Config, error) { cfg.WalDir = "" cfg.InitialCluster = c.InitialCluster cfg.ClusterState = c.InitialClusterState + cfg.InitialClusterToken = c.InitialClusterToken cfg.EnablePprof = true cfg.PreVote = c.PreVote cfg.StrictReconfigCheck = !c.DisableStrictReconfigCheck From 89783db5b188ad73099f6c1380d23311906ed7e4 Mon Sep 17 00:00:00 2001 From: disksing Date: Wed, 29 Jul 2020 14:31:47 +0800 Subject: [PATCH 2/3] add test Signed-off-by: disksing --- tests/server/server_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/server/server_test.go b/tests/server/server_test.go index 76c552ab127..96c46de55fe 100644 --- a/tests/server/server_test.go +++ b/tests/server/server_test.go @@ -25,6 +25,7 @@ import ( "go.uber.org/goleak" // Register schedulers. + "github.com/pingcap/pd/v4/server/config" _ "github.com/pingcap/pd/v4/server/schedulers" ) @@ -113,6 +114,14 @@ func (s *serverTestSuite) TestClusterID(c *C) { for _, s := range cluster.GetServers() { c.Assert(s.GetClusterID(), Equals, clusterID) } + + cluster2, err := tests.NewTestCluster(s.ctx, 3, func(conf *config.Config) { conf.InitialClusterToken = "foobar" }) + defer cluster2.Destroy() + c.Assert(err, IsNil) + err = cluster2.RunInitialServers() + c.Assert(err, IsNil) + clusterID2 := cluster2.GetServer("pd1").GetClusterID() + c.Assert(clusterID2, Not(Equals), clusterID) } func (s *serverTestSuite) TestLeader(c *C) { From 65e128c89444b29b5bee910ff4c5d2cec115c313 Mon Sep 17 00:00:00 2001 From: disksing Date: Wed, 29 Jul 2020 17:46:30 +0800 Subject: [PATCH 3/3] address comment Signed-off-by: disksing --- tests/server/server_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/server/server_test.go b/tests/server/server_test.go index 96c46de55fe..5357d849261 100644 --- a/tests/server/server_test.go +++ b/tests/server/server_test.go @@ -21,11 +21,11 @@ import ( "github.com/pingcap/pd/v4/pkg/tempurl" "github.com/pingcap/pd/v4/pkg/testutil" "github.com/pingcap/pd/v4/server" + "github.com/pingcap/pd/v4/server/config" "github.com/pingcap/pd/v4/tests" "go.uber.org/goleak" // Register schedulers. - "github.com/pingcap/pd/v4/server/config" _ "github.com/pingcap/pd/v4/server/schedulers" )