GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Spanner: Create new instance if existing Spanner is closed by olavloite · Pull Request #5200 · googleapis/google-cloud-java

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.

Sign up for GitHub

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

Uh oh!

There was an error while loading. Please reload this page.

Merged

Spanner: Create new instance if existing Spanner is closed #5200

Show file tree
Hide file tree
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 May 15, 2019
599535f
fix small merge error
olavloite May 15, 2019
9bbd0e2
create a new instance if the service/rpc is closed
olavloite May 18, 2019
5561b8e
add test case to ensure correct caching behavior
olavloite Jun 14, 2019
d273479
use shouldRefreshService instead of createNewService
olavloite Jun 15, 2019
fb2a4db
fix merge conflicts
olavloite Jun 15, 2019
5fbb912
added documentation to shouldRefresh... methods
olavloite Jun 17, 2019
7692138
removed overrides only for comments
olavloite Jun 21, 2019
b8bf651
fixed naming
olavloite Jun 26, 2019
2cc0635
added assertions for isClosed()
olavloite Jun 26, 2019
1ca1c92
fixed formatting
olavloite Jun 27, 2019
File filter
Filter by extension

Clear filters
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

20 changes: 18 additions & 2 deletions 20 google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -494,24 +494,40 @@ static String getServiceAccountProjectId(String credentialsPath) {
*/
@SuppressWarnings("unchecked")
public ServiceT getService() {
if (service == null) {
if (shouldRefreshService(service)) {
service = serviceFactory.create((OptionsT) this);
}
return service;
}

/**
* @param cachedService The currently cached service object
* @return true if the currently cached service object should be refreshed.
*/
protected boolean shouldRefreshService(ServiceT cachedService) {
Comment thread

@chingor13 chingor13 Jun 28, 2019

Copy link
Copy Markdown
Contributor

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 shouldRefreshRpc be 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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@olavloite olavloite Jun 29, 2019

Copy link
Copy Markdown
Contributor Author

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 Service and ServiceRpc classes are not closeable and therefore only does a cachedService == null check.

The Spanner class (a subclass of Service) implements AutoCloseable and SpannerOptions therefore implements a custom check in the shouldRefreshService() method that calls Spanner#isClosed() instead of the cachedService == null check.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
return cachedService == null;
}

/**
* Returns a Service RPC object for the current service. For instance, when using Google Cloud
* Storage, it returns a StorageRpc object.
*/
@SuppressWarnings("unchecked")
public ServiceRpc getRpc() {
if (rpc == null) {
if (shouldRefreshRpc(rpc)) {
rpc = serviceRpcFactory.create((OptionsT) this);
}
return rpc;
}

/**
* @param cachedRpc The currently cached service object
* @return true if the currently cached service object should be refreshed.
*/
protected boolean shouldRefreshRpc(ServiceRpc cachedRpc) {
return cachedRpc == null;
}

/**
* Returns the project ID. Return value can be null (for services that don't require a project
* ID).
Expand Down
3 changes: 3 additions & 0 deletions 3 ...le-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Spanner.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ public interface Spanner extends Service<SpannerOptions>, AutoCloseable {
*/
@Override
void close();

/** @return <code>true</code> if this {@link Spanner} object is closed. */
boolean isClosed();
}
5 changes: 5 additions & 0 deletions 5 ...loud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ public void close() {
}
}

@Override
public boolean isClosed() {
return spannerIsClosed;
}

/**
* Checks that the current context is still valid, throwing a CANCELLED or DEADLINE_EXCEEDED error
* if not.
Expand Down
20 changes: 20 additions & 0 deletions 20 ...d-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,26 @@ protected SpannerRpc getSpannerRpcV1() {
return (SpannerRpc) getRpc();
}

/**
* @return <code>true</code> if the cached Spanner service instance is <code>null</code> or
* closed. This will cause the method {@link #getService()} to create a new {@link SpannerRpc}
* instance when one is requested.
*/
@Override
protected boolean shouldRefreshService(Spanner cachedService) {
return cachedService == null || cachedService.isClosed();
}

/**
* @return <code>true</code> if the cached {@link ServiceRpc} instance is <code>null</code> or
* closed. This will cause the method {@link #getRpc()} to create a new {@link Spanner}
* instance when one is requested.
*/
@Override
protected boolean shouldRefreshRpc(ServiceRpc cachedRpc) {
return cachedRpc == null || ((SpannerRpc) cachedRpc).isClosed();
}

@SuppressWarnings("unchecked")
@Override
public Builder toBuilder() {
Expand Down
7 changes: 7 additions & 0 deletions 7 ...s/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ private synchronized void shutdown() {
private static final int DEFAULT_PERIOD_SECONDS = 10;

private final ManagedInstantiatingExecutorProvider executorProvider;
private boolean rpcIsClosed;
private final SpannerStub spannerStub;
private final InstanceAdminStub instanceAdminStub;
private final DatabaseAdminStub databaseAdminStub;
Expand Down Expand Up @@ -600,13 +601,19 @@ private GrpcCallContext newCallContext(@Nullable Map<Option, ?> options, String

@Override
public void shutdown() {
this.rpcIsClosed = true;
this.spannerStub.close();
this.instanceAdminStub.close();
this.databaseAdminStub.close();
this.spannerWatchdog.shutdown();
this.executorProvider.shutdown();
}

@Override
public boolean isClosed() {
return rpcIsClosed;
}

/**
* A {@code ResponseObserver} that exposes the {@code StreamController} and delegates callbacks to
* the {@link ResultStreamConsumer}.
Expand Down
2 changes: 2 additions & 0 deletions 2 ...lients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/SpannerRpc.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,6 @@ PartitionResponse partitionRead(PartitionReadRequest request, @Nullable Map<Opti
throws SpannerException;

public void shutdown();

boolean isClosed();
}
42 changes: 42 additions & 0 deletions 42 ...-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
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 static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

import com.google.api.core.NanoClock;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceRpc;
import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import java.util.HashMap;
Expand Down Expand Up @@ -122,4 +126,42 @@ public void getDbclientAfterCloseThrows() {
assertThat(e.getMessage()).contains("Cloud Spanner client has been closed");
}
}

@Test
public void testSpannerClosed() throws InterruptedException {
SpannerOptions options = createSpannerOptions();
Spanner spanner1 = options.getService();
Spanner spanner2 = options.getService();
ServiceRpc rpc1 = options.getRpc();
ServiceRpc rpc2 = options.getRpc();
// The SpannerOptions object should return the same instance.
assertThat(spanner1 == spanner2, is(true));
assertThat(rpc1 == rpc2, is(true));
spanner1.close();
// A new instance should be returned as the Spanner instance has been closed.
Spanner spanner3 = options.getService();
assertThat(spanner1 == spanner3, is(false));
// A new instance should be returned as the Spanner instance has been closed.
ServiceRpc rpc3 = options.getRpc();
assertThat(rpc1 == rpc3, is(false));
// Creating a copy of the SpannerOptions should result in new instances.
options = options.toBuilder().build();
Spanner spanner4 = options.getService();
ServiceRpc rpc4 = options.getRpc();
assertThat(spanner4 == spanner3, is(false));
assertThat(rpc4 == rpc3, is(false));
Spanner spanner5 = options.getService();
ServiceRpc rpc5 = options.getRpc();
assertThat(spanner4 == spanner5, is(true));
assertThat(rpc4 == rpc5, is(true));
spanner3.close();
spanner4.close();
}

private SpannerOptions createSpannerOptions() {
return SpannerOptions.newBuilder()
.setProjectId("[PROJECT]")
.setCredentials(NoCredentials.getInstance())
.build();
}
}
29 changes: 29 additions & 0 deletions 29 ...ients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Comment thread

@kolea2 kolea2 Jun 25, 2019

Copy link
Copy Markdown
Contributor

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.

for completeness and coverage, can you add in a few assertions like:
assertThat(spanner1.isClosed()).isFalse();

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@olavloite olavloite Jun 26, 2019

Copy link
Copy Markdown
Contributor Author

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.

Added assertions.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.