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
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
40 changes: 38 additions & 2 deletions server/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ package server

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

"github.com/coreos/etcd/clientv3"
Expand All @@ -26,6 +28,15 @@ 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

retryTimes = 100
)

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

// Cases with data directory.
filePath := filepath.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 @@ -138,7 +161,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to use MkdirAll here?

if err != nil && !os.IsExist(err) {
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithStack?

}

for i := 0; i < retryTimes; i++ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we don't need to retry. It's ok to exit directly.

err = ioutil.WriteFile(filePath, []byte(cfg.InitialCluster), privateFileMode)
if err != nil {
log.Errorf("persist join config failed: %s", err)
continue
}
break
}
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithStack?

}

func isDataExist(d string) bool {
Expand Down