-
Notifications
You must be signed in to change notification settings - Fork 0
Migrate from openark/raft fork to upstream hashicorp/raft v1.7 #62
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,8 +18,7 @@ type Store struct { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| raftBind string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| raftAdvertise string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| raft *raft.Raft // The consensus mechanism | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peerStore raft.PeerStore | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| raft *raft.Raft // The consensus mechanism | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| applier CommandApplier | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| snapshotCreatorApplier SnapshotCreatorApplier | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -41,6 +40,16 @@ func NewStore(raftDir string, raftBind string, raftAdvertise string, applier Com | |||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // addUniquePeer adds a peer to the slice if not already present. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| func addUniquePeer(peers []string, peer string) []string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, p := range peers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if p == peer { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return peers | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return append(peers, peer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Open opens the store. If enableSingle is set, and there are no existing peers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // then this node becomes the first node, and therefore leader, of the cluster. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (store *Store) Open(peerNodes []string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -49,6 +58,7 @@ func (store *Store) Open(peerNodes []string) error { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| config.SnapshotThreshold = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| config.SnapshotInterval = snapshotInterval | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| config.ShutdownOnRemove = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| config.LocalID = raft.ServerID(store.raftAdvertise) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Setup Raft communication. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| advertise, err := net.ResolveTCPAddr("tcp", store.raftAdvertise) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -66,24 +76,10 @@ func (store *Store) Open(peerNodes []string) error { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| peers := make([]string, 0, 10) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, peerNode := range peerNodes { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peerNode = strings.TrimSpace(peerNode) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peers = raft.AddUniquePeer(peers, peerNode) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peers = addUniquePeer(peers, peerNode) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Debugf("raft: peers=%+v", peers) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Create peer storage. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| peerStore := &raft.StaticPeers{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := peerStore.SetPeers(peers); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Allow the node to enter single-mode, potentially electing itself, if | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // explicitly enabled and there is only 1 node in the cluster already. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(peerNodes) == 0 && len(peers) <= 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Infof("enabling single-node mode") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| config.EnableSingleNode = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| config.DisableBootstrapAfterElect = false | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if _, err := os.Stat(store.raftDir); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if os.IsNotExist(err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // path does not exist | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -107,11 +103,53 @@ func (store *Store) Open(peerNodes []string) error { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| logStore := NewRelationalStore(store.raftDir) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Debugf("raft: logStore=%+v", logStore) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Bootstrap the cluster if no existing state and this is a single-node setup | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // or the initial set of peers is provided. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| hasState, err := raft.HasExistingState(logStore, logStore, snapshots) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("error checking existing state: %s", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !hasState { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| var servers []raft.Server | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(peerNodes) == 0 && len(peers) <= 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Single-node mode: bootstrap with just this server | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Infof("bootstrapping single-node cluster") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| servers = []raft.Server{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ID: raft.ServerID(store.raftAdvertise), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Address: raft.ServerAddress(store.raftAdvertise), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Multi-node mode: bootstrap with all known peers | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Infof("bootstrapping cluster with peers: %+v", peers) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| localIncluded := false | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, peer := range peers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if peer == store.raftAdvertise { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| localIncluded = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| servers = append(servers, raft.Server{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ID: raft.ServerID(peer), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Address: raft.ServerAddress(peer), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+125
to
+135
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Infof("bootstrapping cluster with peers: %+v", peers) | |
| for _, peer := range peers { | |
| servers = append(servers, raft.Server{ | |
| ID: raft.ServerID(peer), | |
| Address: raft.ServerAddress(peer), | |
| }) | |
| } | |
| log.Infof("bootstrapping cluster with peers: %+v", peers) | |
| localIncluded := false | |
| for _, peer := range peers { | |
| if peer == store.raftAdvertise { | |
| localIncluded = true | |
| } | |
| servers = append(servers, raft.Server{ | |
| ID: raft.ServerID(peer), | |
| Address: raft.ServerAddress(peer), | |
| }) | |
| } | |
| if !localIncluded { | |
| // Ensure the local server is always part of the initial configuration | |
| servers = append(servers, raft.Server{ | |
| ID: raft.ServerID(store.raftAdvertise), | |
| Address: raft.ServerAddress(store.raftAdvertise), | |
| }) | |
| } |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
store.raftAdvertiseasLocalIDcouples server identity to its network address. If the advertised address changes across restarts (IP change, port change, DNS change, etc.), raft will treat the node as a different server ID, which can complicate recovery and membership operations. Prefer a stable node identity (e.g., configured node name/UUID) forLocalID, while keepingServerAddressas the address.