Skip to content

Commit 1c2989e

Browse files
export node State
1 parent f72d204 commit 1c2989e

File tree

8 files changed

+125
-123
lines changed

8 files changed

+125
-123
lines changed

memberlist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ func (m *Memberlist) hasLeft() bool {
736736
return atomic.LoadInt32(&m.leave) == 1
737737
}
738738

739-
func (m *Memberlist) getNodeState(addr string) nodeStateType {
739+
func (m *Memberlist) getNodeState(addr string) NodeStateType {
740740
m.nodeLock.RLock()
741741
defer m.nodeLock.RUnlock()
742742

memberlist_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,9 +604,9 @@ func TestMemberList_Members(t *testing.T) {
604604

605605
m := &Memberlist{}
606606
nodes := []*nodeState{
607-
&nodeState{Node: *n1, State: stateAlive},
608-
&nodeState{Node: *n2, State: stateDead},
609-
&nodeState{Node: *n3, State: stateSuspect},
607+
&nodeState{Node: *n1, State: StateAlive},
608+
&nodeState{Node: *n2, State: StateDead},
609+
&nodeState{Node: *n3, State: StateSuspect},
610610
}
611611
m.nodes = nodes
612612

@@ -992,7 +992,7 @@ func TestMemberlist_Leave(t *testing.T) {
992992
t.Fatalf("should have 1 node")
993993
}
994994

995-
if m2.nodeMap[c1.Name].State != stateLeft {
995+
if m2.nodeMap[c1.Name].State != StateLeft {
996996
t.Fatalf("bad state")
997997
}
998998
}

net.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ type pushNodeState struct {
176176
Port uint16
177177
Meta []byte
178178
Incarnation uint32
179-
State nodeStateType
179+
State NodeStateType
180180
Vsn []uint8 // Protocol versions
181181
}
182182

@@ -1148,16 +1148,17 @@ func (m *Memberlist) mergeRemoteState(join bool, remoteNodes []pushNodeState, us
11481148
nodes := make([]*Node, len(remoteNodes))
11491149
for idx, n := range remoteNodes {
11501150
nodes[idx] = &Node{
1151-
Name: n.Name,
1152-
Addr: n.Addr,
1153-
Port: n.Port,
1154-
Meta: n.Meta,
1155-
PMin: n.Vsn[0],
1156-
PMax: n.Vsn[1],
1157-
PCur: n.Vsn[2],
1158-
DMin: n.Vsn[3],
1159-
DMax: n.Vsn[4],
1160-
DCur: n.Vsn[5],
1151+
Name: n.Name,
1152+
Addr: n.Addr,
1153+
Port: n.Port,
1154+
Meta: n.Meta,
1155+
State: n.State,
1156+
PMin: n.Vsn[0],
1157+
PMax: n.Vsn[1],
1158+
PCur: n.Vsn[2],
1159+
DMin: n.Vsn[3],
1160+
DMax: n.Vsn[4],
1161+
DCur: n.Vsn[5],
11611162
}
11621163
}
11631164
if err := m.config.Merge.NotifyMerge(nodes); err != nil {

net_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func TestTCPPushPull(t *testing.T) {
443443
Port: uint16(m.config.BindPort),
444444
},
445445
Incarnation: 0,
446-
State: stateSuspect,
446+
State: StateSuspect,
447447
StateChange: time.Now().Add(-1 * time.Second),
448448
})
449449

@@ -459,17 +459,17 @@ func TestTCPPushPull(t *testing.T) {
459459
localNodes[0].Addr = net.ParseIP(m.config.BindAddr)
460460
localNodes[0].Port = uint16(m.config.BindPort)
461461
localNodes[0].Incarnation = 1
462-
localNodes[0].State = stateAlive
462+
localNodes[0].State = StateAlive
463463
localNodes[1].Name = "Test 1"
464464
localNodes[1].Addr = net.ParseIP(m.config.BindAddr)
465465
localNodes[1].Port = uint16(m.config.BindPort)
466466
localNodes[1].Incarnation = 1
467-
localNodes[1].State = stateAlive
467+
localNodes[1].State = StateAlive
468468
localNodes[2].Name = "Test 2"
469469
localNodes[2].Addr = net.ParseIP(m.config.BindAddr)
470470
localNodes[2].Port = uint16(m.config.BindPort)
471471
localNodes[2].Incarnation = 1
472-
localNodes[2].State = stateAlive
472+
localNodes[2].State = StateAlive
473473

474474
// Send our node state
475475
header := pushPullHeader{Nodes: 3}
@@ -552,7 +552,7 @@ func TestTCPPushPull(t *testing.T) {
552552
if n.Incarnation != 0 {
553553
t.Fatal("bad incarnation")
554554
}
555-
if n.State != stateSuspect {
555+
if n.State != StateSuspect {
556556
t.Fatal("bad state")
557557
}
558558
}

state.go

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@ import (
1313
metrics "github.com/armon/go-metrics"
1414
)
1515

16-
type nodeStateType int
16+
type NodeStateType int
1717

1818
const (
19-
stateAlive nodeStateType = iota
20-
stateSuspect
21-
stateDead
22-
stateLeft
19+
StateAlive NodeStateType = iota
20+
StateSuspect
21+
StateDead
22+
StateLeft
2323
)
2424

2525
// Node represents a node in the cluster.
2626
type Node struct {
27-
Name string
28-
Addr net.IP
29-
Port uint16
30-
Meta []byte // Metadata from the delegate for this node.
31-
PMin uint8 // Minimum protocol version this understands
32-
PMax uint8 // Maximum protocol version this understands
33-
PCur uint8 // Current version node is speaking
34-
DMin uint8 // Min protocol version for the delegate to understand
35-
DMax uint8 // Max protocol version for the delegate to understand
36-
DCur uint8 // Current version delegate is speaking
27+
Name string
28+
Addr net.IP
29+
Port uint16
30+
Meta []byte // Metadata from the delegate for this node.
31+
State NodeStateType // State of the node.
32+
PMin uint8 // Minimum protocol version this understands
33+
PMax uint8 // Maximum protocol version this understands
34+
PCur uint8 // Current version node is speaking
35+
DMin uint8 // Min protocol version for the delegate to understand
36+
DMax uint8 // Max protocol version for the delegate to understand
37+
DCur uint8 // Current version delegate is speaking
3738
}
3839

3940
// Address returns the host:port form of a node's address, suitable for use
@@ -60,7 +61,7 @@ func (n *Node) String() string {
6061
type nodeState struct {
6162
Node
6263
Incarnation uint32 // Last known incarnation number
63-
State nodeStateType // Current state
64+
State NodeStateType // Current state
6465
StateChange time.Time // Time last state change happened
6566
}
6667

@@ -77,7 +78,7 @@ func (n *nodeState) FullAddress() Address {
7778
}
7879

7980
func (n *nodeState) DeadOrLeft() bool {
80-
return n.State == stateDead || n.State == stateLeft
81+
return n.State == StateDead || n.State == StateLeft
8182
}
8283

8384
// ackHandler is used to register handlers for incoming acks and nacks.
@@ -321,7 +322,7 @@ func (m *Memberlist) probeNode(node *nodeState) {
321322
defer func() {
322323
m.awareness.ApplyDelta(awarenessDelta)
323324
}()
324-
if node.State == stateAlive {
325+
if node.State == StateAlive {
325326
if err := m.encodeAndSendMsg(node.FullAddress(), pingMsg, &ping); err != nil {
326327
m.logger.Printf("[ERR] memberlist: Failed to send ping: %s", err)
327328
if failedRemote(err) {
@@ -396,7 +397,7 @@ HANDLE_REMOTE_FAILURE:
396397
kNodes := kRandomNodes(m.config.IndirectChecks, m.nodes, func(n *nodeState) bool {
397398
return n.Name == m.config.Name ||
398399
n.Name == node.Name ||
399-
n.State != stateAlive
400+
n.State != StateAlive
400401
})
401402
m.nodeLock.RUnlock()
402403

@@ -573,10 +574,10 @@ func (m *Memberlist) gossip() {
573574
}
574575

575576
switch n.State {
576-
case stateAlive, stateSuspect:
577+
case StateAlive, StateSuspect:
577578
return false
578579

579-
case stateDead:
580+
case StateDead:
580581
return time.Since(n.StateChange) > m.config.GossipToTheDeadTime
581582

582583
default:
@@ -623,7 +624,7 @@ func (m *Memberlist) pushPull() {
623624
m.nodeLock.RLock()
624625
nodes := kRandomNodes(1, m.nodes, func(n *nodeState) bool {
625626
return n.Name == m.config.Name ||
626-
n.State != stateAlive
627+
n.State != StateAlive
627628
})
628629
m.nodeLock.RUnlock()
629630

@@ -681,7 +682,7 @@ func (m *Memberlist) verifyProtocol(remote []pushNodeState) error {
681682

682683
for _, rn := range remote {
683684
// If the node isn't alive, then skip it
684-
if rn.State != stateAlive {
685+
if rn.State != StateAlive {
685686
continue
686687
}
687688

@@ -710,7 +711,7 @@ func (m *Memberlist) verifyProtocol(remote []pushNodeState) error {
710711

711712
for _, n := range m.nodes {
712713
// Ignore non-alive nodes
713-
if n.State != stateAlive {
714+
if n.State != StateAlive {
714715
continue
715716
}
716717

@@ -981,7 +982,7 @@ func (m *Memberlist) aliveNode(a *alive, notify chan struct{}, bootstrap bool) {
981982
Port: a.Port,
982983
Meta: a.Meta,
983984
},
984-
State: stateDead,
985+
State: StateDead,
985986
}
986987
if len(a.Vsn) > 5 {
987988
state.PMin = a.Vsn[0]
@@ -1021,7 +1022,7 @@ func (m *Memberlist) aliveNode(a *alive, notify chan struct{}, bootstrap bool) {
10211022
time.Since(state.StateChange) > m.config.DeadNodeReclaimTime)
10221023

10231024
// Allow the address to be updated if a dead node is being replaced.
1024-
if state.State == stateLeft || (state.State == stateDead && canReclaim) {
1025+
if state.State == StateLeft || (state.State == StateDead && canReclaim) {
10251026
m.logger.Printf("[INFO] memberlist: Updating address for left or failed node %s from %v:%d to %v:%d",
10261027
state.Name, state.Addr, state.Port, net.IP(a.Addr), a.Port)
10271028
updatesNode = true
@@ -1106,8 +1107,8 @@ func (m *Memberlist) aliveNode(a *alive, notify chan struct{}, bootstrap bool) {
11061107
state.Meta = a.Meta
11071108
state.Addr = a.Addr
11081109
state.Port = a.Port
1109-
if state.State != stateAlive {
1110-
state.State = stateAlive
1110+
if state.State != StateAlive {
1111+
state.State = StateAlive
11111112
state.StateChange = time.Now()
11121113
}
11131114
}
@@ -1117,7 +1118,7 @@ func (m *Memberlist) aliveNode(a *alive, notify chan struct{}, bootstrap bool) {
11171118

11181119
// Notify the delegate of any relevant updates
11191120
if m.config.Events != nil {
1120-
if oldState == stateDead || oldState == stateLeft {
1121+
if oldState == StateDead || oldState == StateLeft {
11211122
// if Dead/Left -> Alive, notify of join
11221123
m.config.Events.NotifyJoin(&state.Node)
11231124

@@ -1157,7 +1158,7 @@ func (m *Memberlist) suspectNode(s *suspect) {
11571158
}
11581159

11591160
// Ignore non-alive nodes
1160-
if state.State != stateAlive {
1161+
if state.State != StateAlive {
11611162
return
11621163
}
11631164

@@ -1175,7 +1176,7 @@ func (m *Memberlist) suspectNode(s *suspect) {
11751176

11761177
// Update the state
11771178
state.Incarnation = s.Incarnation
1178-
state.State = stateSuspect
1179+
state.State = StateSuspect
11791180
changeTime := time.Now()
11801181
state.StateChange = changeTime
11811182

@@ -1199,7 +1200,7 @@ func (m *Memberlist) suspectNode(s *suspect) {
11991200
fn := func(numConfirmations int) {
12001201
m.nodeLock.Lock()
12011202
state, ok := m.nodeMap[s.Node]
1202-
timeout := ok && state.State == stateSuspect && state.StateChange == changeTime
1203+
timeout := ok && state.State == StateSuspect && state.StateChange == changeTime
12031204
m.nodeLock.Unlock()
12041205

12051206
if timeout {
@@ -1265,9 +1266,9 @@ func (m *Memberlist) deadNode(d *dead) {
12651266
// If the dead message was send by the node itself, mark it is left
12661267
// instead of dead.
12671268
if d.Node == d.From {
1268-
state.State = stateLeft
1269+
state.State = StateLeft
12691270
} else {
1270-
state.State = stateDead
1271+
state.State = StateDead
12711272
}
12721273
state.StateChange = time.Now()
12731274

@@ -1282,7 +1283,7 @@ func (m *Memberlist) deadNode(d *dead) {
12821283
func (m *Memberlist) mergeState(remote []pushNodeState) {
12831284
for _, r := range remote {
12841285
switch r.State {
1285-
case stateAlive:
1286+
case StateAlive:
12861287
a := alive{
12871288
Incarnation: r.Incarnation,
12881289
Node: r.Name,
@@ -1293,14 +1294,14 @@ func (m *Memberlist) mergeState(remote []pushNodeState) {
12931294
}
12941295
m.aliveNode(&a, nil, false)
12951296

1296-
case stateLeft:
1297+
case StateLeft:
12971298
d := dead{Incarnation: r.Incarnation, Node: r.Name, From: r.Name}
12981299
m.deadNode(&d)
1299-
case stateDead:
1300+
case StateDead:
13001301
// If the remote node believes a node is dead, we prefer to
13011302
// suspect that node instead of declaring it dead instantly
13021303
fallthrough
1303-
case stateSuspect:
1304+
case StateSuspect:
13041305
s := suspect{Incarnation: r.Incarnation, Node: r.Name, From: m.config.Name}
13051306
m.suspectNode(&s)
13061307
}

0 commit comments

Comments
 (0)