Skip to content

Commit 109bdbb

Browse files
committed
sql: clean up TestReacquireLeaseOnRestart while here
Epic: none Release note: none
1 parent 62beeab commit 109bdbb

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

pkg/sql/txn_restart_test.go

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,36 +1272,35 @@ func TestReacquireLeaseOnRestart(t *testing.T) {
12721272
testKey := []byte("test_key")
12731273
var s serverutils.ApplicationLayerInterface
12741274
var nodeID roachpb.NodeID
1275-
var clockUpdate, restartDone int32
1275+
var clockUpdate atomic.Bool
1276+
var restartDone atomic.Int32
12761277
testingResponseFilter := func(
12771278
ctx context.Context, ba *kvpb.BatchRequest, br *kvpb.BatchResponse,
12781279
) *kvpb.Error {
12791280
for _, ru := range ba.Requests {
1280-
if req := ru.GetGet(); req != nil {
1281-
if bytes.Contains(req.Key, testKey) {
1282-
if atomic.LoadInt32(&clockUpdate) == 0 {
1283-
atomic.AddInt32(&clockUpdate, 1)
1284-
// Hack to advance the transaction timestamp on a
1285-
// transaction restart.
1286-
advancement := 2 * leaseDuration
1287-
now := s.Clock().NowAsClockTimestamp()
1288-
now.WallTime += advancement.Nanoseconds()
1289-
s.Clock().Update(now)
1290-
}
1281+
if req := ru.GetGet(); req == nil || !bytes.Contains(req.Key, testKey) {
1282+
continue
1283+
}
1284+
if !clockUpdate.Load() {
1285+
clockUpdate.Store(true)
1286+
// Hack to advance the transaction timestamp on a transaction restart.
1287+
advancement := 2 * leaseDuration
1288+
now := s.Clock().NowAsClockTimestamp()
1289+
now.WallTime += advancement.Nanoseconds()
1290+
s.Clock().Update(now)
1291+
}
12911292

1292-
// Allow a set number of restarts so that the auto retry on
1293-
// the first few uncertainty interval errors also fails.
1294-
if atomic.LoadInt32(&restartDone) <= refreshAttempts {
1295-
atomic.AddInt32(&restartDone, 1)
1296-
// Return ReadWithinUncertaintyIntervalError to update
1297-
// the transaction timestamp on retry.
1298-
txn := ba.Txn.Clone()
1299-
txn.ResetObservedTimestamps()
1300-
now := s.Clock().NowAsClockTimestamp()
1301-
txn.UpdateObservedTimestamp(nodeID, now)
1302-
return kvpb.NewErrorWithTxn(kvpb.NewReadWithinUncertaintyIntervalError(now.ToTimestamp(), now, txn, now.ToTimestamp(), now), txn)
1303-
}
1304-
}
1293+
// Allow a set number of restarts so that the auto retry on the first few
1294+
// uncertainty interval errors also fails.
1295+
if restartDone.Load() <= refreshAttempts {
1296+
restartDone.Add(1)
1297+
// Return ReadWithinUncertaintyIntervalError to update the transaction
1298+
// timestamp on retry.
1299+
txn := ba.Txn.Clone()
1300+
txn.ResetObservedTimestamps()
1301+
now := s.Clock().NowAsClockTimestamp()
1302+
txn.UpdateObservedTimestamp(nodeID, now)
1303+
return kvpb.NewErrorWithTxn(kvpb.NewReadWithinUncertaintyIntervalError(now.ToTimestamp(), now, txn, now.ToTimestamp(), now), txn)
13051304
}
13061305
}
13071306
return nil
@@ -1321,30 +1320,22 @@ func TestReacquireLeaseOnRestart(t *testing.T) {
13211320
nodeID = srv.NodeID()
13221321

13231322
sqlDB.SetMaxOpenConns(1)
1324-
if _, err := sqlDB.Exec(`
1323+
_, err := sqlDB.Exec(`
13251324
CREATE DATABASE t;
13261325
CREATE TABLE t.test (k TEXT PRIMARY KEY, v TEXT);
13271326
INSERT INTO t.test (k, v) VALUES ('test_key', 'test_val');
1328-
`); err != nil {
1329-
t.Fatal(err)
1330-
}
1327+
`)
1328+
require.NoError(t, err)
13311329
// Acquire the lease and enable the auto-retry. The first few read attempts
13321330
// will trigger ReadWithinUncertaintyIntervalError and advance the
13331331
// transaction timestamp due to txnSpanRefresher-initiated span refreshes.
13341332
// The transaction timestamp will exceed the lease expiration time, and the
13351333
// last read attempt will re-acquire the lease.
1336-
if _, err := sqlDB.Exec(`
1337-
SELECT * from t.test WHERE k = 'test_key';
1338-
`); err != nil {
1339-
t.Fatal(err)
1340-
}
1334+
_, err = sqlDB.Exec(`SELECT * from t.test WHERE k = 'test_key';`)
1335+
require.NoError(t, err)
13411336

1342-
if u := atomic.LoadInt32(&clockUpdate); u != 1 {
1343-
t.Errorf("expected exacltly one clock update, but got %d", u)
1344-
}
1345-
if u, e := atomic.LoadInt32(&restartDone), int32(refreshAttempts+1); u != e {
1346-
t.Errorf("expected exactly %d restarts, but got %d", e, u)
1347-
}
1337+
require.True(t, clockUpdate.Load())
1338+
require.Equal(t, int32(refreshAttempts)+1, restartDone.Load())
13481339
}
13491340

13501341
// Verifies that the uncommitted descriptor cache is flushed on a txn restart.

0 commit comments

Comments
 (0)