-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathreplica_proposal_buf_test.go
349 lines (301 loc) · 9.29 KB
/
replica_proposal_buf_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
// 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 kvserver
import (
"context"
"math/rand"
"sync"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.etcd.io/etcd/raft"
"golang.org/x/sync/errgroup"
)
// testProposer is a testing implementation of proposer.
type testProposer struct {
syncutil.RWMutex
ds destroyStatus
lai uint64
enqueued int
registered int
}
func (t *testProposer) locker() sync.Locker {
return &t.RWMutex
}
func (t *testProposer) rlocker() sync.Locker {
return t.RWMutex.RLocker()
}
func (t *testProposer) replicaID() roachpb.ReplicaID {
return 1
}
func (t *testProposer) destroyed() destroyStatus {
return t.ds
}
func (t *testProposer) leaseAppliedIndex() uint64 {
return t.lai
}
func (t *testProposer) enqueueUpdateCheck() {
t.enqueued++
}
func (t *testProposer) withGroupLocked(fn func(*raft.RawNode) error) error {
// Pass nil for the RawNode, which FlushLockedWithRaftGroup supports.
return fn(nil)
}
func (t *testProposer) registerProposalLocked(p *ProposalData) {
t.registered++
}
func (t *testProposer) rejectProposalWithRedirectLocked(
ctx context.Context, prop *ProposalData, redirectTo roachpb.ReplicaID,
) {
panic("unimplemented")
}
func newPropData(leaseReq bool) (*ProposalData, []byte) {
var ba roachpb.BatchRequest
if leaseReq {
ba.Add(&roachpb.RequestLeaseRequest{})
} else {
ba.Add(&roachpb.PutRequest{})
}
return &ProposalData{
command: &kvserverpb.RaftCommand{},
Request: &ba,
}, make([]byte, 0, kvserverpb.MaxRaftCommandFooterSize())
}
// TestProposalBuffer tests the basic behavior of the Raft proposal buffer.
func TestProposalBuffer(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
var p testProposer
var b propBuf
b.Init(&p)
// Insert propBufArrayMinSize proposals. The buffer should not be flushed.
num := propBufArrayMinSize
for i := 0; i < num; i++ {
leaseReq := i == 3
pd, data := newPropData(leaseReq)
mlai, err := b.Insert(ctx, pd, data)
require.Nil(t, err)
if leaseReq {
expMlai := uint64(i)
require.Equal(t, uint64(0), mlai)
require.Equal(t, expMlai, pd.command.MaxLeaseIndex)
require.Equal(t, expMlai, b.LastAssignedLeaseIndexRLocked())
} else {
expMlai := uint64(i + 1)
require.Equal(t, expMlai, mlai)
require.Equal(t, expMlai, pd.command.MaxLeaseIndex)
require.Equal(t, expMlai, b.LastAssignedLeaseIndexRLocked())
}
require.Equal(t, i+1, b.Len())
require.Equal(t, 1, p.enqueued)
require.Equal(t, 0, p.registered)
}
// Insert another proposal. This causes the buffer to flush. Doing so
// results in a lease applied index being skipped, which is harmless.
// Remember that the lease request above did not receive a lease index.
pd, data := newPropData(false)
mlai, err := b.Insert(ctx, pd, data)
require.Nil(t, err)
expMlai := uint64(num + 1)
require.Equal(t, expMlai, mlai)
require.Equal(t, expMlai, pd.command.MaxLeaseIndex)
require.Equal(t, expMlai, b.LastAssignedLeaseIndexRLocked())
require.Equal(t, 1, b.Len())
require.Equal(t, 2, p.enqueued)
require.Equal(t, num, p.registered)
require.Equal(t, uint64(num), b.liBase)
require.Equal(t, 2*propBufArrayMinSize, b.arr.len())
// Increase the proposer's applied lease index and flush. The buffer's
// lease index offset should jump up.
p.lai = 10
require.Nil(t, b.flushLocked(ctx))
require.Equal(t, 0, b.Len())
require.Equal(t, 2, p.enqueued)
require.Equal(t, num+1, p.registered)
require.Equal(t, p.lai, b.liBase)
// Insert one more proposal. The lease applied index should adjust to
// the increase accordingly.
mlai, err = b.Insert(ctx, pd, data)
require.Nil(t, err)
expMlai = p.lai + 1
require.Equal(t, expMlai, mlai)
require.Equal(t, expMlai, pd.command.MaxLeaseIndex)
require.Equal(t, expMlai, b.LastAssignedLeaseIndexRLocked())
require.Equal(t, 1, b.Len())
require.Equal(t, 3, p.enqueued)
require.Equal(t, num+1, p.registered)
// Flush the buffer repeatedly until its array shrinks. We've already
// flushed once above, so start iterating at 1.
for i := 1; i < propBufArrayShrinkDelay; i++ {
require.Equal(t, 2*propBufArrayMinSize, b.arr.len())
require.Nil(t, b.flushLocked(ctx))
}
require.Equal(t, propBufArrayMinSize, b.arr.len())
}
// TestProposalBufferConcurrentWithDestroy tests the concurrency properties of
// the Raft proposal buffer.
func TestProposalBufferConcurrentWithDestroy(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
var p testProposer
var b propBuf
b.Init(&p)
mlais := make(map[uint64]struct{})
dsErr := errors.New("destroyed")
// Run 20 concurrent producers.
var g errgroup.Group
const concurrency = 20
for i := 0; i < concurrency; i++ {
g.Go(func() error {
for {
pd, data := newPropData(false)
mlai, err := b.Insert(ctx, pd, data)
if err != nil {
if errors.Is(err, dsErr) {
return nil
}
return errors.Wrap(err, "Insert")
}
p.Lock()
if _, ok := mlais[mlai]; ok {
p.Unlock()
return errors.New("max lease index collision")
}
mlais[mlai] = struct{}{}
p.Unlock()
}
})
}
// Run a concurrent consumer.
g.Go(func() error {
for {
if stop, err := func() (bool, error) {
p.Lock()
defer p.Unlock()
if !p.ds.IsAlive() {
return true, nil
}
if err := b.flushLocked(ctx); err != nil {
return true, errors.Wrap(err, "flushLocked")
}
return false, nil
}(); stop {
return err
}
}
})
// Wait for a random duration before destroying.
time.Sleep(time.Duration(rand.Intn(1000)) * time.Microsecond)
// Destroy the proposer. All producers and consumers should notice.
p.Lock()
p.ds.Set(dsErr, destroyReasonRemoved)
p.Unlock()
require.Nil(t, g.Wait())
t.Logf("%d successful proposals before destroy", len(mlais))
}
// TestProposalBufferRegistersAllOnProposalError tests that all proposals in the
// proposal buffer are registered with the proposer when the buffer is flushed,
// even if an error is seen when proposing a batch of entries.
func TestProposalBufferRegistersAllOnProposalError(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
var p testProposer
var b propBuf
b.Init(&p)
num := propBufArrayMinSize
for i := 0; i < num; i++ {
pd, data := newPropData(false)
_, err := b.Insert(ctx, pd, data)
require.Nil(t, err)
}
require.Equal(t, num, b.Len())
propNum := 0
propErr := errors.New("failed proposal")
b.testing.submitProposalFilter = func(*ProposalData) (drop bool, err error) {
propNum++
require.Equal(t, propNum, p.registered)
if propNum == 2 {
return false, propErr
}
return false, nil
}
err := b.flushLocked(ctx)
require.Equal(t, propErr, err)
require.Equal(t, num, p.registered)
}
// TestProposalBufferRegistrationWithInsertionErrors tests that if during
// proposal insertion we reserve array indexes but are unable to actually insert
// them due to errors, we simply ignore said indexes when flushing proposals.
func TestProposalBufferRegistrationWithInsertionErrors(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
var p testProposer
var b propBuf
b.Init(&p)
num := propBufArrayMinSize / 2
for i := 0; i < num; i++ {
pd, data := newPropData(i%2 == 0)
_, err := b.Insert(ctx, pd, data)
require.Nil(t, err)
}
var insertErr = errors.New("failed insertion")
b.testing.leaseIndexFilter = func(*ProposalData) (indexOverride uint64, err error) {
return 0, insertErr
}
for i := 0; i < num; i++ {
pd, data := newPropData(i%2 == 0)
_, err := b.Insert(ctx, pd, data)
require.Equal(t, insertErr, err)
}
require.Equal(t, 2*num, b.Len())
require.Nil(t, b.flushLocked(ctx))
require.Equal(t, 0, b.Len())
require.Equal(t, num, p.registered)
}
// TestPropBufCnt tests the basic behavior of the counter maintained by the
// proposal buffer.
func TestPropBufCnt(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
var count propBufCnt
const numReqs = 10
reqLeaseInc := makePropBufCntReq(true)
reqLeaseNoInc := makePropBufCntReq(false)
for i := 0; i < numReqs; i++ {
count.update(reqLeaseInc)
}
res := count.read()
assert.Equal(t, numReqs, res.arrayLen())
assert.Equal(t, numReqs-1, res.arrayIndex())
assert.Equal(t, uint64(numReqs), res.leaseIndexOffset())
for i := 0; i < numReqs; i++ {
count.update(reqLeaseNoInc)
}
res = count.read()
assert.Equal(t, 2*numReqs, res.arrayLen())
assert.Equal(t, (2*numReqs)-1, res.arrayIndex())
assert.Equal(t, uint64(numReqs), res.leaseIndexOffset())
count.clear()
res = count.read()
assert.Equal(t, 0, res.arrayLen())
assert.Equal(t, -1, res.arrayIndex())
assert.Equal(t, uint64(0), res.leaseIndexOffset())
}