-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
task_test.go
371 lines (332 loc) · 10.4 KB
/
task_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
// Copyright 2018 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 rangefeed
import (
"context"
"sort"
"testing"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/stretchr/testify/require"
)
func makeKV(key, val string, ts int64) storage.MVCCKeyValue {
return storage.MVCCKeyValue{
Key: storage.MVCCKey{
Key: roachpb.Key(key),
Timestamp: hlc.Timestamp{WallTime: ts},
},
Value: []byte(val),
}
}
func makeProvisionalKV(key, val string, ts int64) storage.MVCCKeyValue {
return makeKV(key, val, ts)
}
func makeMetaKV(key string, meta enginepb.MVCCMetadata) storage.MVCCKeyValue {
b, err := protoutil.Marshal(&meta)
if err != nil {
panic(err)
}
return storage.MVCCKeyValue{
Key: storage.MVCCKey{
Key: roachpb.Key(key),
},
Value: b,
}
}
func makeInline(key, val string) storage.MVCCKeyValue {
return makeMetaKV(key, enginepb.MVCCMetadata{
RawBytes: []byte(val),
})
}
func makeIntent(key string, txnID uuid.UUID, txnKey string, txnTS int64) storage.MVCCKeyValue {
return makeMetaKV(key, enginepb.MVCCMetadata{
Txn: &enginepb.TxnMeta{
ID: txnID,
Key: []byte(txnKey),
WriteTimestamp: hlc.Timestamp{WallTime: txnTS},
MinTimestamp: hlc.Timestamp{WallTime: txnTS},
},
Timestamp: hlc.LegacyTimestamp{WallTime: txnTS},
})
}
type testIterator struct {
kvs []storage.MVCCKeyValue
cur int
closed bool
err error
block chan struct{}
done chan struct{}
}
func newTestIterator(kvs []storage.MVCCKeyValue) *testIterator {
// Ensure that the key-values are sorted.
if !sort.SliceIsSorted(kvs, func(i, j int) bool {
return kvs[i].Key.Less(kvs[j].Key)
}) {
panic("unsorted kvs")
}
// Ensure that every intent has a matching MVCCMetadata key
// and provisional key-value pair.
const missingErr = "missing provisional kv (makeProvisionalKV) for intent meta key (makeIntent)"
var meta enginepb.MVCCMetadata
for i := 0; i < len(kvs); i++ {
kv := kvs[i]
if !kv.Key.IsValue() {
if err := protoutil.Unmarshal(kv.Value, &meta); err != nil {
panic(err)
}
if !meta.IsInline() {
i++
if i == len(kvs) {
panic(missingErr)
}
expNextKey := storage.MVCCKey{
Key: kv.Key.Key,
Timestamp: hlc.Timestamp(meta.Timestamp),
}
if !kvs[i].Key.Equal(expNextKey) {
panic(missingErr)
}
}
}
}
return &testIterator{
kvs: kvs,
cur: -1,
done: make(chan struct{}),
}
}
func (s *testIterator) Close() {
s.closed = true
close(s.done)
}
func (s *testIterator) SeekGE(key storage.MVCCKey) {
if s.closed {
panic("testIterator closed")
}
if s.block != nil {
<-s.block
}
if s.err != nil {
return
}
if s.cur == -1 {
s.cur++
}
for ; s.cur < len(s.kvs); s.cur++ {
if !s.curKV().Key.Less(key) {
break
}
}
}
func (s *testIterator) Valid() (bool, error) {
if s.err != nil {
return false, s.err
}
if s.cur == -1 || s.cur >= len(s.kvs) {
return false, nil
}
return true, nil
}
func (s *testIterator) Next() { s.cur++ }
func (s *testIterator) NextKey() {
if s.cur == -1 {
s.cur = 0
return
}
origKey := s.curKV().Key.Key
for s.cur++; s.cur < len(s.kvs); s.cur++ {
if !s.curKV().Key.Key.Equal(origKey) {
break
}
}
}
func (s *testIterator) UnsafeKey() storage.MVCCKey {
return s.curKV().Key
}
func (s *testIterator) UnsafeValue() []byte {
return s.curKV().Value
}
func (s *testIterator) curKV() storage.MVCCKeyValue {
return s.kvs[s.cur]
}
func TestInitResolvedTSScan(t *testing.T) {
defer leaktest.AfterTest(t)()
// Mock processor. We just needs its eventC.
p := Processor{
Config: Config{
Span: roachpb.RSpan{
Key: roachpb.RKey("d"),
EndKey: roachpb.RKey("w"),
},
},
eventC: make(chan event, 100),
}
// Run an init rts scan over a test iterator with the following keys.
txn1, txn2 := uuid.MakeV4(), uuid.MakeV4()
iter := newTestIterator([]storage.MVCCKeyValue{
makeKV("a", "val1", 10),
makeInline("b", "val2"),
makeIntent("c", txn1, "txnKey1", 15),
makeProvisionalKV("c", "txnKey1", 15),
makeKV("c", "val3", 11),
makeKV("c", "val4", 9),
makeIntent("d", txn2, "txnKey2", 21),
makeProvisionalKV("d", "txnKey2", 21),
makeKV("d", "val5", 20),
makeKV("d", "val6", 19),
makeInline("g", "val7"),
makeKV("m", "val8", 1),
makeIntent("n", txn1, "txnKey1", 12),
makeProvisionalKV("n", "txnKey1", 12),
makeIntent("r", txn1, "txnKey1", 19),
makeProvisionalKV("r", "txnKey1", 19),
makeKV("r", "val9", 4),
makeIntent("w", txn1, "txnKey1", 3),
makeProvisionalKV("w", "txnKey1", 3),
makeInline("x", "val10"),
makeIntent("z", txn2, "txnKey2", 21),
makeProvisionalKV("z", "txnKey2", 21),
makeKV("z", "val11", 4),
})
initScan := newInitResolvedTSScan(&p, iter)
initScan.Run(context.Background())
require.True(t, iter.closed)
// Compare the event channel to the expected events.
expEvents := []event{
{ops: []enginepb.MVCCLogicalOp{
writeIntentOpWithKey(txn2, []byte("txnKey2"), hlc.Timestamp{WallTime: 21}),
}},
{ops: []enginepb.MVCCLogicalOp{
writeIntentOpWithKey(txn1, []byte("txnKey1"), hlc.Timestamp{WallTime: 12}),
}},
{ops: []enginepb.MVCCLogicalOp{
writeIntentOpWithKey(txn1, []byte("txnKey1"), hlc.Timestamp{WallTime: 19}),
}},
{initRTS: true},
}
require.Equal(t, len(expEvents), len(p.eventC))
for _, expEvent := range expEvents {
require.Equal(t, expEvent, <-p.eventC)
}
}
type testTxnPusher struct {
pushTxnsFn func([]enginepb.TxnMeta, hlc.Timestamp) ([]*roachpb.Transaction, error)
resolveIntentsFn func(ctx context.Context, intents []roachpb.LockUpdate) error
}
func (tp *testTxnPusher) PushTxns(
ctx context.Context, txns []enginepb.TxnMeta, ts hlc.Timestamp,
) ([]*roachpb.Transaction, error) {
return tp.pushTxnsFn(txns, ts)
}
func (tp *testTxnPusher) ResolveIntents(ctx context.Context, intents []roachpb.LockUpdate) error {
return tp.resolveIntentsFn(ctx, intents)
}
func (tp *testTxnPusher) mockPushTxns(
fn func([]enginepb.TxnMeta, hlc.Timestamp) ([]*roachpb.Transaction, error),
) {
tp.pushTxnsFn = fn
}
func (tp *testTxnPusher) intentsToTxns(intents []roachpb.LockUpdate) []enginepb.TxnMeta {
txns := make([]enginepb.TxnMeta, 0)
txnIDs := make(map[uuid.UUID]struct{})
for _, intent := range intents {
txn := intent.Txn
if _, ok := txnIDs[txn.ID]; ok {
continue
}
txns = append(txns, txn)
txnIDs[txn.ID] = struct{}{}
}
return txns
}
func (tp *testTxnPusher) mockResolveIntentsFn(
fn func(context.Context, []roachpb.LockUpdate) error,
) {
tp.resolveIntentsFn = fn
}
func TestTxnPushAttempt(t *testing.T) {
defer leaktest.AfterTest(t)()
// Create a set of transactions.
txn1, txn2, txn3, txn4 := uuid.MakeV4(), uuid.MakeV4(), uuid.MakeV4(), uuid.MakeV4()
ts1, ts2, ts3, ts4 := hlc.Timestamp{WallTime: 1}, hlc.Timestamp{WallTime: 2}, hlc.Timestamp{WallTime: 3}, hlc.Timestamp{WallTime: 4}
txn2LockSpans := []roachpb.Span{
{Key: roachpb.Key("a"), EndKey: roachpb.Key("b")},
{Key: roachpb.Key("c"), EndKey: roachpb.Key("d")},
}
txn4LockSpans := []roachpb.Span{
{Key: roachpb.Key("e"), EndKey: roachpb.Key("f")},
{Key: roachpb.Key("g"), EndKey: roachpb.Key("h")},
}
txn1Meta := enginepb.TxnMeta{ID: txn1, Key: keyA, WriteTimestamp: ts1, MinTimestamp: ts1}
txn2Meta := enginepb.TxnMeta{ID: txn2, Key: keyB, WriteTimestamp: ts2, MinTimestamp: ts2}
txn3Meta := enginepb.TxnMeta{ID: txn3, Key: keyC, WriteTimestamp: ts3, MinTimestamp: ts3}
txn4Meta := enginepb.TxnMeta{ID: txn4, Key: keyC, WriteTimestamp: ts3, MinTimestamp: ts4}
txn1Proto := &roachpb.Transaction{TxnMeta: txn1Meta, Status: roachpb.PENDING}
txn2Proto := &roachpb.Transaction{TxnMeta: txn2Meta, Status: roachpb.COMMITTED, LockSpans: txn2LockSpans}
txn3Proto := &roachpb.Transaction{TxnMeta: txn3Meta, Status: roachpb.ABORTED}
// txn4 has its LockSpans populated, simulated a transaction that has been
// rolled back by its coordinator (which populated the LockSpans), but then
// not GC'ed for whatever reason.
txn4Proto := &roachpb.Transaction{TxnMeta: txn4Meta, Status: roachpb.ABORTED, LockSpans: txn4LockSpans}
// Run a txnPushAttempt.
var tp testTxnPusher
tp.mockPushTxns(func(txns []enginepb.TxnMeta, ts hlc.Timestamp) ([]*roachpb.Transaction, error) {
require.Equal(t, 4, len(txns))
require.Equal(t, txn1Meta, txns[0])
require.Equal(t, txn2Meta, txns[1])
require.Equal(t, txn3Meta, txns[2])
require.Equal(t, txn4Meta, txns[3])
require.Equal(t, hlc.Timestamp{WallTime: 15}, ts)
// Return all four protos. The PENDING txn is pushed.
txn1ProtoPushed := txn1Proto.Clone()
txn1ProtoPushed.WriteTimestamp = ts
return []*roachpb.Transaction{txn1ProtoPushed, txn2Proto, txn3Proto, txn4Proto}, nil
})
tp.mockResolveIntentsFn(func(ctx context.Context, intents []roachpb.LockUpdate) error {
require.Len(t, intents, 4)
require.Equal(t, txn2LockSpans[0], intents[0].Span)
require.Equal(t, txn2LockSpans[1], intents[1].Span)
require.Equal(t, txn4LockSpans[0], intents[2].Span)
require.Equal(t, txn4LockSpans[1], intents[3].Span)
txns := tp.intentsToTxns(intents)
require.Equal(t, 2, len(txns))
require.Equal(t, txn2Meta, txns[0])
// Note that we don't expect intents for txn3 to be resolved since that txn
// doesn't have its LockSpans populated.
require.Equal(t, txn4Meta, txns[1])
return nil
})
// Mock processor. We just needs its eventC.
p := Processor{eventC: make(chan event, 100)}
p.TxnPusher = &tp
txns := []enginepb.TxnMeta{txn1Meta, txn2Meta, txn3Meta, txn4Meta}
doneC := make(chan struct{})
pushAttempt := newTxnPushAttempt(&p, txns, hlc.Timestamp{WallTime: 15}, doneC)
pushAttempt.Run(context.Background())
<-doneC // check if closed
// Compare the event channel to the expected events.
expEvents := []event{
{ops: []enginepb.MVCCLogicalOp{
updateIntentOp(txn1, hlc.Timestamp{WallTime: 15}),
updateIntentOp(txn2, hlc.Timestamp{WallTime: 2}),
abortTxnOp(txn3),
abortTxnOp(txn4),
}},
}
require.Equal(t, len(expEvents), len(p.eventC))
for _, expEvent := range expEvents {
require.Equal(t, expEvent, <-p.eventC)
}
}