Skip to content

Commit

Permalink
raft: Allow Join to be called when already part of a cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
  • Loading branch information
aaronlehmann committed May 23, 2017
1 parent 00b418f commit eefde43
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions manager/state/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,16 @@ func (n *Node) JoinAndStart(ctx context.Context) (err error) {
// restore from snapshot
if loadAndStartErr == nil {
if n.opts.JoinAddr != "" {
log.G(ctx).Warning("ignoring request to join cluster, because raft state already exists")
if err := n.joinCluster(ctx); err != nil {
return errors.Wrap(err, "failed to rejoin cluster")
}
}
n.campaignWhenAble = true
n.initTransport()
n.raftNode = raft.RestartNode(n.Config)
return nil
}

// first member of cluster
if n.opts.JoinAddr == "" {
// First member in the cluster, self-assign ID
n.Config.ID = uint64(rand.Int63()) + 1
Expand All @@ -417,6 +418,22 @@ func (n *Node) JoinAndStart(ctx context.Context) (err error) {
}

// join to existing cluster

if err := n.joinCluster(ctx); err != nil {
return err
}

if _, err := n.newRaftLogs(n.opts.ID); err != nil {
return err
}

n.initTransport()
n.raftNode = raft.StartNode(n.Config, nil)

return nil
}

func (n *Node) joinCluster(ctx context.Context) error {
if n.opts.Addr == "" {
return errors.New("attempted to join raft cluster without knowing own address")
}
Expand All @@ -438,15 +455,7 @@ func (n *Node) JoinAndStart(ctx context.Context) (err error) {
}

n.Config.ID = resp.RaftID

if _, err := n.newRaftLogs(n.opts.ID); err != nil {
return err
}
n.bootstrapMembers = resp.Members

n.initTransport()
n.raftNode = raft.StartNode(n.Config, nil)

return nil
}

Expand Down

0 comments on commit eefde43

Please sign in to comment.