Skip to content

feat: delay transaction start option #2462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: only create tx manager if needed
  • Loading branch information
olavloite committed May 25, 2023
commit 9db90fabe69561b15a6ff42ed16402dc5006c286
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ private ReadWriteTransaction(Builder builder) {
this.savepointSupport = builder.savepointSupport;
this.transactionRetryListeners = builder.transactionRetryListeners;
this.transactionOptions = extractOptions(builder);
this.txManager = dbClient.transactionManager(this.transactionOptions);
}

private TransactionOption[] extractOptions(Builder builder) {
Expand Down Expand Up @@ -247,6 +246,7 @@ void checkValidTransaction(ParsedStatement statement) {
&& (!delayTransactionStartUntilFirstWrite
|| (statement != null && statement.isUpdate())
|| (statement == COMMIT_STATEMENT && !mutations.isEmpty()))) {
txManager = dbClient.transactionManager(this.transactionOptions);
canUseSingleUseRead = false;
txContextFuture =
executeStatementAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.spanner.v1.CommitRequest;
import com.google.spanner.v1.ExecuteBatchDmlRequest;
import com.google.spanner.v1.ExecuteSqlRequest;
import com.google.spanner.v1.RollbackRequest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -118,12 +119,7 @@ public void testDisable() {
@Test
public void testDefaultUsesRealTransactions() {
try (Connection connection = createConnection()) {
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.commit();

assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
Expand All @@ -140,18 +136,22 @@ public void testSingleQuery() {
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
for (boolean commit : new boolean[] {true, false}) {
executeRandomQuery(connection);
if (commit) {
connection.commit();
} else {
connection.rollback();
}
}
connection.commit();

assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
ExecuteSqlRequest request = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
assertFalse(request.hasTransaction());
assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
ExecuteSqlRequest request = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
assertFalse(request.hasTransaction());
assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
assertEquals(0, mockSpanner.countRequestsOfType(RollbackRequest.class));

mockSpanner.clearRequests();
}
}
}

Expand All @@ -160,24 +160,22 @@ public void testTwoQueries() {
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
for (boolean commit : new boolean[] {true, false}) {
executeRandomQuery(connection);
executeRandomQuery(connection);
if (commit) {
connection.commit();
} else {
connection.rollback();
}
}
connection.commit();

assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
assertFalse(mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0).hasTransaction());
assertFalse(mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(1).hasTransaction());
assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
assertFalse(mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0).hasTransaction());
assertFalse(mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(1).hasTransaction());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the second query has a transaction? I would expect both of them to be using a singleUse (because there were no writes)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you misread the assertion... Or I'm missing something. The line says assertFalse(... hasTransaction()).

So your assumption is right. Both are single-use read-only.

assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));

mockSpanner.clearRequests();
}
}
}

Expand All @@ -203,12 +201,7 @@ public void testQueryFollowedByDml() {
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.executeUpdate(INSERT_STATEMENT);
connection.commit();

Expand All @@ -230,12 +223,7 @@ public void testDmlFollowedByQuery() {
connection.setDelayTransactionStartUntilFirstWrite(true);

connection.executeUpdate(INSERT_STATEMENT);
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.commit();

assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
Expand All @@ -256,12 +244,7 @@ public void testQueryFollowedByBatchDml() {
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.executeBatchUpdate(ImmutableList.of(INSERT_STATEMENT, INSERT_STATEMENT));
connection.commit();

Expand All @@ -285,12 +268,7 @@ public void testBatchDmlFollowedByQuery() {
connection.setDelayTransactionStartUntilFirstWrite(true);

connection.executeBatchUpdate(ImmutableList.of(INSERT_STATEMENT, INSERT_STATEMENT));
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.commit();

assertEquals(1, mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class));
Expand All @@ -313,12 +291,7 @@ public void testQueryFollowedByDmlReturning() {
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
try (ResultSet resultSet =
connection.executeQuery(
dialect == Dialect.POSTGRESQL
Expand Down Expand Up @@ -358,12 +331,7 @@ public void testDmlReturningFollowedByQuery() {
// ignore
}
}
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.commit();

assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
Expand All @@ -384,12 +352,7 @@ public void testQueryFollowedByMutations() {
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
// Mutations don't start a transaction, as they are only included in the commit call anyways.
connection.bufferedWrite(
Mutation.newInsertOrUpdateBuilder("foo").set("id").to(1L).set("value").to("one").build());
Expand All @@ -412,12 +375,7 @@ public void testMutationsFollowedByQuery() {
// Mutations don't start a transaction, as they are only included in the commit call anyways.
connection.bufferedWrite(
Mutation.newInsertOrUpdateBuilder("foo").set("id").to(1L).set("value").to("one").build());
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.commit();

assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
Expand All @@ -434,12 +392,7 @@ public void testQueryFollowedByDmlAborted() {
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.executeUpdate(INSERT_STATEMENT);
mockSpanner.abortNextStatement();
connection.commit();
Expand Down Expand Up @@ -473,12 +426,7 @@ public void testDmlFollowedByQueryAborted() {
connection.setDelayTransactionStartUntilFirstWrite(true);

connection.executeUpdate(INSERT_STATEMENT);
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
mockSpanner.abortNextStatement();
connection.commit();

Expand Down Expand Up @@ -517,12 +465,7 @@ public void testDmlFollowedByQueryWithFailedRetry() {
connection.setDelayTransactionStartUntilFirstWrite(true);

connection.executeUpdate(INSERT_STATEMENT);
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
mockSpanner.abortNextStatement();
// Change the results that is returned by the query. This will make the retry fail.
mockSpanner.putStatementResult(
Expand Down Expand Up @@ -567,12 +510,7 @@ public void testQueryFollowedByDmlAborted_RetrySucceedsWithModifiedQueryResults(
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
executeRandomQuery(connection);
connection.executeUpdate(INSERT_STATEMENT);
mockSpanner.abortNextStatement();
// Change the results that is returned by the query. This will not affect the retry, as the
Expand Down Expand Up @@ -603,4 +541,35 @@ public void testQueryFollowedByDmlAborted_RetrySucceedsWithModifiedQueryResults(
assertEquals(2, mockSpanner.countRequestsOfType(CommitRequest.class));
}
}

@Test
public void testRollbackToSavepointWithoutRealTransaction() {
try (Connection connection = createConnection()) {
connection.setDelayTransactionStartUntilFirstWrite(true);

executeRandomQuery(connection);
connection.savepoint("s1");
executeRandomQuery(connection);
connection.rollbackToSavepoint("s1");
executeRandomQuery(connection);

connection.commit();

assertEquals(3, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
assertFalse(mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0).hasTransaction());
assertFalse(mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(1).hasTransaction());
assertFalse(mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(2).hasTransaction());
assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
assertEquals(0, mockSpanner.countRequestsOfType(RollbackRequest.class));
}
}

private void executeRandomQuery(Connection connection) {
try (ResultSet resultSet = connection.executeQuery(SELECT_RANDOM_STATEMENT)) {
//noinspection StatementWithEmptyBody
while (resultSet.next()) {
// ignore
}
}
}
}