Skip to content

Commit

Permalink
checking for entry in map
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed Jun 7, 2023
1 parent d118518 commit d46a074
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 79 deletions.
73 changes: 26 additions & 47 deletions command/operator/raft/listpeers/operator_raft_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand All @@ -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, "_"))
}
}

Expand Down
29 changes: 1 addition & 28 deletions command/operator/raft/listpeers/operator_raft_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions website/content/commands/operator/raft.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d46a074

Please sign in to comment.