Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: add initial-cluster-token config #2691

Merged
merged 5 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions conf/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 4 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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"

Expand Down Expand Up @@ -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) {
Expand Down