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

Conversation

@olavloite

@olavloite olavloite commented May 18, 2019

Copy link
Copy Markdown
Contributor

SpannerOptions caches any Spanner instance that has been created, and hands this cached instance out to all subsequent calls to SpannerOptions.getService(). This also included closed Spanner instances. The getService() method now creates a new instance if the Spanner instance has already been closed.

Sorry, something went wrong.

Uh oh!

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

All reactions
@olavloite
olavloite requested a review from a team May 18, 2019 12:39
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 18, 2019
@olavloite
olavloite requested review from kolea2 and snehashah16 May 18, 2019 12:39
@olavloite olavloite added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 21, 2019
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 21, 2019
@chingor13 chingor13 assigned kolea2 May 22, 2019
sduskis
sduskis reviewed May 24, 2019
View reviewed changes
Comment thread ...loud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java Outdated
@Override
public Spanner getService() {
Spanner spanner = super.getService();
if (spanner.isClosed()) {

@sduskis sduskis May 24, 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.

I think that the new object needs to be cached. Is there a way to make sure that super.getService()'s object is refreshed? The same comment applies to getRpc()

Sorry, something went wrong.

Uh oh!

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

All reactions

@olavloite olavloite May 24, 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.

Yes, that is done by the method call ServiceOptions.createNewService() on L311 (and on L326 for getRpc()).

Sorry, something went wrong.

Uh oh!

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

All reactions

@sduskis sduskis Jun 13, 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.

Where is the object cached?

Sorry, something went wrong.

Uh oh!

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

All reactions

@olavloite olavloite Jun 13, 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.

It's cached in the super class. The super.getService(), which is called on first line of the method, gets the cached object from the super class. If it is closed, the createNewService() method (also defined in the super class ServiceOptions) will refresh it.

The implementation of ServiceOptions.getService() looks like this:

  ...
  private transient ServiceT service;
  ...
  public ServiceT getService() {
    if (service == null) {
      service = serviceFactory.create((OptionsT) this);
    }
    return service;
  }

Sorry, something went wrong.

Uh oh!

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

All reactions

@sduskis sduskis Jun 13, 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.

Won't the super class will cache the closed service, hence after super.service is closed, each new call to getService() will get a new instance?

Sorry, something went wrong.

Uh oh!

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

All reactions

@olavloite olavloite Jun 13, 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 easiest way to show what happens is by showing the code as if it was in one class (in reality it is split between the super class and sub class, the createNewService() method is in the super class). This is what is going on:

  ...
  private transient ServiceT service;
  ...
  @Override
  public Spanner getService() {
    Spanner spanner = super.getService();
    if (spanner.isClosed()) {
      spanner = createNewService();
    }
    return spanner;
  }

  protected ServiceT createNewService() {
    service = serviceFactory.create((OptionsT) this);
    return service;
  }

Sorry, something went wrong.

Uh oh!

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

All reactions

@olavloite olavloite Jun 14, 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.

I've added an additional test case to ensure that the correct caching behavior is actually happening here:

google-cloud-java/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java

Line 89 in 75d68e7

public void testDoNotCacheClosedSpannerInstance() {

Sorry, something went wrong.

Uh oh!

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

All reactions

@sduskis sduskis Jun 14, 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.

ugh... I reviewed the code a little closer, and debugged it. I see what you did now. it was a bit confusing.

Perhaps change this:

  @SuppressWarnings("unchecked")
  public ServiceT getService() {
    if (service == null) {
      service = serviceFactory.create((OptionsT) this);
    }
    return service;
  }

  @SuppressWarnings("unchecked")
  protected ServiceT createNewService() {
    service = serviceFactory.create((OptionsT) this);
    return service;
  }

to

  @SuppressWarnings("unchecked")
  public ServiceT getService() {
    if (shouldRefreshService()) {
      service = serviceFactory.create((OptionsT) this);
    }
    return service;
  }

  @SuppressWarnings("unchecked")
  protected boolean shouldRefreshService() {
    return service == null;
  }

That would make the logic a bit less spread out and readable.

Sorry, something went wrong.

Uh oh!

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

All reactions

@olavloite olavloite Jun 16, 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.

That is a cleaner solution. The subclass does need access to the cached object, so I've included the cached service object to the signature of the shouldRefreshService(ServiceT) method.

Sorry, something went wrong.

Uh oh!

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

All reactions
@ajaaym

ajaaym commented May 24, 2019

Copy link
Copy Markdown
Contributor

I think the design followed here is one instance of ServiceOption refer to one instance of Service, rather than treating ServiceOption as factory for Service. If service returned from serviceOptions is used after close it would throw proper error message which would direct user to use new ServiceOptions. This change would be inconsistent with the other client in this library.

All reactions

Sorry, something went wrong.

Uh oh!

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

@olavloite olavloite mentioned this pull request May 27, 2019
Spanner: Do not hand out a closed Spanner instance from SpannerOptions.getService() #5187
Closed
@yoshi-automation yoshi-automation added the 🚨 critical P0 critical issue. Requires immediate fix label Jun 4, 2019
@codecov

codecov Bot commented Jun 14, 2019 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

Codecov Report

Merging #5200 into master will decrease coverage by 4.09%.
The diff coverage is 0%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #5200      +/-   ##
============================================
- Coverage     50.82%   46.72%    -4.1%     
- Complexity    24174    24628     +454     
============================================
  Files          2271     2351      +80     
  Lines        229912   255993   +26081     
  Branches      25007    29279    +4272     
============================================
+ Hits         116845   119622    +2777     
- Misses       104435   127450   +23015     
- Partials       8632     8921     +289
Impacted Files Coverage Δ Complexity Δ
...src/main/java/com/google/cloud/ServiceOptions.java 40.25% <0%> (-0.36%) 29 <4> (+2)
...ble/data/v2/stub/EnhancedBigtableStubSettings.java 94.66% <0%> (-3.95%) 17% <0%> (ø)
...a/com/google/cloud/dialogflow/v2/AgentsClient.java 47.72% <0%> (-2.88%) 19% <0%> (ø)
.../google/cloud/dialogflow/v2beta1/AgentsClient.java 47.72% <0%> (-2.88%) 19% <0%> (ø)
...om/google/cloud/bigtable/data/v2/models/Query.java 66.32% <0%> (-2.05%) 22% <0%> (-1%)
.../v1beta1/stub/DataLabelingServiceStubSettings.java 84.42% <0%> (-1.79%) 50% <0%> (+8%)
.../cloud/dialogflow/v2beta1/stub/GrpcAgentsStub.java 85.64% <0%> (-1.4%) 12% <0%> (ø)
...oogle/cloud/dialogflow/v2/stub/GrpcAgentsStub.java 85.64% <0%> (-1.4%) 12% <0%> (ø)
...com/google/cloud/dialogflow/v2/AgentsSettings.java 13.72% <0%> (-1.17%) 2% <0%> (ø)
...oogle/cloud/dialogflow/v2beta1/AgentsSettings.java 13.72% <0%> (-1.17%) 2% <0%> (ø)
... and 107 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 32de049...1ca1c92. Read the comment docs.

All reactions

Sorry, something went wrong.

Uh oh!

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

olavloite added 6 commits June 15, 2019 12:52
@olavloite
do not hand out a closed Spanner instance
4a89f89
SpannerOptions caches any Spanner instance that has been created, and
hands this cached instance out to all subsequent calls to
SpannerOptions.getService(). This also included closed Spanner
instances. The getService() method now returns an error if the Spanner
instance has already been closed.
@olavloite
fix small merge error
599535f
@olavloite
create a new instance if the service/rpc is closed
9bbd0e2
SpannerOptions.getService() and SpannerOptions.getRpc() should return a
new instance instead of throwing an exception if the service/rpc object
has been closed.
@olavloite
add test case to ensure correct caching behavior
5561b8e
@olavloite
use shouldRefreshService instead of createNewService
d273479
@olavloite
fix merge conflicts
fb2a4db
@olavloite
olavloite force-pushed the spanner-create-new-instance-if-close branch from a651565 to fb2a4db Compare June 15, 2019 11:04
kolea2
kolea2 suggested changes Jun 17, 2019
View reviewed changes
Comment thread ...loud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java Outdated
*/
@Override
public Spanner getService() {
// Method is only overridden in order to supply additional documentation.

@kolea2 kolea2 Jun 17, 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.

My preference here would be to instead add documentation to getService in ServiceOptions so we don't have to override this (same with getRpc).

Sorry, something went wrong.

Uh oh!

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

All reactions

@olavloite olavloite Jun 17, 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.

That's not possible as there are Service classes other than Spanner that are not closeable and do not have an isClosed() method, which means that the behavior that is described for Spanner is only applicable to Spanner. That is probably also the reason that the default implementation is to cache the Service object indefinitely without checking its validity.

One possible generic solution could be to change the ServiceOptions class to implement the behavior of this PR for all Service classes that implement a custom interface CloseableService. All other Service classes would keep the current default behavior.

Sorry, something went wrong.

Uh oh!

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

All reactions

@kolea2 kolea2 Jun 17, 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.

Oh, sorry, didn't realize that was part of google-cloud-core. Can we perhaps add documentation to shouldRefreshService?

Sorry, something went wrong.

Uh oh!

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

All reactions

@olavloite olavloite Jun 17, 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.

I've added documentation to the shouldRefresh... methods.
Note that we cannot/should not rely on the documentation of these methods for our users, as these methods are protected.

Sorry, something went wrong.

Uh oh!

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

All reactions
olavloite added 2 commits June 17, 2019 19:04
@olavloite
added documentation to shouldRefresh... methods
5fbb912
@olavloite
removed overrides only for comments
7692138
@olavloite olavloite added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 21, 2019
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 21, 2019
@kolea2 kolea2 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 24, 2019
@yoshi-kokoro yoshi-kokoro removed kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Jun 24, 2019
kolea2
kolea2 reviewed Jun 25, 2019
View reviewed changes
Comment thread google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java Outdated
}

/**
* @param cachedService The currently cached service object

@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.

minor - rename to cachedRpc

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.

Changed.

Sorry, something went wrong.

Uh oh!

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

All reactions
kolea2
kolea2 reviewed Jun 25, 2019
View reviewed changes
Comment thread ...-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java
.setCredentials(NoCredentials.getInstance())
.build();
// Getting a service twice should give the same instance.
Spanner service1 = options.getService();

@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
@kolea2

kolea2 commented Jun 25, 2019

Copy link
Copy Markdown
Contributor

LGTM besides a few minor comments

All reactions

Sorry, something went wrong.

Uh oh!

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

olavloite added 3 commits June 26, 2019 15:31
@olavloite
fixed naming
b8bf651
@olavloite
added assertions for isClosed()
2cc0635
@olavloite
fixed formatting
1ca1c92
chingor13
chingor13 reviewed Jun 28, 2019
View reviewed changes
Comment thread google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java
* @param cachedService The currently cached service object
* @return true if the currently cached service object should be refreshed.
*/
protected boolean shouldRefreshService(ServiceT cachedService) {

@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
chingor13
chingor13 approved these changes Jul 1, 2019
View reviewed changes
kolea2
kolea2 approved these changes Jul 1, 2019
View reviewed changes
@kolea2
kolea2 merged commit 17cb858 into googleapis:master Jul 1, 2019
This was referenced Jul 2, 2019
Release google-cloud-java v0.99.0 #5654
Closed
Release google-cloud-java v0.99.0 #5657
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@chingor13 chingor13 chingor13 approved these changes
@snehashah16 snehashah16 Awaiting requested review from snehashah16
+2 more reviewers
@sduskis sduskis sduskis left review comments
@kolea2 kolea2 kolea2 approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

@kolea2 kolea2

Labels

cla: yes This human has signed the Contributor License Agreement. 🚨 critical P0 critical issue. Requires immediate fix

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

8 participants

@olavloite @ajaaym @kolea2 @chingor13 @sduskis @googlebot @yoshi-automation @yoshi-kokoro
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.