Skip to content
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

feat: move session lastUseTime parameter from PooledSession to SessionImpl class. Fix updation of the parameter for chained RPCs within one transaction. #2704

Merged
merged 32 commits into from Nov 3, 2023

Conversation

arpan14
Copy link
Collaborator

@arpan14 arpan14 commented Oct 27, 2023

For fixing - #2659

Base Refactoring Done

  • Refactored lastUseTime parameter from PooledSession to SessionImpl class.
    • A lot of existing tests had to be refactored to now work with the above refactoring.
  • FakeClock earlier was only being used within SessionPool class. Now it has to be used with multiple classes since lastUseTime was moved to SessionImpl class. Now moved it to a new class due to multiple usages.

New Changes Made

  • Modified TransactionRunnerImpl and AbstractReadContext class.
  • Introduced two new unit tests in DatabaseClientImplTest that now ensures that lastUseTime now gets updated with every RPC. Previously these tests were failing.

@arpan14 arpan14 requested a review from a team as a code owner October 27, 2023 10:23
@product-auto-label product-auto-label bot added size: l Pull request size is large. api: spanner Issues related to the googleapis/java-spanner API. labels Oct 27, 2023
@@ -73,6 +73,8 @@ abstract static class Builder<B extends Builder<?, T>, T extends AbstractReadCon
private QueryOptions defaultQueryOptions = SpannerOptions.Builder.DEFAULT_QUERY_OPTIONS;
private ExecutorProvider executorProvider;

private Clock clock;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe set this by default to = new Clock() and remove the null check in the AbstractReadContext constructor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, this is the clock field within the builder class. I have set it to = new Clock() by default at the member definition (L402)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually meant setting this one (so Builder.clock) by default to = new Clock(). That way, the clock in the AbstractReadContext can be final.

@@ -416,6 +425,7 @@ void initTransaction() {
this.defaultQueryOptions = builder.defaultQueryOptions;
this.span = builder.span;
this.executorProvider = builder.executorProvider;
this.clock = builder.clock == null ? new Clock() : builder.clock;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: see above, we could remove this null check by setting a default in the builder. Otherwise, prefer the use of com.google.common.base.MoreObjects.firstNonNull(..)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this.clock = firstNonNull(builder.clock, this.clock);

Options.priority(RpcPriority.HIGH))) {
while (resultSet.next()) {}
}
poolMaintainerClock.currentTimeMillis += Duration.ofMillis(2050).toMillis();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure: Because the IdleTimeThreshold is 3 seconds, this means that no session will be cleaned up, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the code change, this unit test was failing. We are asserting the number of sessions removed at the end.

    assertEquals(0, client.pool.numLeakedSessionsRemoved());

In this test, we have two read RPCs executed. After the first RPC we advance the clock by 1050ms and after the second RPC we advance the clock by 2050ms. This means the overall transaction took > 3s. Hence the current logic would recycle the session thinking its a long running transaction. This is exactly the bug we want to fix.

Post making the code changes, we are ensuring that the session's lastUseTime is getting correctly updated per RPC. And hence, we won't be recycling this session. This again gets tested through the assert statement.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation, that sounds good to me.

}

@Override
public void prepareReadWriteTransaction() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably for a different PR, but this seems like a left-over from before InlineBeginTransaction.

@product-auto-label product-auto-label bot added size: xl Pull request size is extra large. and removed size: l Pull request size is large. labels Nov 1, 2023
@arpan14 arpan14 added the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 1, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 1, 2023
@gcf-owl-bot gcf-owl-bot bot requested a review from a team as a code owner November 1, 2023 13:04
Copy link
Collaborator

@olavloite olavloite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with a minor nit on the Builder classes for AbstractReadContext and TransactionContext

@@ -73,6 +73,8 @@ abstract static class Builder<B extends Builder<?, T>, T extends AbstractReadCon
private QueryOptions defaultQueryOptions = SpannerOptions.Builder.DEFAULT_QUERY_OPTIONS;
private ExecutorProvider executorProvider;

private Clock clock;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually meant setting this one (so Builder.clock) by default to = new Clock(). That way, the clock in the AbstractReadContext can be final.

@@ -392,6 +399,8 @@ void initTransaction() {
private final int defaultPrefetchChunks;
private final QueryOptions defaultQueryOptions;

private Clock clock = new Clock();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, I think this can be made final

Options.priority(RpcPriority.HIGH))) {
while (resultSet.next()) {}
}
poolMaintainerClock.currentTimeMillis += Duration.ofMillis(2050).toMillis();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation, that sounds good to me.

