@@ -443,6 +443,7 @@ func (twb *txnWriteBuffer) estimateSize(ba *kvpb.BatchRequest) int64 {
443
443
seq : t .Sequence ,
444
444
}
445
445
estimate += scratch .size ()
446
+ estimate += lockKeyInfoSize
446
447
case * kvpb.PutRequest :
447
448
// NB: when estimating, we're being conservative by assuming the Put is to
448
449
// a key that isn't already present in the buffer. If it were, we could
@@ -453,6 +454,9 @@ func (twb *txnWriteBuffer) estimateSize(ba *kvpb.BatchRequest) int64 {
453
454
seq : t .Sequence ,
454
455
}
455
456
estimate += scratch .size ()
457
+ if t .MustAcquireExclusiveLock {
458
+ estimate += lockKeyInfoSize
459
+ }
456
460
case * kvpb.DeleteRequest :
457
461
// NB: Similar to Put, we're assuming we're deleting a key that isn't
458
462
// already present in the buffer.
@@ -461,6 +465,9 @@ func (twb *txnWriteBuffer) estimateSize(ba *kvpb.BatchRequest) int64 {
461
465
seq : t .Sequence ,
462
466
}
463
467
estimate += scratch .size ()
468
+ if t .MustAcquireExclusiveLock {
469
+ estimate += lockKeyInfoSize
470
+ }
464
471
}
465
472
// No other request is buffered.
466
473
}
@@ -648,7 +655,10 @@ func (twb *txnWriteBuffer) rollbackToSavepointLocked(ctx context.Context, s save
648
655
toDelete := make ([]* bufferedWrite , 0 )
649
656
it := twb .buffer .MakeIter ()
650
657
for it .First (); it .Valid (); it .Next () {
651
- it .Cur ().rollbackLockInfo (s .seqNum )
658
+ if held := it .Cur ().rollbackLockInfo (s .seqNum ); ! held {
659
+ // If we aren't still held, update our buffer size.
660
+ twb .bufferSize -= lockKeyInfoSize
661
+ }
652
662
bufferedVals := it .Cur ().vals
653
663
// NB: the savepoint is being rolled back to s.seqNum (inclusive). So,
654
664
// idx is the index of the first value that is considered rolled back.
@@ -1346,7 +1356,9 @@ func (twb *txnWriteBuffer) addToBuffer(
1346
1356
val := bufferedValue {val : val , seq : seq , kvNemesisSeq : kvNemSeq }
1347
1357
bw .vals = append (bw .vals , val )
1348
1358
if lockInfo != nil {
1349
- bw .acquireLock (lockInfo )
1359
+ if firstAcquisition := bw .acquireLock (lockInfo ); firstAcquisition {
1360
+ twb .bufferSize += lockKeyInfoSize
1361
+ }
1350
1362
}
1351
1363
twb .bufferSize += val .size ()
1352
1364
} else {
@@ -1511,14 +1523,21 @@ func (bw *bufferedWrite) size() int64 {
1511
1523
for _ , v := range bw .vals {
1512
1524
size += v .size ()
1513
1525
}
1526
+ if bw .lki != nil {
1527
+ size += lockKeyInfoSize
1528
+ }
1514
1529
return size
1515
1530
}
1516
1531
1517
- func (bw * bufferedWrite ) acquireLock (li * lockAcquisition ) {
1532
+ // acquireLock updates the lock information for this buffered write. It returns
1533
+ // true if this is the first lock acquisition.
1534
+ func (bw * bufferedWrite ) acquireLock (li * lockAcquisition ) bool {
1518
1535
if bw .lki == nil {
1519
1536
bw .lki = newLockedKeyInfo (li .str , li .seq , li .ts )
1537
+ return true
1520
1538
} else {
1521
1539
bw .lki .acquireLock (li .str , li .seq , li .ts )
1540
+ return false
1522
1541
}
1523
1542
}
1524
1543
@@ -1539,14 +1558,18 @@ func (bw *bufferedWrite) heldStr(seq enginepb.TxnSeq) lock.Strength {
1539
1558
return bw .lki .heldStr (seq )
1540
1559
}
1541
1560
1542
- func (bw * bufferedWrite ) rollbackLockInfo (seq enginepb.TxnSeq ) {
1561
+ // rollbackLockInfo updates the lock information based on the rollback. It
1562
+ // returns true if the lock is still held.
1563
+ func (bw * bufferedWrite ) rollbackLockInfo (seq enginepb.TxnSeq ) bool {
1543
1564
if bw .lki == nil {
1544
- return
1565
+ return false
1545
1566
}
1546
1567
1547
- if ! bw .lki .rollbackSequence (seq ) {
1568
+ stillHeld := bw .lki .rollbackSequence (seq )
1569
+ if ! stillHeld {
1548
1570
bw .lki = nil
1549
1571
}
1572
+ return stillHeld
1550
1573
}
1551
1574
1552
1575
const bufferedValueStructOverhead = int64 (unsafe .Sizeof (bufferedValue {}))
@@ -1685,6 +1708,8 @@ type lockedKeyInfo struct {
1685
1708
ts hlc.Timestamp
1686
1709
}
1687
1710
1711
+ var lockKeyInfoSize = int64 (unsafe .Sizeof (lockedKeyInfo {}))
1712
+
1688
1713
func newLockedKeyInfo (str lock.Strength , seqNum enginepb.TxnSeq , ts hlc.Timestamp ) * lockedKeyInfo {
1689
1714
lki := & lockedKeyInfo {
1690
1715
ts : ts ,
0 commit comments