Skip to content

Commit

Permalink
server: use the same initcluster to restart joined member
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch committed Oct 18, 2018
1 parent a6b3a6c commit 2847674
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/integration_test/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package integration

import (
"context"
"os"
"time"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -44,6 +45,8 @@ func (s *integrationTestSuite) TestSimpleJoin(c *C) {
c.Assert(err, IsNil)
err = pd2.Run(context.TODO())
c.Assert(err, IsNil)
_, err = os.Stat(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 +60,8 @@ func (s *integrationTestSuite) TestSimpleJoin(c *C) {
c.Assert(err, IsNil)
err = pd3.Run(context.TODO())
c.Assert(err, IsNil)
_, err = os.Stat(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
39 changes: 37 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,15 @@ 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

retryTimes = 100
)

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

// Cases with data directory.
filePath := 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 @@ -138,7 +160,20 @@ func PrepareJoinCluster(cfg *Config) error {
initialCluster = strings.Join(pds, ",")
cfg.InitialCluster = initialCluster
cfg.InitialClusterState = embed.ClusterStateFlagExisting
return nil
err = os.Mkdir(cfg.DataDir, privateDirMode)
if err != nil && !os.IsExist(err) {
return err
}

for i := 0; i < retryTimes; i++ {
err = ioutil.WriteFile(filePath, []byte(cfg.InitialCluster), privateFileMode)
if err != nil {
log.Errorf("persist join config failed: %s", err)
continue
}
break
}
return err
}

func isDataExist(d string) bool {
Expand Down

0 comments on commit 2847674

Please sign in to comment.