physical/raft: Add a function that gets the offline, stale configuration (#11821)

* Add a function that gets the offline, stale configuration

* Fix comment
This commit is contained in:
Brian Kassouf 2021-06-11 10:25:02 -07:00 committed by GitHub
parent 6874fdf284
commit 0d9ea8a4b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 1 deletions

View File

@ -21,7 +21,7 @@ import (
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/raft"
autopilot "github.com/hashicorp/raft-autopilot"
"github.com/hashicorp/raft-boltdb/v2"
raftboltdb "github.com/hashicorp/raft-boltdb/v2"
snapshot "github.com/hashicorp/raft-snapshot"
"github.com/hashicorp/vault/helper/metricsutil"
"github.com/hashicorp/vault/sdk/helper/consts"
@ -982,6 +982,39 @@ func (b *RaftBackend) RemovePeer(ctx context.Context, peerID string) error {
return b.autopilot.RemoveServer(raft.ServerID(peerID))
}
// GetConfigurationOffline is used to read the stale, last known raft
// configuration to this node. It accesses the last state written into the
// FSM. When a server is online use GetConfiguration instead.
func (b *RaftBackend) GetConfigurationOffline() (*RaftConfigurationResponse, error) {
b.l.RLock()
defer b.l.RUnlock()
if b.raft != nil {
return nil, errors.New("raft storage is initialized, used GetConfiguration instead")
}
if b.fsm == nil {
return nil, nil
}
state, configuration := b.fsm.LatestState()
config := &RaftConfigurationResponse{
Index: state.Index,
}
for _, server := range configuration.Servers {
entry := &RaftServer{
NodeID: server.Id,
Address: server.Address,
// Since we are offline no node is the leader.
Leader: false,
Voter: raft.ServerSuffrage(server.Suffrage) == raft.Voter,
}
config.Servers = append(config.Servers, entry)
}
return config, nil
}
func (b *RaftBackend) GetConfiguration(ctx context.Context) (*RaftConfigurationResponse, error) {
b.l.RLock()
defer b.l.RUnlock()