From a9feb83003f174d18b2f92483b46189e2141af16 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Tue, 8 Sep 2020 13:43:42 +0800 Subject: [PATCH] config: add initial-cluster-token config (#2691) (#2922) Signed-off-by: disksing --- conf/config.toml | 3 +++ server/config/config.go | 4 ++++ tests/server/server_test.go | 9 +++++++++ 3 files changed, 16 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 45488a764e4..f8a25136b6c 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -65,6 +65,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"` @@ -188,6 +189,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. @@ -481,6 +483,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 { @@ -1159,6 +1162,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 diff --git a/tests/server/server_test.go b/tests/server/server_test.go index c8dc8d94933..6e3c5e75d36 100644 --- a/tests/server/server_test.go +++ b/tests/server/server_test.go @@ -21,6 +21,7 @@ import ( "github.com/tikv/pd/pkg/tempurl" "github.com/tikv/pd/pkg/testutil" "github.com/tikv/pd/server" + "github.com/tikv/pd/server/config" "github.com/tikv/pd/tests" "go.uber.org/goleak" @@ -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) {