-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
raft_applied_index_term_external_test.go
249 lines (235 loc) · 8.7 KB
/
raft_applied_index_term_external_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
// Copyright 2022 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 migrations_test
import (
"context"
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/stateloader"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
func forAllReplicas(tc *testcluster.TestCluster, f func(*kvserver.Replica) error) error {
for i := 0; i < tc.NumServers(); i++ {
err := tc.Server(i).GetStores().(*kvserver.Stores).VisitStores(func(s *kvserver.Store) error {
var err error
s.VisitReplicas(func(repl *kvserver.Replica) bool {
err = f(repl)
return err == nil
})
return err
})
if err != nil {
return err
}
}
return nil
}
func getReplicaCounts(
ctx context.Context, t *testing.T, tc *testcluster.TestCluster,
) (totalReplicas int, replicasWithTerm int) {
t.Helper()
require.NoError(t, forAllReplicas(tc, func(repl *kvserver.Replica) error {
totalReplicas++
sl := stateloader.Make(repl.RangeID)
appliedState, err := sl.LoadRangeAppliedState(ctx, repl.Engine())
if err != nil {
return err
}
// Basic sanity checking of the replica.
if appliedState.RaftAppliedIndex == 0 {
return errors.Errorf("expected initialized replica")
}
appliedIndexTermInReplicaStruct := repl.State(ctx).RaftAppliedIndexTerm
// The in-memory ReplicaState maintained in Replica must be consistent
// with that we are placing in RangeAppliedState.
require.Equal(t, appliedState.RaftAppliedIndexTerm, appliedIndexTermInReplicaStruct)
truncState, err := sl.LoadRaftTruncatedState(ctx, repl.Engine())
if err != nil {
return err
}
if truncState.Index > appliedState.RaftAppliedIndex {
return errors.Errorf("truncated state index %d > applied index %d",
truncState.Index, appliedState.RaftAppliedIndex)
}
// We could simply call repl.GetTerm at this point, but we do some more
// sanity checking.
var appliedIndexTerm uint64
if truncState.Index == appliedState.RaftAppliedIndex {
appliedIndexTerm = truncState.Term
} else {
lastIndex, err := sl.LoadLastIndex(ctx, repl.Engine())
if err != nil {
return err
}
if lastIndex < appliedState.RaftAppliedIndex {
return errors.Errorf("last index in raft log %d < applied index %d",
lastIndex, appliedState.RaftAppliedIndex)
}
if appliedIndexTerm, err = repl.GetTerm(appliedState.RaftAppliedIndex); err != nil {
return err
}
}
// Now we can decide whether term is being populated or not, or being
// incorrectly populated.
if appliedState.RaftAppliedIndexTerm == 0 {
// Not populated
} else if appliedState.RaftAppliedIndexTerm == appliedIndexTerm {
replicasWithTerm++
} else {
return errors.Errorf("expected term %d but found %d", appliedIndexTerm,
appliedState.RaftAppliedIndexTerm)
}
return nil
}))
return totalReplicas, replicasWithTerm
}
func TestRaftAppliedIndexTermMigration(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
binaryVersion := clusterversion.ByKey(clusterversion.PostAddRaftAppliedIndexTermMigration)
bootstrapVersion := clusterversion.ByKey(clusterversion.AddRaftAppliedIndexTermMigration - 1)
makeArgs := func() (args base.TestServerArgs) {
args.Settings = cluster.MakeTestingClusterSettingsWithVersions(
binaryVersion, bootstrapVersion, false,
)
args.Knobs.Server = &server.TestingKnobs{
// Start at the version immediately preceding the migration.
BinaryVersionOverride: bootstrapVersion,
// We want to exercise manual control over the upgrade process.
DisableAutomaticVersionUpgrade: 1,
}
return args
}
tc := testcluster.StartTestCluster(t, 3, base.TestClusterArgs{
ServerArgsPerNode: map[int]base.TestServerArgs{
0: makeArgs(),
1: makeArgs(),
2: makeArgs(),
},
})
defer tc.Stopper().Stop(ctx)
totalReplicas, replicasWithTerm := getReplicaCounts(ctx, t, tc)
require.Equal(t, 0, replicasWithTerm)
require.Less(t, 0, totalReplicas)
t.Logf("totalReplicas: %d, replicasWithTerm: %d", totalReplicas, replicasWithTerm)
var scratchKey roachpb.Key
{
// Do a split.
scratchKey = tc.ScratchRange(t)
desc, err := tc.LookupRange(scratchKey)
require.NoError(t, err)
span := desc.KeySpan()
t.Logf("splitting range: %s", span.String())
splitKey := append(span.Key, '0', '0', '0')
leftDesc, rightDesc, err := tc.SplitRange(roachpb.Key(splitKey))
require.NoError(t, err)
t.Logf("split range into %s and %s", leftDesc.KeySpan(), rightDesc.KeySpan())
total, withTerm := getReplicaCounts(ctx, t, tc)
require.Less(t, totalReplicas, total)
require.Equal(t, 0, withTerm)
totalReplicas, replicasWithTerm = total, withTerm
t.Logf("after split: totalReplicas: %d, replicasWithTerm: %d",
totalReplicas, replicasWithTerm)
}
// Do the migration.
_, err := tc.Conns[0].ExecContext(
ctx, `SET CLUSTER SETTING version = $1`, binaryVersion.String(),
)
require.NoError(t, err)
testutils.SucceedsSoon(t, func() error {
return forAllReplicas(tc, func(repl *kvserver.Replica) error {
// TODO(sumeer): when using PostAddRaftAppliedIndexTermMigration, which
// is 21.2-54, this never completes even though all 3 nodes have log
// entries saying:
// [n1,bump-cluster-version] 3480 active cluster version setting is now 21.2-54 (up from 21.2-53(fence))
if repl.Version().Less(clusterversion.ByKey(
clusterversion.AddRaftAppliedIndexTermMigration)) {
return errors.Newf("unexpected version %s", repl.Version())
}
return nil
})
})
totalReplicas, replicasWithTerm = getReplicaCounts(ctx, t, tc)
require.Equal(t, totalReplicas, replicasWithTerm)
require.Less(t, 0, totalReplicas)
t.Logf("totalReplicas: %d, replicasWithTerm: %d", totalReplicas, replicasWithTerm)
// Do another split
{
desc, err := tc.LookupRange(scratchKey)
require.NoError(t, err)
span := desc.KeySpan()
t.Logf("splitting range: %s", span.String())
splitKey := append(span.Key, '0', '0')
leftDesc, rightDesc, err := tc.SplitRange(roachpb.Key(splitKey))
require.NoError(t, err)
t.Logf("split range into %s and %s", leftDesc.KeySpan(), rightDesc.KeySpan())
total, withTerm := getReplicaCounts(ctx, t, tc)
require.Less(t, totalReplicas, total)
require.Equal(t, total, withTerm)
totalReplicas, replicasWithTerm = total, withTerm
t.Logf("after second split: totalReplicas: %d, replicasWithTerm: %d",
totalReplicas, replicasWithTerm)
}
// Check that the term is maintained when a write happens.
kvDB := tc.Server(0).DB()
rScratchKey, err := keys.Addr(scratchKey)
require.NoError(t, err)
repl := tc.GetRaftLeader(t, rScratchKey)
rs1 := repl.State(ctx).ReplicaState
require.NoError(t, kvDB.Put(ctx, scratchKey, 10))
testutils.SucceedsSoon(t, func() error {
rs2 := repl.State(ctx).ReplicaState
if rs1.RaftAppliedIndex == rs2.RaftAppliedIndex {
return errors.Errorf("waiting for application")
}
return nil
})
rs2 := repl.State(ctx).ReplicaState
require.Less(t, rs1.RaftAppliedIndex, rs2.RaftAppliedIndex)
require.Equal(t, rs1.RaftAppliedIndexTerm, rs2.RaftAppliedIndexTerm)
}
func TestLatestClusterDoesNotNeedRaftAppliedIndexTermMigration(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
binaryVersion := clusterversion.ByKey(clusterversion.PostAddRaftAppliedIndexTermMigration)
makeArgs := func() (args base.TestServerArgs) {
args.Settings = cluster.MakeTestingClusterSettingsWithVersions(
binaryVersion, binaryVersion, false,
)
args.Knobs.Server = &server.TestingKnobs{
BinaryVersionOverride: binaryVersion,
// We want to exercise manual control over the upgrade process.
DisableAutomaticVersionUpgrade: 1,
}
return args
}
tc := testcluster.StartTestCluster(t, 3, base.TestClusterArgs{
ServerArgsPerNode: map[int]base.TestServerArgs{
0: makeArgs(),
1: makeArgs(),
2: makeArgs(),
},
})
defer tc.Stopper().Stop(ctx)
totalReplicas, replicasWithTerm := getReplicaCounts(ctx, t, tc)
require.Equal(t, totalReplicas, replicasWithTerm)
require.Less(t, 0, totalReplicas)
t.Logf("totalReplicas: %d, replicasWithTerm: %d", totalReplicas, replicasWithTerm)
}