Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces dynamic database-level metadata detection (dialect, default transaction isolation level, and default read lock mode) queried from INFORMATION_SCHEMA.DATABASE_OPTIONS to replace client-side hardcoded defaults. The feedback identifies several critical issues: a correctness bug in canEnableLRYW that breaks Leader-Routed Read-Your-Writes for repeatable read transactions with unspecified lock modes, a race condition in metadataSupplier when retrieving the session reference, and a concurrency race condition in AbstractLazyInitializer between reset() and get(). Additionally, it is recommended to add a unit test to prevent regressions for repeatable reads with null lock modes.
374c33b to
9b23481
Compare
228b637 to
d7c22d3
Compare
aad21ed to
ae4280e
Compare
…metadata isolation levels and lock modes
|
@shobhitsg Is this still relevant? If not, can we close it? |
Yes, read lock mode is still not being cached, and we need to make some changes to accommodate that. I'll update this PR. Changing it to draft. |
Take the main branch version of the database metadata code, which already caches the dialect and default isolation level with a single query. The read lock mode and routing support is re-applied on top of it in the next commit.
…t replica Read the default_read_lock_mode database option together with the dialect and default_transaction_isolation, and expose it on DatabaseClient. Read the metadata in the background whenever a multiplexed session is created or replaced, and attach it to the session. Read/write transactions on a multiplexed session resolve their effective read lock mode from the transaction options, the client defaults, and the database defaults, in that order. Transactions with an effective OPTIMISTIC lock mode (set explicitly, or implied by REPEATABLE_READ) no longer set the route-to-leader header, so all requests except Commit can be served by the nearest replica (Local Read-Your-Writes). The decision is made once per transaction attempt, so a retry picks up fresh metadata. Unknown or missing metadata keeps routing to the leader. The mock server ignores simulated execution times and errors for the metadata query by default, like it already ignores the query in the recorded requests, so tests do not have to account for the background query. Also fix the single-use transaction counter that was decremented without being incremented when the grpc-gcp extension is enabled.
Run the database metadata query on the session directly, so it is not counted as an acquired/released session and does not claim a single-use channel hint. This removes the need for tests to reset session counters. Also drop the unused getDatabaseDefaultReadLockModeValue() and two end-to-end routing tests that duplicate TransactionRoutingTest.
When a multiplexed session is created or replaced, the client library now automatically executes an asynchronous gRPC query (
SELECT ... FROM INFORMATION_SCHEMA.DATABASE_OPTIONS) to cache database isolation levels and default read lock modes for Local Read-Your-Writes (LRYW) routing. Because this query executes via the standard query pipeline (singleUse().executeQuery(...)), it emits normal telemetry:Spanner.ExecuteStreamingSqlandCloudSpannerOperation.ExecuteStreamingQueryspan.Tests that strictly assert exact RPC invocation counts, channel allocations, and OpenTelemetry/OpenCensus span arrays (particularly under Java 8 where reflection-heavy tracer tests run) required updates to account for this automated warmup query.
Database metadata introspection is kept asynchronous during session creation to avoid adding network latency to customer transactions in production. In test suites, background task completion is synchronized using
client.getDialect()and telemetry assertions are updated to account for the metadata query, avoiding test-specific flags in production code.Alternative Approaches
disableMetadataIntrospection)SessionPoolOptionsor a system property switch to suppress the metadata query during unit tests so test files wouldn't need updating one by one.@Before setUp()setUp()and callspanExporter.reset()right before individual test methods execute to erase startup query spans.SpanTestandOpenTelemetrySpanTestexplicitly assert that the test's initial transaction generates theCreateMultiplexedSessionspan and annotation. Erasing spans in setup destroyed this required session-creation span, breaking assertions.