Skip to content

Commit ff88244

Browse files
committed
fix(bigtable): remove dead SessionList.inUseSessions and clear PendingVRpc references
1 parent c5f3794 commit ff88244

3 files changed

Lines changed: 73 additions & 34 deletions

File tree

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/SessionList.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class SessionList {
6363
// - starting sessions
6464
// - closing sessions
6565
private final Set<SessionHandle> allSessions = new HashSet<>();
66-
private final Set<SessionHandle> inUseSessions = new HashSet<>();
6766

6867
// pool level statistics across all the afes
6968
private final PoolStats poolStats = new PoolStats();
@@ -119,7 +118,6 @@ public Optional<SessionHandle> checkoutSession(AfeHandle afeHandle) {
119118
handle -> {
120119
poolStats.readyCount--;
121120
poolStats.inUseCount++;
122-
inUseSessions.add(handle);
123121
if (handle.afe.get().sessions.isEmpty()) {
124122
afesWithReadySessions.remove(afeHandle);
125123
}
@@ -221,7 +219,6 @@ private void releaseToPool() {
221219
AfeHandle afeHandle = this.afe.get();
222220

223221
poolStats.inUseCount--;
224-
inUseSessions.remove(this);
225222

226223
if (!closing && session.getState() == SessionState.READY) {
227224
poolStats.readyCount++;

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/session/SessionPoolImpl.java‎

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ class PendingVRpc<ReqT extends Message, RespT extends Message> implements VRpc<R
911911
private boolean isCancelled = false;
912912
private VRpc<ReqT, RespT> realCall;
913913

914-
private BigtableTimer.Timeout deadlineTimeout;
914+
private volatile BigtableTimer.Timeout deadlineTimeout;
915915

916916
public PendingVRpc(VRpcDescriptor<?, ReqT, RespT> desc) {
917917
this.desc = desc;
@@ -922,7 +922,7 @@ public PendingVRpc(VRpcDescriptor<?, ReqT, RespT> desc) {
922922
// region. Bounded acquisition on the request hot path; see poolLock.
923923
@SuppressWarnings("GuardedBy")
924924
public void start(ReqT req, VRpcCallContext ctx, VRpcListener<RespT> listener) {
925-
Preconditions.checkState(this.req == null, "request is already started");
925+
Preconditions.checkState(this.ctx == null, "request is already started");
926926
Preconditions.checkNotNull(req, "request can't be null");
927927
Preconditions.checkNotNull(ctx, "ctx can't be null");
928928
Preconditions.checkNotNull(listener, "listener can't be null");
@@ -940,34 +940,22 @@ public void start(ReqT req, VRpcCallContext ctx, VRpcListener<RespT> listener) {
940940
VRpcResult.createUncommitedError(
941941
Status.UNAVAILABLE.withCause(
942942
new IllegalStateException("SessionPool lock unavailable (pool wedged)")));
943-
ctx.getExecutor()
944-
.execute(
945-
() -> {
946-
if (isCancelled) return;
947-
isCancelled = true;
948-
listener.onClose(result);
949-
});
943+
cancelWithResult(result);
950944
return;
951945
}
952946
try {
953947
if (SessionPoolImpl.this.poolState != PoolState.STARTED) {
954948
VRpcResult result =
955949
VRpcResult.createUncommitedError(
956950
Status.UNAVAILABLE.withCause(new IllegalStateException("SessionPool is closed")));
957-
ctx.getExecutor().execute(() -> listener.onClose(result));
951+
cancelWithResult(result);
958952
return;
959953
}
960954
if (poolSizer.isDisabled()) {
961955
VRpcResult result =
962956
VRpcResult.createRejectedError(
963957
Status.UNAVAILABLE.withDescription("SessionPool configuration disabled"));
964-
ctx.getExecutor()
965-
.execute(
966-
() -> {
967-
if (isCancelled) return;
968-
isCancelled = true;
969-
listener.onClose(result);
970-
});
958+
cancelWithResult(result);
971959
return;
972960
}
973961
// Only arm the deadline monitor after we've committed to queueing; otherwise the
@@ -1013,8 +1001,10 @@ public void cancel(@Nullable String message, @Nullable Throwable cause) {
10131001
// region below.
10141002
@SuppressWarnings("GuardedBy")
10151003
private void cancel(Status status, boolean onlyCancelPendingCall) {
1016-
if (deadlineTimeout != null) {
1017-
deadlineTimeout.cancel();
1004+
BigtableTimer.Timeout timeout = deadlineTimeout;
1005+
deadlineTimeout = null;
1006+
if (timeout != null) {
1007+
timeout.cancel();
10181008
}
10191009
// The eager removal is only an optimization to skip a wasted session pick in
10201010
// tryDrainPendingRpcs.
@@ -1037,26 +1027,36 @@ private void cancel(Status status, boolean onlyCancelPendingCall) {
10371027
() -> {
10381028
if (isCancelled) return;
10391029
isCancelled = true;
1030+
VRpcListener<RespT> pendingListener = listener;
1031+
req = null;
1032+
listener = null;
10401033
if (realCall != null) {
10411034
if (!onlyCancelPendingCall) {
10421035
realCall.cancel(status.getDescription(), status.getCause());
10431036
}
1044-
} else {
1045-
listener.onClose(VRpcResult.createRejectedError(status));
1037+
} else if (pendingListener != null) {
1038+
pendingListener.onClose(VRpcResult.createRejectedError(status));
10461039
}
10471040
});
10481041
}
10491042

10501043
void cancelWithResult(VRpcResult result) {
1051-
if (deadlineTimeout != null) {
1052-
deadlineTimeout.cancel();
1044+
BigtableTimer.Timeout timeout = deadlineTimeout;
1045+
deadlineTimeout = null;
1046+
if (timeout != null) {
1047+
timeout.cancel();
10531048
}
10541049
ctx.getExecutor()
10551050
.execute(
10561051
() -> {
10571052
if (isCancelled) return;
10581053
isCancelled = true;
1059-
listener.onClose(result);
1054+
VRpcListener<RespT> pendingListener = listener;
1055+
req = null;
1056+
listener = null;
1057+
if (pendingListener != null) {
1058+
pendingListener.onClose(result);
1059+
}
10601060
});
10611061
}
10621062

@@ -1077,8 +1077,10 @@ public void requestNext() {
10771077
}
10781078

10791079
private void drainTo(SessionHandle handle) {
1080-
if (deadlineTimeout != null) {
1081-
deadlineTimeout.cancel();
1080+
BigtableTimer.Timeout timeout = deadlineTimeout;
1081+
deadlineTimeout = null;
1082+
if (timeout != null) {
1083+
timeout.cancel();
10821084
}
10831085
ctx.getExecutor()
10841086
.execute(
@@ -1087,15 +1089,15 @@ private void drainTo(SessionHandle handle) {
10871089
SessionPoolImpl.this.onPendingVRpcCancelled(handle);
10881090
return;
10891091
}
1092+
ReqT pendingReq = req;
1093+
VRpcListener<RespT> pendingListener = listener;
1094+
req = null;
1095+
listener = null;
10901096
realCall = newRealCall(desc, handle);
1091-
realCall.start(req, ctx, listener);
1097+
realCall.start(pendingReq, ctx, pendingListener);
10921098
});
10931099
}
10941100

1095-
private VRpcListener<RespT> getListener() {
1096-
return listener;
1097-
}
1098-
10991101
private BigtableTimer.Timeout monitorDeadline(Deadline deadline) {
11001102
// Body runs on backgroundExecutor (off the tick thread). onlyCancelPendingCall=true avoids
11011103
// racing with a user cancel that already attached a real call.

‎java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/SessionPoolImplTest.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,46 @@ void configUpdateToEmptyClosesSessionsAndScalesBackUpWhenEnabled() throws Except
14481448
assertThat(sessionPool.hasSession()).isTrue();
14491449
}
14501450

1451+
@Test
1452+
void pendingVRpcClearsReferencesAfterDrainAndCancel() throws Exception {
1453+
sessionPool.start(OpenFakeSessionRequest.getDefaultInstance(), new Metadata());
1454+
1455+
// Create both PendingVRpcs before any session becomes READY so both return PendingVRpc
1456+
UnaryResponseFuture<SessionFakeScriptedResponse> drainedFuture = new UnaryResponseFuture<>();
1457+
VRpc<SessionFakeScriptedRequest, SessionFakeScriptedResponse> drainedRpc =
1458+
sessionPool.newCall(FakeDescriptor.SCRIPTED);
1459+
UnaryResponseFuture<SessionFakeScriptedResponse> cancelledFuture = new UnaryResponseFuture<>();
1460+
VRpc<SessionFakeScriptedRequest, SessionFakeScriptedResponse> cancelledRpc =
1461+
sessionPool.newCall(FakeDescriptor.SCRIPTED);
1462+
1463+
cancelledRpc.start(
1464+
SessionFakeScriptedRequest.getDefaultInstance(),
1465+
VRpcCallContext.create(Deadline.after(1, TimeUnit.MINUTES), true, vrpcTracer),
1466+
cancelledFuture);
1467+
cancelledRpc.cancel("user cancelled", null);
1468+
1469+
drainedRpc.start(
1470+
SessionFakeScriptedRequest.getDefaultInstance(),
1471+
VRpcCallContext.create(Deadline.after(1, TimeUnit.MINUTES), true, vrpcTracer),
1472+
drainedFuture);
1473+
drainedFuture.get(5, TimeUnit.SECONDS);
1474+
1475+
Field reqField = drainedRpc.getClass().getDeclaredField("req");
1476+
Field listenerField = drainedRpc.getClass().getDeclaredField("listener");
1477+
Field timeoutField = drainedRpc.getClass().getDeclaredField("deadlineTimeout");
1478+
reqField.setAccessible(true);
1479+
listenerField.setAccessible(true);
1480+
timeoutField.setAccessible(true);
1481+
1482+
assertThat(reqField.get(drainedRpc)).isNull();
1483+
assertThat(listenerField.get(drainedRpc)).isNull();
1484+
assertThat(timeoutField.get(drainedRpc)).isNull();
1485+
1486+
assertThat(reqField.get(cancelledRpc)).isNull();
1487+
assertThat(listenerField.get(cancelledRpc)).isNull();
1488+
assertThat(timeoutField.get(cancelledRpc)).isNull();
1489+
}
1490+
14511491
private void waitForOpenRequestCount(int minCount, Duration timeout) throws InterruptedException {
14521492
long deadline = System.currentTimeMillis() + timeout.toMillis();
14531493
while (System.currentTimeMillis() < deadline) {

0 commit comments

Comments
 (0)