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

server: use same initialcluster config to restart joined member #1279

Merged
merged 10 commits into from
Oct 25, 2018
6 changes: 6 additions & 0 deletions pkg/integration_test/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package integration

import (
"context"
"os"
"path"
"time"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -44,6 +46,8 @@ func (s *integrationTestSuite) TestSimpleJoin(c *C) {
c.Assert(err, IsNil)
err = pd2.Run(context.TODO())
c.Assert(err, IsNil)
_, err = os.Stat(path.Join(pd2.GetConfig().DataDir, "join"))
c.Assert(os.IsNotExist(err), IsFalse)
members, err = etcdutil.ListEtcdMembers(client)
c.Assert(err, IsNil)
c.Assert(members.Members, HasLen, 2)
Expand All @@ -57,6 +61,8 @@ func (s *integrationTestSuite) TestSimpleJoin(c *C) {
c.Assert(err, IsNil)
err = pd3.Run(context.TODO())
c.Assert(err, IsNil)
_, err = os.Stat(path.Join(pd3.GetConfig().DataDir, "join"))
c.Assert(os.IsNotExist(err), IsFalse)
members, err = etcdutil.ListEtcdMembers(client)
c.Assert(err, IsNil)
c.Assert(members.Members, HasLen, 3)
Expand Down
36 changes: 34 additions & 2 deletions server/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package server

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand All @@ -26,6 +27,13 @@ import (
log "github.com/sirupsen/logrus"
)

const (
// privateFileMode grants owner to read/write a file.
privateFileMode = 0600
nolouch marked this conversation as resolved.
Show resolved Hide resolved
// privateDirMode grants owner to make/remove files inside the directory.
privateDirMode = 0700
)

// PrepareJoinCluster sends MemberAdd command to PD cluster,
// and returns the initial configuration of the PD cluster.
//
Expand Down Expand Up @@ -73,8 +81,20 @@ func PrepareJoinCluster(cfg *Config) error {
return errors.New("join self is forbidden")
}

// Cases with data directory.
filePath := path.Join(cfg.DataDir, "join")
// Read the persist join config
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
s, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatal("read the join config meet error: ", err)
}
cfg.InitialCluster = strings.TrimSpace(string(s))
cfg.InitialClusterState = embed.ClusterStateFlagExisting
return nil
}

initialCluster := ""
// Cases with data directory.
if isDataExist(path.Join(cfg.DataDir, "member")) {
nolouch marked this conversation as resolved.
Show resolved Hide resolved
cfg.InitialCluster = initialCluster
cfg.InitialClusterState = embed.ClusterStateFlagExisting
Expand Down Expand Up @@ -103,6 +123,9 @@ func PrepareJoinCluster(cfg *Config) error {

existed := false
for _, m := range listResp.Members {
if len(m.Name) == 0 {
return errors.New("there is a member that has not joined successfully")
}
if m.Name == cfg.Name {
existed = true
}
Expand Down Expand Up @@ -131,14 +154,23 @@ func PrepareJoinCluster(cfg *Config) error {
if memb.ID == addResp.Member.ID {
n = cfg.Name
}
if len(n) == 0 {
return errors.New("there is a member that has not joined successfully")
}
for _, m := range memb.PeerURLs {
pds = append(pds, fmt.Sprintf("%s=%s", n, m))
}
}
initialCluster = strings.Join(pds, ",")
cfg.InitialCluster = initialCluster
cfg.InitialClusterState = embed.ClusterStateFlagExisting
return nil
err = os.MkdirAll(cfg.DataDir, privateDirMode)
if err != nil && !os.IsExist(err) {
return errors.WithStack(err)
}

err = ioutil.WriteFile(filePath, []byte(cfg.InitialCluster), privateFileMode)
return errors.WithStack(err)
}

func isDataExist(d string) bool {
Expand Down