Skip to content

Commit 1df2c30

Browse files
craig[bot]stevendanna
andcommitted
Merge #153144
153144: kvnemesis: add MustAcquireExclusiveLock to Put and Del r=miraradeva a=stevendanna Fixes #144157 Release note: None Co-authored-by: Steven Danna <[email protected]>
2 parents e014575 + bd96399 commit 1df2c30

File tree

5 files changed

+128
-20
lines changed

5 files changed

+128
-20
lines changed

pkg/kv/kvnemesis/applier.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ func applyClientOp(
330330
}
331331
case *PutOperation:
332332
_, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
333-
b.Put(o.Key, o.Value())
333+
if o.MustAcquireExclusiveLock {
334+
b.PutMustAcquireExclusiveLock(o.Key, o.Value())
335+
} else {
336+
b.Put(o.Key, o.Value())
337+
}
334338
setLastReqSeq(b, o.Seq)
335339
})
336340
o.Result = resultInit(ctx, err)
@@ -381,7 +385,11 @@ func applyClientOp(
381385
}
382386
case *DeleteOperation:
383387
res, ts, err := dbRunWithResultAndTimestamp(ctx, db, func(b *kv.Batch) {
384-
b.Del(o.Key)
388+
if o.MustAcquireExclusiveLock {
389+
b.DelMustAcquireExclusiveLock(o.Key)
390+
} else {
391+
b.Del(o.Key)
392+
}
385393
setLastReqSeq(b, o.Seq)
386394
})
387395
o.Result = resultInit(ctx, err)
@@ -547,7 +555,11 @@ func applyBatchOp(
547555
b.Get(subO.Key)
548556
}
549557
case *PutOperation:
550-
b.Put(subO.Key, subO.Value())
558+
if subO.MustAcquireExclusiveLock {
559+
b.PutMustAcquireExclusiveLock(subO.Key, subO.Value())
560+
} else {
561+
b.Put(subO.Key, subO.Value())
562+
}
551563
setLastReqSeq(b, subO.Seq)
552564
case *ScanOperation:
553565
if subO.SkipLocked {
@@ -575,7 +587,11 @@ func applyBatchOp(
575587
}
576588
}
577589
case *DeleteOperation:
578-
b.Del(subO.Key)
590+
if subO.MustAcquireExclusiveLock {
591+
b.DelMustAcquireExclusiveLock(subO.Key)
592+
} else {
593+
b.Del(subO.Key)
594+
}
579595
setLastReqSeq(b, subO.Seq)
580596
case *DeleteRangeOperation:
581597
b.DelRange(subO.Key, subO.EndKey, true /* returnKeys */)

pkg/kv/kvnemesis/generator.go

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ type ClientOperationConfig struct {
179179
PutMissing int
180180
// PutExisting is an operation that Puts a key that likely exists.
181181
PutExisting int
182+
// PutMustAcquireExclusiveLockMissing is an operation that Puts a key that
183+
// definitely doesn't exist, with MustAcquireExclusiveLock set to true.
184+
PutMustAcquireExclusiveLockMissing int
185+
// PutMustAcquireExclusiveLockExisting is an operation that Puts a key that
186+
// likely exists, with MustAcquireExclusiveLock set to true.
187+
PutMustAcquireExclusiveLockExisting int
182188
// Scan is an operation that Scans a key range that may contain values.
183189
Scan int
184190
// ScanForUpdate is an operation that Scans a key range that may contain
@@ -259,6 +265,12 @@ type ClientOperationConfig struct {
259265
DeleteMissing int
260266
// DeleteExisting is an operation that Deletes a key that likely exists.
261267
DeleteExisting int
268+
// DeleteMustAcquireExclusiveLockMissing is an operation that Deletes a key
269+
// that definitely doesn't exist, with MustAcquireExclusiveLock set to true.
270+
DeleteMustAcquireExclusiveLockMissing int
271+
// DeleteExisting is an operation that Deletes a key that likely exists, with
272+
// MustAcquireExclusiveLock set to true.
273+
DeleteMustAcquireExclusiveLockExisting int
262274
// DeleteRange is an operation that Deletes a key range that may contain values.
263275
DeleteRange int
264276
// DeleteRange is an operation that invokes DeleteRangeUsingTombstone.
@@ -388,15 +400,17 @@ func newAllOperationsConfig() GeneratorConfig {
388400
GetExistingForUpdateSkipLockedGuaranteedDurability: 1,
389401
GetExistingForShareSkipLocked: 1,
390402
GetExistingForShareSkipLockedGuaranteedDurability: 1,
391-
PutMissing: 1,
392-
PutExisting: 1,
393-
Scan: 1,
394-
ScanForUpdate: 1,
395-
ScanForUpdateGuaranteedDurability: 1,
396-
ScanForShare: 1,
397-
ScanForShareGuaranteedDurability: 1,
398-
ScanSkipLocked: 1,
399-
ScanForUpdateSkipLocked: 1,
403+
PutMissing: 1,
404+
PutExisting: 1,
405+
PutMustAcquireExclusiveLockMissing: 1,
406+
PutMustAcquireExclusiveLockExisting: 1,
407+
Scan: 1,
408+
ScanForUpdate: 1,
409+
ScanForUpdateGuaranteedDurability: 1,
410+
ScanForShare: 1,
411+
ScanForShareGuaranteedDurability: 1,
412+
ScanSkipLocked: 1,
413+
ScanForUpdateSkipLocked: 1,
400414
ScanForUpdateSkipLockedGuaranteedDurability: 1,
401415
ScanForShareSkipLocked: 1,
402416
ScanForShareSkipLockedGuaranteedDurability: 1,
@@ -412,6 +426,8 @@ func newAllOperationsConfig() GeneratorConfig {
412426
ReverseScanForShareSkipLockedGuaranteedDurability: 1,
413427
DeleteMissing: 1,
414428
DeleteExisting: 1,
429+
DeleteMustAcquireExclusiveLockMissing: 1,
430+
DeleteMustAcquireExclusiveLockExisting: 1,
415431
DeleteRange: 1,
416432
DeleteRangeUsingTombstone: 1,
417433
AddSSTable: 1,
@@ -777,7 +793,10 @@ func (g *generator) registerClientOps(allowed *[]opGen, c *ClientOperationConfig
777793
c.GetMissingForShareSkipLockedGuaranteedDurability,
778794
)
779795
addOpGen(allowed, randPutMissing, c.PutMissing)
796+
addOpGen(allowed, randPutMustAcquireExclusiveLockMissing, c.PutMustAcquireExclusiveLockMissing)
780797
addOpGen(allowed, randDelMissing, c.DeleteMissing)
798+
addOpGen(allowed, randDelMustAcquireExclusiveLockMissing, c.DeleteMustAcquireExclusiveLockMissing)
799+
781800
if len(g.keys) > 0 {
782801
addOpGen(allowed, randGetExisting, c.GetExisting)
783802
addOpGen(allowed, randGetExistingForUpdate, c.GetExistingForUpdate)
@@ -806,7 +825,9 @@ func (g *generator) registerClientOps(allowed *[]opGen, c *ClientOperationConfig
806825
c.GetExistingForShareSkipLockedGuaranteedDurability,
807826
)
808827
addOpGen(allowed, randPutExisting, c.PutExisting)
828+
addOpGen(allowed, randPutMustAcquireExclusiveLockExisting, c.PutMustAcquireExclusiveLockExisting)
809829
addOpGen(allowed, randDelExisting, c.DeleteExisting)
830+
addOpGen(allowed, randDelMustAcquireExclusiveLockExisting, c.DeleteMustAcquireExclusiveLockExisting)
810831
}
811832
addOpGen(allowed, randScan, c.Scan)
812833
addOpGen(allowed, randScanForUpdate, c.ScanForUpdate)
@@ -1001,6 +1022,19 @@ func randPutExisting(g *generator, rng *rand.Rand) Operation {
10011022
return put(key, seq)
10021023
}
10031024

1025+
func randPutMustAcquireExclusiveLockMissing(g *generator, rng *rand.Rand) Operation {
1026+
seq := g.nextSeq()
1027+
key := randKey(rng)
1028+
g.keys[key] = struct{}{}
1029+
return putMustAcquireLock(key, seq)
1030+
}
1031+
1032+
func randPutMustAcquireExclusiveLockExisting(g *generator, rng *rand.Rand) Operation {
1033+
seq := g.nextSeq()
1034+
key := randMapKey(rng, g.keys)
1035+
return putMustAcquireLock(key, seq)
1036+
}
1037+
10041038
func randAddSSTable(g *generator, rng *rand.Rand) Operation {
10051039
ctx := context.Background()
10061040

@@ -1315,6 +1349,19 @@ func randDelExisting(g *generator, rng *rand.Rand) Operation {
13151349
return del(key, seq)
13161350
}
13171351

1352+
func randDelMustAcquireExclusiveLockExisting(g *generator, rng *rand.Rand) Operation {
1353+
key := randMapKey(rng, g.keys)
1354+
seq := g.nextSeq()
1355+
return delMustAcquireLock(key, seq)
1356+
}
1357+
1358+
func randDelMustAcquireExclusiveLockMissing(g *generator, rng *rand.Rand) Operation {
1359+
key := randKey(rng)
1360+
g.keys[key] = struct{}{}
1361+
seq := g.nextSeq()
1362+
return delMustAcquireLock(key, seq)
1363+
}
1364+
13181365
func randDelRange(g *generator, rng *rand.Rand) Operation {
13191366
// We don't write any new keys to `g.keys` on a DeleteRange operation,
13201367
// because DelRange(..) only deletes existing keys.
@@ -1906,7 +1953,16 @@ func getForShareSkipLockedGuaranteedDurability(key string) Operation {
19061953
}
19071954

19081955
func put(key string, seq kvnemesisutil.Seq) Operation {
1909-
return Operation{Put: &PutOperation{Key: []byte(key), Seq: seq}}
1956+
return Operation{Put: &PutOperation{
1957+
Key: []byte(key),
1958+
Seq: seq}}
1959+
}
1960+
1961+
func putMustAcquireLock(key string, seq kvnemesisutil.Seq) Operation {
1962+
return Operation{Put: &PutOperation{
1963+
Key: []byte(key),
1964+
MustAcquireExclusiveLock: true,
1965+
Seq: seq}}
19101966
}
19111967

19121968
func scan(key, endKey string) Operation {
@@ -1996,6 +2052,14 @@ func del(key string, seq kvnemesisutil.Seq) Operation {
19962052
}}
19972053
}
19982054

2055+
func delMustAcquireLock(key string, seq kvnemesisutil.Seq) Operation {
2056+
return Operation{Delete: &DeleteOperation{
2057+
Key: []byte(key),
2058+
Seq: seq,
2059+
MustAcquireExclusiveLock: true,
2060+
}}
2061+
}
2062+
19992063
func delRange(key, endKey string, seq kvnemesisutil.Seq) Operation {
20002064
return Operation{DeleteRange: &DeleteRangeOperation{Key: []byte(key), EndKey: []byte(endKey), Seq: seq}}
20012065
}

pkg/kv/kvnemesis/generator_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,17 @@ func TestRandStep(t *testing.T) {
167167
}
168168
case *PutOperation:
169169
if _, ok := keys[string(o.Key)]; ok {
170-
client.PutExisting++
170+
if o.MustAcquireExclusiveLock {
171+
client.PutMustAcquireExclusiveLockExisting++
172+
} else {
173+
client.PutExisting++
174+
}
171175
} else {
172-
client.PutMissing++
176+
if o.MustAcquireExclusiveLock {
177+
client.PutMustAcquireExclusiveLockMissing++
178+
} else {
179+
client.PutMissing++
180+
}
173181
}
174182
case *ScanOperation:
175183
if o.Reverse {
@@ -236,9 +244,17 @@ func TestRandStep(t *testing.T) {
236244
}
237245
case *DeleteOperation:
238246
if _, ok := keys[string(o.Key)]; ok {
239-
client.DeleteExisting++
247+
if o.MustAcquireExclusiveLock {
248+
client.DeleteMustAcquireExclusiveLockExisting++
249+
} else {
250+
client.DeleteExisting++
251+
}
240252
} else {
241-
client.DeleteMissing++
253+
if o.MustAcquireExclusiveLock {
254+
client.DeleteMustAcquireExclusiveLockMissing++
255+
} else {
256+
client.DeleteMissing++
257+
}
242258
}
243259
case *DeleteRangeOperation:
244260
client.DeleteRange++

pkg/kv/kvnemesis/operations.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,11 @@ func (op GetOperation) format(w *strings.Builder, fctx formatCtx) {
253253
}
254254

255255
func (op PutOperation) format(w *strings.Builder, fctx formatCtx) {
256-
fmt.Fprintf(w, `%s.Put(%s%s, sv(%d))`, fctx.receiver, fctx.maybeCtx(), fmtKey(op.Key), op.Seq)
256+
verb := "Put"
257+
if op.MustAcquireExclusiveLock {
258+
verb = "PutMustAcquireExclusiveLock"
259+
}
260+
fmt.Fprintf(w, `%s.%s(%s%s, sv(%d))`, fctx.receiver, verb, fctx.maybeCtx(), fmtKey(op.Key), op.Seq)
257261
op.Result.format(w)
258262
}
259263

@@ -298,7 +302,11 @@ func (op ScanOperation) format(w *strings.Builder, fctx formatCtx) {
298302
}
299303

300304
func (op DeleteOperation) format(w *strings.Builder, fctx formatCtx) {
301-
fmt.Fprintf(w, `%s.Del(%s%s /* @%s */)`, fctx.receiver, fctx.maybeCtx(), fmtKey(op.Key), op.Seq)
305+
verb := "Del"
306+
if op.MustAcquireExclusiveLock {
307+
verb = "DelMustAcquireExclusiveLock"
308+
}
309+
fmt.Fprintf(w, `%s.%s(%s%s /* @%s */)`, fctx.receiver, verb, fctx.maybeCtx(), fmtKey(op.Key), op.Seq)
302310
op.Result.format(w)
303311
}
304312

pkg/kv/kvnemesis/operations.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ message ScanOperation {
6060
message PutOperation {
6161
bytes key = 1;
6262
uint32 seq = 2 [(gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/kv/kvnemesis/kvnemesisutil.Seq"];
63+
bool must_acquire_exclusive_lock = 4;
64+
6365
Result result = 3 [(gogoproto.nullable) = false];
6466
}
6567

6668
message DeleteOperation {
6769
bytes key = 1;
6870
uint32 seq = 2 [(gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/kv/kvnemesis/kvnemesisutil.Seq"];
71+
bool must_acquire_exclusive_lock = 4;
72+
6973
Result result = 3 [(gogoproto.nullable) = false];
7074
}
7175

0 commit comments

Comments
 (0)