arpan14 and others added 4 commits November 2, 2023 17:58
…ansactionRunnerImpl.java

Co-authored-by: Knut Olav Løite <koloite@gmail.com>
…ansactionRunnerImpl.java

Co-authored-by: Knut Olav Løite <koloite@gmail.com>
…ansactionRunnerImpl.java

Co-authored-by: Knut Olav Løite <koloite@gmail.com>
@arpan14 arpan14 added the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 3, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 3, 2023
@arpan14 arpan14 merged commit e75a281 into googleapis:main Nov 3, 2023
22 checks passed
@arpan14 arpan14 deleted the session-lastusetime-refactor branch November 3, 2023 06:40
gcf-merge-on-green bot pushed a commit that referenced this pull request Nov 8, 2023
🤖 I have created a release *beep* *boop*
---


## [6.53.0](https://togithub.com/googleapis/java-spanner/compare/v6.52.1...v6.53.0) (2023-11-06)


### Features

* Move session lastUseTime parameter from PooledSession to SessionImpl class. Fix updation of the parameter for chained RPCs within one transaction. ([#2704](https://togithub.com/googleapis/java-spanner/issues/2704)) ([e75a281](https://togithub.com/googleapis/java-spanner/commit/e75a2818124621a3ab837151a8e1094fa6c3b8f3))
* Rely on graal-sdk version declaration from property in java-shared-config ([#2696](https://togithub.com/googleapis/java-spanner/issues/2696)) ([cfab83a](https://togithub.com/googleapis/java-spanner/commit/cfab83ad3bd1a026e0b3da5a4cc2154b0f8c3ddf))


### Bug Fixes

* Prevent illegal negative timeout values into thread sleep() method in ITTransactionManagerTest. ([#2715](https://togithub.com/googleapis/java-spanner/issues/2715)) ([1c26cf6](https://togithub.com/googleapis/java-spanner/commit/1c26cf60efa1b98203af9b21a47e37c8fb1e0e97))


### Dependencies

* Update dependency com.google.cloud:google-cloud-shared-dependencies to v3.19.0 ([#2719](https://togithub.com/googleapis/java-spanner/issues/2719)) ([e320753](https://togithub.com/googleapis/java-spanner/commit/e320753b2bd125f94775db9c71a4b7803fa49c38))
* Update dependency com.google.cloud:google-cloud-trace to v2.28.0 ([#2670](https://togithub.com/googleapis/java-spanner/issues/2670)) ([078b7ca](https://togithub.com/googleapis/java-spanner/commit/078b7ca95548ac984c79d29197032b3f813abbcf))
* Update dependency com.google.cloud:google-cloud-trace to v2.29.0 ([#2714](https://togithub.com/googleapis/java-spanner/issues/2714)) ([b400eca](https://togithub.com/googleapis/java-spanner/commit/b400ecabb9fa6f262befa903163746fac2c7c15e))
* Update dependency commons-cli:commons-cli to v1.6.0 ([#2710](https://togithub.com/googleapis/java-spanner/issues/2710)) ([e3e8f6a](https://togithub.com/googleapis/java-spanner/commit/e3e8f6ac82d827280299038d3962fe66b110e0c4))
* Update dependency commons-io:commons-io to v2.15.0 ([#2712](https://togithub.com/googleapis/java-spanner/issues/2712)) ([a5f59aa](https://togithub.com/googleapis/java-spanner/commit/a5f59aa3e992d0594519983880a29f17301923e7))
* Update dependency org.graalvm.buildtools:junit-platform-native to v0.9.28 ([#2692](https://togithub.com/googleapis/java-spanner/issues/2692)) ([d8a2b02](https://togithub.com/googleapis/java-spanner/commit/d8a2b02d43a68e04bebb2349af61cc8901ccd667))
* Update dependency org.graalvm.buildtools:native-maven-plugin to v0.9.28 ([#2705](https://togithub.com/googleapis/java-spanner/issues/2705)) ([2b17f09](https://togithub.com/googleapis/java-spanner/commit/2b17f095a294defa5ea022c243fa750486b7d496))
* Update dependency org.junit.vintage:junit-vintage-engine to v5.10.1 ([#2723](https://togithub.com/googleapis/java-spanner/issues/2723)) ([9cf6d0e](https://togithub.com/googleapis/java-spanner/commit/9cf6d0eae5d2a86c89de2d252d0f4a4dab0b54a4))

---
This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/java-spanner API. size: xl Pull request size is extra large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants