Skip to content

Commit

Permalink
sql: fix flake in TestTxnContentionEventsTable
Browse files Browse the repository at this point in the history
In causeContention we deliberately hold a transaction open using
pg_sleep to block an update statement. The timing we're trying to
achieve is:

1. transaction insert
2. update starts and blocks
3. transaction held open using pg_sleep

We were using a WaitGroup to order (2) after (1), but there was no
synchronization to ensure (3) came after (2).

This commit adds a retry loop that checks
`crdb_internal.cluster_queries` to ensure (3) comes after (2).

Fixes: #118236

Release note: None
  • Loading branch information
michae2 committed Feb 3, 2024
1 parent 15961a1 commit 199a586
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pkg/sql/crdb_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,22 +1004,35 @@ func causeContention(
insertValue)
require.NoError(t, errTxn)
wgTxnStarted.Done()
// Wait for the update to show up in cluster_queries.
testutils.SucceedsSoon(t, func() error {
row := tx.QueryRowContext(
ctx, "SELECT EXISTS (SELECT * FROM crdb_internal.cluster_queries WHERE query LIKE '%/* shuba */')",
)
var seen bool
if err := row.Scan(&seen); err != nil {
return err
}
if !seen {
return errors.Errorf("did not see update statement")
}
return nil
})
_, errTxn = tx.ExecContext(ctx, "select pg_sleep(.5);")
require.NoError(t, errTxn)
errTxn = tx.Commit()
require.NoError(t, errTxn)
}()

start := timeutil.Now()

// Need to wait for the txn to start to ensure lock contention.
wgTxnStarted.Wait()
// This will be blocked until the updateRowWithDelay finishes.
// This will be blocked until the insert txn finishes.
start := timeutil.Now()
_, errUpdate := conn.ExecContext(
ctx, fmt.Sprintf("UPDATE %s SET s = $1 where id = 'test';", table), updateValue)
ctx, fmt.Sprintf("UPDATE %s SET s = $1 where id = 'test' /* shuba */;", table), updateValue)
require.NoError(t, errUpdate)
end := timeutil.Now()
require.GreaterOrEqual(t, end.Sub(start), 499*time.Millisecond)
require.GreaterOrEqual(t, end.Sub(start), 500*time.Millisecond)

wgTxnDone.Wait()
}
Expand Down

0 comments on commit 199a586

Please sign in to comment.