Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions manager/state/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type Node struct {
wait *wait
wal *wal.WAL
snapshotter *snap.Snapshotter
restored bool
campaignWhenAble bool
signalledLeadership uint32
isMember uint32
joinAddr string
Expand Down Expand Up @@ -289,12 +289,7 @@ func (n *Node) JoinAndStart() (err error) {
return err
}
n.Node = raft.StartNode(n.Config, []raft.Peer{peer})
if err := n.Campaign(n.Ctx); err != nil {
if walErr := n.wal.Close(); err != nil {
n.Config.Logger.Errorf("raft: error closing WAL: %v", walErr)
}
return err
}
n.campaignWhenAble = true
}
atomic.StoreUint32(&n.isMember, 1)
return nil
Expand All @@ -303,6 +298,7 @@ func (n *Node) JoinAndStart() (err error) {
if n.joinAddr != "" {
n.Config.Logger.Warning("ignoring request to join cluster, because raft state already exists")
}
n.campaignWhenAble = true
n.Node = raft.RestartNode(n.Config)
atomic.StoreUint32(&n.isMember, 1)
return nil
Expand Down Expand Up @@ -458,17 +454,19 @@ func (n *Node) Run(ctx context.Context) error {
// Advance the state machine
n.Advance()

// If we are the only registered member after
// restoring from the state, campaign to be the
// leader.
if !n.restored {
// Node ID should be in the progress list to Campaign
if len(n.cluster.Members()) <= 1 {
// On the first startup, or if we are the only
// registered member after restoring from the state,
// campaign to be the leader.
if n.campaignWhenAble {
members := n.cluster.Members()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder why not just campaign once if you're sole member?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, I see. Can you pls change comment, so it'll reflect that we start campaign also on inital cluster start?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Comment updated, thanks for noticing that.

if len(members) >= 1 {
n.campaignWhenAble = false
}
if len(members) == 1 && members[n.Config.ID] != nil {
if err := n.Campaign(n.Ctx); err != nil {
panic("raft: cannot campaign to be the leader on node restore")
}
}
n.restored = true
}

case snapshotIndex := <-n.snapshotInProgress:
Expand Down Expand Up @@ -947,6 +945,7 @@ func (n *Node) registerNode(node *api.RaftMember) error {
}
return err
}

return nil
}

Expand Down