Skip to content
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

feat: support getting the commit timestamp of a transaction or statement #52

Merged
merged 3 commits into from
Oct 19, 2021

Conversation

olavloite
Copy link
Collaborator

Cloud Spanner returns the commit timestamp of a read/write transaction as a part of the commit response.
This PR adds support for getting that timestamp in two ways:

  1. Through a CommitTimestamp() method on the SpannerConn interface.
  2. Through the SHOW VARIABLE COMMIT_TIMESTAMP client side statement.

Cloud Spanner returns the commit timestamp of a read/write transaction as a part of the commit response.
This PR adds support for getting that timestamp in two ways:
1. Through a CommitTimestamp() method on the SpannerConn interface.
2. Through the SHOW VARIABLE COMMIT_TIMESTAMP client side statement.
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Oct 13, 2021
} {
c.commitTs = test.wantValue

it, err := s.ShowCommitTimestamp(ctx, c, "", nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this commit timestamp is from the previous successful txn, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct

},
retryAborts: c.retryAborts,
}
c.commitTs = nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do we clear commitTs here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The commit timestamp is cleared when a new transaction is started to avoid any confusion regarding whether the commit timestamp is related to the current transaction or the one before. So if you try to get the commit timestamp while in a transaction that has not yet committed, you will get an error or NULL instead of the commit timestamp of the previous transaction.
This is also consistent with the behavior of the JDBC driver.

if err != nil {
t.Fatalf("failed to get a connection: %v", err)
}
if _, err := conn.ExecContext(ctx, testutil.UpdateBarSetFoo); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this will do an autocommit, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that is correct. If you execute an update (or any other statement) on a connection that has no active transaction, the statement will be executed in autocommit mode. That means that:

  1. Update statements will automatically begin an implicit read/write transaction and commit that transaction directly after the update statement if the update was successful.
  2. Queries will automatically use a single-use read-only transaction, as that is the most efficient way of executing a single query.

Copy link
Collaborator

@hengfengli hengfengli left a comment

Choose a reason for hiding this comment

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

LGTM.

@olavloite olavloite merged commit 802e7be into main Oct 19, 2021
@olavloite olavloite deleted the commit-timestamp branch October 19, 2021 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants