From c8870314c1ae950e5d2094ec061867d90dcbf097 Mon Sep 17 00:00:00 2001 From: Vishal Nayak Date: Mon, 12 Apr 2021 09:33:21 -0400 Subject: [PATCH] Support autopilot when raft is for HA only (#11260) --- vault/logical_system_raft.go | 18 +++++++++--------- vault/raft.go | 2 +- vault/request_forwarding_rpc.go | 19 ++++++++----------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/vault/logical_system_raft.go b/vault/logical_system_raft.go index 8db5f2d331..22ed1da997 100644 --- a/vault/logical_system_raft.go +++ b/vault/logical_system_raft.go @@ -402,8 +402,8 @@ func (b *SystemBackend) handleStorageRaftSnapshotRead() framework.OperationFunc func (b *SystemBackend) handleStorageRaftAutopilotState() framework.OperationFunc { return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { - raftBackend, ok := b.Core.underlyingPhysical.(*raft.RaftBackend) - if !ok { + raftBackend := b.Core.getRaftBackend() + if raftBackend == nil { return logical.ErrorResponse("raft storage is not in use"), logical.ErrInvalidRequest } @@ -431,12 +431,12 @@ func (b *SystemBackend) handleStorageRaftAutopilotState() framework.OperationFun func (b *SystemBackend) handleStorageRaftAutopilotConfigRead() framework.OperationFunc { return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { - raftStorage, ok := b.Core.underlyingPhysical.(*raft.RaftBackend) - if !ok { + raftBackend := b.Core.getRaftBackend() + if raftBackend == nil { return logical.ErrorResponse("raft storage is not in use"), logical.ErrInvalidRequest } - config := raftStorage.AutopilotConfig() + config := raftBackend.AutopilotConfig() if config == nil { return nil, nil } @@ -456,8 +456,8 @@ func (b *SystemBackend) handleStorageRaftAutopilotConfigRead() framework.Operati func (b *SystemBackend) handleStorageRaftAutopilotConfigUpdate() framework.OperationFunc { return func(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { - raftStorage, ok := b.Core.underlyingPhysical.(*raft.RaftBackend) - if !ok { + raftBackend := b.Core.getRaftBackend() + if raftBackend == nil { return logical.ErrorResponse("raft storage is not in use"), logical.ErrInvalidRequest } @@ -506,7 +506,7 @@ func (b *SystemBackend) handleStorageRaftAutopilotConfigUpdate() framework.Opera persist = true } - effectiveConf := raftStorage.AutopilotConfig() + effectiveConf := raftBackend.AutopilotConfig() effectiveConf.Merge(config) if effectiveConf.CleanupDeadServers && effectiveConf.MinQuorum < 3 { @@ -525,7 +525,7 @@ func (b *SystemBackend) handleStorageRaftAutopilotConfigUpdate() framework.Opera } // Set the effectiveConfig - raftStorage.SetAutopilotConfig(effectiveConf) + raftBackend.SetAutopilotConfig(effectiveConf) return nil, nil } diff --git a/vault/raft.go b/vault/raft.go index ae81bd7d36..2c5dec5bd8 100644 --- a/vault/raft.go +++ b/vault/raft.go @@ -169,7 +169,7 @@ func (c *Core) setupRaftActiveNode(ctx context.Context) error { c.logger.Error("failed to load autopilot config from storage when setting up cluster; continuing since autopilot falls back to default config", "error", err) } disableAutopilot := c.disableAutopilot - if c.isRaftHAOnly() || c.IsDRSecondary() { + if c.IsDRSecondary() { disableAutopilot = true } raftBackend.SetupAutopilot(c.activeContext, autopilotConfig, c.raftFollowerStates, disableAutopilot) diff --git a/vault/request_forwarding_rpc.go b/vault/request_forwarding_rpc.go index 0a90f14394..02d3a9dbd0 100644 --- a/vault/request_forwarding_rpc.go +++ b/vault/request_forwarding_rpc.go @@ -7,9 +7,10 @@ import ( "sync/atomic" "time" + "github.com/hashicorp/vault/sdk/helper/consts" + "github.com/hashicorp/vault/helper/forwarding" "github.com/hashicorp/vault/physical/raft" - "github.com/hashicorp/vault/sdk/helper/consts" "github.com/hashicorp/vault/vault/replication" ) @@ -83,10 +84,8 @@ func (s *forwardedRequestRPCServer) Echo(ctx context.Context, in *EchoRequest) ( } if raftBackend := s.core.getRaftBackend(); raftBackend != nil { - if !s.core.isRaftHAOnly() { - reply.RaftAppliedIndex = raftBackend.AppliedIndex() - reply.RaftNodeID = raftBackend.NodeID() - } + reply.RaftAppliedIndex = raftBackend.AppliedIndex() + reply.RaftNodeID = raftBackend.NodeID() } return reply, nil @@ -114,12 +113,10 @@ func (c *forwardingClient) startHeartbeat() { } if raftBackend := c.core.getRaftBackend(); raftBackend != nil { - if !c.core.isRaftHAOnly() { - req.RaftAppliedIndex = raftBackend.AppliedIndex() - req.RaftNodeID = raftBackend.NodeID() - req.RaftTerm = raftBackend.Term() - req.RaftDesiredSuffrage = raftBackend.DesiredSuffrage() - } + req.RaftAppliedIndex = raftBackend.AppliedIndex() + req.RaftNodeID = raftBackend.NodeID() + req.RaftTerm = raftBackend.Term() + req.RaftDesiredSuffrage = raftBackend.DesiredSuffrage() } ctx, cancel := context.WithTimeout(c.echoContext, 2*time.Second)