Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Apr 7, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 141
fix: Retry creation of multiplexed session#4288
Merged
sakthivelmanii
merged 2 commits into
main
from
retry_multiplexed_session_till_session_wait_timeJan 7, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
77 changes: 59 additions & 18 deletions
77 ...loud-spanner/src/main/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -40,6 +40,7 @@ | ||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.BitSet; | ||
| import java.util.EnumSet; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutionException; | ||
| @@ -262,6 +263,9 @@ public void close() { | ||
| */ | ||
| private static final Map<SpannerImpl, BitSet> CHANNEL_USAGE = new HashMap<>(); | ||
| private static final EnumSet<ErrorCode> RETRYABLE_ERROR_CODES = | ||
| EnumSet.of(ErrorCode.DEADLINE_EXCEEDED, ErrorCode.RESOURCE_EXHAUSTED, ErrorCode.UNAVAILABLE); | ||
| private final BitSet channelUsage; | ||
| private final int numChannels; | ||
| @@ -358,11 +362,19 @@ public void close() { | ||
| SettableApiFuture.create(); | ||
| this.readWriteBeginTransactionReferenceFuture = SettableApiFuture.create(); | ||
| this.multiplexedSessionReference = new AtomicReference<>(initialSessionReferenceFuture); | ||
| asyncCreateMultiplexedSession(initialSessionReferenceFuture); | ||
| maybeWaitForSessionCreation( | ||
| sessionClient.getSpanner().getOptions().getSessionPoolOptions(), | ||
| initialSessionReferenceFuture); | ||
| } | ||
| private void asyncCreateMultiplexedSession( | ||
| SettableApiFuture<SessionReference> sessionReferenceFuture) { | ||
| this.sessionClient.asyncCreateMultiplexedSession( | ||
| new SessionConsumer() { | ||
| @Override | ||
| public void onSessionReady(SessionImpl session) { | ||
| initialSessionReferenceFuture.set(session.getSessionReference()); | ||
| sessionReferenceFuture.set(session.getSessionReference()); | ||
| // only start the maintainer if we actually managed to create a session in the first | ||
| // place. | ||
| maintainer.start(); | ||
| @@ -395,33 +407,62 @@ public void onSessionCreateFailure(Throwable t, int createFailureForSessionCount | ||
| // Mark multiplexes sessions as unimplemented and fall back to regular sessions if | ||
| // UNIMPLEMENTED is returned. | ||
| maybeMarkUnimplemented(t); | ||
| initialSessionReferenceFuture.setException(t); | ||
| sessionReferenceFuture.setException(t); | ||
| } | ||
| }); | ||
| maybeWaitForSessionCreation( | ||
| sessionClient.getSpanner().getOptions().getSessionPoolOptions(), | ||
| initialSessionReferenceFuture); | ||
| } | ||
| void setPool(SessionPool pool) { | ||
| this.pool = pool; | ||
| } | ||
| private static void maybeWaitForSessionCreation( | ||
| SessionPoolOptions sessionPoolOptions, ApiFuture<SessionReference> future) { | ||
| private void maybeWaitForSessionCreation( | ||
| SessionPoolOptions sessionPoolOptions, | ||
| SettableApiFuture<SessionReference> initialSessionReferenceFuture) { | ||
| Duration waitDuration = sessionPoolOptions.getWaitForMinSessions(); | ||
| if (waitDuration != null && !waitDuration.isZero()) { | ||
| long timeoutMillis = waitDuration.toMillis(); | ||
| try { | ||
| future.get(timeoutMillis, TimeUnit.MILLISECONDS); | ||
| } catch (ExecutionException executionException) { | ||
| throw SpannerExceptionFactory.asSpannerException(executionException.getCause()); | ||
| } catch (InterruptedException interruptedException) { | ||
| throw SpannerExceptionFactory.propagateInterrupt(interruptedException); | ||
| } catch (TimeoutException timeoutException) { | ||
| throw SpannerExceptionFactory.newSpannerException( | ||
| ErrorCode.DEADLINE_EXCEEDED, | ||
| "Timed out after waiting " + timeoutMillis + "ms for multiplexed session creation"); | ||
| SpannerException lastException = null; | ||
| SettableApiFuture<SessionReference> sessionReferenceFuture = initialSessionReferenceFuture; | ||
| Duration remainingTime; | ||
| Instant endTime = Instant.now().plus(waitDuration); | ||
| while ((remainingTime = Duration.between(Instant.now(), endTime)).toMillis() > 0) { | ||
| // If any exception is thrown, then retry the multiplexed session creation | ||
| if (sessionReferenceFuture == null) { | ||
| sessionReferenceFuture = SettableApiFuture.create(); | ||
| asyncCreateMultiplexedSession(sessionReferenceFuture); | ||
| this.multiplexedSessionReference.set(sessionReferenceFuture); | ||
| } | ||
| try { | ||
| sessionReferenceFuture.get(remainingTime.toMillis(), TimeUnit.MILLISECONDS); | ||
| lastException = null; | ||
| break; | ||
| } catch (ExecutionException executionException) { | ||
| lastException = SpannerExceptionFactory.asSpannerException(executionException.getCause()); | ||
| } catch (InterruptedException interruptedException) { | ||
| lastException = SpannerExceptionFactory.propagateInterrupt(interruptedException); | ||
| } catch (TimeoutException timeoutException) { | ||
| lastException = | ||
| SpannerExceptionFactory.newSpannerException( | ||
| ErrorCode.DEADLINE_EXCEEDED, | ||
| "Timed out after waiting " | ||
| + waitDuration.toMillis() | ||
| + "ms for multiplexed session creation"); | ||
| } | ||
| // if any exception is thrown, then set the session reference to null to retry the | ||
| // multiplexed session creation only if the error code is DEADLINE EXCEEDED, UNAVAILABLE or | ||
| // RESOURCE_EXHAUSTED | ||
| if (RETRYABLE_ERROR_CODES.contains(lastException.getErrorCode())) { | ||
| sessionReferenceFuture = null; | ||
| } else { | ||
| break; | ||
| } | ||
sakthivelmanii marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| // if the wait time elapsed and multiplexed session fetch failed then throw the last exception | ||
| // that we have received | ||
| if (lastException != null) { | ||
| throw lastException; | ||
| } | ||
| } | ||
| } | ||
8 changes: 4 additions & 4 deletions
8 google-cloud-spanner/src/test/java/com/google/cloud/spanner/MockSpannerServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.