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
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
edc5bbf
fix: prevent illegal negative timeout values into thread sleep() meth…
arpan14 Feb 6, 2023
49a85df
Merge pull request #1 from arpan14/retryerror
arpan14 Feb 8, 2023
4cd497b
Fixing lint issues.
arpan14 Feb 8, 2023
4a6aa8e
Merge branch 'googleapis:main' into main
arpan14 Mar 13, 2023
b2aa09d
Merge branch 'googleapis:main' into main
arpan14 Mar 15, 2023
8d6d71e
Merge branch 'googleapis:main' into main
arpan14 May 9, 2023
77e6e7d
Merge branch 'googleapis:main' into main
arpan14 Jul 17, 2023
e8b7fad
Merge branch 'googleapis:main' into main
arpan14 Jul 25, 2023
8aa84e1
Merge branch 'googleapis:main' into main
arpan14 Oct 10, 2023
922f324
refactor: move session lastUseTime parameter from PooledSession to Se…
arpan14 Oct 17, 2023
b544080
chore: add clock instances in callees of SessionImpl.
arpan14 Oct 19, 2023
9862265
chore: partially fix failing unit tests in SessionPoolTest and Sessio…
arpan14 Oct 26, 2023
5e5f769
chore: fix failing tests in SessionPoolStressTest.
arpan14 Oct 27, 2023
a385ceb
chore: update lastUseTime for methods in SessionPoolTransactionContex…
arpan14 Oct 27, 2023
fd3bb41
chore: lint errors.
arpan14 Oct 27, 2023
4864053
chore: fix tests in DatabaseClientImplTest by passing the mocked cloc…
arpan14 Oct 27, 2023
f5b82fa
fix: update session lastUseTime field for AbstractReadContext class. …
arpan14 Oct 28, 2023
73f0192
fix: failing tests in TransactionRunnerImplTest.
arpan14 Oct 29, 2023
ff32178
fix: failing test in SessionPoolMaintainerTest.
arpan14 Oct 29, 2023
1316579
refactor: move FakeClock to a new class.
arpan14 Oct 29, 2023
80dd971
refactor: move Clock to a new class.
arpan14 Oct 30, 2023
1acd645
chore: resolving PR comments.
arpan14 Oct 31, 2023
6af8187
chore: address review comments.
arpan14 Oct 31, 2023
999a39b
chore: updating lastUseTime state in TransactionRunnerImpl. Removing …
arpan14 Oct 31, 2023
ec80d6a
chore: remove redundant update statements from SessionPool class. Add…
arpan14 Nov 1, 2023
6cdef81
chore: add more tests for TransactionRunner.
arpan14 Nov 1, 2023
593a10b
chore: remove dead code from constructor of SessionPoolTransactionCon…
arpan14 Nov 1, 2023
ced1e06
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Nov 1, 2023
0485aee
Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/Tr…
arpan14 Nov 2, 2023
c4163d8
Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/Tr…
arpan14 Nov 2, 2023
b75b19f
Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/Tr…
arpan14 Nov 2, 2023
86327e2
chore: fixing precondition errors due to null clock.
arpan14 Nov 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Builder() {}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -110,6 +112,11 @@ B setExecutorProvider(ExecutorProvider executorProvider) {
return self();
}

B setClock(Clock clock) {
this.clock = clock;
return self();
}

abstract T build();
}

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

private final Clock clock;

@GuardedBy("lock")
private boolean isValid = true;

Expand All @@ -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);

}

@Override
Expand Down Expand Up @@ -826,6 +836,7 @@ CloseableIterator<PartialResultSet> startStream(@Nullable ByteString resumeToken
SpannerRpc.StreamingCall call =
rpc.read(
builder.build(), stream.consumer(), session.getOptions(), isRouteToLeader());
session.markUsed(clock.instant());
olavloite marked this conversation as resolved.
Show resolved Hide resolved
call.request(prefetchChunks);
stream.setCall(call, /* withBeginTransaction = */ builder.getTransaction().hasBegin());
return stream;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spanner;

import org.threeten.bp.Instant;

/**
* Wrapper around current time so that we can fake it in tests. TODO(user): Replace with Java 8
* Clock.
*/
class Clock {
Instant instant() {
return Instant.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.Map;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
import org.threeten.bp.Instant;

/**
* Implementation of {@link Session}. Sessions are managed internally by the client library, and
Expand Down Expand Up @@ -98,12 +99,14 @@ interface SessionTransaction {
ByteString readyTransactionId;
private final Map<SpannerRpc.Option, ?> options;
private Span currentSpan;
private volatile Instant lastUseTime;

SessionImpl(SpannerImpl spanner, String name, Map<SpannerRpc.Option, ?> options) {
this.spanner = spanner;
this.options = options;
this.name = checkNotNull(name);
this.databaseId = SessionId.of(name).getDatabaseId();
this.lastUseTime = Instant.now();
arpan14 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand All @@ -123,6 +126,14 @@ Span getCurrentSpan() {
return currentSpan;
}

Instant getLastUseTime() {
return lastUseTime;
}

void markUsed(Instant instant) {
lastUseTime = instant;
}

@Override
public long executePartitionedUpdate(Statement stmt, UpdateOption... options) {
setActive(null);
Expand Down Expand Up @@ -385,6 +396,10 @@ ApiFuture<ByteString> beginTransactionAsync(Options transactionOptions, boolean
}

TransactionContextImpl newTransaction(Options options) {
final Clock clock =
spanner.getOptions().getSessionPoolOptions() != null
arpan14 marked this conversation as resolved.
Show resolved Hide resolved
? spanner.getOptions().getSessionPoolOptions().getPoolMaintainerClock()
: null;
return TransactionContextImpl.newBuilder()
.setSession(this)
.setOptions(options)
Expand All @@ -396,6 +411,7 @@ TransactionContextImpl newTransaction(Options options) {
.setDefaultPrefetchChunks(spanner.getDefaultPrefetchChunks())
.setSpan(currentSpan)
.setExecutorProvider(spanner.getAsyncExecutorProvider())
.setClock(clock)
.build();
}

Expand Down
Loading
Loading