Skip to content

Commit 0bff309

Browse files
authored
Purge left nodes along with dead ones (#254)
Keep the state from infinitely expanding
1 parent 5f7e384 commit 0bff309

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ func pushPullScale(interval time.Duration, n int) time.Duration {
9696
return time.Duration(multiplier) * interval
9797
}
9898

99-
// moveDeadNodes moves nodes that are dead and beyond the gossip to the dead interval
99+
// moveDeadNodes moves dead and left nodes that that have not changed during the gossipToTheDeadTime interval
100100
// to the end of the slice and returns the index of the first moved node.
101101
func moveDeadNodes(nodes []*nodeState, gossipToTheDeadTime time.Duration) int {
102102
numDead := 0
103103
n := len(nodes)
104104
for i := 0; i < n-numDead; i++ {
105-
if nodes[i].State != StateDead {
105+
if !nodes[i].DeadOrLeft() {
106106
continue
107107
}
108108

util_test.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ func TestMoveDeadNodes(t *testing.T) {
178178
State: StateDead,
179179
StateChange: time.Now().Add(-10 * time.Second),
180180
},
181+
// This left node should not be moved, as its state changed
182+
// less than the specified GossipToTheDead time ago
183+
&nodeState{
184+
State: StateLeft,
185+
StateChange: time.Now().Add(-10 * time.Second),
186+
},
187+
&nodeState{
188+
State: StateLeft,
189+
StateChange: time.Now().Add(-20 * time.Second),
190+
},
181191
&nodeState{
182192
State: StateAlive,
183193
StateChange: time.Now().Add(-20 * time.Second),
@@ -190,10 +200,14 @@ func TestMoveDeadNodes(t *testing.T) {
190200
State: StateAlive,
191201
StateChange: time.Now().Add(-20 * time.Second),
192202
},
203+
&nodeState{
204+
State: StateLeft,
205+
StateChange: time.Now().Add(-20 * time.Second),
206+
},
193207
}
194208

195209
idx := moveDeadNodes(nodes, (15 * time.Second))
196-
if idx != 4 {
210+
if idx != 5 {
197211
t.Fatalf("bad index")
198212
}
199213
for i := 0; i < idx; i++ {
@@ -204,14 +218,19 @@ func TestMoveDeadNodes(t *testing.T) {
204218
if nodes[i].State != StateDead {
205219
t.Fatalf("Bad state %d", i)
206220
}
221+
case 3:
222+
//Recently left node should remain at 3
223+
if nodes[i].State != StateLeft {
224+
t.Fatalf("Bad State %d", i)
225+
}
207226
default:
208227
if nodes[i].State != StateAlive {
209228
t.Fatalf("Bad state %d", i)
210229
}
211230
}
212231
}
213232
for i := idx; i < len(nodes); i++ {
214-
if nodes[i].State != StateDead {
233+
if !nodes[i].DeadOrLeft() {
215234
t.Fatalf("Bad state %d", i)
216235
}
217236
}

0 commit comments

Comments
 (0)