-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
truncate_log_test.go
196 lines (171 loc) · 5.5 KB
/
truncate_log_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
// Copyright 2019 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 batcheval
import (
"context"
"fmt"
"testing"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/stretchr/testify/assert"
)
func putTruncatedState(
t *testing.T,
eng engine.Engine,
rangeID roachpb.RangeID,
truncState roachpb.RaftTruncatedState,
legacy bool,
) {
key := keys.RaftTruncatedStateKey(rangeID)
if legacy {
key = keys.RaftTruncatedStateLegacyKey(rangeID)
}
if err := engine.MVCCPutProto(
context.Background(), eng, nil, key,
hlc.Timestamp{}, nil /* txn */, &truncState,
); err != nil {
t.Fatal(err)
}
}
func readTruncStates(
t *testing.T, eng engine.Engine, rangeID roachpb.RangeID,
) (legacy roachpb.RaftTruncatedState, unreplicated roachpb.RaftTruncatedState) {
t.Helper()
legacyFound, err := engine.MVCCGetProto(
context.Background(), eng, keys.RaftTruncatedStateLegacyKey(rangeID),
hlc.Timestamp{}, &legacy, engine.MVCCGetOptions{},
)
if err != nil {
t.Fatal(err)
}
if legacyFound != (legacy != roachpb.RaftTruncatedState{}) {
t.Fatalf("legacy key found=%t but state is %+v", legacyFound, legacy)
}
unreplicatedFound, err := engine.MVCCGetProto(
context.Background(), eng, keys.RaftTruncatedStateKey(rangeID),
hlc.Timestamp{}, &unreplicated, engine.MVCCGetOptions{},
)
if err != nil {
t.Fatal(err)
}
if unreplicatedFound != (unreplicated != roachpb.RaftTruncatedState{}) {
t.Fatalf("unreplicated key found=%t but state is %+v", unreplicatedFound, unreplicated)
}
return
}
const (
expectationNeither = iota
expectationLegacy
expectationUnreplicated
)
type unreplicatedTruncStateTest struct {
startsWithLegacy bool
hasVersionBumped bool
exp int // see consts above
}
func TestTruncateLogUnreplicatedTruncatedState(t *testing.T) {
defer leaktest.AfterTest(t)()
// Follow the reference below for more information on what's being tested.
_ = cluster.VersionUnreplicatedRaftTruncatedState
const (
startsLegacy = true
startsUnreplicated = false
newVersion = true
oldVersion = false
)
testCases := []unreplicatedTruncStateTest{
// Steady states: we have one type of TruncatedState and will end up with
// the same type: either we've already migrated, or we haven't but aren't
// allowed to migrate yet.
{startsUnreplicated, oldVersion, expectationUnreplicated},
{startsUnreplicated, newVersion, expectationUnreplicated},
{startsLegacy, oldVersion, expectationLegacy},
// This is the case in which the migration is triggered. As a result,
// we see neither of the keys written. The new key will be written
// atomically as a side effect (outside of the scope of this test).
{startsLegacy, newVersion, expectationNeither},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) {
runUnreplicatedTruncatedState(t, tc)
})
}
}
func runUnreplicatedTruncatedState(t *testing.T, tc unreplicatedTruncStateTest) {
ctx := context.Background()
versionOff := cluster.VersionByKey(cluster.VersionUnreplicatedRaftTruncatedState - 1)
versionOn := cluster.VersionByKey(cluster.VersionUnreplicatedRaftTruncatedState)
st := cluster.MakeClusterSettings(versionOff, versionOn)
if tc.hasVersionBumped {
assert.NoError(t, st.InitializeVersion(cluster.ClusterVersion{Version: versionOn}))
} else {
assert.NoError(t, st.InitializeVersion(cluster.ClusterVersion{Version: versionOff}))
}
const (
rangeID = 12
term = 10
firstIndex = 100
)
evalCtx := mockEvalCtx{
clusterSettings: st,
desc: &roachpb.RangeDescriptor{RangeID: rangeID},
term: term,
firstIndex: firstIndex,
}
eng := engine.NewInMem(roachpb.Attributes{}, 1<<20)
defer eng.Close()
truncState := roachpb.RaftTruncatedState{
Index: firstIndex + 1,
Term: term,
}
// Put down the TruncatedState specified by the test case.
putTruncatedState(t, eng, rangeID, truncState, tc.startsWithLegacy)
// Send a truncation request.
req := roachpb.TruncateLogRequest{
RangeID: rangeID,
Index: firstIndex + 7,
}
cArgs := CommandArgs{
EvalCtx: &evalCtx,
Args: &req,
}
resp := &roachpb.TruncateLogResponse{}
res, err := TruncateLog(ctx, eng, cArgs, resp)
if err != nil {
t.Fatal(err)
}
expTruncState := roachpb.RaftTruncatedState{
Index: req.Index - 1,
Term: term,
}
legacy, unreplicated := readTruncStates(t, eng, rangeID)
switch tc.exp {
case expectationLegacy:
assert.Equal(t, expTruncState, legacy)
assert.Zero(t, unreplicated)
case expectationUnreplicated:
// The unreplicated key that we see should be the initial truncated
// state (it's only updated below Raft).
assert.Equal(t, truncState, unreplicated)
assert.Zero(t, legacy)
case expectationNeither:
assert.Zero(t, unreplicated)
assert.Zero(t, legacy)
default:
t.Fatalf("unknown expectation %d", tc.exp)
}
assert.NotNil(t, res.Replicated.State)
assert.NotNil(t, res.Replicated.State.TruncatedState)
assert.Equal(t, expTruncState, *res.Replicated.State.TruncatedState)
}