From d46a0744c6017567b5f19db2be34198f9202afeb Mon Sep 17 00:00:00 2001 From: Ashesh Vidyut Date: Wed, 7 Jun 2023 10:14:12 +0530 Subject: [PATCH] checking for entry in map --- .../raft/listpeers/operator_raft_list.go | 73 +++++++------------ .../raft/listpeers/operator_raft_list_test.go | 29 +------- website/content/commands/operator/raft.mdx | 4 - 3 files changed, 27 insertions(+), 79 deletions(-) diff --git a/command/operator/raft/listpeers/operator_raft_list.go b/command/operator/raft/listpeers/operator_raft_list.go index 9cc4db67250a..4e841dcfaa8e 100644 --- a/command/operator/raft/listpeers/operator_raft_list.go +++ b/command/operator/raft/listpeers/operator_raft_list.go @@ -56,22 +56,12 @@ func (c *cmd) Run(args []string) int { return 1 } - // Fetch the current configuration. - if c.detailed { - result, err := raftListPeersDetailed(client, c.http.Stale()) - if err != nil { - c.UI.Error(fmt.Sprintf("Error getting peers: %v", err)) - return 1 - } - c.UI.Output(result) - } else { - result, err := raftListPeers(client, c.http.Stale()) - if err != nil { - c.UI.Error(fmt.Sprintf("Error getting peers: %v", err)) - return 1 - } - c.UI.Output(result) + result, err := raftListPeers(client, c.http.Stale()) + if err != nil { + c.UI.Error(fmt.Sprintf("Error getting peers: %v", err)) + return 1 } + c.UI.Output(result) return 0 } @@ -85,47 +75,29 @@ func raftListPeers(client *api.Client, stale bool) (string, error) { return "", fmt.Errorf("Failed to retrieve raft configuration: %v", err) } - // Format it as a nice table. - result := []string{"Node\x1fID\x1fAddress\x1fState\x1fVoter\x1fRaftProtocol"} - for _, s := range reply.Servers { - raftProtocol := s.ProtocolVersion - - if raftProtocol == "" { - raftProtocol = "<=1" - } - state := "follower" - if s.Leader { - state = "leader" - } - result = append(result, fmt.Sprintf("%s\x1f%s\x1f%s\x1f%s\x1f%v\x1f%s", - s.Node, s.ID, s.Address, state, s.Voter, raftProtocol)) - } - - return columnize.Format(result, &columnize.Config{Delim: string([]byte{0x1f})}), nil -} - -func raftListPeersDetailed(client *api.Client, stale bool) (string, error) { - q := &api.QueryOptions{ - AllowStale: stale, - } - reply, err := client.Operator().RaftGetConfiguration(q) - if err != nil { - return "", fmt.Errorf("Failed to retrieve raft configuration: %v", err) - } - autoPilotReply, err := client.Operator().GetAutoPilotHealth(q) if err != nil { return "", fmt.Errorf("Failed to retrieve autopilot health: %v", err) } serverHealthDataMap := make(map[string]api.ServerHealth) + leaderLastCommitIndex := uint64(0) for _, serverHealthData := range autoPilotReply.Servers { serverHealthDataMap[serverHealthData.ID] = serverHealthData } + for _, s := range reply.Servers { + if s.Leader { + serverHealthDataLeader, ok := serverHealthDataMap[s.ID] + if ok { + leaderLastCommitIndex = serverHealthDataLeader.LastIndex + } + } + } + // Format it as a nice table. - result := []string{"Node\x1fID\x1fAddress\x1fState\x1fVoter\x1fRaftProtocol\x1fCommitIndex"} + result := []string{"Node\x1fID\x1fAddress\x1fState\x1fVoter\x1fRaftProtocol\x1fCommit Index\x1fTrails Leader By"} for _, s := range reply.Servers { raftProtocol := s.ProtocolVersion @@ -139,11 +111,18 @@ func raftListPeersDetailed(client *api.Client, stale bool) (string, error) { serverHealthData, ok := serverHealthDataMap[s.ID] if ok { - 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, serverHealthData.LastIndex)) + trailsLeaderBy := leaderLastCommitIndex - serverHealthData.LastIndex + trailsLeaderByText := fmt.Sprintf("%d Commits", trailsLeaderBy) + if s.Leader { + trailsLeaderByText = "_" + } else if trailsLeaderBy <= 1 { + 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)) } 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, "")) + s.Node, s.ID, s.Address, state, s.Voter, raftProtocol, "_")) } } diff --git a/command/operator/raft/listpeers/operator_raft_list_test.go b/command/operator/raft/listpeers/operator_raft_list_test.go index f60cad21e279..f6db9a3d0749 100644 --- a/command/operator/raft/listpeers/operator_raft_list_test.go +++ b/command/operator/raft/listpeers/operator_raft_list_test.go @@ -28,34 +28,7 @@ func TestOperatorRaftListPeersCommand(t *testing.T) { a := agent.NewTestAgent(t, ``) defer a.Shutdown() - expected := fmt.Sprintf("%s %s 127.0.0.1:%d leader true 3", - a.Config.NodeName, a.Config.NodeID, a.Config.ServerPort) - - // Test the list-peers subcommand directly - ui := cli.NewMockUi() - c := New(ui) - args := []string{"-http-addr=" + a.HTTPAddr()} - - code := c.Run(args) - if code != 0 { - t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) - } - output := strings.TrimSpace(ui.OutputWriter.String()) - if !strings.Contains(output, expected) { - t.Fatalf("bad: %q, %q", output, expected) - } -} - -func TestOperatorRaftListPeersCommandDetailed(t *testing.T) { - if testing.Short() { - t.Skip("too slow for testing.Short") - } - - t.Parallel() - a := agent.NewTestAgent(t, ``) - defer a.Shutdown() - - expected := fmt.Sprintf("%s %s 127.0.0.1:%d leader true 3 1", + expected := fmt.Sprintf("%s %s 127.0.0.1:%d leader true 3 1 _", a.Config.NodeName, a.Config.NodeID, a.Config.ServerPort) // Test the list-peers subcommand directly diff --git a/website/content/commands/operator/raft.mdx b/website/content/commands/operator/raft.mdx index 76f9be838b54..e5bf6d1e0c81 100644 --- a/website/content/commands/operator/raft.mdx +++ b/website/content/commands/operator/raft.mdx @@ -73,10 +73,6 @@ configuration. we recommend setting this option to `true`. Default is `false`. -- `-detailed` - Outputs additional information 'commit_index' which is -the index of the server's last committed Raft log entry. -Default is `false`. - ## remove-peer Corresponding HTTP API Endpoint: [\[DELETE\] /v1/operator/raft/peer](/consul/api-docs/operator/raft#delete-raft-peer)