Skip to content

Commit 0ee9faf

Browse files
address ai code review
1 parent 918def4 commit 0ee9faf

3 files changed

Lines changed: 67 additions & 37 deletions

File tree

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/api/Client.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,26 @@ public void close() {
291291
// to a terminal Done — that delivery hops through op executor → userCallbackExecutor, both
292292
// of which must still be alive at this moment.
293293
//
294+
// channelPool.close() runs BEFORE userCallbackExecutor.close() so any classic (non-session)
295+
// gRPC call still in flight has its StreamListener.onClose delivered while the callback
296+
// executor is alive — otherwise the callback lands on a shut-down executor, hits REE, and
297+
// the caller's listener never sees a terminal onClose. Phase 2 only drains session-based
298+
// pools; classic callables share this same userCallbackExecutor.
299+
//
294300
// userCallbackExecutor.close() next, with a 5s drain to catch the listener.onClose tasks
295-
// queued by both the session drain (Phase 2) and the just-fired retry shutdowns.
301+
// queued by the session drain (Phase 2), the just-fired retry shutdowns, and channel
302+
// teardown.
303+
//
304+
// metrics.close() and configManager.close() don't depend on the shared channelPool (metrics
305+
// uses its own OTel exporter channel; configManager should have finished any config RPCs
306+
// before close).
296307
//
297308
// backgroundExecutor must close last because it's the timer's dispatcher and the op
298309
// executor's chain ultimately runs ScheduledExecutorService tasks here.
299310
sessionTimer.stop();
311+
channelPool.close();
300312
userCallbackExecutor.close();
301313
metrics.close();
302-
channelPool.close();
303314
configManager.close();
304315
backgroundExecutor.close();
305316
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ public void forceClose(CloseSessionRequest closeReason) {
294294
public void start(OpenSessionRequest req, Metadata headers, Listener sessionListener) {
295295
sessionSyncContext.execute(
296296
() -> {
297+
// Publish sessionListener FIRST so that if anything below throws (tracer.onStart,
298+
// OpenParams.create, stream.start), abortFromUncaughtException → notifyTerminalClose
299+
// can still fire sessionListener.onClose. Otherwise the handle stays in
300+
// SessionList.allSessions and drainedFuture never completes.
301+
this.sessionListener = sessionListener;
302+
297303
debugTagTracer.checkPrecondition(
298304
state == SessionState.NEW,
299305
"session_start_wrong_state",
@@ -305,7 +311,6 @@ public void start(OpenSessionRequest req, Metadata headers, Listener sessionList
305311

306312
updateState(SessionState.STARTING);
307313
openParams = OpenParams.create(headers, req);
308-
this.sessionListener = sessionListener;
309314

310315
SessionRequest wrappedReq = SessionRequest.newBuilder().setOpenSession(req).build();
311316
stream.start(
@@ -801,7 +806,7 @@ private void notifyTerminalClose(
801806
t);
802807
}
803808
try {
804-
tracer.onVRpcClose(Status.UNAVAILABLE.getCode());
809+
tracer.onVRpcClose(status.getCode());
805810
} catch (Throwable t) {
806811
logger.log(
807812
Level.WARNING,

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

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ public Optional<SessionHandle> checkoutSession(AfeHandle afeHandle) {
112112

113113
/** Closes all the sessions with this reason. */
114114
void close(CloseSessionRequest req) {
115-
// Notify all sessions to close and have the callbacks clean up the rest of the state
116-
for (SessionHandle s : allSessions) {
115+
// Snapshot before iterating: session.close(req) enqueues on sessionSyncContext, and
116+
// io.grpc.SynchronizationContext.execute drains inline when the caller is the drainer. A
117+
// pre-existing queued task (e.g. dispatchStreamClosed from a transport error just before
118+
// close, or a heartbeat-miss forceClose that already fired) can drain here and remove its
119+
// handle via onSessionClosed → allSessions.remove, tripping a fail-fast iterator CME.
120+
for (SessionHandle s : new ArrayList<>(allSessions)) {
117121
s.getSession().close(req);
118122
}
119123
}
@@ -239,41 +243,51 @@ void onSessionClosing() {
239243
}
240244

241245
void onSessionClosed(SessionState prevState) {
242-
if (inExpectedCount) {
243-
poolStats.expectedCapacity--;
244-
inExpectedCount = false;
245-
}
246-
// only update counts after the session started and has an afe associated
247-
afe.ifPresent(afeHandle -> afeHandle.refCount--);
248-
249-
// NOTE: don't need to update vRpc counters, onVRpcFinish will have been invoked already
250-
switch (prevState) {
251-
case NEW:
252-
throw new IllegalStateException("NEW session was closed");
253-
case STARTING:
254-
poolStats.startingCount--;
255-
break;
256-
case READY:
257-
{
258-
AfeHandle afeHandle = afe.get();
259-
// If the session was available & idle, then we need to remove it
260-
if (afeHandle.sessions.remove(this)) {
261-
poolStats.readyCount--;
262-
if (afeHandle.sessions.isEmpty()) {
263-
afesWithReadySessions.remove(afeHandle);
246+
// Always drop from allSessions on the way out, even if the branch below throws — otherwise a
247+
// stranded handle blocks drainedFuture and pool.awaitTerminated hangs until the shared close
248+
// deadline elapses.
249+
try {
250+
if (inExpectedCount) {
251+
poolStats.expectedCapacity--;
252+
inExpectedCount = false;
253+
}
254+
// only update counts after the session started and has an afe associated
255+
afe.ifPresent(afeHandle -> afeHandle.refCount--);
256+
257+
// NOTE: don't need to update vRpc counters, onVRpcFinish will have been invoked already
258+
switch (prevState) {
259+
case NEW:
260+
throw new IllegalStateException("NEW session was closed");
261+
case STARTING:
262+
poolStats.startingCount--;
263+
break;
264+
case READY:
265+
{
266+
// afe may be empty if SessionPoolImpl.onSessionReady early-returned on poolState !=
267+
// STARTED (pool closed after SessionImpl transitioned to READY but before
268+
// handle.onSessionStarted ran). Skip the AFE bookkeeping cleanly rather than NPE.
269+
if (afe.isPresent()) {
270+
AfeHandle afeHandle = afe.get();
271+
// If the session was available & idle, then we need to remove it
272+
if (afeHandle.sessions.remove(this)) {
273+
poolStats.readyCount--;
274+
if (afeHandle.sessions.isEmpty()) {
275+
afesWithReadySessions.remove(afeHandle);
276+
}
277+
}
264278
}
279+
break;
265280
}
281+
case CLOSING:
282+
case WAIT_SERVER_CLOSE:
283+
// noop
266284
break;
267-
}
268-
case CLOSING:
269-
case WAIT_SERVER_CLOSE:
270-
// noop
271-
break;
272-
case CLOSED:
273-
throw new IllegalStateException("double close");
285+
case CLOSED:
286+
throw new IllegalStateException("double close");
287+
}
288+
} finally {
289+
allSessions.remove(this);
274290
}
275-
276-
allSessions.remove(this);
277291
}
278292
}
279293

0 commit comments

Comments
 (0)