Skip to content

Commit

Permalink
server: use same initialcluster config to restart joined member (tikv…
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch committed Nov 12, 2018
1 parent 2cf6f01 commit b102b60
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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
// 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")) {
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

0 comments on commit b102b60

Please sign in to comment.