-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
store_pool_test.go
510 lines (462 loc) · 14 KB
/
store_pool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Bram Gruneir ([email protected])
package storage
import (
"reflect"
"sort"
"testing"
"time"
"github.com/pkg/errors"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/testutils/gossiputil"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
// TestSetDeterministic makes StorePool return results in a deterministic way.
func (sp *StorePool) TestSetDeterministic(deterministic bool) {
sp.mu.Lock()
defer sp.mu.Unlock()
sp.mu.deterministic = deterministic
}
var uniqueStore = []*roachpb.StoreDescriptor{
{
StoreID: 2,
Attrs: roachpb.Attributes{Attrs: []string{"ssd"}},
Node: roachpb.NodeDescriptor{
NodeID: 2,
Attrs: roachpb.Attributes{Attrs: []string{"a"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
}
// createTestStorePool creates a stopper, gossip and storePool for use in
// tests. Stopper must be stopped by the caller.
func createTestStorePool(
timeUntilStoreDead time.Duration,
) (*stop.Stopper, *gossip.Gossip, *hlc.ManualClock, *StorePool) {
stopper := stop.NewStopper()
mc := hlc.NewManualClock(0)
clock := hlc.NewClock(mc.UnixNano)
rpcContext := rpc.NewContext(log.AmbientContext{}, &base.Config{Insecure: true}, clock, stopper)
server := rpc.NewServer(rpcContext) // never started
g := gossip.NewTest(1, rpcContext, server, nil, stopper, metric.NewRegistry())
storePool := NewStorePool(
log.AmbientContext{},
g,
clock,
rpcContext,
timeUntilStoreDead,
stopper,
)
return stopper, g, mc, storePool
}
// TestStorePoolGossipUpdate ensures that the gossip callback in StorePool
// correctly updates a store's details.
func TestStorePoolGossipUpdate(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, sp := createTestStorePool(TestTimeUntilStoreDead)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
sp.mu.RLock()
if _, ok := sp.mu.storeDetails[2]; ok {
t.Fatalf("store 2 is already in the pool's store list")
}
sp.mu.RUnlock()
sg.GossipStores(uniqueStore, t)
sp.mu.RLock()
if _, ok := sp.mu.storeDetails[2]; !ok {
t.Fatalf("store 2 isn't in the pool's store list")
}
if e, a := 1, sp.mu.queue.Len(); e > a {
t.Fatalf("wrong number of stores in the queue expected at least:%d actual:%d", e, a)
}
sp.mu.RUnlock()
}
// waitUntilDead will block until the specified store is marked as dead.
func waitUntilDead(t *testing.T, mc *hlc.ManualClock, sp *StorePool, storeID roachpb.StoreID) {
lastTime := timeutil.Now()
util.SucceedsSoon(t, func() error {
curTime := timeutil.Now()
mc.Increment(curTime.UnixNano() - lastTime.UnixNano())
lastTime = curTime
sp.mu.RLock()
defer sp.mu.RUnlock()
store, ok := sp.mu.storeDetails[storeID]
if !ok {
t.Fatalf("store %s isn't in the pool's store list", storeID)
}
exitcode := store.dead
if exitcode {
return nil
}
return errors.New("store not marked as dead yet")
})
}
// TestStorePoolDies ensures that a store is marked as dead after it
// times out and that it will be revived after a new update is received.
func TestStorePoolDies(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, mc, sp := createTestStorePool(TestTimeUntilStoreDead)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
sg.GossipStores(uniqueStore, t)
{
sp.mu.RLock()
store2, ok := sp.mu.storeDetails[2]
if !ok {
t.Fatalf("store 2 isn't in the pool's store list")
}
if store2.dead {
t.Errorf("store 2 is dead before it times out")
}
if e, a := 0, store2.timesDied; e != a {
t.Errorf("store 2 has been counted dead %d times, expected %d", a, e)
}
if store2.index == -1 {
t.Errorf("store 2 is mot the queue, it should be")
}
if e, a := 1, sp.mu.queue.Len(); e > a {
t.Errorf("wrong number of stores in the queue expected to be at least:%d actual:%d", e, a)
}
sp.mu.RUnlock()
}
// Timeout store 2.
waitUntilDead(t, mc, sp, 2)
{
sp.mu.RLock()
store2, ok := sp.mu.storeDetails[2]
if !ok {
t.Fatalf("store 2 isn't in the pool's store list")
}
if e, a := 1, store2.timesDied; e != a {
t.Errorf("store 2 has been counted dead %d times, expected %d", a, e)
}
if store2.index != -1 {
t.Errorf("store 2 is in the queue, it shouldn't be")
}
sp.mu.RUnlock()
}
sg.GossipStores(uniqueStore, t)
{
sp.mu.RLock()
store2, ok := sp.mu.storeDetails[2]
if !ok {
t.Fatalf("store 2 isn't in the pool's store list")
}
if store2.dead {
t.Errorf("store 2 is dead still, it should be alive")
}
if e, a := 1, store2.timesDied; e != a {
t.Errorf("store 2 has been counted dead %d times, expected %d", a, e)
}
if store2.index == -1 {
t.Errorf("store 2 is mot the queue, it should be")
}
sp.mu.RUnlock()
}
// Timeout store 2 again.
waitUntilDead(t, mc, sp, 2)
{
sp.mu.RLock()
store2, ok := sp.mu.storeDetails[2]
if !ok {
t.Fatalf("store 2 isn't in the pool's store list")
}
if e, a := 2, store2.timesDied; e != a {
t.Errorf("store 2 has been counted dead %d times, expected %d", a, e)
}
if store2.index != -1 {
t.Errorf("store 2 is in the queue, it shouldn't be")
}
sp.mu.RUnlock()
}
}
// verifyStoreList ensures that the returned list of stores is correct.
func verifyStoreList(
sp *StorePool,
rangeID roachpb.RangeID,
expected []int,
expectedAliveStoreCount int,
expectedThrottledStoreCount int,
) error {
var actual []int
sl, aliveStoreCount, throttledStoreCount := sp.getStoreList(rangeID)
if aliveStoreCount != expectedAliveStoreCount {
return errors.Errorf("expected AliveStoreCount %d does not match actual %d",
expectedAliveStoreCount, aliveStoreCount)
}
if throttledStoreCount != expectedThrottledStoreCount {
return errors.Errorf("expected ThrottledStoreCount %d does not match actual %d",
expectedThrottledStoreCount, throttledStoreCount)
}
for _, store := range sl.stores {
actual = append(actual, int(store.StoreID))
}
sort.Ints(expected)
sort.Ints(actual)
if !reflect.DeepEqual(expected, actual) {
return errors.Errorf("expected %+v stores, actual %+v", expected, actual)
}
return nil
}
// TestStorePoolGetStoreList ensures that the store list returns only stores
// that are alive.
func TestStorePoolGetStoreList(t *testing.T) {
defer leaktest.AfterTest(t)()
// We're going to manually mark stores dead in this test.
stopper, g, _, sp := createTestStorePool(TestTimeUntilStoreDeadOff)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
// Nothing yet.
if sl, _, _ := sp.getStoreList(roachpb.RangeID(0)); len(sl.stores) != 0 {
t.Errorf("expected no stores, instead %+v", sl.stores)
}
matchingStore := roachpb.StoreDescriptor{
StoreID: 1,
Node: roachpb.NodeDescriptor{NodeID: 1},
}
supersetStore := roachpb.StoreDescriptor{
StoreID: 2,
Node: roachpb.NodeDescriptor{NodeID: 1},
}
deadStore := roachpb.StoreDescriptor{
StoreID: 3,
Node: roachpb.NodeDescriptor{NodeID: 1},
}
declinedStore := roachpb.StoreDescriptor{
StoreID: 4,
Node: roachpb.NodeDescriptor{NodeID: 1},
}
corruptReplicaStore := roachpb.StoreDescriptor{
StoreID: 7,
Node: roachpb.NodeDescriptor{NodeID: 1},
}
corruptedRangeID := roachpb.RangeID(1)
allStores := []*roachpb.StoreDescriptor{
&matchingStore,
&supersetStore,
&deadStore,
&declinedStore,
&corruptReplicaStore,
}
// Mark all alive initially.
sg.GossipStores(allStores, t)
// Add some corrupt replicas that should not affect getStoreList().
sp.mu.Lock()
sp.mu.storeDetails[matchingStore.StoreID].deadReplicas[roachpb.RangeID(10)] =
[]roachpb.ReplicaDescriptor{{
StoreID: matchingStore.StoreID,
NodeID: matchingStore.Node.NodeID,
}}
sp.mu.storeDetails[matchingStore.StoreID].deadReplicas[roachpb.RangeID(11)] =
[]roachpb.ReplicaDescriptor{{
StoreID: matchingStore.StoreID,
NodeID: matchingStore.Node.NodeID,
}}
sp.mu.storeDetails[corruptReplicaStore.StoreID].deadReplicas[roachpb.RangeID(10)] =
[]roachpb.ReplicaDescriptor{{
StoreID: corruptReplicaStore.StoreID,
NodeID: corruptReplicaStore.Node.NodeID,
}}
sp.mu.Unlock()
if err := verifyStoreList(
sp,
corruptedRangeID,
[]int{
int(matchingStore.StoreID),
int(supersetStore.StoreID),
int(deadStore.StoreID),
int(declinedStore.StoreID),
int(corruptReplicaStore.StoreID),
},
/* expectedAliveStoreCount */ len(allStores),
/* expectedThrottledStoreCount */ 0,
); err != nil {
t.Error(err)
}
sp.mu.Lock()
// Set deadStore as dead.
sp.mu.storeDetails[deadStore.StoreID].markDead(sp.clock.Now())
// Set declinedStore as throttled.
sp.mu.storeDetails[declinedStore.StoreID].throttledUntil = sp.clock.Now().GoTime().Add(time.Hour)
// Add a corrupt replica to corruptReplicaStore.
sp.mu.storeDetails[corruptReplicaStore.StoreID].deadReplicas[roachpb.RangeID(1)] =
[]roachpb.ReplicaDescriptor{{
StoreID: corruptReplicaStore.StoreID,
NodeID: corruptReplicaStore.Node.NodeID,
}}
sp.mu.Unlock()
if err := verifyStoreList(
sp,
corruptedRangeID,
[]int{
int(matchingStore.StoreID),
int(supersetStore.StoreID),
},
/* expectedAliveStoreCount */ len(allStores)-1,
/* expectedThrottledStoreCount */ 1,
); err != nil {
t.Error(err)
}
}
func TestStorePoolGetStoreDetails(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, sp := createTestStorePool(TestTimeUntilStoreDeadOff)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
sg.GossipStores(uniqueStore, t)
sp.mu.Lock()
defer sp.mu.Unlock()
if detail := sp.getStoreDetailLocked(roachpb.StoreID(1)); detail.dead {
t.Errorf("Present storeDetail came back as dead, expected it to be alive. %+v", detail)
}
if detail := sp.getStoreDetailLocked(roachpb.StoreID(2)); detail.dead {
t.Errorf("Absent storeDetail came back as dead, expected it to be alive. %+v", detail)
}
}
func TestStorePoolFindDeadReplicas(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, mc, sp := createTestStorePool(TestTimeUntilStoreDead)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
stores := []*roachpb.StoreDescriptor{
{
StoreID: 1,
Node: roachpb.NodeDescriptor{NodeID: 1},
},
{
StoreID: 2,
Node: roachpb.NodeDescriptor{NodeID: 2},
},
{
StoreID: 3,
Node: roachpb.NodeDescriptor{NodeID: 3},
},
{
StoreID: 4,
Node: roachpb.NodeDescriptor{NodeID: 4},
},
{
StoreID: 5,
Node: roachpb.NodeDescriptor{NodeID: 5},
},
}
replicas := []roachpb.ReplicaDescriptor{
{
NodeID: 1,
StoreID: 1,
ReplicaID: 1,
},
{
NodeID: 2,
StoreID: 2,
ReplicaID: 2,
},
{
NodeID: 3,
StoreID: 3,
ReplicaID: 4,
},
{
NodeID: 4,
StoreID: 5,
ReplicaID: 4,
},
{
NodeID: 5,
StoreID: 5,
ReplicaID: 5,
},
}
sg.GossipStores(stores, t)
deadReplicas := sp.deadReplicas(0, replicas)
if len(deadReplicas) > 0 {
t.Fatalf("expected no dead replicas initially, found %d (%v)", len(deadReplicas), deadReplicas)
}
// Timeout all stores, but specifically store 5.
waitUntilDead(t, mc, sp, 5)
// Resurrect all stores except for 4 and 5.
sg.GossipStores(stores[:3], t)
deadReplicas = sp.deadReplicas(0, replicas)
if a, e := deadReplicas, replicas[3:]; !reflect.DeepEqual(a, e) {
t.Fatalf("findDeadReplicas did not return expected values; got \n%v, expected \n%v", a, e)
}
}
// TestStorePoolDefaultState verifies that the default state of a
// store is neither alive nor dead. This is a regression test for a
// bug in which a call to deadReplicas involving an unknown store
// would have the side effect of marking that store as alive and
// eligible for return by getStoreList. It is therefore significant
// that the two methods are tested in the same test, and in this
// order.
func TestStorePoolDefaultState(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, _, _, sp := createTestStorePool(TestTimeUntilStoreDeadOff)
defer stopper.Stop()
if dead := sp.deadReplicas(0, []roachpb.ReplicaDescriptor{{StoreID: 1}}); len(dead) > 0 {
t.Errorf("expected 0 dead replicas; got %v", dead)
}
sl, alive, throttled := sp.getStoreList(roachpb.RangeID(0))
if len(sl.stores) > 0 {
t.Errorf("expected no live stores; got list of %v", sl)
}
if alive != 0 {
t.Errorf("expected no live stores; got an alive count of %d", alive)
}
if throttled != 0 {
t.Errorf("expected no live stores; got a throttled count of %d", throttled)
}
}
func TestStorePoolThrottle(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, sp := createTestStorePool(TestTimeUntilStoreDeadOff)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
sg.GossipStores(uniqueStore, t)
{
expected := sp.clock.Now().GoTime().Add(sp.declinedReservationsTimeout)
sp.throttle(throttleDeclined, 1)
sp.mu.Lock()
detail := sp.getStoreDetailLocked(1)
sp.mu.Unlock()
if !detail.throttledUntil.Equal(expected) {
t.Errorf("expected store to have been throttled to %v, found %v",
expected, detail.throttledUntil)
}
}
{
expected := sp.clock.Now().GoTime().Add(sp.failedReservationsTimeout)
sp.throttle(throttleFailed, 1)
sp.mu.Lock()
detail := sp.getStoreDetailLocked(1)
sp.mu.Unlock()
if !detail.throttledUntil.Equal(expected) {
t.Errorf("expected store to have been throttled to %v, found %v",
expected, detail.throttledUntil)
}
}
}