Skip to content

Commit

Permalink
[yugabyte#13222] YSQL: Ensure that the list of aborted sub-txns is no…
Browse files Browse the repository at this point in the history
…t spuriously removed by asynchronous heartbeats

Summary:
4fb1676 added logic that enabled transactions to
ignore conflicting intents of aborted sub-txns. This was done by asynchronously
sending the aborted sub-txn list to the transactions coordinator using the
existing heartbeats. The aborted sub-txns list is then used by transaction
participants during conflicting resolution.

85ac8d8, which came later, ensured that YSQL
synchronously updates the list of aborted sub-txns on the transaction status
record of a status tablet when some sub-txn is rolled back. This was to
eliminate any window where a transaction might face a conflict error when another
transaction's aborted intents.

But the second commit introduces the following buggy line in
PendingReplicationFinished() that causes the aborted sub-txns list to disappear
in the next asynchronous heartbeat from YSQL to the status tablet:
`aborted_ = data.state.aborted();`.
The second commit removed the aborted sub-txns list from the asynchronous
heartbeats. And due to the above buggy line, the empty list replaces the
aborted sub-txn list on the status record in the next heartbeat.

The implication of this is: other transactions are not able to ignore conflicting
intents that are part of aborted sub-txns.

The two tests introduced in 4fb1676 are moved
to the isolation_regress test suite:

(1) testIgnoresIntentOfRolledBackSavepointCrossTxn
(2) testIgnoresLockOnlyConflictOfCommittedTxn

The bug introduced by 85ac8d8 is tested now
by an extra case in testIgnoresIntentOfRolledBackSavepointCrossTxn: a sleep
is introduced before the second transaction attempts to insert the same row.
Without the fix, this case fails since the sleep is enough for an asynchronous
heartbeat to clear the aborted sub-txn list and cause the second transation
to face a conflict error.

Test Plan:
./yb_build.sh --java-test org.yb.pgsql.TestPgIsolationRegress#isolationRegress
./yb_build.sh --java-test org.yb.pgsql.TestPgIsolationRegress#withDelayedTxnApply

Reviewers: rsami

Subscribers: yql

Differential Revision: https://phabricator.dev.yugabyte.com/D18226
  • Loading branch information
pkj415 committed Jul 9, 2022
1 parent 8555253 commit 2f726f2
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 34 deletions.
32 changes: 0 additions & 32 deletions java/yb-pgsql/src/test/java/org/yb/pgsql/TestPgSavepoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private OptionalInt getSingleValue(Connection c, int k) throws SQLException {
protected Map<String, String> getTServerFlags() {
Map<String, String> flags = super.getTServerFlags();
flags.put("txn_max_apply_batch_records", String.format("%d", LARGE_BATCH_ROW_THRESHOLD));
flags.put("TEST_inject_sleep_before_applying_intents_ms", "10000");
return flags;
}

Expand Down Expand Up @@ -155,37 +154,6 @@ public void testIgnoresIntentOfRolledBackSavepointSameTxn() throws Exception {
}
}

@Test
public void testIgnoresIntentOfRolledBackSavepointCrossTxn() throws Exception {
createTable();

Connection conn1 = getConnectionBuilder()
.withIsolationLevel(IsolationLevel.READ_COMMITTED)
.withAutoCommit(AutoCommit.DISABLED)
.connect();
Connection conn2 = getConnectionBuilder()
.withIsolationLevel(IsolationLevel.READ_COMMITTED)
.withAutoCommit(AutoCommit.DISABLED)
.connect();

Statement s1 = conn1.createStatement();
Statement s2 = conn2.createStatement();

s1.execute("INSERT INTO t VALUES (1, 1)");
s1.execute("SAVEPOINT a");
s1.execute("INSERT INTO t VALUES (2, 1)");
s1.execute("ROLLBACK TO a");

s2.execute("INSERT INTO t VALUES (2, 2)");

conn1.commit();
conn2.commit();


assertEquals(OptionalInt.of(1), getSingleValue(conn1, 1));
assertEquals(OptionalInt.of(2), getSingleValue(conn1, 2));
}

@Test
public void testIgnoresLockOnlyConflictOfCommittedTxn() throws Exception {
createTable();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Parsed test spec with 2 sessions

starting permutation: s1_select s2_select s1_savepoint_a s1_update_k_1 s1_rollback_to_a s1_lock_k_1 s1_update_k_3 s1_commit s2_update_k_1 s2_commit s1_select
step s1_select: SELECT * FROM t;
k v

5 0
1 0
6 0
7 0
9 0
10 0
4 0
2 0
8 0
3 0
step s2_select: SELECT * FROM t;
k v

5 0
1 0
6 0
7 0
9 0
10 0
4 0
2 0
8 0
3 0
step s1_savepoint_a: SAVEPOINT a;
step s1_update_k_1: UPDATE t SET v=1 WHERE k=1;
step s1_rollback_to_a: ROLLBACK TO a;
step s1_lock_k_1: SELECT * FROM t WHERE k=1 FOR UPDATE;
k v

1 0
step s1_update_k_3: UPDATE t SET v=1 WHERE k=3;
step s1_commit: COMMIT;
step s2_update_k_1: UPDATE t SET v=2 WHERE k=1;
step s2_commit: COMMIT;
step s1_select: SELECT * FROM t;
k v

5 0
1 2
6 0
7 0
9 0
10 0
4 0
2 0
8 0
3 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Parsed test spec with 2 sessions

starting permutation: s1_insert_1_1 s1_savepoint_a s1_insert_2_1 s1_rollback_to_a s2_insert_2_2 s1_commit s2_commit s1_select
step s1_insert_1_1: INSERT INTO t VALUES (1, 1);
step s1_savepoint_a: SAVEPOINT a;
step s1_insert_2_1: INSERT INTO t VALUES (2, 1);
step s1_rollback_to_a: ROLLBACK TO a;
step s2_insert_2_2: INSERT INTO t VALUES (2, 2);
step s1_commit: COMMIT;
step s2_commit: COMMIT;
step s1_select: SELECT * FROM t;
key v

1 1
2 2

starting permutation: s1_insert_1_1 s1_savepoint_a s1_insert_2_1 s1_rollback_to_a s1_sleep s2_insert_2_2 s1_commit s2_commit s1_select
step s1_insert_1_1: INSERT INTO t VALUES (1, 1);
step s1_savepoint_a: SAVEPOINT a;
step s1_insert_2_1: INSERT INTO t VALUES (2, 1);
step s1_rollback_to_a: ROLLBACK TO a;
step s1_sleep: SELECT pg_sleep(2);
pg_sleep


step s2_insert_2_2: INSERT INTO t VALUES (2, 2);
step s1_commit: COMMIT;
step s2_commit: COMMIT;
step s1_select: SELECT * FROM t;
key v

1 1
2 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This tests ensures the following guarantee:
#
# 1. Txn 1 registers a savepoint and modifies a row
# 2. Txn 1 rolls back to the savepoint, undoing the modification
# 3. Txn 1 then locks the same row
# 4. Txn 1 commits but is yet to apply the intents on the txn participant.
# 5. Another transaction tries to modify the same row. This new transaction sees 2 conflicts - a
# conflict with the modification from Txn 1 and a conflict with the lock-only intent from Txn 1.
# The conflict with the modification should be ignored since it is part of an aborted sub-txn.
# The conflict with the lock-only intent should also be ignored since the Txn 1 has committed
# and no longer holds the lock.
#
# This scenario is only tested with "org.yb.pgsql.TestPgIsolationRegress#withDelayedTxnApply" which
# uses TEST_inject_sleep_before_applying_intents_ms to induce a delay before applying intents. This
# is needed for step (4) above. Without this, the lock-only intent would be removed and the second
# transaction wouldn't see it (and we want to test the fact that the second transaction should
# ignore it after seeing it in intents db because it is a lock-only intent).
#
# This test was introduced due to a review comment in 4fb1676a785319fa35e2d75241a870e3115f1637. The
# comment was due to the following issue in the work in progress code: a modification intent that
# was part of an aborted sub-txn was causing a non-aborted lock-only intent of an aborted txn to
# conflict with other transactions.

setup
{
CREATE TABLE t (k INT PRIMARY KEY, v INT);
INSERT INTO t SELECT generate_series(1, 10), 0;
}

teardown
{
DROP TABLE t;
}

session "s1"
setup { BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; }
step "s1_select" { SELECT * FROM t; }
step "s1_savepoint_a" { SAVEPOINT a; }
step "s1_update_k_1" { UPDATE t SET v=1 WHERE k=1; }
step "s1_rollback_to_a" { ROLLBACK TO a; }
step "s1_lock_k_1" { SELECT * FROM t WHERE k=1 FOR UPDATE; }
step "s1_update_k_3" { UPDATE t SET v=1 WHERE k=3; }
step "s1_commit" { COMMIT; }

session "s2"
setup { BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; }
step "s2_select" { SELECT * FROM t; }
step "s2_update_k_1" { UPDATE t SET v=2 WHERE k=1; }
step "s2_commit" { COMMIT; }

permutation "s1_select" "s2_select" "s1_savepoint_a" "s1_update_k_1" "s1_rollback_to_a" "s1_lock_k_1" "s1_update_k_3" "s1_commit" "s2_update_k_1" "s2_commit" "s1_select"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
setup
{
CREATE TABLE t (key INT PRIMARY KEY, v INT);
}

teardown
{
DROP TABLE t;
}

session "s1"
setup { BEGIN; }
step "s1_insert_1_1" { INSERT INTO t VALUES (1, 1); }
step "s1_savepoint_a" { SAVEPOINT a; }
step "s1_insert_2_1" { INSERT INTO t VALUES (2, 1); }
step "s1_rollback_to_a" { ROLLBACK TO a; }
step "s1_commit" { COMMIT; }
step "s1_select" { SELECT * FROM t; }
step "s1_sleep" { SELECT pg_sleep(2); }

session "s2"
setup { BEGIN; }
step "s2_insert_2_2" { INSERT INTO t VALUES (2, 2); }
step "s2_commit" { COMMIT; }

permutation "s1_insert_1_1" "s1_savepoint_a" "s1_insert_2_1" "s1_rollback_to_a" "s2_insert_2_2" "s1_commit" "s2_commit" "s1_select"
permutation "s1_insert_1_1" "s1_savepoint_a" "s1_insert_2_1" "s1_rollback_to_a" "s1_sleep" "s2_insert_2_2" "s1_commit" "s2_commit" "s1_select"
4 changes: 3 additions & 1 deletion src/postgres/src/test/isolation/yb_pg_isolation_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ test: read-only-anomaly
test: nowait-2
test: nowait-3
test: aborted-keyrevoke
test: delete-abort-savept-2
test: delete-abort-savept-2
test: ignore-intent-of-rolled-back-savepoint-cross-txn
test: ensure-lock-only-conflict-of-committed-txn-is-ignored-even-when-modification-conflict-is-rolled-back
3 changes: 2 additions & 1 deletion src/yb/tablet/transaction_coordinator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,8 @@ class TransactionState {
first_entry_raft_index_ = data.op_id.index;

// TODO(savepoints) -- consider swapping instead of copying here.
aborted_ = data.state.aborted();
if (!data.state.aborted().set().empty())
aborted_ = data.state.aborted();

return Status::OK();
}
Expand Down

0 comments on commit 2f726f2

Please sign in to comment.