Skip to content

Commit

Permalink
Move Raft protocol version for list peers end point to server side, f…
Browse files Browse the repository at this point in the history
…ix unit tests. This fixes #3449
  • Loading branch information
Preetha Appan committed Sep 26, 2017
1 parent 99246d3 commit 3c4a108
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 43 deletions.
13 changes: 8 additions & 5 deletions agent/consul/operator_raft_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ func (op *Operator) RaftGetConfiguration(args *structs.DCSpecificRequest, reply
reply.Index = future.Index()
for _, server := range future.Configuration().Servers {
node := "(unknown)"
raftProtocolVersion := "unknown"
if member, ok := serverMap[server.Address]; ok {
node = member.Name
raftProtocolVersion = member.Tags["raft_vsn"]
}

entry := &structs.RaftServer{
ID: server.ID,
Node: node,
Address: server.Address,
Leader: server.Address == leader,
Voter: server.Suffrage == raft.Voter,
ID: server.ID,
Node: node,
Address: server.Address,
Leader: server.Address == leader,
Voter: server.Suffrage == raft.Voter,
ProtocolVersion: raftProtocolVersion,
}
reply.Servers = append(reply.Servers, entry)
}
Expand Down
32 changes: 15 additions & 17 deletions agent/consul/operator_raft_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package consul
import (
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/hashicorp/consul/testrpc"
"github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/raft"
"github.com/pascaldekloe/goe/verify"
)

func TestOperator_RaftGetConfiguration(t *testing.T) {
Expand Down Expand Up @@ -44,18 +44,17 @@ func TestOperator_RaftGetConfiguration(t *testing.T) {
expected := structs.RaftConfigurationResponse{
Servers: []*structs.RaftServer{
&structs.RaftServer{
ID: me.ID,
Node: s1.config.NodeName,
Address: me.Address,
Leader: true,
Voter: true,
ID: me.ID,
Node: s1.config.NodeName,
Address: me.Address,
Leader: true,
Voter: true,
ProtocolVersion: "3",
},
},
Index: future.Index(),
}
if !reflect.DeepEqual(reply, expected) {
t.Fatalf("bad: %v", reply)
}
verify.Values(t, "", reply, expected)
}

func TestOperator_RaftGetConfiguration_ACLDeny(t *testing.T) {
Expand Down Expand Up @@ -121,18 +120,17 @@ func TestOperator_RaftGetConfiguration_ACLDeny(t *testing.T) {
expected := structs.RaftConfigurationResponse{
Servers: []*structs.RaftServer{
&structs.RaftServer{
ID: me.ID,
Node: s1.config.NodeName,
Address: me.Address,
Leader: true,
Voter: true,
ID: me.ID,
Node: s1.config.NodeName,
Address: me.Address,
Leader: true,
Voter: true,
ProtocolVersion: "3",
},
},
Index: future.Index(),
}
if !reflect.DeepEqual(reply, expected) {
t.Fatalf("bad: %v", reply)
}
verify.Values(t, "", reply, expected)
}

func TestOperator_RaftRemovePeerByAddress(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion agent/structs/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ type RaftServer struct {
// Leader is true if this server is the current cluster leader.
Leader bool

// Protocol version is the raft protocol version used by the server
ProtocolVersion string

// Voter is true if this server has a vote in the cluster. This might
// be false if the server is staging and still coming online, or if
// it's a non-voting server, which will be added in a future release of
// Consul.
Voter bool
}

// RaftConfigrationResponse is returned when querying for the current Raft
// RaftConfigurationResponse is returned when querying for the current Raft
// configuration.
type RaftConfigurationResponse struct {
// Servers has the list of servers in the Raft configuration.
Expand Down
5 changes: 4 additions & 1 deletion api/operator_raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ type RaftServer struct {
// Leader is true if this server is the current cluster leader.
Leader bool

// Protocol version is the raft protocol version used by the server
ProtocolVersion string

// Voter is true if this server has a vote in the cluster. This might
// be false if the server is staging and still coming online, or if
// it's a non-voting server, which will be added in a future release of
// Consul.
Voter bool
}

// RaftConfigration is returned when querying for the current Raft configuration.
// RaftConfiguration is returned when querying for the current Raft configuration.
type RaftConfiguration struct {
// Servers has the list of servers in the Raft configuration.
Servers []*RaftServer
Expand Down
21 changes: 2 additions & 19 deletions command/operator_raft_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/serf/serf"
"github.com/ryanuber/columnize"
)

Expand Down Expand Up @@ -58,23 +57,6 @@ func (c *OperatorRaftListCommand) Run(args []string) int {
}

func raftListPeers(client *api.Client, stale bool) (string, error) {
raftProtocols := make(map[string]string)
members, err := client.Agent().Members(false)
if err != nil {
return "", err
}

for _, member := range members {
if serf.MemberStatus(member.Status) == serf.StatusLeft {
continue
}

if member.Tags["role"] != "consul" {
continue
}

raftProtocols[member.Name] = member.Tags["raft_vsn"]
}

q := &api.QueryOptions{
AllowStale: stale,
Expand All @@ -87,7 +69,8 @@ func raftListPeers(client *api.Client, stale bool) (string, error) {
// Format it as a nice table.
result := []string{"Node|ID|Address|State|Voter|RaftProtocol"}
for _, s := range reply.Servers {
raftProtocol := raftProtocols[s.Node]
raftProtocol := s.ProtocolVersion

if raftProtocol == "" {
raftProtocol = "<=1"
}
Expand Down

0 comments on commit 3c4a108

Please sign in to comment.