This is an automated email from the ASF dual-hosted git repository.
Caideyipi pushed a commit to branch dev/1.3
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/dev/1.3 by this push:
new 8cc3a911849 Ensure Session.close() closes all connections after
failures (#19104) (#19144)
8cc3a911849 is described below
commit 8cc3a9118495e899dc06842f1d91529cc22e14b5
Author: Caideyipi <[email protected]>
AuthorDate: Fri Nov 6 10:02:50 2026 +0800
Ensure Session.close() closes all connections after failures (#19104)
(#19144)
* Make Session.close resilient to connection failures
* Cover query-only session connection cleanup
(cherry picked from commit ee347f403d7def035d20785a2885de306ef3d0d0)
---
.../java/org/apache/iotdb/session/Session.java | 45 +++++++++++--
.../java/org/apache/iotdb/session/SessionTest.java | 75 ++++++++++++++++++++++
2 files changed, 116 insertions(+), 4 deletions(-)
diff --git
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
index f5901e93624..27017b0cd9b 100644
--- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
+++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
@@ -86,6 +86,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -594,18 +595,47 @@ public class Session implements ISession {
@Override
public synchronized void close() throws IoTDBConnectionException {
+ Throwable closeException = null;
try {
if (isClosed) {
return;
}
- if (enableRedirection) {
+
+ Set<SessionConnection> closedConnections = Collections.newSetFromMap(new
IdentityHashMap<>());
+ if (endPointToSessionConnection != null) {
for (SessionConnection sessionConnection :
endPointToSessionConnection.values()) {
- sessionConnection.close();
+ if (sessionConnection != null &&
closedConnections.add(sessionConnection)) {
+ try {
+ sessionConnection.close();
+ } catch (IoTDBConnectionException | RuntimeException e) {
+ if (closeException == null) {
+ closeException = e;
+ } else if (closeException != e) {
+ closeException.addSuppressed(e);
+ }
+ }
+ }
+ }
+ }
+
+ if (defaultSessionConnection != null &&
closedConnections.add(defaultSessionConnection)) {
+ try {
+ defaultSessionConnection.close();
+ } catch (IoTDBConnectionException | RuntimeException e) {
+ if (closeException == null) {
+ closeException = e;
+ } else if (closeException != e) {
+ closeException.addSuppressed(e);
+ }
}
- } else {
- defaultSessionConnection.close();
}
} finally {
+ if (!isClosed) {
+ if (endPointToSessionConnection != null) {
+ endPointToSessionConnection.clear();
+ }
+ defaultSessionConnection = null;
+ }
// if executorService is null, it means that availableNodes is got from
SessionPool and we
// shouldn't clean that
if (this.executorService != null) {
@@ -616,6 +646,13 @@ public class Session implements ISession {
}
isClosed = true;
}
+
+ if (closeException instanceof IoTDBConnectionException) {
+ throw (IoTDBConnectionException) closeException;
+ }
+ if (closeException instanceof RuntimeException) {
+ throw (RuntimeException) closeException;
+ }
}
public SessionConnection constructSessionConnection(
diff --git
a/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java
b/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java
index 7a377aeb90d..71523c061d2 100644
---
a/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java
+++
b/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java
@@ -21,6 +21,7 @@ package org.apache.iotdb.session;
import org.apache.iotdb.common.rpc.thrift.TAggregationType;
import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+import org.apache.iotdb.isession.INodeSupplier;
import org.apache.iotdb.isession.ISession;
import org.apache.iotdb.isession.SessionConfig;
import org.apache.iotdb.isession.util.Version;
@@ -51,8 +52,10 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.ScheduledExecutorService;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@@ -96,6 +99,78 @@ public class SessionTest {
}
}
+ @Test
+ public void testCloseAttemptsEveryConnectionAndCleansUpAfterFailure() throws
Exception {
+ SessionConnection redirectedConnection =
Mockito.mock(SessionConnection.class);
+ IoTDBConnectionException defaultCloseException = new
IoTDBConnectionException("default");
+ IoTDBConnectionException redirectedCloseException = new
IoTDBConnectionException("redirected");
+ Mockito.doThrow(defaultCloseException).when(sessionConnection).close();
+
Mockito.doThrow(redirectedCloseException).when(redirectedConnection).close();
+
+ Map<TEndPoint, SessionConnection> connections = new LinkedHashMap<>();
+ connections.put(new TEndPoint("default", 1), sessionConnection);
+ connections.put(new TEndPoint("redirected", 2), redirectedConnection);
+ Whitebox.setInternalState(session, "endPointToSessionConnection",
connections);
+ Whitebox.setInternalState(session, "enableRedirection", true);
+ Whitebox.setInternalState(session, "isClosed", false);
+
+ ScheduledExecutorService executorService =
Mockito.mock(ScheduledExecutorService.class);
+ INodeSupplier availableNodes = Mockito.mock(INodeSupplier.class);
+ Whitebox.setInternalState(session, "executorService", executorService);
+ Whitebox.setInternalState(session, "availableNodes", availableNodes);
+
+ try {
+ session.close();
+ fail("Expected the first connection close failure to be propagated");
+ } catch (IoTDBConnectionException e) {
+ Assert.assertSame(defaultCloseException, e);
+ Assert.assertArrayEquals(
+ new Throwable[] {redirectedCloseException},
defaultCloseException.getSuppressed());
+ }
+
+ Mockito.verify(sessionConnection).close();
+ Mockito.verify(redirectedConnection).close();
+ Assert.assertTrue(connections.isEmpty());
+ Assert.assertNull(Whitebox.getInternalState(session,
"defaultSessionConnection"));
+ Assert.assertNull(Whitebox.getInternalState(session, "executorService"));
+ Assert.assertNull(Whitebox.getInternalState(session, "availableNodes"));
+ Mockito.verify(executorService).shutdown();
+ Mockito.verify(availableNodes).close();
+
+ session.close();
+ Mockito.verify(sessionConnection).close();
+ Mockito.verify(redirectedConnection).close();
+ }
+
+ @Test
+ public void testCloseClosesQueryRedirectedConnections() throws Exception {
+ SessionConnection redirectedConnection =
Mockito.mock(SessionConnection.class);
+ Map<TEndPoint, SessionConnection> connections = new LinkedHashMap<>();
+ connections.put(new TEndPoint("default", 1), sessionConnection);
+ connections.put(new TEndPoint("redirected", 2), redirectedConnection);
+ Whitebox.setInternalState(session, "endPointToSessionConnection",
connections);
+ Whitebox.setInternalState(session, "enableRedirection", false);
+ Whitebox.setInternalState(session, "enableQueryRedirection", true);
+ Whitebox.setInternalState(session, "isClosed", false);
+
+ session.close();
+
+ Mockito.verify(sessionConnection).close();
+ Mockito.verify(redirectedConnection).close();
+ Assert.assertTrue(connections.isEmpty());
+ }
+
+ @Test
+ public void testCloseDoesNotCloseBorrowedNodeSupplier() throws Exception {
+ INodeSupplier availableNodes = Mockito.mock(INodeSupplier.class);
+ Whitebox.setInternalState(session, "availableNodes", availableNodes);
+ Whitebox.setInternalState(session, "isClosed", false);
+
+ session.close();
+
+ Mockito.verify(availableNodes, Mockito.never()).close();
+ }
+
@Test
public void testBuildSession() {
Session session1 =