Skip to content

feat: make transaction retry configurable#4370

Draft
olavloite wants to merge 2 commits intomainfrom
transaction-retry-config
Draft

feat: make transaction retry configurable#4370
olavloite wants to merge 2 commits intomainfrom
transaction-retry-config

Conversation

@olavloite
Copy link
Collaborator

[Do not merge, just a prototype]

@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: spanner Issues related to the googleapis/java-spanner API. labels Mar 2, 2026
@olavloite olavloite added do not merge Indicates a pull request not ready for merge, due to either quality or timing. and removed api: spanner Issues related to the googleapis/java-spanner API. labels Mar 2, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Google Cloud Spanner client library by making transaction retry mechanisms configurable. It refactors the core retry logic into a dedicated, instance-based helper and integrates new options into SpannerOptions, providing greater control over how aborted transactions are handled and retried. This change aims to improve flexibility for developers managing transaction resilience.

Highlights

  • Configurable Transaction Retries: Introduced the ability to configure retry settings for aborted transactions, allowing users to customize retry behavior through SpannerOptions.
  • Refactored Retry Logic: The static SpannerRetryHelper class was renamed to TransactionRetryHelper and refactored into an instance-based class, enabling it to hold specific RetrySettings.
  • Default Retry Settings: Defined DEFAULT_TRANSACTION_RETRY_SETTINGS in SpannerOptions, which links to the rollbackSettings of SpannerStubSettings and sets a 24-hour total timeout with no maximum attempts for transaction retries.
  • Integration with Spanner Components: Updated SessionImpl and TransactionRunnerImpl to utilize the new instance-based TransactionRetryHelper obtained from SpannerImpl.
Changelog
  • google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionImpl.java
    • Updated the writeAtLeastOnceWithOptions method to use the new instance-based TransactionRetryHelper from the spanner object instead of the static SpannerRetryHelper.
  • google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java
    • Added a new private field transactionRetryHelper of type TransactionRetryHelper.
    • Initialized transactionRetryHelper in the constructor using SpannerOptions.getDefaultTransactionRetrySettings().
    • Added a public getter method getTransactionRetryHelper() to expose the new helper instance.
  • google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java
    • Added imports for com.google.cloud.spanner.v1.stub.SpannerStub and com.google.spanner.v1.RollbackRequest.
    • Defined a new public static final RetrySettings DEFAULT_TRANSACTION_RETRY_SETTINGS for transaction retries, configured with a 24-hour total timeout and no max attempts, referencing SpannerStubSettings.rollbackSettings().
    • Added a private final field defaultTransactionRetrySettings to the SpannerOptions class.
    • Updated the SpannerOptions constructor to initialize defaultTransactionRetrySettings from the builder.
    • Modified the Builder class to include defaultTransactionRetrySettings, defaulting to TransactionRetryHelper.DEFAULT_TRANSACTION_RETRY_SETTINGS.
    • Updated the Builder copy constructor to include defaultTransactionRetrySettings.
    • Added a public setter method setDefaultTransactionRetrySettings(RetrySettings retrySettings) to the Builder class.
    • Added a public getter method getDefaultTransactionRetrySettings() to the SpannerOptions class.
  • google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerRetryHelper.java
    • Renamed the file from SpannerRetryHelper.java to TransactionRetryHelper.java.
    • Renamed the class from SpannerRetryHelper to TransactionRetryHelper.
    • Refactored the class from a static utility class to an instance-based class, requiring RetrySettings in its constructor.
    • Removed the static txRetrySettings field and its associated Javadoc.
    • Updated all runTxWithRetriesOnAborted methods to be instance methods and use the retrySettings field.
  • google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunnerImpl.java
    • Updated the runInternal method to use the new instance-based TransactionRetryHelper from session.getSpanner().
  • google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerRetryHelperTest.java
    • Renamed the file from SpannerRetryHelperTest.java to TransactionRetryHelperTest.java.
    • Renamed the test class from SpannerRetryHelperTest to TransactionRetryHelperTest.
    • Instantiated TransactionRetryHelper as a field retryHelper using SpannerOptions.DEFAULT_TRANSACTION_RETRY_SETTINGS.
    • Updated all test methods to call retryHelper.runTxWithRetriesOnAborted instead of the static SpannerRetryHelper methods.
Activity
  • The pull request is marked as a prototype and not intended for immediate merge, indicating it's an early draft or proof-of-concept.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces configurability for transaction retry settings by refactoring the retry logic from a static helper class (SpannerRetryHelper) into a new TransactionRetryHelper instance that is configured via SpannerOptions. This is a good improvement for flexibility. The changes are mostly correct, but I found one compilation error that needs to be fixed.

private String experimentalHost = null;
private boolean usePlainText = false;
private TransactionOptions defaultTransactionOptions = TransactionOptions.getDefaultInstance();
private RetrySettings defaultTransactionRetrySettings =
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This line will cause a compilation error. The static field DEFAULT_TRANSACTION_RETRY_SETTINGS was moved from SpannerRetryHelper (now TransactionRetryHelper) to SpannerOptions. You should reference it directly from SpannerOptions.

Suggested change
private RetrySettings defaultTransactionRetrySettings =
private RetrySettings defaultTransactionRetrySettings = DEFAULT_TRANSACTION_RETRY_SETTINGS;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not merge Indicates a pull request not ready for merge, due to either quality or timing. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants