New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: stop preparing session on most errors #181
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -469,12 +469,25 @@ public void testDatabaseOrInstanceDoesNotExistOnReplenish() throws Exception { | |
|
||
@Test | ||
public void testPermissionDeniedOnPrepareSession() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case covers the |
||
testExceptionOnPrepareSession( | ||
Status.PERMISSION_DENIED | ||
.withDescription( | ||
"Caller is missing IAM permission spanner.databases.beginOrRollbackReadWriteTransaction on resource") | ||
.asRuntimeException()); | ||
} | ||
|
||
@Test | ||
public void testFailedPreconditionOnPrepareSession() throws Exception { | ||
testExceptionOnPrepareSession( | ||
Status.FAILED_PRECONDITION | ||
.withDescription("FAILED_PRECONDITION: Database is in read-only mode") | ||
.asRuntimeException()); | ||
} | ||
|
||
private void testExceptionOnPrepareSession(StatusRuntimeException exception) | ||
throws InterruptedException { | ||
mockSpanner.setBeginTransactionExecutionTime( | ||
SimulatedExecutionTime.ofStickyException( | ||
Status.PERMISSION_DENIED | ||
.withDescription( | ||
"Caller is missing IAM permission spanner.databases.beginOrRollbackReadWriteTransaction on resource") | ||
.asRuntimeException())); | ||
SimulatedExecutionTime.ofStickyException(exception)); | ||
DatabaseClientImpl dbClient = | ||
(DatabaseClientImpl) | ||
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE)); | ||
|
@@ -503,10 +516,39 @@ public Void run(TransactionContext transaction) throws Exception { | |
return null; | ||
} | ||
}); | ||
fail("missing expected PERMISSION_DENIED exception"); | ||
fail(String.format("missing expected %s exception", exception.getStatus().getCode().name())); | ||
} catch (SpannerException e) { | ||
assertThat(e.getErrorCode(), is(equalTo(ErrorCode.PERMISSION_DENIED))); | ||
assertThat(e.getErrorCode(), is(equalTo(ErrorCode.fromGrpcStatus(exception.getStatus())))); | ||
} | ||
// Remove the semi-permanent error condition. Getting a read/write transaction should now | ||
// succeed, and the automatic preparing of sessions should be restarted. | ||
mockSpanner.setBeginTransactionExecutionTime(SimulatedExecutionTime.none()); | ||
dbClient | ||
.readWriteTransaction() | ||
.run( | ||
new TransactionCallable<Void>() { | ||
@Override | ||
public Void run(TransactionContext transaction) throws Exception { | ||
return null; | ||
} | ||
}); | ||
for (int i = 0; i < spanner.getOptions().getSessionPoolOptions().getMinSessions(); i++) { | ||
dbClient.pool.getReadSession().close(); | ||
} | ||
int expectedPreparedSessions = | ||
(int) | ||
Math.ceil( | ||
dbClient.pool.getNumberOfSessionsInPool() | ||
* spanner.getOptions().getSessionPoolOptions().getWriteSessionsFraction()); | ||
watch = watch.reset().start(); | ||
while (watch.elapsed(TimeUnit.SECONDS) < 5 | ||
&& dbClient.pool.getNumberOfAvailableWritePreparedSessions() < expectedPreparedSessions) { | ||
Thread.sleep(1L); | ||
} | ||
assertThat(dbClient.pool.getNumberOfSessionsBeingPrepared(), is(equalTo(0))); | ||
assertThat( | ||
dbClient.pool.getNumberOfAvailableWritePreparedSessions(), | ||
is(equalTo(expectedPreparedSessions))); | ||
} | ||
|
||
/** | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the previous logic, we executed this logic also for PERMISSION_DENIED errors. It doesn't anymore. Is this a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it is still executed for
PERMISSION_DENIED
errors, but that is now covered by the general error handling that also handles other types of errors. The specific errorsDatabaseNotFound
andInstanceNotFound
are handled specifically, as these should not allow the normal operation of the session pool to be restarted once the error does no longer occur. The reasoning behind that is that aDatabaseNotFound
exception can only be fixed by creating a new database with the same name as the old one, but that is still a new database.The other errors, such as
PERMISSION_DENIED
and now also for examleFAILED_PRECONDITION
, should allow the session pool to restart the automatic preparing of sessions once the error has been fixed. This could be granting the user the necessary permissions or changing the state of database to fulfill all requirements.