Skip to content

Conversation

@purva9413
Copy link
Contributor

@purva9413 purva9413 commented Jul 2, 2025

Feature to add ReadLockMode option for ReadWrite transactions only. Two ways users can set the option -

  1. Set it on the base transactionOption array like any other txn option. This provides consistency with how our other option configurations work
  2. Set it directly on the readWrite options inside the transactionOption array. This provides flexibility for the user since the readLockMode is a special nested option inside of the readWrite.

Some comment changes to be in accordance with this new feature and unit tests have been added.

@purva9413 purva9413 force-pushed the spanner-occ branch 4 times, most recently from 4ef3de8 to 5d0b91c Compare July 10, 2025 19:28
@purva9413 purva9413 marked this pull request as ready for review July 10, 2025 20:17
@purva9413 purva9413 requested review from a team as code owners July 10, 2025 20:17
@purva9413
Copy link
Contributor Author

Opening this up for review to get early feedback. The style failures seem to be for already existing code lines in the unit test class, so not sure if I need to do anything there. Will follow up on that.

Copy link
Contributor

@bshaffer bshaffer left a comment

Choose a reason for hiding this comment

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

This looks great!

As mentioned in the team meeting, I don't want to support the nested version of this option. We should only document and support one way to use it. If it's usable the other "nested" way, that's fine, but we don't need to test it and support it specifically.

Can you confirm that this option should NOT be supported in the other places TransactionOptions is used? This PR adds support for Database::runTransaction and Database::transaction, but #7749 shows that transaction options are used in the following methods as well:

  • Database::executePartitionedUpdate
  • Operation::transaction (this is called by Database::transaction)
  • Operation::execute
  • Operation::executeUpdate
  • Database::batchUpdate

Comment on lines 156 to 163
// The `readLockMode` can be set on the base options following convention or as a nested option if need be
if (isset($options['readLockMode']) || isset($options['readWrite']['readLockMode'])) {
$readLockModeOption = $options['readLockMode'] ?? $options['readWrite']['readLockMode'];
$transactionOptions['readWrite']['readLockMode'] = $readLockModeOption;
}
Copy link
Contributor

@bshaffer bshaffer Jul 10, 2025

Choose a reason for hiding this comment

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

Let's only support it on the top level to keep it simple

Suggested change
// The `readLockMode` can be set on the base options following convention or as a nested option if need be
if (isset($options['readLockMode']) || isset($options['readWrite']['readLockMode'])) {
$readLockModeOption = $options['readLockMode'] ?? $options['readWrite']['readLockMode'];
$transactionOptions['readWrite']['readLockMode'] = $readLockModeOption;
}
// The `readLockMode` can be set on the base options following convention or as a nested option if need be
if (isset($options['readLockMode'])) {
$transactionOptions['readWrite']['readLockMode'] = $options['readLockMode']
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we discussed keeping this officially documented as a option settable on the top level. But if someone wants to set it in the nested way they can? If we remove this condition, and someone tries to set it in the nested way it will not set the option at all as we are creating an empty readWrite array at the beginning of this function.

Let me know if that makes sense or we still want to let users only set this as a top level option (having that as the only choice might cause some confusion if folks go in and check the way the ReadLockMode is setup as a nested option in our compiled proto).

@purva9413 purva9413 force-pushed the spanner-occ branch 3 times, most recently from 19d250d to 4d3cea5 Compare July 17, 2025 19:05
@purva9413 purva9413 requested a review from bshaffer July 17, 2025 19:07
@purva9413 purva9413 added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Jul 23, 2025
@purva9413 purva9413 force-pushed the spanner-occ branch 2 times, most recently from 230d1e7 to 1aae26c Compare July 23, 2025 23:32
Copy link
Contributor

@bshaffer bshaffer left a comment

Choose a reason for hiding this comment

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

looks good! All small suggestions except for one regarding DX

$transaction = $this->prophesize(Transaction::class)->reveal();

$operation->executeUpdate($this->session, $transaction, $sql, [
'transaction' => ['begin' => ['readWrite' => [],'readLockMode' => $expectedReadLockMode,]]
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we supplying 'readWrite' => [] here? Is it not sufficient to supply the readLockMode option? This seems like a weird API... supplying an empty array to readWrite does not make sense to me from a DX standpoint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds reasonable, agreed it is probably not the best way to go about in terms of dev experience. I have removed the 'readWrite' => []. But in order to now format the readLockMode correctly and only for readWrite transactions, I have added a condition into Grpc::formatTransactionOptions(). Lmk if that looks good.

bshaffer
bshaffer previously approved these changes Aug 14, 2025
Copy link
Contributor

@bshaffer bshaffer left a comment

Choose a reason for hiding this comment

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

great work! This is still marked as "do not merge", but it's approved on my end.

@bshaffer bshaffer added the api: spanner Issues related to the Spanner API. label Oct 2, 2025
@purva9413 purva9413 requested a review from a team as a code owner October 2, 2025 18:34
@bshaffer bshaffer added the next release PRs to be included in the next release label Oct 2, 2025
@snippet-bot
Copy link

snippet-bot bot commented Oct 2, 2025

No region tags are edited in this PR.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

tests: Add in unit tests for ReadLockMode option

chore: Fix lint issues

chore: Fix lint issues

chore: Fix lint issues

feat: Add ability to set this ReadLockMode through operations

chore: Cleanup code and remove debug logging

chore: Lint fixes

chore: Fix style warnings

chore: Review comment resolution
@purva9413 purva9413 removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Oct 2, 2025
@Hectorhammett Hectorhammett merged commit 8a3db2c into googleapis:main Oct 3, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. next release PRs to be included in the next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants