-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
applier.go
602 lines (571 loc) · 18.3 KB
/
applier.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Copyright 2020 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 kvnemesis
import (
"context"
"time"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvnemesis/kvnemesisutil"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
"github.com/cockroachdb/errors"
"google.golang.org/protobuf/proto"
)
// Applier executes Steps.
type Applier struct {
env *Env
dbs []*kv.DB
mu struct {
dbIdx int
syncutil.Mutex
txns map[string]*kv.Txn
}
}
// MakeApplier constructs an Applier that executes against the given DBs.
func MakeApplier(env *Env, dbs ...*kv.DB) *Applier {
a := &Applier{
env: env,
dbs: dbs,
}
a.mu.txns = make(map[string]*kv.Txn)
return a
}
// Apply executes the given Step and mutates it with the result of execution. An
// error is only returned from Apply if there is an internal coding error within
// Applier, errors from a Step execution are saved in the Step itself.
func (a *Applier) Apply(ctx context.Context, step *Step) (trace tracingpb.Recording, retErr error) {
var db *kv.DB
db, step.DBID = a.getNextDBRoundRobin()
step.Before = db.Clock().Now()
recCtx, collectAndFinish := tracing.ContextWithRecordingSpan(ctx, db.Tracer, "txn step")
defer func() {
step.After = db.Clock().Now()
if p := recover(); p != nil {
retErr = errors.Errorf(`panic applying step %s: %v`, step, p)
}
trace = collectAndFinish()
}()
applyOp(recCtx, a.env, db, &step.Op)
return collectAndFinish(), nil
}
func (a *Applier) getNextDBRoundRobin() (*kv.DB, int32) {
a.mu.Lock()
dbIdx := a.mu.dbIdx
a.mu.dbIdx = (a.mu.dbIdx + 1) % len(a.dbs)
a.mu.Unlock()
return a.dbs[dbIdx], int32(dbIdx)
}
// Sentinel errors.
var (
errOmitted = errors.New("omitted")
errClosureTxnRollback = errors.New("rollback")
errDelRangeUsingTombstoneStraddlesRangeBoundary = errors.New("DeleteRangeUsingTombstone can not straddle range boundary")
)
func exceptOmitted(err error) bool { // true if errOmitted
return errors.Is(err, errOmitted)
}
func exceptRollback(err error) bool { // true if intentional txn rollback
return errors.Is(err, errClosureTxnRollback)
}
func exceptRetry(err error) bool { // true if retry error
return errors.HasInterface(err, (*kvpb.ClientVisibleRetryError)(nil))
}
func exceptUnhandledRetry(err error) bool {
return errors.HasType(err, (*kvpb.UnhandledRetryableError)(nil))
}
func exceptAmbiguous(err error) bool { // true if ambiguous result
return errors.HasInterface(err, (*kvpb.ClientVisibleAmbiguousError)(nil))
}
func exceptDelRangeUsingTombstoneStraddlesRangeBoundary(err error) bool {
return errors.Is(err, errDelRangeUsingTombstoneStraddlesRangeBoundary)
}
func exceptSharedLockPromotionError(err error) bool { // true if lock promotion error
return errors.Is(err, &concurrency.LockPromotionError{})
}
func applyOp(ctx context.Context, env *Env, db *kv.DB, op *Operation) {
switch o := op.GetValue().(type) {
case *GetOperation,
*PutOperation,
*ScanOperation,
*BatchOperation,
*DeleteOperation,
*DeleteRangeOperation,
*DeleteRangeUsingTombstoneOperation,
*AddSSTableOperation:
applyClientOp(ctx, db, op, false)
case *SplitOperation:
err := db.AdminSplit(ctx, o.Key, hlc.MaxTimestamp)
o.Result = resultInit(ctx, err)
case *MergeOperation:
err := db.AdminMerge(ctx, o.Key)
o.Result = resultInit(ctx, err)
case *ChangeReplicasOperation:
desc := getRangeDesc(ctx, o.Key, db)
_, err := db.AdminChangeReplicas(ctx, o.Key, desc, o.Changes)
o.Result = resultInit(ctx, err)
case *TransferLeaseOperation:
err := db.AdminTransferLease(ctx, o.Key, o.Target)
o.Result = resultInit(ctx, err)
case *ChangeZoneOperation:
err := updateZoneConfigInEnv(ctx, env, o.Type)
o.Result = resultInit(ctx, err)
case *ClosureTxnOperation:
// Use a backoff loop to avoid thrashing on txn aborts. Don't wait between
// epochs of the same transaction to avoid waiting while holding locks.
retryOnAbort := retry.StartWithCtx(ctx, retry.Options{
InitialBackoff: 1 * time.Millisecond,
MaxBackoff: 250 * time.Millisecond,
})
var savedTxn *kv.Txn
txnErr := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
if err := txn.SetIsoLevel(o.IsoLevel); err != nil {
panic(err)
}
if savedTxn != nil && txn.TestingCloneTxn().Epoch == 0 {
// If the txn's current epoch is 0 and we've run at least one prior
// iteration, we were just aborted.
retryOnAbort.Next()
}
savedTxn = txn
// First error. Because we need to mark everything that
// we didn't "reach" due to a prior error with errOmitted,
// we *don't* return eagerly on this but save it to the end.
var err error
{
for i := range o.Ops {
op := &o.Ops[i]
op.Result().Reset() // in case we're a retry
if err != nil {
// If a previous op failed, mark this op as never invoked. We need
// to do this because we want, as an invariant, to have marked all
// operations as either failed or succeeded.
*op.Result() = resultInit(ctx, errOmitted)
if op.Batch != nil {
for _, op := range op.Batch.Ops {
*op.Result() = resultInit(ctx, errOmitted)
}
}
continue
}
applyClientOp(ctx, txn, op, true)
// The KV api disallows use of a txn after an operation on it errors.
if r := op.Result(); r.Type == ResultType_Error {
err = errors.DecodeError(ctx, *r.Err)
}
}
}
if err != nil {
if o.CommitInBatch != nil {
// We failed before committing, so set errOmitted everywhere
// and then return the original error.
o.CommitInBatch.Result = resultInit(ctx, errOmitted)
for _, op := range o.CommitInBatch.Ops {
// NB: the `op` is definitely not a batch since we can't nest
// batches within each other, so we don't need that second level of
// recursion here.
*op.Result() = resultInit(ctx, errOmitted)
}
}
return err
}
if o.CommitInBatch != nil {
b := txn.NewBatch()
applyBatchOp(ctx, b, txn.CommitInBatch, o.CommitInBatch)
// The KV api disallows use of a txn after an operation on it errors.
if r := o.CommitInBatch.Result; r.Type == ResultType_Error {
return errors.DecodeError(ctx, *r.Err)
}
}
switch o.Type {
case ClosureTxnType_Commit:
return nil
case ClosureTxnType_Rollback:
return errClosureTxnRollback
default:
panic(errors.AssertionFailedf(`unknown closure txn type: %s`, o.Type))
}
})
o.Result = resultInit(ctx, txnErr)
if txnErr == nil {
o.Txn = savedTxn.TestingCloneTxn()
o.Result.OptionalTimestamp = o.Txn.WriteTimestamp
}
default:
panic(errors.AssertionFailedf(`unknown operation type: %T %v`, o, o))
}
}
type dbRunI interface {
Run(context.Context, *kv.Batch) error
}
type clientI interface {
dbRunI
Get(context.Context, interface{}) (kv.KeyValue, error)
GetForUpdate(context.Context, interface{}) (kv.KeyValue, error)
GetForShare(context.Context, interface{}) (kv.KeyValue, error)
Put(context.Context, interface{}, interface{}) error
Scan(context.Context, interface{}, interface{}, int64) ([]kv.KeyValue, error)
ScanForUpdate(context.Context, interface{}, interface{}, int64) ([]kv.KeyValue, error)
ScanForShare(context.Context, interface{}, interface{}, int64) ([]kv.KeyValue, error)
ReverseScan(context.Context, interface{}, interface{}, int64) ([]kv.KeyValue, error)
ReverseScanForUpdate(context.Context, interface{}, interface{}, int64) ([]kv.KeyValue, error)
ReverseScanForShare(context.Context, interface{}, interface{}, int64) ([]kv.KeyValue, error)
Del(context.Context, ...interface{}) ([]roachpb.Key, error)
DelRange(context.Context, interface{}, interface{}, bool) ([]roachpb.Key, error)
}
func dbRunWithResultAndTimestamp(
ctx context.Context, db dbRunI, ops ...func(b *kv.Batch),
) ([]kv.Result, hlc.Timestamp, error) {
b := &kv.Batch{}
for _, op := range ops {
op(b)
}
ts, err := batchRun(ctx, db.Run, b)
if err != nil {
return nil, hlc.Timestamp{}, err
}
return b.Results, ts, nil
}
func batchRun(
ctx context.Context, run func(context.Context, *kv.Batch) error, b *kv.Batch,
) (hlc.Timestamp, error) {
if err := run(ctx, b); err != nil {
return hlc.Timestamp{}, err
}
var ts hlc.Timestamp
if rr := b.RawResponse(); rr != nil {
ts = rr.Timestamp
}
return ts, nil
}
func applyClientOp(ctx context.Context, db clientI, op *Operation, inTxn bool) {
switch o := op.GetValue().(type) {
case *GetOperation:
res, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
if o.SkipLocked {
b.Header.WaitPolicy = lock.WaitPolicy_SkipLocked
}
dur := kvpb.BestEffort
if o.ForUpdate {
(*kv.Batch).GetForUpdate(b, o.Key, dur)
} else if o.ForShare {
(*kv.Batch).GetForShare(b, o.Key, dur)
} else {
(*kv.Batch).Get(b, o.Key)
}
})
o.Result = resultInit(ctx, err)
if err != nil {
return
}
o.Result.Type = ResultType_Value
o.Result.OptionalTimestamp = ts
kv := res[0].Rows[0]
if kv.Value != nil {
o.Result.Value = kv.Value.RawBytes
} else {
o.Result.Value = nil
}
case *PutOperation:
_, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
b.Put(o.Key, o.Value())
setLastReqSeq(b, o.Seq)
})
o.Result = resultInit(ctx, err)
if err != nil {
return
}
o.Result.OptionalTimestamp = ts
case *ScanOperation:
res, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
if o.SkipLocked {
b.Header.WaitPolicy = lock.WaitPolicy_SkipLocked
}
dur := kvpb.BestEffort
if o.Reverse {
if o.ForUpdate {
(*kv.Batch).ReverseScanForUpdate(b, o.Key, o.EndKey, dur)
} else if o.ForShare {
(*kv.Batch).ReverseScanForShare(b, o.Key, o.EndKey, dur)
} else {
(*kv.Batch).ReverseScan(b, o.Key, o.EndKey)
}
} else {
if o.ForUpdate {
(*kv.Batch).ScanForUpdate(b, o.Key, o.EndKey, dur)
} else if o.ForShare {
(*kv.Batch).ScanForShare(b, o.Key, o.EndKey, dur)
} else {
(*kv.Batch).Scan(b, o.Key, o.EndKey)
}
}
})
o.Result = resultInit(ctx, err)
if err != nil {
return
}
kvs := res[0].Rows
o.Result.OptionalTimestamp = ts
o.Result.Type = ResultType_Values
o.Result.Values = make([]KeyValue, len(kvs))
for i, kv := range kvs {
o.Result.Values[i] = KeyValue{
Key: kv.Key,
Value: kv.Value.RawBytes,
}
}
case *DeleteOperation:
res, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
b.Del(o.Key)
setLastReqSeq(b, o.Seq)
})
o.Result = resultInit(ctx, err)
if err != nil {
return
}
deletedKeys := res[0].Keys
o.Result.OptionalTimestamp = ts
o.Result.Type = ResultType_Keys
o.Result.Keys = make([][]byte, len(deletedKeys))
for i, deletedKey := range deletedKeys {
o.Result.Keys[i] = deletedKey
}
case *DeleteRangeOperation:
res, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
b.DelRange(o.Key, o.EndKey, true /* returnKeys */)
setLastReqSeq(b, o.Seq)
})
o.Result = resultInit(ctx, err)
if err != nil {
return
}
deletedKeys := res[0].Keys
o.Result.OptionalTimestamp = ts
o.Result.Type = ResultType_Keys
o.Result.Keys = make([][]byte, len(deletedKeys))
for i, deletedKey := range deletedKeys {
o.Result.Keys[i] = deletedKey
}
case *DeleteRangeUsingTombstoneOperation:
_, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
b.DelRangeUsingTombstone(o.Key, o.EndKey)
setLastReqSeq(b, o.Seq)
})
o.Result = resultInit(ctx, err)
if err != nil {
return
}
o.Result.OptionalTimestamp = ts
case *AddSSTableOperation:
if inTxn {
panic(errors.AssertionFailedf(`AddSSTable cannot be used in transactions`))
}
_, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
// Unlike other write operations, AddSSTable sends raw MVCC values
// directly through to storage, including an MVCCValueHeader already
// tagged with the sequence number. We therefore don't need to pass the
// sequence number via the RequestHeader here.
b.AddRawRequest(&kvpb.AddSSTableRequest{
RequestHeader: kvpb.RequestHeader{
Key: o.Span.Key,
EndKey: o.Span.EndKey,
},
Data: o.Data,
SSTTimestampToRequestTimestamp: o.SSTTimestamp,
DisallowConflicts: true,
IngestAsWrites: o.AsWrites,
})
})
o.Result = resultInit(ctx, err)
if err != nil {
return
}
o.Result.OptionalTimestamp = ts
case *BatchOperation:
b := &kv.Batch{}
applyBatchOp(ctx, b, db.Run, o)
default:
panic(errors.AssertionFailedf(`unknown batch operation type: %T %v`, o, o))
}
}
func setLastReqSeq(b *kv.Batch, seq kvnemesisutil.Seq) {
sl := b.Requests()
req := sl[len(sl)-1].GetInner()
h := req.Header()
h.KVNemesisSeq.Set(seq)
req.SetHeader(h)
}
func applyBatchOp(
ctx context.Context, b *kv.Batch, run func(context.Context, *kv.Batch) error, o *BatchOperation,
) {
for i := range o.Ops {
switch subO := o.Ops[i].GetValue().(type) {
case *GetOperation:
// TODO(arul): Looks like I forgot to add shared locks here.
dur := kvpb.BestEffort
if subO.ForUpdate {
b.GetForUpdate(subO.Key, dur)
} else {
b.Get(subO.Key)
}
case *PutOperation:
b.Put(subO.Key, subO.Value())
setLastReqSeq(b, subO.Seq)
case *ScanOperation:
dur := kvpb.BestEffort
if subO.Reverse && subO.ForUpdate {
b.ReverseScanForUpdate(subO.Key, subO.EndKey, dur)
} else if subO.Reverse {
b.ReverseScan(subO.Key, subO.EndKey)
} else if subO.ForUpdate {
b.ScanForUpdate(subO.Key, subO.EndKey, dur)
} else {
b.Scan(subO.Key, subO.EndKey)
}
case *DeleteOperation:
b.Del(subO.Key)
setLastReqSeq(b, subO.Seq)
case *DeleteRangeOperation:
b.DelRange(subO.Key, subO.EndKey, true /* returnKeys */)
setLastReqSeq(b, subO.Seq)
case *DeleteRangeUsingTombstoneOperation:
b.DelRangeUsingTombstone(subO.Key, subO.EndKey)
setLastReqSeq(b, subO.Seq)
case *AddSSTableOperation:
panic(errors.AssertionFailedf(`AddSSTable cannot be used in batches`))
default:
panic(errors.AssertionFailedf(`unknown batch operation type: %T %v`, subO, subO))
}
}
ts, err := batchRun(ctx, run, b)
o.Result = resultInit(ctx, err)
// NB: we intentionally fall through; the batch propagates the error
// to each result.
err = nil
o.Result.OptionalTimestamp = ts
for i := range o.Ops {
switch subO := o.Ops[i].GetValue().(type) {
case *GetOperation:
if b.Results[i].Err != nil {
subO.Result = resultInit(ctx, b.Results[i].Err)
} else {
subO.Result.Type = ResultType_Value
result := b.Results[i].Rows[0]
if result.Value != nil {
subO.Result.Value = result.Value.RawBytes
} else {
subO.Result.Value = nil
}
}
case *PutOperation:
err := b.Results[i].Err
subO.Result = resultInit(ctx, err)
case *ScanOperation:
kvs, err := b.Results[i].Rows, b.Results[i].Err
if err != nil {
subO.Result = resultInit(ctx, err)
} else {
subO.Result.Type = ResultType_Values
subO.Result.Values = make([]KeyValue, len(kvs))
for j, kv := range kvs {
subO.Result.Values[j] = KeyValue{
Key: []byte(kv.Key),
Value: kv.Value.RawBytes,
}
}
}
case *DeleteOperation:
err := b.Results[i].Err
subO.Result = resultInit(ctx, err)
case *DeleteRangeOperation:
keys, err := b.Results[i].Keys, b.Results[i].Err
if err != nil {
subO.Result = resultInit(ctx, err)
} else {
subO.Result.Type = ResultType_Keys
subO.Result.Keys = make([][]byte, len(keys))
for j, key := range keys {
subO.Result.Keys[j] = key
}
}
case *DeleteRangeUsingTombstoneOperation:
subO.Result = resultInit(ctx, err)
case *AddSSTableOperation:
panic(errors.AssertionFailedf(`AddSSTable cannot be used in batches`))
default:
panic(errors.AssertionFailedf(`unknown batch operation type: %T %v`, subO, subO))
}
}
}
func resultInit(ctx context.Context, err error) Result {
if err == nil {
return Result{Type: ResultType_NoError}
}
ee := errors.EncodeError(ctx, err)
return Result{
Type: ResultType_Error,
Err: &ee,
}
}
func getRangeDesc(ctx context.Context, key roachpb.Key, dbs ...*kv.DB) roachpb.RangeDescriptor {
var dbIdx int
var opts = retry.Options{}
for r := retry.StartWithCtx(ctx, opts); r.Next(); dbIdx = (dbIdx + 1) % len(dbs) {
sender := dbs[dbIdx].NonTransactionalSender()
descs, _, err := kv.RangeLookup(ctx, sender, key, kvpb.CONSISTENT, 0, false)
if err != nil {
log.Infof(ctx, "looking up descriptor for %s: %+v", key, err)
continue
}
if len(descs) != 1 {
log.Infof(ctx, "unexpected number of descriptors for %s: %d", key, len(descs))
continue
}
return descs[0]
}
panic(`unreachable`)
}
func newGetReplicasFn(dbs ...*kv.DB) GetReplicasFn {
ctx := context.Background()
return func(key roachpb.Key) []roachpb.ReplicationTarget {
desc := getRangeDesc(ctx, key, dbs...)
replicas := desc.Replicas().Descriptors()
targets := make([]roachpb.ReplicationTarget, len(replicas))
for i, replica := range replicas {
targets[i] = roachpb.ReplicationTarget{
NodeID: replica.NodeID,
StoreID: replica.StoreID,
}
}
return targets
}
}
func updateZoneConfig(zone *zonepb.ZoneConfig, change ChangeZoneType) {
switch change {
case ChangeZoneType_ToggleGlobalReads:
cur := zone.GlobalReads != nil && *zone.GlobalReads
zone.GlobalReads = proto.Bool(!cur)
default:
panic(errors.AssertionFailedf(`unknown ChangeZoneType: %v`, change))
}
}
func updateZoneConfigInEnv(ctx context.Context, env *Env, change ChangeZoneType) error {
return env.UpdateZoneConfig(ctx, int(GeneratorDataTableID), func(zone *zonepb.ZoneConfig) {
updateZoneConfig(zone, change)
})
}