-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
manager_test.go
720 lines (639 loc) · 20.8 KB
/
manager_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
// 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 spanlatch
import (
"bytes"
"context"
"fmt"
"math/rand"
"strings"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/poison"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanset"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
var read = false
var write = true
var zeroTS = hlc.Timestamp{}
func spans(from, to string, write bool, ts hlc.Timestamp) *spanset.SpanSet {
var spanSet spanset.SpanSet
add(&spanSet, from, to, write, ts)
return &spanSet
}
func add(spanSet *spanset.SpanSet, from, to string, write bool, ts hlc.Timestamp) {
var start, end roachpb.Key
if to == "" {
start = roachpb.Key(from)
} else {
start = roachpb.Key(from)
end = roachpb.Key(to)
}
if strings.HasPrefix(from, "local") {
start = append(keys.LocalRangePrefix, start...)
if end != nil {
end = append(keys.LocalRangePrefix, end...)
}
}
access := spanset.SpanReadOnly
if write {
access = spanset.SpanReadWrite
}
if strings.HasPrefix(from, "local") {
spanSet.AddNonMVCC(access, roachpb.Span{Key: start, EndKey: end})
} else {
spanSet.AddMVCC(access, roachpb.Span{Key: start, EndKey: end}, ts)
}
}
func testLatchSucceeds(t *testing.T, lgC <-chan *Guard) *Guard {
t.Helper()
select {
case lg := <-lgC:
return lg
case <-time.After(testutils.DefaultSucceedsSoonDuration):
// False positives are not ok, so we use a more
// conservative timeout than in testLatchBlocks.
t.Fatal("latch acquisition should succeed")
}
return nil
}
func testLatchBlocks(t *testing.T, lgC <-chan *Guard) {
t.Helper()
select {
case <-lgC:
t.Fatal("latch acquisition should block")
case <-time.After(3 * time.Millisecond):
// False positives are ok as long as they are rare, so we
// use an aggressive timeout to avoid slowing down tests.
}
}
// MustAcquire is like Acquire, except it can't return context cancellation
// errors.
func (m *Manager) MustAcquire(spans *spanset.SpanSet) *Guard {
lg, err := m.Acquire(context.Background(), spans, poison.Policy_Error)
if err != nil {
panic(err)
}
return lg
}
// MustAcquireCh is like Acquire, except it only sequences the latch attempt
// synchronously and waits on dependent latches asynchronously. It returns a
// a channel that is signaled with the *Guard returned after waiting (and nil
// on error). Use MustAcquireChExt if more control is desired.
func (m *Manager) MustAcquireCh(spans *spanset.SpanSet) <-chan *Guard {
chG, _ := m.MustAcquireChExt(context.Background(), spans, poison.Policy_Error)
return chG
}
// MustAcquireChExt is like MustAcquireCh, except it accepts a context and
// poison.Policy, and returns an additional channel that returns an error
// if a nil guard is returned on the first channel.
func (m *Manager) MustAcquireChExt(
ctx context.Context, spans *spanset.SpanSet, pp poison.Policy,
) (<-chan *Guard, <-chan error) {
chErr := make(chan error, 1)
chG := make(chan *Guard, 1)
lg, snap := m.sequence(spans, pp)
go func() {
err := m.wait(ctx, lg, snap)
if err != nil {
m.Release(lg)
}
chErr <- err
chG <- lg
}()
return chG, chErr
}
func TestLatchManager(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Try latches with no overlapping already-acquired latches.
lg1 := m.MustAcquire(spans("a", "", write, zeroTS))
m.Release(lg1)
lg2 := m.MustAcquire(spans("a", "b", write, zeroTS))
m.Release(lg2)
// Add a latch and verify overlapping latches wait on it.
lg3 := m.MustAcquire(spans("a", "b", write, zeroTS))
lg4C := m.MustAcquireCh(spans("a", "b", write, zeroTS))
// Second write should block.
testLatchBlocks(t, lg4C)
// First write completes, second grabs latch.
m.Release(lg3)
testLatchSucceeds(t, lg4C)
}
func TestLatchManagerAcquireOverlappingSpans(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Acquire overlapping latches with different access patterns.
// |----------| <- Read latch [a-c)@t1
// |----------| <- Write latch [b-d)@t1
//
// ^ ^ ^ ^
// | | | |
// a b c d
//
var ts0, ts1 = hlc.Timestamp{WallTime: 0}, hlc.Timestamp{WallTime: 1}
var spanSet spanset.SpanSet
add(&spanSet, "a", "c", read, ts1)
add(&spanSet, "b", "d", write, ts1)
lg1 := m.MustAcquire(&spanSet)
lg2C := m.MustAcquireCh(spans("a", "b", read, ts0))
lg2 := testLatchSucceeds(t, lg2C)
m.Release(lg2)
// We acquire reads at lower timestamps than writes to check for blocked
// acquisitions based on the original latch, not the latches declared in
// earlier test cases.
var latchCs []<-chan *Guard
latchCs = append(latchCs, m.MustAcquireCh(spans("a", "b", write, ts1)))
latchCs = append(latchCs, m.MustAcquireCh(spans("b", "c", read, ts0)))
latchCs = append(latchCs, m.MustAcquireCh(spans("b", "c", write, ts1)))
latchCs = append(latchCs, m.MustAcquireCh(spans("c", "d", write, ts1)))
latchCs = append(latchCs, m.MustAcquireCh(spans("c", "d", read, ts0)))
for _, lgC := range latchCs {
testLatchBlocks(t, lgC)
}
m.Release(lg1)
for _, lgC := range latchCs {
lg := testLatchSucceeds(t, lgC)
m.Release(lg)
}
}
func TestLatchManagerAcquiringReadsVaryingTimestamps(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
var ts0, ts1 = hlc.Timestamp{WallTime: 0}, hlc.Timestamp{WallTime: 1}
var spanSet spanset.SpanSet
add(&spanSet, "a", "", read, ts0)
add(&spanSet, "a", "", read, ts1)
lg1 := m.MustAcquire(&spanSet)
for _, walltime := range []int64{0, 1, 2} {
ts := hlc.Timestamp{WallTime: walltime}
lg := testLatchSucceeds(t, m.MustAcquireCh(spans("a", "", read, ts)))
m.Release(lg)
}
var latchCs []<-chan *Guard
for _, walltime := range []int64{0, 1, 2} {
ts := hlc.Timestamp{WallTime: walltime}
latchCs = append(latchCs, m.MustAcquireCh(spans("a", "", write, ts)))
}
for _, lgC := range latchCs {
testLatchBlocks(t, lgC)
}
m.Release(lg1)
for _, lgC := range latchCs {
lg := testLatchSucceeds(t, lgC)
m.Release(lg)
}
}
func TestLatchManagerNoWaitOnReadOnly(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Acquire latch for read-only span.
m.MustAcquire(spans("a", "", read, zeroTS))
// Verify no wait with another read-only span.
m.MustAcquire(spans("a", "", read, zeroTS))
}
func TestLatchManagerWriteWaitForMultipleReads(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Acquire latch for read-only span.
lg1 := m.MustAcquire(spans("a", "", read, zeroTS))
// Acquire another one on top.
lg2 := m.MustAcquire(spans("a", "", read, zeroTS))
// A write span should have to wait for **both** reads.
lg3C := m.MustAcquireCh(spans("a", "", write, zeroTS))
// Certainly blocks now.
testLatchBlocks(t, lg3C)
// The second read releases latch, but the first one remains.
m.Release(lg2)
// Should still block.
testLatchBlocks(t, lg3C)
// First read releases latch.
m.Release(lg1)
// Now it goes through.
testLatchSucceeds(t, lg3C)
}
func TestLatchManagerMultipleOverlappingLatches(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Acquire multiple latches.
lg1C := m.MustAcquireCh(spans("a", "", write, zeroTS))
lg2C := m.MustAcquireCh(spans("b", "c", write, zeroTS))
lg3C := m.MustAcquireCh(spans("a", "d", write, zeroTS))
// Attempt to acquire latch which overlaps them all.
lg4C := m.MustAcquireCh(spans("0", "z", write, zeroTS))
testLatchBlocks(t, lg4C)
m.Release(<-lg1C)
testLatchBlocks(t, lg4C)
m.Release(<-lg2C)
testLatchBlocks(t, lg4C)
m.Release(<-lg3C)
testLatchSucceeds(t, lg4C)
}
func TestLatchManagerMultipleOverlappingSpans(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Acquire multiple latches.
lg1 := m.MustAcquire(spans("a", "", write, zeroTS))
lg2 := m.MustAcquire(spans("b", "c", read, zeroTS))
lg3 := m.MustAcquire(spans("d", "f", write, zeroTS))
lg4 := m.MustAcquire(spans("g", "", write, zeroTS))
// Attempt to acquire latches overlapping each of them.
var spans spanset.SpanSet
spans.AddNonMVCC(spanset.SpanReadWrite, roachpb.Span{Key: roachpb.Key("a")})
spans.AddNonMVCC(spanset.SpanReadWrite, roachpb.Span{Key: roachpb.Key("b")})
spans.AddNonMVCC(spanset.SpanReadWrite, roachpb.Span{Key: roachpb.Key("e")})
lg5C := m.MustAcquireCh(&spans)
// Blocks until the first three prerequisite latches release.
testLatchBlocks(t, lg5C)
m.Release(lg2)
testLatchBlocks(t, lg5C)
m.Release(lg3)
testLatchBlocks(t, lg5C)
m.Release(lg1)
lg5 := testLatchSucceeds(t, lg5C)
m.Release(lg4)
m.Release(lg5)
}
func TestLatchManagerDependentLatches(t *testing.T) {
defer leaktest.AfterTest(t)()
cases := []struct {
name string
sp1 *spanset.SpanSet
sp2 *spanset.SpanSet
dependent bool
}{
{
name: "point writes, same key",
sp1: spans("a", "", write, zeroTS),
sp2: spans("a", "", write, zeroTS),
dependent: true,
},
{
name: "point writes, different key",
sp1: spans("a", "", write, zeroTS),
sp2: spans("b", "", write, zeroTS),
dependent: false,
},
{
name: "range writes, overlapping span",
sp1: spans("a", "c", write, zeroTS),
sp2: spans("b", "d", write, zeroTS),
dependent: true,
},
{
name: "range writes, non-overlapping span",
sp1: spans("a", "b", write, zeroTS),
sp2: spans("b", "c", write, zeroTS),
dependent: false,
},
{
name: "point reads, same key",
sp1: spans("a", "", read, zeroTS),
sp2: spans("a", "", read, zeroTS),
dependent: false,
},
{
name: "point reads, different key",
sp1: spans("a", "", read, zeroTS),
sp2: spans("b", "", read, zeroTS),
dependent: false,
},
{
name: "range reads, overlapping span",
sp1: spans("a", "c", read, zeroTS),
sp2: spans("b", "d", read, zeroTS),
dependent: false,
},
{
name: "range reads, non-overlapping span",
sp1: spans("a", "b", read, zeroTS),
sp2: spans("b", "c", read, zeroTS),
dependent: false,
},
{
name: "read and write, same ts",
sp1: spans("a", "", write, hlc.Timestamp{WallTime: 1}),
sp2: spans("a", "", read, hlc.Timestamp{WallTime: 1}),
dependent: true,
},
{
name: "read and write, causal ts",
sp1: spans("a", "", write, hlc.Timestamp{WallTime: 1}),
sp2: spans("a", "", read, hlc.Timestamp{WallTime: 2}),
dependent: true,
},
{
name: "read and write, non-causal ts",
sp1: spans("a", "", write, hlc.Timestamp{WallTime: 2}),
sp2: spans("a", "", read, hlc.Timestamp{WallTime: 1}),
dependent: false,
},
{
name: "read and write, zero ts read",
sp1: spans("a", "", write, hlc.Timestamp{WallTime: 1}),
sp2: spans("a", "", read, hlc.Timestamp{WallTime: 0}),
dependent: true,
},
{
name: "point reads, different ts",
sp1: spans("a", "", read, hlc.Timestamp{WallTime: 1}),
sp2: spans("a", "", read, hlc.Timestamp{WallTime: 0}),
dependent: false,
},
{
name: "read and write, zero ts write",
sp1: spans("a", "", write, hlc.Timestamp{WallTime: 0}),
sp2: spans("a", "", read, hlc.Timestamp{WallTime: 1}),
dependent: true,
},
{
name: "read and write, non-overlapping",
sp1: spans("a", "b", write, zeroTS),
sp2: spans("b", "", read, zeroTS),
dependent: false,
},
{
name: "local range writes, overlapping span",
sp1: spans("local a", "local c", write, zeroTS),
sp2: spans("local b", "local d", write, zeroTS),
dependent: true,
},
{
name: "local range writes, non-overlapping span",
sp1: spans("local a", "local b", write, zeroTS),
sp2: spans("local b", "local c", write, zeroTS),
dependent: false,
},
{
name: "local range reads, overlapping span",
sp1: spans("local a", "local c", read, zeroTS),
sp2: spans("local b", "local d", read, zeroTS),
dependent: false,
},
{
name: "local range reads, non-overlapping span",
sp1: spans("local a", "local b", read, zeroTS),
sp2: spans("local b", "local c", read, zeroTS),
dependent: false,
},
{
name: "local read and write, same ts",
sp1: spans("local a", "", write, hlc.Timestamp{WallTime: 1}),
sp2: spans("local a", "", read, hlc.Timestamp{WallTime: 1}),
dependent: true,
},
{
name: "local read and write, causal ts",
sp1: spans("local a", "", write, hlc.Timestamp{WallTime: 1}),
sp2: spans("local a", "", read, hlc.Timestamp{WallTime: 2}),
dependent: true,
},
{
name: "local read and write, non-causal ts",
sp1: spans("local a", "", write, hlc.Timestamp{WallTime: 2}),
sp2: spans("local a", "", read, hlc.Timestamp{WallTime: 1}),
dependent: true,
},
{
name: "local read and write, zero ts read",
sp1: spans("local a", "", write, hlc.Timestamp{WallTime: 1}),
sp2: spans("local a", "", read, hlc.Timestamp{WallTime: 0}),
dependent: true,
},
{
name: "local read and write, zero ts write",
sp1: spans("local a", "", write, hlc.Timestamp{WallTime: 0}),
sp2: spans("local a", "", read, hlc.Timestamp{WallTime: 1}),
dependent: true,
},
{
name: "local read and write, non-overlapping",
sp1: spans("a", "b", write, zeroTS),
sp2: spans("b", "", read, zeroTS),
dependent: false,
},
{
name: "local read and global write, overlapping",
sp1: spans("a", "b", write, zeroTS),
sp2: spans("local b", "", read, zeroTS),
dependent: false,
},
{
name: "local write and global read, overlapping",
sp1: spans("local a", "local b", write, zeroTS),
sp2: spans("b", "", read, zeroTS),
dependent: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
testutils.RunTrueAndFalse(t, "inv", func(t *testing.T, inv bool) {
c := c
if inv {
c.sp1, c.sp2 = c.sp2, c.sp1
}
var m Manager
lg1 := m.MustAcquire(c.sp1)
lg2C := m.MustAcquireCh(c.sp2)
if c.dependent {
testLatchBlocks(t, lg2C)
m.Release(lg1)
lg2 := testLatchSucceeds(t, lg2C)
m.Release(lg2)
} else {
lg2 := testLatchSucceeds(t, lg2C)
m.Release(lg1)
m.Release(lg2)
}
})
})
}
}
func TestLatchManagerPoison(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
var m Manager
cha1, _ := m.MustAcquireChExt(ctx, spans("a", "", write, zeroTS), poison.Policy_Wait)
cha2, erra2 := m.MustAcquireChExt(ctx, spans("a", "", write, zeroTS), poison.Policy_Error)
cha3, erra3 := m.MustAcquireChExt(ctx, spans("a", "", write, zeroTS), poison.Policy_Error)
ga1 := testLatchSucceeds(t, cha1)
defer m.Release(ga1)
// cha3 blocks on cha2 blocks on cha1.
testLatchBlocks(t, cha3)
testLatchBlocks(t, cha2)
// Poison a1.
ga1.poison.signal()
testLatchPoisons := func(t *testing.T, ch <-chan error) {
t.Helper()
select {
case err := <-ch:
require.True(t, errors.HasType(err, (*poison.PoisonedError)(nil)), "%+v", err)
case <-time.After(testutils.DefaultSucceedsSoonDuration):
t.Fatal("timed out")
}
}
testLatchPoisons(t, erra2)
testLatchPoisons(t, erra3)
}
func TestLatchManagerContextCancellation(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Attempt to acquire three latches that all block on each other.
lg1 := m.MustAcquire(spans("a", "", write, zeroTS))
// The second one is given a cancelable context.
ctx2, cancel2 := context.WithCancel(context.Background())
lg2C, _ := m.MustAcquireChExt(ctx2, spans("a", "", write, zeroTS), poison.Policy_Error)
lg3C := m.MustAcquireCh(spans("a", "", write, zeroTS))
// The second and third latch attempt block on the first.
testLatchBlocks(t, lg2C)
testLatchBlocks(t, lg3C)
// Cancel the second acquisition's context. It should stop waiting.
cancel2()
require.Nil(t, <-lg2C)
// The third latch attempt still blocks.
testLatchBlocks(t, lg3C)
// Release the first latch. The third succeeds in acquiring the latch.
m.Release(lg1)
testLatchSucceeds(t, lg3C)
}
func TestLatchManagerOptimistic(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Acquire latches, no conflict.
lg1 := m.AcquireOptimistic(spans("d", "f", write, zeroTS), poison.Policy_Error)
require.True(t, m.CheckOptimisticNoConflicts(lg1, spans("d", "f", write, zeroTS)), poison.Policy_Error)
lg1, err := m.WaitUntilAcquired(context.Background(), lg1)
require.NoError(t, err)
// Optimistic acquire encounters conflict in some cases.
lg2 := m.AcquireOptimistic(spans("a", "e", read, zeroTS), poison.Policy_Error)
require.False(t, m.CheckOptimisticNoConflicts(lg2, spans("a", "e", read, zeroTS)))
require.True(t, m.CheckOptimisticNoConflicts(lg2, spans("a", "d", read, zeroTS)))
waitUntilAcquiredCh := func(g *Guard) <-chan *Guard {
ch := make(chan *Guard)
go func() {
lg, err := m.WaitUntilAcquired(context.Background(), g)
require.NoError(t, err)
ch <- lg
}()
return ch
}
ch2 := waitUntilAcquiredCh(lg2)
testLatchBlocks(t, ch2)
m.Release(lg1)
testLatchSucceeds(t, ch2)
// Optimistic acquire encounters conflict.
lg3 := m.AcquireOptimistic(spans("a", "e", write, zeroTS), poison.Policy_Error)
require.False(t, m.CheckOptimisticNoConflicts(lg3, spans("a", "e", write, zeroTS)))
m.Release(lg2)
// There is still a conflict even though lg2 has been released.
require.False(t, m.CheckOptimisticNoConflicts(lg3, spans("a", "e", write, zeroTS)))
lg3, err = m.WaitUntilAcquired(context.Background(), lg3)
require.NoError(t, err)
m.Release(lg3)
// Optimistic acquire for read below write encounters no conflict.
oneTS, twoTS := hlc.Timestamp{WallTime: 1}, hlc.Timestamp{WallTime: 2}
lg4 := m.MustAcquire(spans("c", "e", write, twoTS))
lg5 := m.AcquireOptimistic(spans("a", "e", read, oneTS), poison.Policy_Error)
require.True(t, m.CheckOptimisticNoConflicts(lg5, spans("a", "e", read, oneTS)))
require.True(t, m.CheckOptimisticNoConflicts(lg5, spans("a", "c", read, oneTS)))
lg5, err = m.WaitUntilAcquired(context.Background(), lg5)
require.NoError(t, err)
m.Release(lg5)
m.Release(lg4)
}
func TestLatchManagerWaitFor(t *testing.T) {
defer leaktest.AfterTest(t)()
var m Manager
// Acquire latches, no conflict.
lg1, err := m.Acquire(context.Background(), spans("d", "f", write, zeroTS), poison.Policy_Error)
require.NoError(t, err)
// See if WaitFor waits for above latch.
waitForCh := func() <-chan *Guard {
ch := make(chan *Guard)
go func() {
err := m.WaitFor(context.Background(), spans("a", "e", read, zeroTS), poison.Policy_Error)
require.NoError(t, err)
ch <- &Guard{}
}()
return ch
}
ch2 := waitForCh()
testLatchBlocks(t, ch2)
m.Release(lg1)
testLatchSucceeds(t, ch2)
// Optimistic acquire should _not_ encounter conflict - as WaitFor should
// not lay any latches.
lg3 := m.AcquireOptimistic(spans("a", "e", write, zeroTS), poison.Policy_Error)
require.True(t, m.CheckOptimisticNoConflicts(lg3, spans("a", "e", write, zeroTS)))
lg3, err = m.WaitUntilAcquired(context.Background(), lg3)
require.NoError(t, err)
m.Release(lg3)
}
func BenchmarkLatchManagerReadOnlyMix(b *testing.B) {
for _, size := range []int{1, 4, 16, 64, 128, 256} {
b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
var m Manager
ss := spans("a", "b", read, zeroTS)
for i := 0; i < size; i++ {
_ = m.MustAcquire(ss)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = m.MustAcquire(ss)
}
})
}
}
func BenchmarkLatchManagerReadWriteMix(b *testing.B) {
for _, readsPerWrite := range []int{0, 1, 4, 16, 64, 128, 256} {
b.Run(fmt.Sprintf("readsPerWrite=%d", readsPerWrite), func(b *testing.B) {
var m Manager
lgBuf := make(chan *Guard, 16)
spans := make([]spanset.SpanSet, b.N)
for i := range spans {
a, b := randBytes(100), randBytes(100)
// Overwrite first byte so that we do not mix local and global ranges
a[0], b[0] = 'a', 'a'
if bytes.Compare(a, b) > 0 {
a, b = b, a
}
span := roachpb.Span{Key: a, EndKey: b}
access := spanset.SpanReadOnly
if i%(readsPerWrite+1) == 0 {
access = spanset.SpanReadWrite
}
spans[i].AddNonMVCC(access, span)
}
b.ResetTimer()
for i := range spans {
lg, snap := m.sequence(&spans[i], poison.Policy_Error)
snap.close()
if len(lgBuf) == cap(lgBuf) {
m.Release(<-lgBuf)
}
lgBuf <- lg
}
})
}
}
func randBytes(n int) []byte {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return b
}