-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathlock_table_test.go
1355 lines (1260 loc) · 37.1 KB
/
lock_table_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
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 concurrency
import (
"context"
"fmt"
"runtime"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanlatch"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanset"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"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/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uint128"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/errors"
"golang.org/x/exp/rand"
"golang.org/x/sync/errgroup"
)
/*
Test needs to handle caller constraints wrt latches being held. The datadriven
test uses the following format:
new-lock-table maxlocks=<int>
----
Creates a lockTable. The lockTable is initially enabled.
new-txn txn=<name> ts=<int>[,<int>] epoch=<int> [seq=<int>]
----
Creates a Transaction.
new-child-txn parent=<name> txn=<name> ts=<int>[,<int>] epoch=<int> [seq=<int>]
----
Creates a child Transaction.
new-request r=<name> txn=<name>|none ts=<int>[,<int>] spans=r|w@<start>[,<end>]+...
----
Creates a Request.
scan r=<name>
----
<error string>|start-waiting: <bool>
Calls lockTable.ScanAndEnqueue. If the request has an existing guard, uses it.
If a guard is returned, stores it for later use.
acquire r=<name> k=<key> durability=r|u
----
<error string>
Acquires lock for the request, using the existing guard for that request.
release txn=<name> span=<start>[,<end>]
----
<error string>
Releases locks for the named transaction.
update txn=<name> ts=<int>[,<int>] epoch=<int> span=<start>[,<end>] [ignored-seqs=<int>[-<int>][,<int>[-<int>]]]
----
<error string>
Updates locks for the named transaction.
txn-finalized txn=<name> status=committed|aborted
----
Informs the lock table that the named transaction is finalized.
add-discovered r=<name> k=<key> txn=<name> [lease-seq=<seq>]
----
<error string>
Adds a discovered lock that is discovered by the named request.
dequeue r=<name>
----
<error string>
Calls lockTable.Dequeue for the named request. The request and guard are
discarded after this.
guard-state r=<name>
----
new|old: state=<state> [txn=<name> ts=<ts>]
Calls lockTableGuard.NewStateChan in a non-blocking manner, followed by
CurState.
should-wait r=<name>
----
<bool>
Calls lockTableGuard.ShouldWait.
enable [lease-seq=<seq>]
----
Calls lockTable.Enable.
clear [disable]
----
<state of lock table>
Calls lockTable.Clear. Optionally disables the lockTable.
print
----
<state of lock table>
Calls lockTable.String.
*/
func TestLockTableBasic(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
datadriven.Walk(t, "testdata/lock_table", func(t *testing.T, path string) {
var lt lockTable
var txnsByName map[string]*roachpb.Transaction
var txnCounter uint128.Uint128
var requestsByName map[string]Request
var guardsByReqName map[string]lockTableGuard
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
switch d.Cmd {
case "new-lock-table":
var maxLocks int
d.ScanArgs(t, "maxlocks", &maxLocks)
lt = &lockTableImpl{
enabled: true,
enabledSeq: 1,
maxLocks: int64(maxLocks),
}
txnsByName = make(map[string]*roachpb.Transaction)
txnCounter = uint128.FromInts(0, 0)
requestsByName = make(map[string]Request)
guardsByReqName = make(map[string]lockTableGuard)
return ""
case "new-txn":
// UUIDs for transactions are numbered from 1 by this test code and
// lockTableImpl.String() knows about UUIDs and not transaction names.
// Assigning txnNames of the form txn1, txn2, ... keeps the two in sync,
// which makes test cases easier to understand.
var txnName string
d.ScanArgs(t, "txn", &txnName)
ts := scanTimestamp(t, d)
var epoch int
d.ScanArgs(t, "epoch", &epoch)
var seq int
if d.HasArg("seq") {
d.ScanArgs(t, "seq", &seq)
}
txn, ok := txnsByName[txnName]
var id uuid.UUID
if ok {
id = txn.ID
} else {
id = nextUUID(&txnCounter)
}
txnsByName[txnName] = &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
ID: id,
Epoch: enginepb.TxnEpoch(epoch),
Sequence: enginepb.TxnSeq(seq),
WriteTimestamp: ts,
},
ReadTimestamp: ts,
}
return ""
case "new-child-txn":
var parentName string
d.ScanArgs(t, "parent", &parentName)
parent, ok := txnsByName[parentName]
if !ok {
d.Fatalf(t, "unknown parent transaction %q", parentName)
}
var txnName string
d.ScanArgs(t, "txn", &txnName)
ts := scanTimestamp(t, d)
var epoch int
d.ScanArgs(t, "epoch", &epoch)
var seq int
if d.HasArg("seq") {
d.ScanArgs(t, "seq", &seq)
}
txn, ok := txnsByName[txnName]
var id uuid.UUID
if ok {
id = txn.ID
} else {
id = nextUUID(&txnCounter)
}
txnsByName[txnName] = &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
ID: id,
Epoch: enginepb.TxnEpoch(epoch),
Sequence: enginepb.TxnSeq(seq),
WriteTimestamp: ts,
},
ReadTimestamp: ts,
Parent: parent.Clone(),
}
return ""
case "txn-finalized":
var txnName string
d.ScanArgs(t, "txn", &txnName)
txn, ok := txnsByName[txnName]
if !ok {
return fmt.Sprintf("txn %s not found", txnName)
}
var statusStr string
d.ScanArgs(t, "status", &statusStr)
switch statusStr {
case "committed":
txn.Status = roachpb.COMMITTED
case "aborted":
txn.Status = roachpb.ABORTED
default:
return fmt.Sprintf("unknown txn status %s", statusStr)
}
lt.TransactionIsFinalized(txn)
return ""
case "new-request":
// Seqnums for requests are numbered from 1 by lockTableImpl and
// lockTableImpl.String() does not know about request names. Assigning
// request names of the form req1, req2, ... keeps the two in sync,
// which makes test cases easier to understand.
var reqName string
d.ScanArgs(t, "r", &reqName)
if _, ok := requestsByName[reqName]; ok {
d.Fatalf(t, "duplicate request: %s", reqName)
}
var txnName string
d.ScanArgs(t, "txn", &txnName)
txn, ok := txnsByName[txnName]
if !ok && txnName != "none" {
d.Fatalf(t, "unknown txn %s", txnName)
}
ts := scanTimestamp(t, d)
spans := scanSpans(t, d, ts)
req := Request{
Timestamp: ts,
LatchSpans: spans,
LockSpans: spans,
}
if txn != nil {
// Update the transaction's timestamp, if necessary. The transaction
// may have needed to move its timestamp for any number of reasons.
txn.WriteTimestamp = ts
req.Txn = txn.Clone()
req.Txn.ReadTimestamp = ts
}
requestsByName[reqName] = req
return ""
case "scan":
var reqName string
d.ScanArgs(t, "r", &reqName)
req, ok := requestsByName[reqName]
if !ok {
d.Fatalf(t, "unknown request: %s", reqName)
}
g := guardsByReqName[reqName]
g, err := lt.ScanAndEnqueue(req, g)
if err != nil {
return fmt.Sprintf("err: %v", err)
}
guardsByReqName[reqName] = g
return fmt.Sprintf("start-waiting: %t", g.ShouldWait())
case "acquire":
var reqName string
d.ScanArgs(t, "r", &reqName)
req, ok := requestsByName[reqName]
if !ok {
d.Fatalf(t, "unknown request: %s", reqName)
}
var key string
d.ScanArgs(t, "k", &key)
var s string
d.ScanArgs(t, "durability", &s)
if len(s) != 1 || (s[0] != 'r' && s[0] != 'u') {
d.Fatalf(t, "incorrect durability: %s", s)
}
durability := lock.Unreplicated
if s[0] == 'r' {
durability = lock.Replicated
}
if err := lt.AcquireLock(&req.Txn.TxnMeta, roachpb.Key(key), lock.Exclusive, durability); err != nil {
return err.Error()
}
return lt.(*lockTableImpl).String()
case "release":
var txnName string
d.ScanArgs(t, "txn", &txnName)
txn, ok := txnsByName[txnName]
if !ok {
d.Fatalf(t, "unknown txn %s", txnName)
}
var s string
d.ScanArgs(t, "span", &s)
span := getSpan(t, d, s)
// TODO(sbhola): also test ABORTED.
intent := &roachpb.LockUpdate{Span: span, Txn: txn.TxnMeta, Status: roachpb.COMMITTED}
if err := lt.UpdateLocks(intent); err != nil {
return err.Error()
}
return lt.(*lockTableImpl).String()
case "update":
var txnName string
d.ScanArgs(t, "txn", &txnName)
txn, ok := txnsByName[txnName]
if !ok {
d.Fatalf(t, "unknown txn %s", txnName)
}
ts := scanTimestamp(t, d)
var epoch int
d.ScanArgs(t, "epoch", &epoch)
txn = &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
ID: txn.ID,
Sequence: txn.Sequence,
WriteTimestamp: ts,
Epoch: enginepb.TxnEpoch(epoch),
},
ReadTimestamp: ts,
}
txnsByName[txnName] = txn
var s string
d.ScanArgs(t, "span", &s)
span := getSpan(t, d, s)
var ignored []enginepb.IgnoredSeqNumRange
if d.HasArg("ignored-seqs") {
var seqsStr string
d.ScanArgs(t, "ignored-seqs", &seqsStr)
parts := strings.Split(seqsStr, ",")
for _, p := range parts {
pair := strings.Split(p, "-")
if len(pair) != 1 && len(pair) != 2 {
d.Fatalf(t, "error parsing %s", parts)
}
startNum, err := strconv.ParseInt(pair[0], 10, 32)
if err != nil {
d.Fatalf(t, "error parsing ignored seqnums: %s", err)
}
ignoredRange := enginepb.IgnoredSeqNumRange{
Start: enginepb.TxnSeq(startNum), End: enginepb.TxnSeq(startNum)}
if len(pair) == 2 {
endNum, err := strconv.ParseInt(pair[1], 10, 32)
if err != nil {
d.Fatalf(t, "error parsing ignored seqnums: %s", err)
}
ignoredRange.End = enginepb.TxnSeq(endNum)
}
ignored = append(ignored, ignoredRange)
}
}
// TODO(sbhola): also test STAGING.
intent := &roachpb.LockUpdate{
Span: span, Txn: txn.TxnMeta, Status: roachpb.PENDING, IgnoredSeqNums: ignored}
if err := lt.UpdateLocks(intent); err != nil {
return err.Error()
}
return lt.(*lockTableImpl).String()
case "add-discovered":
var reqName string
d.ScanArgs(t, "r", &reqName)
g := guardsByReqName[reqName]
if g == nil {
d.Fatalf(t, "unknown guard: %s", reqName)
}
var key string
d.ScanArgs(t, "k", &key)
var txnName string
d.ScanArgs(t, "txn", &txnName)
txn, ok := txnsByName[txnName]
if !ok {
d.Fatalf(t, "unknown txn %s", txnName)
}
intent := roachpb.MakeIntent(&txn.TxnMeta, roachpb.Key(key))
seq := int(1)
if d.HasArg("lease-seq") {
d.ScanArgs(t, "lease-seq", &seq)
}
leaseSeq := roachpb.LeaseSequence(seq)
if _, err := lt.AddDiscoveredLock(&intent, leaseSeq, g); err != nil {
return err.Error()
}
return lt.(*lockTableImpl).String()
case "dequeue":
var reqName string
d.ScanArgs(t, "r", &reqName)
g := guardsByReqName[reqName]
if g == nil {
d.Fatalf(t, "unknown guard: %s", reqName)
}
lt.Dequeue(g)
delete(guardsByReqName, reqName)
delete(requestsByName, reqName)
return lt.(*lockTableImpl).String()
case "should-wait":
var reqName string
d.ScanArgs(t, "r", &reqName)
g := guardsByReqName[reqName]
if g == nil {
d.Fatalf(t, "unknown guard: %s", reqName)
}
return fmt.Sprintf("%t", g.ShouldWait())
case "guard-state":
var reqName string
d.ScanArgs(t, "r", &reqName)
g := guardsByReqName[reqName]
if g == nil {
d.Fatalf(t, "unknown guard: %s", reqName)
}
var str string
stateTransition := false
select {
case <-g.NewStateChan():
str = "new: "
stateTransition = true
default:
str = "old: "
}
state, err := g.CurState()
if err != nil {
return str + "err=" + err.String()
}
var typeStr string
switch state.kind {
case waitForDistinguished:
typeStr = "waitForDistinguished"
case waitFor:
typeStr = "waitFor"
case waitElsewhere:
typeStr = "waitElsewhere"
case waitSelf:
return str + "state=waitSelf"
case doneWaiting:
var toResolveStr string
if stateTransition {
if toResolve := g.ResolveBeforeScanning(); len(toResolve) > 0 {
var buf strings.Builder
fmt.Fprintf(&buf, "\nIntents to resolve:")
for i := range toResolve {
fmt.Fprintf(&buf, "\n key=%s txn=%s status=%s", toResolve[i].Key,
toResolve[i].Txn.ID.Short(), toResolve[i].Status)
}
toResolveStr = buf.String()
}
}
return str + "state=doneWaiting" + toResolveStr
}
id := state.txn.ID
var txnS string
for k, v := range txnsByName {
if v.ID.Equal(id) {
txnS = k
break
}
}
if txnS == "" {
txnS = fmt.Sprintf("unknown txn with ID: %v", state.txn.ID)
}
return fmt.Sprintf("%sstate=%s txn=%s key=%s held=%t guard-access=%s",
str, typeStr, txnS, state.key, state.held, state.guardAccess)
case "enable":
seq := int(1)
if d.HasArg("lease-seq") {
d.ScanArgs(t, "lease-seq", &seq)
}
lt.Enable(roachpb.LeaseSequence(seq))
return ""
case "clear":
lt.Clear(d.HasArg("disable"))
return lt.(*lockTableImpl).String()
case "print":
return lt.(*lockTableImpl).String()
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
})
}
func nextUUID(counter *uint128.Uint128) uuid.UUID {
*counter = counter.Add(1)
return uuid.FromUint128(*counter)
}
func scanTimestamp(t *testing.T, d *datadriven.TestData) hlc.Timestamp {
var tsS string
d.ScanArgs(t, "ts", &tsS)
ts, err := hlc.ParseTimestamp(tsS)
if err != nil {
d.Fatalf(t, "%v", err)
}
return ts
}
func getSpan(t *testing.T, d *datadriven.TestData, str string) roachpb.Span {
parts := strings.Split(str, ",")
span := roachpb.Span{Key: roachpb.Key(parts[0])}
if len(parts) > 2 {
d.Fatalf(t, "incorrect span format: %s", str)
} else if len(parts) == 2 {
span.EndKey = roachpb.Key(parts[1])
}
return span
}
func scanSpans(t *testing.T, d *datadriven.TestData, ts hlc.Timestamp) *spanset.SpanSet {
spans := &spanset.SpanSet{}
var spansStr string
d.ScanArgs(t, "spans", &spansStr)
parts := strings.Split(spansStr, "+")
for _, p := range parts {
if len(p) < 2 || p[1] != '@' {
d.Fatalf(t, "incorrect span with access format: %s", p)
}
c := p[0]
p = p[2:]
var sa spanset.SpanAccess
switch c {
case 'r':
sa = spanset.SpanReadOnly
case 'w':
sa = spanset.SpanReadWrite
default:
d.Fatalf(t, "incorrect span access: %c", c)
}
spans.AddMVCC(sa, getSpan(t, d, p), ts)
}
return spans
}
type workItem struct {
// Contains one of request or intents.
// Request.
request *Request
locksToAcquire []roachpb.Key
// Update locks.
intents []roachpb.LockUpdate
}
func (w *workItem) getRequestTxnID() uuid.UUID {
if w.request != nil && w.request.Txn != nil {
return w.request.Txn.ID
}
return uuid.UUID{}
}
func doWork(ctx context.Context, item *workItem, e *workloadExecutor) error {
defer func() {
e.doneWork <- item
}()
if item.request != nil {
var lg *spanlatch.Guard
var g lockTableGuard
var err error
for {
// Since we can't do a select involving latch acquisition and context
// cancellation, the code makes sure to release latches when returning
// early due to error. Otherwise other requests will get stuck and
// group.Wait() will not return until the test times out.
lg, err = e.lm.Acquire(context.Background(), item.request.LatchSpans)
if err != nil {
return err
}
g, _ = e.lt.ScanAndEnqueue(*item.request, g)
if !g.ShouldWait() {
break
}
e.lm.Release(lg)
var lastID uuid.UUID
L:
for {
select {
case <-g.NewStateChan():
case <-ctx.Done():
return ctx.Err()
}
state, pErr := g.CurState()
if pErr != nil {
err = pErr.GoError()
e.lt.Dequeue(g)
return err
}
switch state.kind {
case doneWaiting:
if !lastID.Equal(uuid.UUID{}) && item.request.Txn != nil {
_, err = e.waitingFor(item.request.Txn.ID, lastID, uuid.UUID{})
if err != nil {
e.lt.Dequeue(g)
return err
}
}
break L
case waitSelf:
if item.request.Txn == nil {
e.lt.Dequeue(g)
return errors.Errorf("non-transactional request cannot waitSelf")
}
case waitForDistinguished, waitFor, waitElsewhere:
if item.request.Txn != nil {
var aborted bool
aborted, err = e.waitingFor(item.request.Txn.ID, lastID, state.txn.ID)
if !aborted {
lastID = state.txn.ID
}
if aborted {
e.lt.Dequeue(g)
return err
}
}
default:
return errors.Errorf("unexpected state: %v", state.kind)
}
}
}
// acquire locks.
for _, k := range item.locksToAcquire {
err = e.acquireLock(&item.request.Txn.TxnMeta, k)
if err != nil {
break
}
}
e.lt.Dequeue(g)
e.lm.Release(lg)
return err
}
for i := range item.intents {
if err := e.lt.UpdateLocks(&item.intents[i]); err != nil {
return err
}
}
return nil
}
// Contains either a request or the ID of the transactions whose locks should
// be released.
type workloadItem struct {
// Request to be executed, iff request != nil
request *Request
// locks to be acquired by the request.
locksToAcquire []roachpb.Key
// Non-empty when transaction should release locks.
finish uuid.UUID
}
// state of a transaction maintained by workloadExecutor, for deadlock
// detection, and deciding when transaction can be finished (when a
// workloadItem has instructed that it be finished and all its ongoing
// requests have finished). Requests can be aborted due to deadlock. For
// workload execution convenience this does not abort the whole transaction,
// but it does mean that the locks acquired by the transaction can be a subset
// of what it was instructed to acquire. The locks acquired are tracked in
// acquiredLocks.
type transactionState struct {
txn *enginepb.TxnMeta
// A map of each transaction it depends on and a refcount (the refcount is >
// 0).
dependsOn map[uuid.UUID]int
ongoingRequests map[*workItem]struct{}
acquiredLocks []roachpb.Key
finish bool
}
func makeWorkItemFinishTxn(tstate *transactionState) workItem {
wItem := workItem{}
for i := range tstate.acquiredLocks {
wItem.intents = append(wItem.intents, roachpb.LockUpdate{
Span: roachpb.Span{Key: tstate.acquiredLocks[i]},
Txn: *tstate.txn,
Status: roachpb.COMMITTED,
})
}
return wItem
}
func makeWorkItemForRequest(wi workloadItem) workItem {
wItem := workItem{
request: wi.request,
locksToAcquire: wi.locksToAcquire,
}
return wItem
}
type workloadExecutor struct {
lm spanlatch.Manager
lt lockTable
// Protects the following fields in transactionState: acquiredLocks and
// dependsOn, and the transactions map.
mu syncutil.Mutex
items []workloadItem
transactions map[uuid.UUID]*transactionState
doneWork chan *workItem
concurrency int
numAborted int
numConcViolations int
}
func newWorkLoadExecutor(items []workloadItem, concurrency int) *workloadExecutor {
return &workloadExecutor{
lm: spanlatch.Manager{},
lt: &lockTableImpl{
enabled: true,
maxLocks: 100000,
},
items: items,
transactions: make(map[uuid.UUID]*transactionState),
doneWork: make(chan *workItem),
concurrency: concurrency,
}
}
func (e *workloadExecutor) acquireLock(txnMeta *enginepb.TxnMeta, k roachpb.Key) error {
err := e.lt.AcquireLock(txnMeta, k, lock.Exclusive, lock.Unreplicated)
if err != nil {
return err
}
e.mu.Lock()
defer e.mu.Unlock()
tstate, ok := e.transactions[txnMeta.ID]
if !ok {
return errors.Errorf("testbug: lock acquiring request with txnID %v has no transaction", txnMeta.ID)
}
tstate.acquiredLocks = append(tstate.acquiredLocks, k)
return nil
}
// Returns true if cycle was found. err != nil => true
func (e *workloadExecutor) findCycle(node uuid.UUID, cycleNode uuid.UUID) (bool, error) {
if node == cycleNode {
return true, nil
}
tstate, ok := e.transactions[node]
if !ok {
return true, errors.Errorf("edge to txn that is not in map")
}
for k := range tstate.dependsOn {
found, err := e.findCycle(k, cycleNode)
if err != nil || found {
return true, err
}
}
return false, nil
}
// Returns true if request should abort. err != nil => true
func (e *workloadExecutor) waitingFor(
waiter uuid.UUID, lastID uuid.UUID, currID uuid.UUID,
) (bool, error) {
e.mu.Lock()
defer e.mu.Unlock()
tstate, ok := e.transactions[waiter]
if !ok {
return true, errors.Errorf("testbug: request calling waitingFor with txnID %v has no transaction", waiter)
}
if !lastID.Equal(uuid.UUID{}) {
refcount := tstate.dependsOn[lastID]
refcount--
if refcount > 0 {
tstate.dependsOn[lastID] = refcount
} else if refcount == 0 {
delete(tstate.dependsOn, lastID)
} else {
return true, errors.Errorf("testbug: txn %v has a negative refcount %d for dependency on %v", waiter, refcount, lastID)
}
}
if !currID.Equal(uuid.UUID{}) {
refcount := tstate.dependsOn[currID]
// Cycle detection to detect if this new edge has introduced any
// cycle. We know there cannot be a cycle preceding this edge,
// so any cycle must involve waiter. We do a trivial recursive
// DFS (does not avoid exploring the same node multiple times).
if refcount == 0 {
found, err := e.findCycle(currID, waiter)
if found {
return found, err
}
}
refcount++
tstate.dependsOn[currID] = refcount
}
return false, nil
}
// Returns true if it started a goroutine.
func (e *workloadExecutor) tryFinishTxn(
ctx context.Context, group *errgroup.Group, txnID uuid.UUID, tstate *transactionState,
) bool {
if !tstate.finish || len(tstate.ongoingRequests) > 0 {
return false
}
if len(tstate.acquiredLocks) > 0 {
work := makeWorkItemFinishTxn(tstate)
group.Go(func() error { return doWork(ctx, &work, e) })
return true
}
return false
}
// Execution can be either in strict or non-strict mode. In strict mode
// the executor expects to be able to limit concurrency to the configured
// value L by waiting for the L ongoing requests to finish. It sets a
// deadline for this, and returns with an error if the deadline expires
// (this is just a slightly cleaner error than the test exceeding the
// deadline since it prints out the lock table contents).
//
// When executing in non-strict mode, the concurrency bound is not necessarily
// respected since requests may be waiting for locks to be released. The
// executor waits for a tiny interval when concurrency is >= L and if
// no request completes it starts another request. Just for our curiosity
// these "concurrency violations" are tracked in a counter. The amount of
// concurrency in this non-strict mode is bounded by maxNonStrictConcurrency.
func (e *workloadExecutor) execute(strict bool, maxNonStrictConcurrency int) error {
numOutstanding := 0
i := 0
group, ctx := errgroup.WithContext(context.Background())
timer := time.NewTimer(time.Second)
timer.Stop()
var err error
L:
for i < len(e.items) || numOutstanding > 0 {
if numOutstanding >= e.concurrency || (i == len(e.items) && numOutstanding > 0) {
strictIter := strict || i == len(e.items)
if strictIter {
timer.Reset(30 * time.Second)
} else {
timer.Reset(time.Millisecond)
}
var w *workItem
select {
case w = <-e.doneWork:
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
if strictIter {
err = errors.Errorf("timer expired with lock table: %v", e.lt)
break L
} else if numOutstanding > maxNonStrictConcurrency {
continue
} else {
e.numConcViolations++
}
}
if w != nil {
numOutstanding--
txnID := w.getRequestTxnID()
if !txnID.Equal(uuid.UUID{}) {
tstate, ok := e.transactions[txnID]
if !ok {
err = errors.Errorf("testbug: finished request with txnID %v has no transaction", txnID)
break
}
delete(tstate.ongoingRequests, w)
if e.tryFinishTxn(ctx, group, txnID, tstate) {
numOutstanding++
continue
}
}
}
}
if i == len(e.items) {
continue
}
wi := e.items[i]
i++
if wi.request != nil {
work := makeWorkItemForRequest(wi)
if wi.request.Txn != nil {
txnID := wi.request.Txn.ID
_, ok := e.transactions[txnID]
if !ok {
// New transaction
tstate := &transactionState{
txn: &wi.request.Txn.TxnMeta,
dependsOn: make(map[uuid.UUID]int),
ongoingRequests: make(map[*workItem]struct{}),
}
e.mu.Lock()
e.transactions[txnID] = tstate
e.mu.Unlock()
}
e.transactions[txnID].ongoingRequests[&work] = struct{}{}
}
group.Go(func() error { return doWork(ctx, &work, e) })
numOutstanding++
continue
}
tstate, ok := e.transactions[wi.finish]
if !ok {
err = errors.Errorf("testbug: txn to finish not found: %v", wi.finish)
break
}
tstate.finish = true
if e.tryFinishTxn(ctx, group, wi.finish, tstate) {
numOutstanding++
}
}
err2 := group.Wait()
if err2 != nil {
err = err2
}
fmt.Printf("items: %d, aborted: %d, concurrency violations: %d, lock table: %v\n",
len(e.items), e.numAborted, e.numConcViolations, e.lt)
return err
}
// Randomized test with each transaction having a single request that does not
// acquire locks. Note that this ensures there will be no deadlocks. And the
// test executor can run in strict concurrency mode (see comment in execute()).
func TestLockTableConcurrentSingleRequests(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
txnCounter := uint128.FromInts(0, 0)
var timestamps []hlc.Timestamp
for i := 0; i < 10; i++ {
timestamps = append(timestamps, hlc.Timestamp{WallTime: int64(i + 1)})
}
var keys []roachpb.Key
for i := 0; i < 10; i++ {
keys = append(keys, roachpb.Key(string(rune('a'+i))))
}
rng := rand.New(rand.NewSource(uint64(timeutil.Now().UnixNano())))
const numKeys = 2
var items []workloadItem
var startedTxnIDs []uuid.UUID // inefficient queue, but ok for a test.
const maxStartedTxns = 10
const numRequests = 10000
for i := 0; i < numRequests; i++ {
ts := timestamps[rng.Intn(len(timestamps))]
keysPerm := rng.Perm(len(keys))
spans := &spanset.SpanSet{}
for i := 0; i < numKeys; i++ {
span := roachpb.Span{Key: keys[keysPerm[i]]}
acc := spanset.SpanAccess(rng.Intn(int(spanset.NumSpanAccess)))
spans.AddMVCC(acc, span, ts)
}
var txn *roachpb.Transaction
if rng.Intn(2) == 0 {
txn = &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
ID: nextUUID(&txnCounter),
WriteTimestamp: ts,
},
ReadTimestamp: ts,
}
}
request := &Request{