Skip to content

chore: normalize query param keys to lowercase in KeyRecipe and fix lock contention in KeyRangeCache#4380

Merged
rahul2393 merged 2 commits intomainfrom
fix-query-param-case
Mar 11, 2026
Merged

chore: normalize query param keys to lowercase in KeyRecipe and fix lock contention in KeyRangeCache#4380
rahul2393 merged 2 commits intomainfrom
fix-query-param-case

Conversation

@rahul2393
Copy link
Contributor

@rahul2393 rahul2393 commented Mar 9, 2026

Fixes: b/482811543

KeyRecipe.queryParamsToTargetRange — case-insensitive parameter lookup

The server sends routing recipe identifiers (e.g. "id") that may differ in case from the query parameter names bound by the user (e.g. "Id" or "ID"). Without normalization, the routing key lookup silently missed, causing all requests to fall back to the default endpoint instead of being routed to the optimal server.

Fix: both the recipe identifier and the user's parameter keys are now normalized with toLowerCase(Locale.ROOT) before the HashMap lookup, so case differences no longer cause routing misses.

KeyRangeCache.CachedGroup.fillRoutingHint — avoid holding lock during channel creation

Previously, CachedGroup.fillRoutingHint was a synchronized method, which meant the group lock was held for the entire duration including the call to endpointCache.get(address). That call can block on a network dial when creating a new gRPC channel for the first time, serializing all concurrent routing decisions on the same group.

Fix: the lock is now released before calling endpointCache.get(). The tablet selection (cheap, in-memory) still runs
under the lock. If the endpoint is already cached, it is returned directly without ever releasing the lock (fast path).
If a new channel needs to be dialed, the address is captured, the lock is released, the channel is created, and the lock is re-acquired to update the tablet's cached endpoint atomically — only if the address hasn't changed in the meantime

@rahul2393 rahul2393 requested review from a team as code owners March 9, 2026 06:50
@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 9, 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 KeyRecipe class by introducing case-insensitive matching for query parameter keys. It achieves this by normalizing all incoming query parameter keys to lowercase before processing, which improves the robustness and user-friendliness of key resolution by accommodating variations in key casing. The change includes necessary utility imports and is thoroughly covered by new unit tests.

Highlights

  • Case-Insensitive Query Parameter Keys: Implemented logic within the KeyRecipe class to normalize query parameter keys to lowercase, enabling case-insensitive matching when resolving target ranges.
  • Duplicate Key Handling: Ensured that if multiple query parameters with keys that normalize to the same lowercase value are provided, the value associated with the last occurrence of that key is used.
  • Comprehensive Testing: Added new unit tests to validate the case-insensitive behavior for query parameters and to confirm the correct handling of duplicate keys with varying casing.
Changelog
  • google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/KeyRecipe.java
    • Implemented case-insensitive lookup for query parameter keys in queryParamsToTargetRange.
    • Added necessary utility imports for HashMap, Locale, and Map.
  • google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/KeyRecipeTest.java
    • Added assertFalse import.
    • Introduced queryParamsCaseInsensitiveFallback test to verify case-insensitive matching.
    • Introduced queryParamsCaseInsensitiveDuplicateUsesLastValue test to verify handling of duplicate keys.
    • Added expectedTagOnlyKey helper method for test setup.
Activity
  • No specific activity has been recorded for this pull request yet.
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.

@rahul2393 rahul2393 requested a review from olavloite March 9, 2026 06:51
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 correctly implements case-insensitive handling for query parameter keys in KeyRecipe by normalizing them to lowercase. The accompanying tests effectively validate this new logic, including edge cases with duplicate keys. I have one minor suggestion to remove a newly added but unused helper method in the test file to improve code clarity.

@rahul2393 rahul2393 force-pushed the fix-query-param-case branch from ef1e1de to cd9b0a7 Compare March 9, 2026 06:55
tablets = newTablets;
}

synchronized ChannelEndpoint fillRoutingHint(
Copy link
Contributor Author

@rahul2393 rahul2393 Mar 9, 2026

Choose a reason for hiding this comment

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

@rahul2393 rahul2393 changed the title chore: normalize query param keys to lowercase in KeyRecipe chore: normalize query param keys to lowercase in KeyRecipe and fix lock contention in KeyRangeCache Mar 9, 2026
@rahul2393
Copy link
Contributor Author

/gemini review

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 two valuable changes. First, it addresses a lock contention issue in KeyRangeCache by refactoring fillRoutingHint to avoid holding a lock during potentially blocking network operations. The implementation correctly uses a pattern similar to double-checked locking to ensure thread safety and handle race conditions. Second, it fixes a bug in KeyRecipe by normalizing query parameter keys to lowercase, enabling case-insensitive lookups. This change is accompanied by thorough unit tests covering various scenarios, including edge cases. Both changes are well-implemented and improve the robustness and performance of the code.

@rahul2393 rahul2393 merged commit f91dbba into main Mar 11, 2026
32 of 33 checks passed
@rahul2393 rahul2393 deleted the fix-query-param-case branch March 11, 2026 03:42
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 googleapis/java-spanner API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants