forked from tsuna/gohbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metacache_test.go
683 lines (634 loc) · 18.4 KB
/
metacache_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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright (C) 2015 The GoHBase Authors. All rights reserved.
// This file is part of GoHBase.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package gohbase
import (
"bytes"
"fmt"
"reflect"
"sort"
"testing"
"github.com/aristanetworks/goarista/test"
"github.com/golang/mock/gomock"
"github.com/tsuna/gohbase/hrpc"
"github.com/tsuna/gohbase/region"
mockRegion "github.com/tsuna/gohbase/test/mock/region"
)
func TestMetaCache(t *testing.T) {
client := newClient("~invalid.quorum~") // We shouldn't connect to ZK.
reg := client.getRegionFromCache([]byte("test"), []byte("theKey"))
if reg != nil {
t.Errorf("Found region %v even though the cache was empty?!", reg)
}
// Inject an entry in the cache. This entry covers the entire key range.
wholeTable := region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
nil,
nil,
)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
regClient := mockRegion.NewMockRegionClient(ctrl)
regClient.EXPECT().String().Return("mock region client").AnyTimes()
regClient.EXPECT().Addr().Return("regionserver:1").AnyTimes()
client.regions.put(wholeTable)
client.clients.put(regClient, wholeTable)
reg = client.getRegionFromCache([]byte("test"), []byte("theKey"))
if !reflect.DeepEqual(reg, wholeTable) {
t.Errorf("Found region %v but expected %v", reg, wholeTable)
}
reg = client.getRegionFromCache([]byte("test"), []byte("")) // edge case.
if !reflect.DeepEqual(reg, wholeTable) {
t.Errorf("Found region %v but expected %v", reg, wholeTable)
}
// Clear our client.
client = newClient("~invalid.quorum~")
// Inject 3 entries in the cache.
region1 := region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte(""),
[]byte("foo"),
)
if os, replaced := client.regions.put(region1); !replaced {
t.Errorf("Expected to put new region into cache, got: %v", os)
} else if len(os) != 0 {
t.Errorf("Didn't expect any overlaps, got: %v", os)
}
client.clients.put(regClient, region1)
region2 := region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,foo,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte("foo"),
[]byte("gohbase"),
)
if os, replaced := client.regions.put(region2); !replaced {
t.Errorf("Expected to put new region into cache, got: %v", os)
} else if len(os) != 0 {
t.Errorf("Didn't expect any overlaps, got: %v", os)
}
client.clients.put(regClient, region2)
region3 := region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,gohbase,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte("gohbase"),
[]byte(""),
)
if os, replaced := client.regions.put(region3); !replaced {
t.Errorf("Expected to put new region into cache, got: %v", os)
} else if len(os) != 0 {
t.Errorf("Didn't expect any overlaps, got: %v", os)
}
client.clients.put(regClient, region3)
testcases := []struct {
key string
reg hrpc.RegionInfo
}{
{key: "theKey", reg: region3},
{key: "", reg: region1},
{key: "bar", reg: region1},
{key: "fon\xFF", reg: region1},
{key: "foo", reg: region2},
{key: "foo\x00", reg: region2},
{key: "gohbase", reg: region3},
}
for i, testcase := range testcases {
reg = client.getRegionFromCache([]byte("test"), []byte(testcase.key))
if !reflect.DeepEqual(reg, testcase.reg) {
t.Errorf("[#%d] Found region %v but expected %v", i, reg, testcase.reg)
}
}
// Change the last region (maybe it got split).
region4 := region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,gohbase,1234567890042.swagswagswagswagswagswagswagswag."),
[]byte("gohbase"),
[]byte("zab"),
)
if os, replaced := client.regions.put(region4); !replaced {
t.Errorf("Expected to put new region into cache, got: %v", os)
} else if len(os) != 1 || os[0] != region3 {
t.Errorf("Expected one overlap, got: %v", os)
}
client.clients.put(regClient, region4)
reg = client.getRegionFromCache([]byte("test"), []byte("theKey"))
if !reflect.DeepEqual(reg, region4) {
t.Errorf("Found region %v but expected %v", reg, region4)
}
reg = client.getRegionFromCache([]byte("test"), []byte("zoo"))
if reg != nil {
t.Errorf("Shouldn't have found any region yet found %v", reg)
}
// attempt putting a region with same name
region5 := region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,gohbase,1234567890042.swagswagswagswagswagswagswagswag."),
nil,
[]byte("zab"),
)
if os, replaced := client.regions.put(region5); replaced {
t.Errorf("Expected to not replace a region in cache, got: %v", os)
} else if len(os) != 1 || os[0] != region4 {
t.Errorf("Expected overlaps, got: %v", os)
}
}
func TestMetaCacheGet(t *testing.T) {
tcases := []struct {
in []hrpc.RegionInfo
table []byte
key []byte
outIndexFromIn int
}{
{
table: []byte("yolo"),
key: []byte("swag"),
outIndexFromIn: -1,
},
{ // the whole table
in: []hrpc.RegionInfo{
region.NewInfo(0, nil, []byte("test"),
[]byte("test,,1234567890042.swagswagswagswagswagswagswagswag."),
nil, nil),
},
table: []byte("test"),
key: []byte("swag"),
outIndexFromIn: 0,
},
{ // one region in cache, different table
in: []hrpc.RegionInfo{
region.NewInfo(0, nil, []byte("test"),
[]byte("test,,1234567890042.swagswagswagswagswagswagswagswag."),
nil, nil),
},
table: []byte("yolo"),
key: []byte("swag"),
outIndexFromIn: -1,
},
{ // key is before the first region in cache
in: []hrpc.RegionInfo{
region.NewInfo(0, nil, []byte("test"),
[]byte("test,foo,1234567890042.swagswagswagswagswagswagswagswag."),
[]byte("foo"), nil),
},
table: []byte("test"),
key: []byte("bar"),
outIndexFromIn: -1,
},
{ // key is between two regions in cache
in: []hrpc.RegionInfo{
region.NewInfo(0, nil, []byte("meow"),
[]byte("meow,bar,1234567890042.swagswagswagswagswagswagswagswag."),
[]byte("bar"), []byte("foo")),
region.NewInfo(0, nil, []byte("test"),
[]byte("test,foo,1234567890042.swagswagswagswagswagswagswagswag."),
[]byte("foo"), nil),
},
table: []byte("meow"),
key: []byte("swag"),
outIndexFromIn: -1,
},
{ // test with namespace region in cache
in: []hrpc.RegionInfo{
region.NewInfo(0, []byte("n1"), []byte("test"),
[]byte("n1:test,,1234567890042.swagswagswagswagswagswagswagswag."),
nil, nil),
},
table: []byte("test"),
key: []byte("swag"),
outIndexFromIn: -1,
},
{ // test with namespace region in cache
in: []hrpc.RegionInfo{
region.NewInfo(0, []byte("n1"), []byte("test"),
[]byte("n1:test,,1234567890042.swagswagswagswagswagswagswagswag."),
nil, nil),
},
table: []byte("n1:test"),
key: []byte("swag"),
outIndexFromIn: 0,
},
{ // test with default namespace in cache, but non-default key
in: []hrpc.RegionInfo{
region.NewInfo(0, nil, []byte("test"),
[]byte("test,,1234567890042.swagswagswagswagswagswagswagswag."),
nil, nil),
},
table: []byte("n1:test"),
key: []byte("swag"),
outIndexFromIn: -1,
},
{ // test with non-default namespace region in cache, but default key
in: []hrpc.RegionInfo{
region.NewInfo(0, []byte("n1"), []byte("test"),
[]byte("n1:test,,1234567890042.swagswagswagswagswagswagswagswag."),
nil, nil),
},
table: []byte("test"),
key: []byte("swag"),
outIndexFromIn: -1,
},
{ // test 3 regions
in: []hrpc.RegionInfo{
region.NewInfo(0, nil, []byte("test"),
[]byte("test,,1234567890042.swagswagswagswagswagswagswagswag."),
nil, []byte("bar")),
region.NewInfo(0, nil, []byte("test"),
[]byte("test,bar,1234567890042.swagswagswagswagswagswagswagswag."),
[]byte("bar"), []byte("foo")),
region.NewInfo(0, nil, []byte("test"),
[]byte("test,foo,1234567890042.swagswagswagswagswagswagswagswag."),
[]byte("foo"), []byte("yolo")),
},
table: []byte("test"),
key: []byte("baz"),
outIndexFromIn: 1,
},
}
for i, tcase := range tcases {
t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
client := newClient("~invalid.quorum~") // We shouldn't connect to ZK.
for _, r := range tcase.in {
overlaps, replaced := client.regions.put(r)
if len(overlaps) != 0 {
t.Fatalf("Didn't expect any overlaps, got %q", overlaps)
}
if !replaced {
t.Fatal("Didn't expect to replace anything in cache")
}
}
// lookup region in cache
region := client.getRegionFromCache(tcase.table, tcase.key)
if tcase.outIndexFromIn == -1 && region != nil {
t.Fatalf("expected to get nil region, got %v", region)
} else {
return
}
if len(tcase.in) == 0 && region != nil {
t.Fatalf("didn't expect to get anything from empty cache, got %v", region)
}
if tcase.in[tcase.outIndexFromIn].String() != region.String() {
t.Errorf("Expected %v, Got %v",
tcase.in[tcase.outIndexFromIn].String(), region.String())
}
})
}
}
func TestRegionCacheAge(t *testing.T) {
tcases := []struct {
cachedRegions []hrpc.RegionInfo
newRegion hrpc.RegionInfo
replaced bool
}{
{ // all older
cachedRegions: []hrpc.RegionInfo{
region.NewInfo(
1, nil, []byte("hello"),
[]byte("hello,,1.yoloyoloyoloyoloyoloyoloyoloyolo."),
[]byte(""), []byte("foo"),
),
region.NewInfo(
1, nil, []byte("hello"),
[]byte("hello,foo,1.swagswagswagswagswagswagswagswag."),
[]byte("foo"), []byte(""),
)},
newRegion: region.NewInfo(
2, nil, []byte("hello"),
[]byte("hello,,2.meowmemowmeowmemowmeowmemowmeow."),
[]byte(""), []byte(""),
),
replaced: true,
},
{ // all younger
cachedRegions: []hrpc.RegionInfo{
region.NewInfo(
2, nil, []byte("hello"),
[]byte("hello,,2.yoloyoloyoloyoloyoloyoloyoloyolo."),
[]byte(""), []byte("foo"),
),
region.NewInfo(
2, nil, []byte("hello"),
[]byte("hello,foo,2.swagswagswagswagswagswagswagswag."),
[]byte("foo"), []byte(""),
)},
newRegion: region.NewInfo(
1, nil, []byte("hello"),
[]byte("hello,,1.meowmemowmeowmemowmeowmemowmeow."),
[]byte(""), []byte(""),
),
replaced: false,
},
{ // one younger, one older
cachedRegions: []hrpc.RegionInfo{
region.NewInfo(
1, nil, []byte("hello"),
[]byte("hello,,1.yoloyoloyoloyoloyoloyoloyoloyolo."),
[]byte(""), []byte("foo"),
),
region.NewInfo(
3, nil, []byte("hello"),
[]byte("hello,foo,3.swagswagswagswagswagswagswagswag."),
[]byte("foo"), []byte(""),
)},
newRegion: region.NewInfo(
2, nil, []byte("hello"),
[]byte("hello,,1.meowmemowmeowmemowmeowmemowmeow."),
[]byte(""), []byte(""),
),
replaced: false,
},
}
client := newClient("~invalid.quorum~")
for i, tcase := range tcases {
client.regions.regions.Clear()
// set up initial cache
for _, region := range tcase.cachedRegions {
client.regions.put(region)
}
overlaps, replaced := client.regions.put(tcase.newRegion)
if replaced != tcase.replaced {
t.Errorf("Test %d: Expected %v, got %v", i, tcase.replaced, replaced)
}
expectedNames := make(regionNames, len(tcase.cachedRegions))
for i, r := range tcase.cachedRegions {
expectedNames[i] = r.Name()
}
osNames := make(regionNames, len(overlaps))
for i, o := range overlaps {
osNames[i] = o.Name()
}
// check overlaps are correct
if diff := test.Diff(expectedNames, osNames); diff != "" {
t.Errorf("Test %d: Expected: %v\nReceived: %v\nDiff:%s",
i, expectedNames, osNames, diff)
}
}
}
type regionNames [][]byte
func (a regionNames) Len() int { return len(a) }
func (a regionNames) Less(i, j int) bool { return bytes.Compare(a[i], a[j]) < 0 }
func (a regionNames) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func TestMetaCacheGetOverlaps(t *testing.T) {
region1 := region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte(""),
[]byte("foo"),
)
regionA := region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte(""),
[]byte("foo"),
)
regionB := region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,foo,987654321042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte("foo"),
[]byte("fox"),
)
regionC := region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,fox,987654321042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte("fox"),
[]byte("yolo"),
)
regionWhole := region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
nil,
nil,
)
regionTests := []struct {
cachedRegions []hrpc.RegionInfo
newRegion hrpc.RegionInfo
expected []hrpc.RegionInfo
}{
{[]hrpc.RegionInfo{}, region1, []hrpc.RegionInfo{}}, // empty cache
{[]hrpc.RegionInfo{region1}, region1, []hrpc.RegionInfo{region1}}, // with itself
{ // different table
[]hrpc.RegionInfo{region1},
region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte(""),
[]byte("fake"),
),
[]hrpc.RegionInfo{},
},
{ // different namespace
[]hrpc.RegionInfo{
region.NewInfo(
0,
[]byte("ns1"),
[]byte("test"),
[]byte("ns1:test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte(""),
[]byte("foo"),
),
},
region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte(""),
[]byte("foo"),
),
[]hrpc.RegionInfo{},
},
{ // overlaps with both
[]hrpc.RegionInfo{regionA, regionB},
region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,bar,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte("bar"),
[]byte("fop"),
),
[]hrpc.RegionInfo{regionA, regionB},
},
{ // overlaps with both, key start == old one
[]hrpc.RegionInfo{regionA, regionB},
region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte(""),
[]byte("yolo"),
),
[]hrpc.RegionInfo{regionA, regionB},
},
{ // overlaps with second
[]hrpc.RegionInfo{regionA, regionB},
region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,fop,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte("fop"),
[]byte("yolo"),
),
[]hrpc.RegionInfo{regionB},
},
{ // overlaps with first, new key start == old one
[]hrpc.RegionInfo{regionA, regionB},
region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte(""),
[]byte("abc"),
),
[]hrpc.RegionInfo{regionA},
},
{ // doesn't overlap, is between existing
[]hrpc.RegionInfo{regionA, regionC},
regionB,
[]hrpc.RegionInfo{},
},
{ // without bounds in cache, replaced by region with both bounds
[]hrpc.RegionInfo{regionWhole},
regionB,
[]hrpc.RegionInfo{regionWhole},
},
{ // without bounds in cache, replaced by the empty stop key only
[]hrpc.RegionInfo{regionWhole},
region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
[]byte("yolo"),
nil,
),
[]hrpc.RegionInfo{regionWhole},
},
{ // without bounds in cache, replaced by the empty start key only
[]hrpc.RegionInfo{regionWhole},
region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
nil,
[]byte("yolo"),
),
[]hrpc.RegionInfo{regionWhole},
},
{ // regions with bounds in cache, replaced by without bounds
[]hrpc.RegionInfo{regionB, regionC},
regionWhole,
[]hrpc.RegionInfo{regionB, regionC},
},
{ // without bounds in cache, replaced by without bounds
[]hrpc.RegionInfo{regionWhole},
region.NewInfo(
0,
nil,
[]byte("hello"),
[]byte("hello,,1234567890042.yoloyoloyoloyoloyoloyoloyoloyolo."),
nil,
nil,
),
[]hrpc.RegionInfo{regionWhole},
},
}
for i, tt := range regionTests {
t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
client := newClient("~invalid.quorum~") // fake client
// set up initial cache
for _, region := range tt.cachedRegions {
client.regions.regions.Set(region.Name(), region)
}
expectedNames := make(regionNames, len(tt.expected))
for i, r := range tt.expected {
expectedNames[i] = r.Name()
}
os := client.regions.getOverlaps(tt.newRegion)
osNames := make(regionNames, len(os))
for i, o := range os {
osNames[i] = o.Name()
}
sort.Sort(expectedNames)
sort.Sort(osNames)
if !reflect.DeepEqual(expectedNames, osNames) {
t.Errorf("Expected overlaps %q, found %q", expectedNames, osNames)
}
})
}
}
func TestClientCachePut(t *testing.T) {
client := newClient("~invalid.quorum~")
ctrl := gomock.NewController(t)
defer ctrl.Finish()
regClient := mockRegion.NewMockRegionClient(ctrl)
regClient.EXPECT().String().Return("mock region client").AnyTimes()
regClient.EXPECT().Addr().Return("regionserver:1").AnyTimes()
existing := client.clients.put(regClient, region.NewInfo(
0,
nil,
[]byte("test"),
[]byte("test,,1234567890042.yoloyoloyoloyoloyoloyoloyoloyolo."),
nil, nil))
if existing != regClient {
t.Errorf("Unexpected regClient from put: %v", existing)
}
if len(client.clients.regions) != 1 {
t.Errorf("Expected 1 client in cache, got %d", len(client.clients.regions))
}
if len(client.clients.regions[regClient]) != 1 {
t.Errorf("Expected 1 region for client in cache, got %d",
len(client.clients.regions[regClient]))
}
// try putting client with the same host port
regClient2 := mockRegion.NewMockRegionClient(ctrl)
regClient.EXPECT().String().Return("mock region client").AnyTimes()
regClient2.EXPECT().Addr().Return("regionserver:1").AnyTimes()
// but put a different region
existing = client.clients.put(regClient, region.NewInfo(
0,
nil,
[]byte("yolo"),
[]byte("yolo,,1234567890042.yoloyoloyoloyoloyoloyoloyoloyolo."),
nil, nil))
if existing != regClient {
t.Errorf("Unexpected regClient from put: %v", existing)
}
// nothing should have changed in clients cache
if len(client.clients.regions) != 1 {
t.Errorf("Expected 1 client in cache, got %d", len(client.clients.regions))
}
if len(client.clients.regions[regClient]) != 2 {
t.Errorf("Expected 2 regions for client in cache, got %d",
len(client.clients.regions[regClient]))
}
}