Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Change node state to enum from string #93

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions dispatcher/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/blevesearch/bleve/search"
"github.com/golang/protobuf/ptypes/any"
"github.com/golang/protobuf/ptypes/empty"
"github.com/hashicorp/raft"
"github.com/mosuka/blast/indexer"
"github.com/mosuka/blast/indexutils"
"github.com/mosuka/blast/manager"
Expand Down Expand Up @@ -99,7 +98,7 @@ func (s *GRPCService) getManagerClient() (*manager.GRPCClient, error) {
continue
}

if node.State == raft.Leader.String() || node.State == raft.Follower.String() {
if node.State == management.Node_FOLLOWER || node.State == management.Node_LEADER {
var ok bool
client, ok = s.managerClients[id]
if ok {
Expand All @@ -108,7 +107,7 @@ func (s *GRPCService) getManagerClient() (*manager.GRPCClient, error) {
s.logger.Error("node does not exist", zap.String("id", id))
}
} else {
s.logger.Debug("node has not available", zap.String("id", id), zap.String("state", node.State))
s.logger.Debug("node has not available", zap.String("id", id), zap.String("state", node.State.String()))
}
}

Expand Down
12 changes: 6 additions & 6 deletions dispatcher/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestServer_Start(t *testing.T) {

managerNode1 := &management.Node{
BindAddress: managerBindAddress1,
State: "",
State: management.Node_UNKNOWN,
Metadata: &management.Metadata{
GrpcAddress: managerGrpcAddress1,
HttpAddress: managerHttpAddress1,
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestServer_Start(t *testing.T) {

managerNode2 := &management.Node{
BindAddress: managerBindAddress2,
State: "",
State: management.Node_UNKNOWN,
Metadata: &management.Metadata{
GrpcAddress: managerGrpcAddress2,
HttpAddress: managerHttpAddress2,
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestServer_Start(t *testing.T) {

managerNode3 := &management.Node{
BindAddress: managerBindAddress3,
State: "",
State: management.Node_UNKNOWN,
Metadata: &management.Metadata{
GrpcAddress: managerGrpcAddress3,
HttpAddress: managerHttpAddress3,
Expand Down Expand Up @@ -167,23 +167,23 @@ func TestServer_Start(t *testing.T) {
Nodes: map[string]*management.Node{
managerNodeId1: {
BindAddress: managerBindAddress1,
State: raft.Leader.String(),
State: management.Node_LEADER,
Metadata: &management.Metadata{
GrpcAddress: managerGrpcAddress1,
HttpAddress: managerHttpAddress1,
},
},
managerNodeId2: {
BindAddress: managerBindAddress2,
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: managerGrpcAddress2,
HttpAddress: managerHttpAddress2,
},
},
managerNodeId3: {
BindAddress: managerBindAddress3,
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: managerGrpcAddress3,
HttpAddress: managerHttpAddress3,
Expand Down
4 changes: 2 additions & 2 deletions indexer/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (s *GRPCService) getManagerClient() (*manager.GRPCClient, error) {
continue
}

if node.State == raft.Leader.String() || node.State == raft.Follower.String() {
if node.State == management.Node_FOLLOWER || node.State == management.Node_LEADER {
var ok bool
client, ok = s.managerClients[id]
if ok {
Expand All @@ -116,7 +116,7 @@ func (s *GRPCService) getManagerClient() (*manager.GRPCClient, error) {
s.logger.Error("node does not exist", zap.String("id", id))
}
} else {
s.logger.Debug("node has not available", zap.String("id", id), zap.String("state", node.State))
s.logger.Debug("node has not available", zap.String("id", id), zap.String("state", node.State.String()))
}
}

Expand Down
47 changes: 21 additions & 26 deletions manager/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,18 @@ func (s *GRPCService) Stop() error {
}

func (s *GRPCService) getLeaderClient() (*GRPCClient, error) {
var client *GRPCClient

for id, node := range s.cluster.Nodes {
state := node.State
if node.State == "" {
s.logger.Warn("missing state", zap.String("id", id), zap.String("state", state))
continue
}

if state == raft.Leader.String() {
var ok bool
client, ok = s.peerClients[id]
if ok {
break
} else {
s.logger.Error("node does not exist", zap.String("id", id))
switch node.State {
case management.Node_LEADER:
if client, exist := s.peerClients[id]; exist {
return client, nil
}
} else {
s.logger.Debug("not a leader", zap.String("id", id))
}
}

if client == nil {
err := errors.New("there is no leader")
s.logger.Error(err.Error())
return nil, err
}

return client, nil
err := errors.New("there is no leader")
s.logger.Error(err.Error())
return nil, err
}

func (s *GRPCService) cloneCluster(cluster *management.Cluster) (*management.Cluster, error) {
Expand Down Expand Up @@ -319,7 +302,19 @@ func (s *GRPCService) NodeID() string {

func (s *GRPCService) getSelfNode() *management.Node {
node := s.raftServer.node
node.State = s.raftServer.State().String()

switch s.raftServer.State() {
case raft.Follower:
node.State = management.Node_FOLLOWER
case raft.Candidate:
node.State = management.Node_CANDIDATE
case raft.Leader:
node.State = management.Node_LEADER
case raft.Shutdown:
node.State = management.Node_SHUTDOWN
default:
node.State = management.Node_UNKNOWN
}

return node
}
Expand All @@ -336,7 +331,7 @@ func (s *GRPCService) getPeerNode(id string) (*management.Node, error) {
s.logger.Debug(err.Error(), zap.String("id", id))
return &management.Node{
BindAddress: "",
State: raft.Shutdown.String(),
State: management.Node_UNKNOWN,
Metadata: &management.Metadata{
GrpcAddress: "",
HttpAddress: "",
Expand Down
29 changes: 14 additions & 15 deletions manager/raft_fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"reflect"
"testing"

"github.com/hashicorp/raft"
"github.com/mosuka/blast/logutils"
"github.com/mosuka/blast/protobuf/management"
)
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestRaftFSM_GetNode(t *testing.T) {
"node1",
&management.Node{
BindAddress: "2100",
State: raft.Leader.String(),
State: management.Node_LEADER,
Metadata: &management.Metadata{
GrpcAddress: "5100",
HttpAddress: "8100",
Expand All @@ -69,7 +68,7 @@ func TestRaftFSM_GetNode(t *testing.T) {
"node2",
&management.Node{
BindAddress: "2110",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5110",
HttpAddress: "8110",
Expand All @@ -80,7 +79,7 @@ func TestRaftFSM_GetNode(t *testing.T) {
"node3",
&management.Node{
BindAddress: "2120",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5120",
HttpAddress: "8120",
Expand All @@ -95,7 +94,7 @@ func TestRaftFSM_GetNode(t *testing.T) {

exp1 := &management.Node{
BindAddress: "2110",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5110",
HttpAddress: "8110",
Expand Down Expand Up @@ -142,7 +141,7 @@ func TestRaftFSM_SetNode(t *testing.T) {
"node1",
&management.Node{
BindAddress: "2100",
State: raft.Leader.String(),
State: management.Node_LEADER,
Metadata: &management.Metadata{
GrpcAddress: "5100",
HttpAddress: "8100",
Expand All @@ -153,7 +152,7 @@ func TestRaftFSM_SetNode(t *testing.T) {
"node2",
&management.Node{
BindAddress: "2110",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5110",
HttpAddress: "8110",
Expand All @@ -164,7 +163,7 @@ func TestRaftFSM_SetNode(t *testing.T) {
"node3",
&management.Node{
BindAddress: "2120",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5120",
HttpAddress: "8120",
Expand All @@ -178,7 +177,7 @@ func TestRaftFSM_SetNode(t *testing.T) {
}
exp1 := &management.Node{
BindAddress: "2110",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5110",
HttpAddress: "8110",
Expand All @@ -193,7 +192,7 @@ func TestRaftFSM_SetNode(t *testing.T) {
"node2",
&management.Node{
BindAddress: "2110",
State: raft.Shutdown.String(),
State: management.Node_SHUTDOWN,
Metadata: &management.Metadata{
GrpcAddress: "5110",
HttpAddress: "8110",
Expand All @@ -207,7 +206,7 @@ func TestRaftFSM_SetNode(t *testing.T) {
}
exp2 := &management.Node{
BindAddress: "2110",
State: raft.Shutdown.String(),
State: management.Node_SHUTDOWN,
Metadata: &management.Metadata{
GrpcAddress: "5110",
HttpAddress: "8110",
Expand Down Expand Up @@ -253,7 +252,7 @@ func TestRaftFSM_DeleteNode(t *testing.T) {
"node1",
&management.Node{
BindAddress: "2100",
State: raft.Leader.String(),
State: management.Node_LEADER,
Metadata: &management.Metadata{
GrpcAddress: "5100",
HttpAddress: "8100",
Expand All @@ -264,7 +263,7 @@ func TestRaftFSM_DeleteNode(t *testing.T) {
"node2",
&management.Node{
BindAddress: "2110",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5110",
HttpAddress: "8110",
Expand All @@ -275,7 +274,7 @@ func TestRaftFSM_DeleteNode(t *testing.T) {
"node3",
&management.Node{
BindAddress: "2120",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5120",
HttpAddress: "8120",
Expand All @@ -289,7 +288,7 @@ func TestRaftFSM_DeleteNode(t *testing.T) {
}
exp1 := &management.Node{
BindAddress: "2110",
State: raft.Follower.String(),
State: management.Node_FOLLOWER,
Metadata: &management.Metadata{
GrpcAddress: "5110",
HttpAddress: "8110",
Expand Down
Loading