-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Spanner: Create new instance if existing Spanner is closed #5200
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
Merged
kolea2
merged 11 commits into
googleapis:master
from
olavloite:spanner-create-new-instance-if-close
Jul 1, 2019
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a89f89
do not hand out a closed Spanner instance
olavloite 599535f
fix small merge error
olavloite 9bbd0e2
create a new instance if the service/rpc is closed
olavloite 5561b8e
add test case to ensure correct caching behavior
olavloite d273479
use shouldRefreshService instead of createNewService
olavloite fb2a4db
fix merge conflicts
olavloite 5fbb912
added documentation to shouldRefresh... methods
olavloite 7692138
removed overrides only for comments
olavloite b8bf651
fixed naming
olavloite 2cc0635
added assertions for isClosed()
olavloite 1ca1c92
fixed formatting
olavloite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -17,10 +17,13 @@ | |
| package com.google.cloud.spanner; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.junit.Assert.assertThat; | ||
|
|
||
| import com.google.api.gax.retrying.RetrySettings; | ||
| import com.google.api.gax.rpc.ServerStreamingCallSettings; | ||
| import com.google.api.gax.rpc.UnaryCallSettings; | ||
| import com.google.cloud.NoCredentials; | ||
| import com.google.cloud.TransportOptions; | ||
| import com.google.cloud.spanner.admin.database.v1.stub.DatabaseAdminStubSettings; | ||
| import com.google.cloud.spanner.admin.instance.v1.stub.InstanceAdminStubSettings; | ||
|
|
@@ -353,4 +356,30 @@ public void testNullSessionLabels() { | |
| thrown.expect(NullPointerException.class); | ||
| SpannerOptions.newBuilder().setSessionLabels(null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDoNotCacheClosedSpannerInstance() { | ||
| SpannerOptions options = | ||
| SpannerOptions.newBuilder() | ||
| .setProjectId("[PROJECT]") | ||
| .setCredentials(NoCredentials.getInstance()) | ||
| .build(); | ||
| // Getting a service twice should give the same instance. | ||
| Spanner service1 = options.getService(); | ||
|
Contributor
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. for completeness and coverage, can you add in a few assertions like:
Author
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. Added assertions. |
||
| Spanner service2 = options.getService(); | ||
| assertThat(service1 == service2, is(true)); | ||
| assertThat(service1.isClosed()).isFalse(); | ||
| // Closing a service instance should cause the SpannerOptions to create a new service. | ||
| service1.close(); | ||
| Spanner service3 = options.getService(); | ||
| assertThat(service3 == service1, is(false)); | ||
| assertThat(service1.isClosed()).isTrue(); | ||
| assertThat(service3.isClosed()).isFalse(); | ||
| ; | ||
| // Getting another service from the SpannerOptions should return the new cached instance. | ||
| Spanner service4 = options.getService(); | ||
| assertThat(service3 == service4, is(true)); | ||
| assertThat(service3.isClosed()).isFalse(); | ||
| service3.close(); | ||
| } | ||
| } | ||
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.
Should this and
shouldRefreshRpcbe static?It looks like we're not using anything from the instance but we're allowing for that possibility in the future. If so, should we instead provide a protected getter for the cached service/rpc?
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.
The idea is to let these two methods be overridable. The base
ServiceandServiceRpcclasses are not closeable and therefore only does acachedService == nullcheck.The
Spannerclass (a subclass ofService) implementsAutoCloseableandSpannerOptionstherefore implements a custom check in theshouldRefreshService()method that callsSpanner#isClosed()instead of thecachedService == nullcheck.