From 8e1f7285d00d968130d98f2d857e7839c5cf2013 Mon Sep 17 00:00:00 2001 From: Ashesh Vidyut Date: Thu, 8 Jun 2023 22:07:18 +0530 Subject: [PATCH] pr comment changes single API instead of two --- agent/consul/operator_raft_endpoint.go | 7 +++++++ agent/structs/operator.go | 3 +++ api/operator_raft.go | 3 +++ .../raft/listpeers/operator_raft_list.go | 21 +++++++------------ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/agent/consul/operator_raft_endpoint.go b/agent/consul/operator_raft_endpoint.go index f619b611f7da..7b0bcbc5cc03 100644 --- a/agent/consul/operator_raft_endpoint.go +++ b/agent/consul/operator_raft_endpoint.go @@ -48,6 +48,12 @@ func (op *Operator) RaftGetConfiguration(args *structs.DCSpecificRequest, reply serverMap[raft.ServerAddress(addr)] = member } + serverIDLastIndexMap := make(map[raft.ServerID]uint64) + + for _, serverState := range op.srv.autopilot.GetState().Servers { + serverIDLastIndexMap[serverState.Server.ID] = serverState.Stats.LastIndex + } + // Fill out the reply. leader := op.srv.raft.Leader() reply.Index = future.Index() @@ -66,6 +72,7 @@ func (op *Operator) RaftGetConfiguration(args *structs.DCSpecificRequest, reply Leader: server.Address == leader, Voter: server.Suffrage == raft.Voter, ProtocolVersion: raftProtocolVersion, + LastIndex: serverIDLastIndexMap[server.ID], } reply.Servers = append(reply.Servers, entry) } diff --git a/agent/structs/operator.go b/agent/structs/operator.go index f5c2b8ac86df..05862861e3f8 100644 --- a/agent/structs/operator.go +++ b/agent/structs/operator.go @@ -34,6 +34,9 @@ type RaftServer struct { // it's a non-voting server, which will be added in a future release of // Consul. Voter bool + + // LastIndex is the last log index this server has a record of in its Raft log. + LastIndex uint64 } // RaftConfigurationResponse is returned when querying for the current Raft diff --git a/api/operator_raft.go b/api/operator_raft.go index 393d6fb3c5f7..d72c00c97b93 100644 --- a/api/operator_raft.go +++ b/api/operator_raft.go @@ -28,6 +28,9 @@ type RaftServer struct { // it's a non-voting server, which will be added in a future release of // Consul. Voter bool + + // LastIndex is the last log index this server has a record of in its Raft log. + LastIndex uint64 } // RaftConfiguration is returned when querying for the current Raft configuration. diff --git a/command/operator/raft/listpeers/operator_raft_list.go b/command/operator/raft/listpeers/operator_raft_list.go index 10c9c57b5a63..495b2f64d185 100644 --- a/command/operator/raft/listpeers/operator_raft_list.go +++ b/command/operator/raft/listpeers/operator_raft_list.go @@ -70,23 +70,18 @@ func raftListPeers(client *api.Client, stale bool) (string, error) { return "", fmt.Errorf("Failed to retrieve raft configuration: %v", err) } - autoPilotReply, err := client.Operator().AutopilotServerHealth(q) - if err != nil { - return "", fmt.Errorf("Failed to retrieve autopilot health: %v", err) - } - - serverHealthDataMap := make(map[string]api.ServerHealth) leaderLastCommitIndex := uint64(0) + serverIdLastIndexMap := make(map[string]uint64) - for _, serverHealthData := range autoPilotReply.Servers { - serverHealthDataMap[serverHealthData.ID] = serverHealthData + for _, raftServer := range reply.Servers { + serverIdLastIndexMap[raftServer.ID] = raftServer.LastIndex } for _, s := range reply.Servers { if s.Leader { - serverHealthDataLeader, ok := serverHealthDataMap[s.ID] + lastIndex, ok := serverIdLastIndexMap[s.ID] if ok { - leaderLastCommitIndex = serverHealthDataLeader.LastIndex + leaderLastCommitIndex = lastIndex } } } @@ -104,9 +99,9 @@ func raftListPeers(client *api.Client, stale bool) (string, error) { state = "leader" } - serverHealthData, ok := serverHealthDataMap[s.ID] + serverLastIndex, ok := serverIdLastIndexMap[s.ID] if ok { - trailsLeaderBy := leaderLastCommitIndex - serverHealthData.LastIndex + trailsLeaderBy := leaderLastCommitIndex - serverLastIndex trailsLeaderByText := fmt.Sprintf("%d commits", trailsLeaderBy) if s.Leader { trailsLeaderByText = "-" @@ -114,7 +109,7 @@ func raftListPeers(client *api.Client, stale bool) (string, error) { trailsLeaderByText = fmt.Sprintf("%d commit", trailsLeaderBy) } result = append(result, fmt.Sprintf("%s\x1f%s\x1f%s\x1f%s\x1f%v\x1f%s\x1f%v\x1f%s", - s.Node, s.ID, s.Address, state, s.Voter, raftProtocol, serverHealthData.LastIndex, trailsLeaderByText)) + s.Node, s.ID, s.Address, state, s.Voter, raftProtocol, serverLastIndex, trailsLeaderByText)) } else { result = append(result, fmt.Sprintf("%s\x1f%s\x1f%s\x1f%s\x1f%v\x1f%s\x1f%v", s.Node, s.ID, s.Address, state, s.Voter, raftProtocol, "-"))