-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
store_replica_btree_test.go
164 lines (144 loc) · 4.89 KB
/
store_replica_btree_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
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package kvserver
import (
"context"
"testing"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/stretchr/testify/require"
)
func makeMockPH(start, end string) *ReplicaPlaceholder {
ph := &ReplicaPlaceholder{}
ph.rangeDesc.StartKey = roachpb.RKey(start)
ph.rangeDesc.EndKey = roachpb.RKey(end)
return ph
}
func TestStoreReplicaBTree_VisitKeyRange(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
ac := makeMockPH("a", "c")
cd := makeMockPH("c", "d")
ef := makeMockPH("e", "f")
b := newStoreReplicaBTree()
require.Nil(t, b.ReplaceOrInsertPlaceholder(ctx, ac).item)
require.Nil(t, b.ReplaceOrInsertPlaceholder(ctx, cd).item)
require.Nil(t, b.ReplaceOrInsertPlaceholder(ctx, ef).item)
collect := func(from, to string, order IterationOrder) []*ReplicaPlaceholder {
t.Helper()
var seen []*ReplicaPlaceholder
require.NoError(t, b.VisitKeyRange(
ctx, roachpb.RKey(from), roachpb.RKey(to), order,
func(ctx context.Context, it replicaOrPlaceholder) error {
seen = append(seen, it.ph)
return nil
}))
return seen
}
testutils.RunTrueAndFalse(t, "reverse", func(t *testing.T, reverse bool) {
check := func(t *testing.T, act []*ReplicaPlaceholder, exp ...*ReplicaPlaceholder) {
t.Helper()
if reverse {
exp = append(([]*ReplicaPlaceholder)(nil), exp...)
for i, n := 0, len(exp); i < n/2; i++ {
exp[i], exp[n-i-1] = exp[n-i-1], exp[i]
}
}
require.Equal(t, exp, act)
}
order := AscendingKeyOrder
if reverse {
order = DescendingKeyOrder
}
check(t, collect("", "a", order))
check(t, collect("", "aa", order), ac)
check(t, collect("a", "aa", order), ac)
check(t, collect("aa", "ab", order), ac)
check(t, collect("aa", "c", order), ac)
check(t, collect("aa", "ca", order), ac, cd)
check(t, collect("c", "ca", order), cd)
check(t, collect("", "zzz", order), ac, cd, ef)
// These test cases are interesting because the logic in VisitKeyRange
// that winds back the start key to align with the current range's start
// key must make sure not to wind back to a range that does not contain
// the original input start key.
check(t, collect("d", "e", order))
check(t, collect("da", "db", order))
check(t, collect("d", "ea", order), ef)
check(t, collect("cz", "e", order), cd)
})
}
func TestStoreReplicaBTree_LookupPrecedingAndNextReplica(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
makeRepl := func(start, end string) *Replica {
desc := &roachpb.RangeDescriptor{}
desc.StartKey = roachpb.RKey(start)
desc.EndKey = roachpb.RKey(end)
r := &Replica{}
r.mu.state.Desc = desc
r.startKey = desc.StartKey // this is what's actually used in the btree
return r
}
b := newStoreReplicaBTree()
repl2 := makeRepl("a", "b")
require.Zero(t, b.ReplaceOrInsertReplica(ctx, repl2))
repl3 := makeRepl("b", "c")
require.Zero(t, b.ReplaceOrInsertReplica(ctx, repl3))
ph := makeMockPH("c", "d")
require.Zero(t, b.ReplaceOrInsertPlaceholder(ctx, ph))
repl5 := makeRepl("e", "f")
require.Zero(t, b.ReplaceOrInsertReplica(ctx, repl5))
for i, tc := range []struct {
key string
preRepl *Replica
nextRepl *Replica
}{
{"", nil, repl2},
{"a", nil, repl2},
{"aa", nil, repl3},
{"b", repl2, repl3},
{"bb", repl2, repl5},
{"c", repl3, repl5},
{"cc", repl3, repl5},
{"d", repl3, repl5},
{"dd", repl3, repl5},
{"e", repl3, repl5},
{"ee", repl3, nil},
{"f", repl5, nil},
{"\xff\xff", repl5, nil},
} {
if got, want := b.LookupPrecedingReplica(ctx, roachpb.RKey(tc.key)), tc.preRepl; got != want {
t.Errorf("%d: expected preceding replica %v; got %v", i, want, got)
}
if got, want := b.LookupNextReplica(ctx, roachpb.RKey(tc.key)), tc.nextRepl; got != want {
t.Errorf("%d: expected next replica %v; got %v", i, want, got)
}
}
}
func TestStoreReplicaBTree_ReplicaCanBeLockedDuringInsert(t *testing.T) {
defer leaktest.AfterTest(t)()
// Verify that the replica can be locked while being inserted (and removed).
// This is important for `Store.maybeMarkReplicaInitializedLockedReplLocked`.
ctx := context.Background()
repl := &Replica{}
k := roachpb.RKey("a")
repl.mu.state.Desc = &roachpb.RangeDescriptor{
RangeID: 12,
}
repl.startKey = k
repl.mu.Lock()
defer repl.mu.Unlock()
br := newStoreReplicaBTree()
require.Nil(t, br.ReplaceOrInsertReplica(ctx, repl).item)
require.Equal(t, repl, br.ReplaceOrInsertReplica(ctx, repl).repl)
require.Equal(t, repl, br.DeleteReplica(ctx, repl).repl)
}