From a6598aecc0a692ea41f1cec9a2d01ff75159e921 Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Thu, 12 Nov 2026 16:53:00 +0800 Subject: [PATCH 1/9] Fix table session pool lifecycle management Track borrowed table sessions until release and close them when the pool closes. Make wrapper close idempotent and preserve pool capacity on failure paths. --- .../pool/IoTDBTableModelSessionPoolIT.java | 30 ++++ .../iotdb/session/pool/SessionPool.java | 129 +++++++++++++----- .../session/pool/TableSessionWrapper.java | 93 ++++++++----- 3 files changed, 179 insertions(+), 73 deletions(-) diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/session/pool/IoTDBTableModelSessionPoolIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/session/pool/IoTDBTableModelSessionPoolIT.java index 3e2cba94c4c7b..31d13362faa1c 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/session/pool/IoTDBTableModelSessionPoolIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/session/pool/IoTDBTableModelSessionPoolIT.java @@ -54,6 +54,36 @@ public void tearDown() throws Exception { EnvFactory.getEnv().cleanClusterEnvironment(); } + @Test + public void testClosePoolClosesOccupiedSession() throws Exception { + ITableSessionPool sessionPool = EnvFactory.getEnv().getTableSessionPool(1); + ITableSession session = sessionPool.getSession(); + + sessionPool.close(); + + try { + session.executeNonQueryStatement("use information_schema"); + fail("The session should be closed with its pool"); + } catch (IoTDBConnectionException expected) { + // expected + } finally { + session.close(); + } + } + + @Test(timeout = 10_000) + public void testTableSessionCloseIsIdempotentAndKeepsCapacity() throws Exception { + try (ITableSessionPool sessionPool = EnvFactory.getEnv().getTableSessionPool(1)) { + ITableSession session = sessionPool.getSession(); + session.close(); + session.close(); + + try (ITableSession nextSession = sessionPool.getSession()) { + nextSession.executeNonQueryStatement("use information_schema"); + } + } + } + @Test public void testUseDatabase() { diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java index 9585d65c3902e..b5f9f8ea29285 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java @@ -707,9 +707,12 @@ private void initAvailableNodes(List endPointList) { // is incorrect. @SuppressWarnings({"squid:S3776", "squid:S2446"}) // Suppress high Cognitive Complexity warning private ISession getSession() throws IoTDBConnectionException { - ISession session = queue.poll(); - if (closed) { - throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); + ISession session; + synchronized (this) { + if (closed) { + throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); + } + session = queue.poll(); } if (session != null) { return session; @@ -720,6 +723,9 @@ private ISession getSession() throws IoTDBConnectionException { long start = System.currentTimeMillis(); while (session == null) { synchronized (this) { + if (closed) { + throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); + } if (size < maxSize) { // we can create more session size++; @@ -804,7 +810,15 @@ private ISession getSession() throws IoTDBConnectionException { } protected ITableSession getPooledTableSession() throws IoTDBConnectionException { - return new TableSessionWrapper((Session) getSession(), this); + Session session = (Session) getSession(); + synchronized (this) { + if (closed) { + closeSession(session); + throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); + } + occupied.put(session, session); + } + return new TableSessionWrapper(session, this); } @Override @@ -819,15 +833,58 @@ public int currentOccupiedSize() { @SuppressWarnings({"squid:S2446"}) protected void putBack(ISession session) { - queue.push(session); synchronized (this) { - // we do not need to notifyAll as any waited thread can continue to work after waked up. - this.notify(); - // comment the following codes as putBack is too frequently called. - // if (logger.isTraceEnabled()) { - // logger.trace("put a session back and notify others..., queue.size = {}", - // queue.size()); - // } + if (!closed) { + queue.push(session); + // we do not need to notifyAll as any waited thread can continue to work after waked up. + this.notify(); + return; + } + } + closeSession(session); + } + + /** + * Releases a session borrowed from this pool. A session that cannot be reused is closed and + * replaced while the pool remains open. + * + * @return whether the session was still owned by the pool and has been released + */ + protected boolean releaseSession(ISession session, boolean reusable) { + boolean replenish = false; + synchronized (this) { + if (occupied.remove(session) == null) { + return false; + } + if (reusable && !closed) { + queue.push(session); + this.notifyAll(); + return true; + } + replenish = !closed; + } + + closeSession(session); + if (replenish) { + tryConstructNewSession(); + } + return true; + } + + private void discardSession(ISession session) { + boolean replenish; + synchronized (this) { + replenish = !closed; + } + closeSession(session); + if (replenish) { + tryConstructNewSession(); + } + } + + protected boolean isPoolClosed() { + synchronized (this) { + return closed; } } @@ -838,22 +895,21 @@ private void occupy(ISession session) { /** close all connections in the pool */ @Override public synchronized void close() { + if (closed) { + return; + } + closed = true; + for (ISession session : queue) { - try { - session.close(); - } catch (IoTDBConnectionException e) { - // do nothing - LOGGER.warn(CLOSE_THE_SESSION_FAILED, e); - } + closeSession(session); } for (ISession session : occupied.keySet()) { - try { - session.close(); - } catch (IoTDBConnectionException e) { - // do nothing - LOGGER.warn(CLOSE_THE_SESSION_FAILED, e); - } + closeSession(session); } + queue.clear(); + occupied.clear(); + this.notifyAll(); + if (this.executorService != null) { this.executorService.shutdown(); this.executorService = null; @@ -865,9 +921,6 @@ public synchronized void close() { this.availableNodes = null; } LOGGER.info(SessionMessages.CLOSING_SESSION_POOL); - this.closed = true; - queue.clear(); - occupied.clear(); } @Override @@ -876,18 +929,20 @@ public void closeResultSet(SessionDataSetWrapper wrapper) { try { wrapper.getSessionDataSet().closeOperationHandle(); } catch (IoTDBConnectionException | StatementExecutionException e) { - tryConstructNewSession(); putback = false; } finally { - ISession session = occupied.remove(wrapper.getSession()); - if (putback && session != null) { - putBack(wrapper.getSession()); - } + releaseSession(wrapper.getSession(), putback); } } @SuppressWarnings({"squid:S2446"}) private void tryConstructNewSession() { + synchronized (this) { + if (closed) { + return; + } + } + Session session = constructNewSession(); try { session.open( @@ -928,8 +983,9 @@ private void closeSession(ISession session) { private void cleanSessionAndMayThrowConnectionException( ISession session, int times, IoTDBConnectionException e) throws IoTDBConnectionException { - closeSession(session); - tryConstructNewSession(); + if (!releaseSession(session, false)) { + discardSession(session); + } if (times == FINAL_RETRY) { throw new IoTDBConnectionException( String.format( @@ -942,8 +998,9 @@ private void cleanSessionAndMayThrowConnectionException( } protected void cleanSessionAndMayThrowConnectionException(ISession session) { - closeSession(session); - tryConstructNewSession(); + if (!releaseSession(session, false)) { + discardSession(session); + } } /** diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java index 4cf7ca980f347..e05585d59825d 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java @@ -31,7 +31,6 @@ import org.slf4j.LoggerFactory; import java.util.Objects; -import java.util.concurrent.atomic.AtomicBoolean; /** * NOTICE: TableSessionWrapper is specific to the table model. @@ -48,12 +47,12 @@ public class TableSessionWrapper implements ITableSession { private final SessionPool sessionPool; - private final AtomicBoolean closed; + private boolean closed; protected TableSessionWrapper(Session session, SessionPool sessionPool) { this.session = session; this.sessionPool = sessionPool; - this.closed = new AtomicBoolean(false); + this.closed = false; } @Override @@ -61,10 +60,7 @@ public void insert(Tablet tablet) throws StatementExecutionException, IoTDBConne try { session.insertRelationalTablet(tablet); } catch (IoTDBConnectionException e) { - sessionPool.cleanSessionAndMayThrowConnectionException(session); - closed.set(true); - session = null; - throw e; + throw releaseOnConnectionFailure(e); } } @@ -74,10 +70,7 @@ public SessionDataSet executeQueryStatement(String sql) try { return session.executeQueryStatement(sql); } catch (IoTDBConnectionException e) { - sessionPool.cleanSessionAndMayThrowConnectionException(session); - closed.set(true); - session = null; - throw e; + throw releaseOnConnectionFailure(e); } } @@ -87,10 +80,7 @@ public SessionDataSet executeQueryStatement(String sql, long timeoutInMs) try { return session.executeQueryStatement(sql, timeoutInMs); } catch (IoTDBConnectionException e) { - sessionPool.cleanSessionAndMayThrowConnectionException(session); - closed.set(true); - session = null; - throw e; + throw releaseOnConnectionFailure(e); } } @@ -100,48 +90,77 @@ public void executeNonQueryStatement(String sql) try { session.executeNonQueryStatement(sql); } catch (IoTDBConnectionException e) { - sessionPool.cleanSessionAndMayThrowConnectionException(session); - closed.set(true); - session = null; - throw e; + throw releaseOnConnectionFailure(e); } } @Override public void close() throws IoTDBConnectionException { - if (!Objects.equals(session.getSqlDialect(), sessionPool.sqlDialect)) { - try { - session.executeNonQueryStatement("set sql_dialect=" + sessionPool.sqlDialect); - } catch (StatementExecutionException e) { - LOGGER.warn( - SessionMessages - .LOG_FAILED_CHANGE_BACK_SQL_DIALECT_EXECUTING_SET_SQL_DIALECT_ARG_947F35E7, - sessionPool.sqlDialect, - e); - session.close(); - session = null; + final Session sessionToRelease; + synchronized (this) { + if (closed) { return; } + closed = true; + sessionToRelease = session; + session = null; + } + + if (sessionToRelease == null) { + return; + } + + if (sessionPool.isPoolClosed()) { + sessionPool.releaseSession(sessionToRelease, false); + return; } - if (closed.compareAndSet(false, true)) { - if (!Objects.equals(session.getDatabase(), sessionPool.database) + boolean reusable = true; + try { + if (!Objects.equals(sessionToRelease.getSqlDialect(), sessionPool.sqlDialect)) { + try { + sessionToRelease.executeNonQueryStatement("set sql_dialect=" + sessionPool.sqlDialect); + } catch (StatementExecutionException e) { + LOGGER.warn( + SessionMessages + .LOG_FAILED_CHANGE_BACK_SQL_DIALECT_EXECUTING_SET_SQL_DIALECT_ARG_947F35E7, + sessionPool.sqlDialect, + e); + reusable = false; + } + } + + if (reusable + && !Objects.equals(sessionToRelease.getDatabase(), sessionPool.database) && sessionPool.database != null) { try { - session.executeNonQueryStatement("use " + sessionPool.database); + sessionToRelease.executeNonQueryStatement("use " + sessionPool.database); } catch (StatementExecutionException e) { LOGGER.warn( SessionMessages.LOG_FAILED_CHANGE_BACK_DATABASE_EXECUTING_USE_ARG_274541CA, sessionPool.database, e); - session.close(); - session = null; - return; + reusable = false; } } + } catch (IoTDBConnectionException | RuntimeException e) { + sessionPool.releaseSession(sessionToRelease, false); + throw e; + } - sessionPool.putBack(session); + sessionPool.releaseSession(sessionToRelease, reusable); + } + + private IoTDBConnectionException releaseOnConnectionFailure(IoTDBConnectionException exception) { + final Session failedSession; + synchronized (this) { + closed = true; + failedSession = session; session = null; } + if (failedSession != null) { + sessionPool.releaseSession(failedSession, false); + } + return exception; } } From 24f9af93129de85b7810926ca420a6b970bfb1d4 Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Thu, 12 Nov 2026 18:06:21 +0800 Subject: [PATCH 2/9] Fix sortTablet session cleanup Return the borrowed session when sortTablet succeeds or throws, and remove an unused connection-exception cleanup overload. --- .../iotdb/session/pool/SessionPool.java | 13 ++++----- .../iotdb/session/pool/SessionPoolTest.java | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java index b5f9f8ea29285..45d1392cd34bb 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java @@ -997,12 +997,6 @@ private void cleanSessionAndMayThrowConnectionException( } } - protected void cleanSessionAndMayThrowConnectionException(ISession session) { - if (!releaseSession(session, false)) { - discardSession(session); - } - } - /** * insert the data of a device. For each timestamp, the number of measurements is the same. * @@ -3028,8 +3022,11 @@ public List showPathsTemplateUsingOn(String templateName) @Override public void sortTablet(Tablet tablet) throws IoTDBConnectionException { ISession session = getSession(); - session.sortTablet(tablet); - putBack(session); + try { + session.sortTablet(tablet); + } finally { + putBack(session); + } } @Override diff --git a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java index d08625cb978c4..60df47a1e86ca 100644 --- a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java +++ b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java @@ -1217,6 +1217,33 @@ public void testSortTablet() throws IoTDBConnectionException, StatementExecution ((ConcurrentLinkedDeque) Whitebox.getInternalState(sessionPool, "queue")).size()); } + @Test + public void testSortTabletExceptionReturnsSessionToPool() throws IoTDBConnectionException { + List schemas = new ArrayList<>(); + MeasurementSchema schema = new MeasurementSchema(); + schema.setMeasurementName("pressure"); + schema.setDataType(TSDataType.BOOLEAN); + schema.setCompressionType(CompressionType.SNAPPY); + schema.setEncoding(TSEncoding.PLAIN); + schemas.add(schema); + long[] timestamp = new long[] {1L, 2L}; + Object[] values = new Object[] {true, false}; + BitMap[] partBitMap = new BitMap[2]; + Tablet tablet = new Tablet("device", schemas, timestamp, values, partBitMap, 2); + Mockito.doThrow(new IllegalArgumentException("sort failed")).when(session).sortTablet(tablet); + + try { + sessionPool.sortTablet(tablet); + fail("Expected sortTablet to throw"); + } catch (IllegalArgumentException expected) { + // expected + } + + assertEquals( + 1, + ((ConcurrentLinkedDeque) Whitebox.getInternalState(sessionPool, "queue")).size()); + } + @Test public void testSetSchemaTemplate() throws IoTDBConnectionException, StatementExecutionException { sessionPool.setSchemaTemplate("template2", "root.ut0.sensor2"); From 5c8ae5f0b1a98b0a5227d4d0a0ada53b1c1891cd Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Thu, 12 Nov 2026 18:20:47 +0800 Subject: [PATCH 3/9] Fix sortTablet test assertion --- .../java/org/apache/iotdb/session/pool/SessionPoolTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java index 60df47a1e86ca..5fb1e088de0ca 100644 --- a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java +++ b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java @@ -1234,7 +1234,7 @@ public void testSortTabletExceptionReturnsSessionToPool() throws IoTDBConnection try { sessionPool.sortTablet(tablet); - fail("Expected sortTablet to throw"); + Assert.fail("Expected sortTablet to throw"); } catch (IllegalArgumentException expected) { // expected } From cc1479943d5ee91065341bc0526183928c282c2b Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Fri, 13 Nov 2026 10:57:44 +0800 Subject: [PATCH 4/9] Add deterministic table session close race tests Coordinate pool close with session borrowing and release to verify that sessions are not returned to a closed pool. --- .../iotdb/session/pool/SessionPoolTest.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java index 5fb1e088de0ca..c5fb34dd53b7b 100644 --- a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java +++ b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java @@ -22,6 +22,7 @@ import org.apache.iotdb.common.rpc.thrift.TAggregationType; import org.apache.iotdb.common.rpc.thrift.TSStatus; import org.apache.iotdb.isession.ISession; +import org.apache.iotdb.isession.ITableSession; import org.apache.iotdb.isession.SessionDataSet; import org.apache.iotdb.isession.pool.ISessionPool; import org.apache.iotdb.isession.pool.SessionDataSetWrapper; @@ -74,6 +75,13 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -98,6 +106,40 @@ public class SessionPoolTest { @Mock private TSFetchMetadataResp fetchMetadataResp; @Mock private TSFetchResultsResp fetchResultsResp; + private static class BlockingAfterPollDeque extends ConcurrentLinkedDeque { + + private final SessionPool pool; + private final CountDownLatch polled = new CountDownLatch(1); + private final AtomicBoolean shouldBlock = new AtomicBoolean(true); + + private BlockingAfterPollDeque(SessionPool pool) { + this.pool = pool; + } + + @Override + public ISession poll() { + ISession session = super.poll(); + if (session != null && shouldBlock.compareAndSet(true, false)) { + polled.countDown(); + // getSession holds the pool monitor here. Waiting releases it so close() can run before + // the borrowed session is registered in occupied. + while (!pool.isPoolClosed()) { + try { + pool.wait(5_000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new AssertionError(e); + } + } + } + return session; + } + + private void awaitPolled() throws InterruptedException { + assertTrue(polled.await(5, TimeUnit.SECONDS)); + } + } + @Test public void testBuilder() { SessionPool pool = @@ -1244,6 +1286,65 @@ public void testSortTabletExceptionReturnsSessionToPool() throws IoTDBConnection ((ConcurrentLinkedDeque) Whitebox.getInternalState(sessionPool, "queue")).size()); } + @Test(timeout = 10_000) + public void testTableSessionBorrowRacingWithPoolClose() throws Exception { + SessionPool concreteSessionPool = (SessionPool) sessionPool; + BlockingAfterPollDeque blockingQueue = new BlockingAfterPollDeque(concreteSessionPool); + blockingQueue.add(session); + Whitebox.setInternalState(concreteSessionPool, "queue", blockingQueue); + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + Future borrowFuture = + executor.submit(() -> concreteSessionPool.getPooledTableSession()); + blockingQueue.awaitPolled(); + + concreteSessionPool.close(); + + try { + borrowFuture.get(); + Assert.fail("Expected the borrow to fail after the pool closed"); + } catch (ExecutionException e) { + assertTrue(e.getCause() instanceof IoTDBConnectionException); + } + Mockito.verify(session).close(); + } finally { + executor.shutdownNow(); + } + } + + @Test(timeout = 10_000) + public void testTableSessionReleaseRacingWithPoolClose() throws Exception { + SessionPool concreteSessionPool = (SessionPool) sessionPool; + Mockito.when(session.getSqlDialect()).thenReturn(concreteSessionPool.sqlDialect); + CountDownLatch aboutToRelease = new CountDownLatch(1); + CountDownLatch continueRelease = new CountDownLatch(1); + Mockito.when(session.getDatabase()) + .thenAnswer( + ignored -> { + aboutToRelease.countDown(); + assertTrue(continueRelease.await(5, TimeUnit.SECONDS)); + return null; + }); + ITableSession tableSession = concreteSessionPool.getPooledTableSession(); + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + Future closeFuture = executor.submit(tableSession::close); + assertTrue(aboutToRelease.await(5, TimeUnit.SECONDS)); + + // The wrapper has observed an open pool and is paused immediately before releaseSession(). + concreteSessionPool.close(); + continueRelease.countDown(); + closeFuture.get(); + + assertEquals(0, concreteSessionPool.currentAvailableSize()); + assertEquals(0, concreteSessionPool.currentOccupiedSize()); + Mockito.verify(session).close(); + } finally { + continueRelease.countDown(); + executor.shutdownNow(); + } + } + @Test public void testSetSchemaTemplate() throws IoTDBConnectionException, StatementExecutionException { sessionPool.setSchemaTemplate("template2", "root.ut0.sensor2"); From 665c926a36d7b7261278c171fabdfd2eac1a8c01 Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Fri, 13 Nov 2026 11:24:46 +0800 Subject: [PATCH 5/9] Fix concurrent close test compilation --- .../org/apache/iotdb/session/pool/SessionPoolTest.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java index c5fb34dd53b7b..9aafc9c3c3e6f 100644 --- a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java +++ b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java @@ -1328,7 +1328,12 @@ public void testTableSessionReleaseRacingWithPoolClose() throws Exception { ITableSession tableSession = concreteSessionPool.getPooledTableSession(); ExecutorService executor = Executors.newSingleThreadExecutor(); try { - Future closeFuture = executor.submit(tableSession::close); + Future closeFuture = + executor.submit( + () -> { + tableSession.close(); + return null; + }); assertTrue(aboutToRelease.await(5, TimeUnit.SECONDS)); // The wrapper has observed an open pool and is paused immediately before releaseSession(). From f1b088c4bf1e34972e18215912a0e1200fa3aca3 Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Mon, 16 Nov 2026 15:40:42 +0800 Subject: [PATCH 6/9] Address table session pool review feedback Close borrowed sessions without holding the pool monitor, guard reuse after connection failure, and cover capacity recovery paths. --- .../org/apache/iotdb/session/Session.java | 100 ++++++----- .../iotdb/session/pool/SessionPool.java | 107 ++++++----- .../session/pool/TableSessionWrapper.java | 34 ++-- .../org/apache/iotdb/session/SessionTest.java | 11 ++ .../iotdb/session/pool/SessionPoolTest.java | 170 +++++++++++++++--- 5 files changed, 293 insertions(+), 129 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 d8bcce5e13def..19f52544e3935 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 @@ -692,27 +692,9 @@ public synchronized void close() throws IoTDBConnectionException { return; } - Set closedConnections = Collections.newSetFromMap(new IdentityHashMap<>()); - if (endPointToSessionConnection != null) { - for (SessionConnection sessionConnection : endPointToSessionConnection.values()) { - 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); - } - } - } - } - } - - SessionConnection defaultSessionConnection = getDefaultSessionConnection(); - if (defaultSessionConnection != null && closedConnections.add(defaultSessionConnection)) { + for (SessionConnection sessionConnection : getSessionConnections()) { try { - defaultSessionConnection.close(); + sessionConnection.close(); } catch (IoTDBConnectionException | RuntimeException e) { if (closeException == null) { closeException = e; @@ -722,23 +704,7 @@ public synchronized void close() throws IoTDBConnectionException { } } } finally { - if (!isClosed) { - if (endPointToSessionConnection != null) { - endPointToSessionConnection.clear(); - } - setDefaultSessionConnection(null); - } - // if executorService is null, it means that availableNodes is got from SessionPool and we - // shouldn't clean that - if (this.executorService != null) { - this.executorService.shutdown(); - this.executorService = null; - if (!this.availableNodes.isCallerManaged()) { - this.availableNodes.close(); - } - this.availableNodes = null; - } - isClosed = true; + finishClose(); } if (closeException instanceof IoTDBConnectionException) { @@ -749,6 +715,55 @@ public synchronized void close() throws IoTDBConnectionException { } } + /** Closes the underlying transports without waiting for a close-session RPC. */ + public synchronized void forceClose() { + if (isClosed) { + return; + } + try { + for (SessionConnection sessionConnection : getSessionConnections()) { + sessionConnection.forceCloseTransport(); + } + } finally { + finishClose(); + } + } + + private Set getSessionConnections() { + Set connections = Collections.newSetFromMap(new IdentityHashMap<>()); + if (endPointToSessionConnection != null) { + for (SessionConnection sessionConnection : endPointToSessionConnection.values()) { + if (sessionConnection != null) { + connections.add(sessionConnection); + } + } + } + if (defaultSessionConnection != null) { + connections.add(defaultSessionConnection); + } + return connections; + } + + private void finishClose() { + if (!isClosed) { + if (endPointToSessionConnection != null) { + endPointToSessionConnection.clear(); + } + setDefaultSessionConnection(null); + } + isClosed = true; + // if executorService is null, it means that availableNodes is got from SessionPool and we + // shouldn't clean that + if (this.executorService != null) { + this.executorService.shutdown(); + this.executorService = null; + if (!this.availableNodes.isCallerManaged()) { + this.availableNodes.close(); + } + this.availableNodes = null; + } + } + public SessionConnection constructSessionConnection( Session session, TEndPoint endpoint, ZoneId zoneId) throws IoTDBConnectionException { if (endpoint == null) { @@ -3810,10 +3825,15 @@ private boolean checkSorted(List times) { return true; } + public void sortTablet(Tablet tablet) { + sortTabletInMemory(tablet); + } + + /** Sorts the rows of the tablet by timestamp without using a server connection. */ @SuppressWarnings({ "squid:S3776" }) // ignore Cognitive Complexity of methods should not be too high - public void sortTablet(Tablet tablet) { + public static void sortTabletInMemory(Tablet tablet) { /* * following part of code sort the batch data by time, * so we can insert continuous data in value list to get a better performance @@ -3856,7 +3876,7 @@ public void sortTablet(Tablet tablet) { * @param index index * @return sorted list */ - private Object sortList(Object valueList, TSDataType dataType, int[] index) { + private static Object sortList(Object valueList, TSDataType dataType, int[] index) { return SessionUtils.sortValueList(valueList, dataType, index); } @@ -3867,7 +3887,7 @@ private Object sortList(Object valueList, TSDataType dataType, int[] index) { * @param index index * @return sorted bitMap */ - private BitMap sortBitMap(BitMap bitMap, int[] index) { + private static BitMap sortBitMap(BitMap bitMap, int[] index) { BitMap sortedBitMap = new BitMap(bitMap.getSize()); for (int i = 0; i < index.length; i++) { if (bitMap.isMarked(index[i])) { diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java index 45d1392cd34bb..dc24af2bcb743 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java @@ -100,7 +100,7 @@ public class SessionPool implements ISessionPool { private static final int FINAL_RETRY = RETRY - 1; private final ConcurrentLinkedDeque queue = new ConcurrentLinkedDeque<>(); - // for session whose resultSet is not released. + // sessions currently borrowed for a result set or a table-session wrapper. private final ConcurrentMap occupied = new ConcurrentHashMap<>(); private int size = 0; private int maxSize = 0; @@ -850,15 +850,17 @@ protected void putBack(ISession session) { * * @return whether the session was still owned by the pool and has been released */ - protected boolean releaseSession(ISession session, boolean reusable) { + boolean releaseSession(ISession session, boolean reusable) { + boolean wasOccupied; boolean replenish = false; synchronized (this) { - if (occupied.remove(session) == null) { - return false; - } + wasOccupied = occupied.remove(session) != null; if (reusable && !closed) { + if (!wasOccupied) { + return false; + } queue.push(session); - this.notifyAll(); + this.notify(); return true; } replenish = !closed; @@ -868,21 +870,10 @@ protected boolean releaseSession(ISession session, boolean reusable) { if (replenish) { tryConstructNewSession(); } - return true; + return wasOccupied; } - private void discardSession(ISession session) { - boolean replenish; - synchronized (this) { - replenish = !closed; - } - closeSession(session); - if (replenish) { - tryConstructNewSession(); - } - } - - protected boolean isPoolClosed() { + boolean isPoolClosed() { synchronized (this) { return closed; } @@ -894,31 +885,38 @@ private void occupy(ISession session) { /** close all connections in the pool */ @Override - public synchronized void close() { - if (closed) { - return; + public void close() { + final List idleSessions; + final List borrowedSessions; + final ScheduledExecutorService executorToShutdown; + final INodeSupplier nodesToClose; + synchronized (this) { + if (closed) { + return; + } + closed = true; + idleSessions = new ArrayList<>(queue); + borrowedSessions = new ArrayList<>(occupied.keySet()); + queue.clear(); + occupied.clear(); + executorToShutdown = this.executorService; + this.executorService = null; + nodesToClose = availableNodes; + availableNodes = null; + this.notifyAll(); } - closed = true; - for (ISession session : queue) { + for (ISession session : idleSessions) { closeSession(session); } - for (ISession session : occupied.keySet()) { - closeSession(session); + for (ISession session : borrowedSessions) { + forceCloseSession(session); } - queue.clear(); - occupied.clear(); - this.notifyAll(); - - if (this.executorService != null) { - this.executorService.shutdown(); - this.executorService = null; + if (executorToShutdown != null) { + executorToShutdown.shutdown(); } - if (availableNodes != null) { - if (!this.availableNodes.isCallerManaged()) { - this.availableNodes.close(); - } - this.availableNodes = null; + if (nodesToClose != null && !nodesToClose.isCallerManaged()) { + nodesToClose.close(); } LOGGER.info(SessionMessages.CLOSING_SESSION_POOL); } @@ -981,11 +979,21 @@ private void closeSession(ISession session) { } } + private void forceCloseSession(ISession session) { + if (session instanceof Session) { + try { + ((Session) session).forceClose(); + } catch (Exception e) { + LOGGER.warn(CLOSE_THE_SESSION_FAILED, e); + } + } else { + closeSession(session); + } + } + private void cleanSessionAndMayThrowConnectionException( ISession session, int times, IoTDBConnectionException e) throws IoTDBConnectionException { - if (!releaseSession(session, false)) { - discardSession(session); - } + releaseSession(session, false); if (times == FINAL_RETRY) { throw new IoTDBConnectionException( String.format( @@ -997,6 +1005,14 @@ private void cleanSessionAndMayThrowConnectionException( } } + /** + * @deprecated Use the pool's internal release path. + */ + @Deprecated + protected void cleanSessionAndMayThrowConnectionException(ISession session) { + releaseSession(session, false); + } + /** * insert the data of a device. For each timestamp, the number of measurements is the same. * @@ -3020,13 +3036,8 @@ public List showPathsTemplateUsingOn(String templateName) } @Override - public void sortTablet(Tablet tablet) throws IoTDBConnectionException { - ISession session = getSession(); - try { - session.sortTablet(tablet); - } finally { - putBack(session); - } + public void sortTablet(Tablet tablet) { + Session.sortTabletInMemory(tablet); } @Override diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java index e05585d59825d..06a71bed97427 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java @@ -32,13 +32,7 @@ import java.util.Objects; -/** - * NOTICE: TableSessionWrapper is specific to the table model. - * - *

used for SessionPool.getSession need to do some other things like calling - * cleanSessionAndMayThrowConnectionException in SessionPool while encountering connection exception - * only need to putBack to SessionPool while closing. - */ +/** Adapts a pooled session to the table-session API and releases it back to the pool on close. */ public class TableSessionWrapper implements ITableSession { private static final Logger LOGGER = LoggerFactory.getLogger(TableSessionWrapper.class); @@ -52,13 +46,13 @@ public class TableSessionWrapper implements ITableSession { protected TableSessionWrapper(Session session, SessionPool sessionPool) { this.session = session; this.sessionPool = sessionPool; - this.closed = false; } @Override public void insert(Tablet tablet) throws StatementExecutionException, IoTDBConnectionException { + Session currentSession = getOpenSession(); try { - session.insertRelationalTablet(tablet); + currentSession.insertRelationalTablet(tablet); } catch (IoTDBConnectionException e) { throw releaseOnConnectionFailure(e); } @@ -67,8 +61,9 @@ public void insert(Tablet tablet) throws StatementExecutionException, IoTDBConne @Override public SessionDataSet executeQueryStatement(String sql) throws StatementExecutionException, IoTDBConnectionException { + Session currentSession = getOpenSession(); try { - return session.executeQueryStatement(sql); + return currentSession.executeQueryStatement(sql); } catch (IoTDBConnectionException e) { throw releaseOnConnectionFailure(e); } @@ -77,8 +72,9 @@ public SessionDataSet executeQueryStatement(String sql) @Override public SessionDataSet executeQueryStatement(String sql, long timeoutInMs) throws StatementExecutionException, IoTDBConnectionException { + Session currentSession = getOpenSession(); try { - return session.executeQueryStatement(sql, timeoutInMs); + return currentSession.executeQueryStatement(sql, timeoutInMs); } catch (IoTDBConnectionException e) { throw releaseOnConnectionFailure(e); } @@ -87,8 +83,9 @@ public SessionDataSet executeQueryStatement(String sql, long timeoutInMs) @Override public void executeNonQueryStatement(String sql) throws IoTDBConnectionException, StatementExecutionException { + Session currentSession = getOpenSession(); try { - session.executeNonQueryStatement(sql); + currentSession.executeNonQueryStatement(sql); } catch (IoTDBConnectionException e) { throw releaseOnConnectionFailure(e); } @@ -106,10 +103,6 @@ public void close() throws IoTDBConnectionException { session = null; } - if (sessionToRelease == null) { - return; - } - if (sessionPool.isPoolClosed()) { sessionPool.releaseSession(sessionToRelease, false); return; @@ -151,6 +144,15 @@ public void close() throws IoTDBConnectionException { sessionPool.releaseSession(sessionToRelease, reusable); } + private Session getOpenSession() throws IoTDBConnectionException { + synchronized (this) { + if (closed || session == null) { + throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN); + } + return session; + } + } + private IoTDBConnectionException releaseOnConnectionFailure(IoTDBConnectionException exception) { final Session failedSession; synchronized (this) { 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 2c93d04cb077b..9e951bbc62a3b 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 @@ -174,6 +174,17 @@ public void testCloseDoesNotCloseBorrowedNodeSupplier() throws Exception { Mockito.verify(availableNodes, Mockito.never()).close(); } + @Test + public void testForceCloseDoesNotWaitForCloseSessionRpc() { + Whitebox.setInternalState(session, "isClosed", false); + + session.forceClose(); + + Mockito.verify(sessionConnection).forceCloseTransport(); + Mockito.verify(sessionConnection, Mockito.never()).close(); + Assert.assertNull(Whitebox.getInternalState(session, "defaultSessionConnection")); + } + @Test public void testBuildSession() { Session session1 = diff --git a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java index 9aafc9c3c3e6f..3f25f671dd849 100644 --- a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java +++ b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java @@ -81,6 +81,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.Assert.assertEquals; @@ -136,7 +137,34 @@ public ISession poll() { } private void awaitPolled() throws InterruptedException { - assertTrue(polled.await(5, TimeUnit.SECONDS)); + assertTrue("Borrow did not reach queue.poll()", polled.await(5, TimeUnit.SECONDS)); + } + } + + private static class MockReplacementSessionPool extends SessionPool { + + private final Session replacementSession; + + private MockReplacementSessionPool(Session replacementSession) { + super( + new SessionPool.Builder() + .host("host") + .port(11) + .user("user") + .password("password123456") + .maxSize(1) + .enableAutoFetch(false)); + this.replacementSession = replacementSession; + } + + @Override + protected Session.Builder createSessionBuilder() { + return new Session.Builder() { + @Override + public Session build() { + return replacementSession; + } + }; } } @@ -1259,31 +1287,119 @@ public void testSortTablet() throws IoTDBConnectionException, StatementExecution ((ConcurrentLinkedDeque) Whitebox.getInternalState(sessionPool, "queue")).size()); } - @Test - public void testSortTabletExceptionReturnsSessionToPool() throws IoTDBConnectionException { - List schemas = new ArrayList<>(); - MeasurementSchema schema = new MeasurementSchema(); - schema.setMeasurementName("pressure"); - schema.setDataType(TSDataType.BOOLEAN); - schema.setCompressionType(CompressionType.SNAPPY); - schema.setEncoding(TSEncoding.PLAIN); - schemas.add(schema); - long[] timestamp = new long[] {1L, 2L}; - Object[] values = new Object[] {true, false}; - BitMap[] partBitMap = new BitMap[2]; - Tablet tablet = new Tablet("device", schemas, timestamp, values, partBitMap, 2); - Mockito.doThrow(new IllegalArgumentException("sort failed")).when(session).sortTablet(tablet); + @Test(timeout = 10_000) + public void testPoolCloseDoesNotBlockBorrowingOnClosingSession() throws Exception { + SessionPool concreteSessionPool = (SessionPool) sessionPool; + ITableSession tableSession = concreteSessionPool.getPooledTableSession(); + CountDownLatch closeStarted = new CountDownLatch(1); + CountDownLatch allowClose = new CountDownLatch(1); + Mockito.doAnswer( + ignored -> { + closeStarted.countDown(); + assertTrue(allowClose.await(5, TimeUnit.SECONDS)); + return null; + }) + .when(session) + .forceClose(); + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future closeFuture = executor.submit(concreteSessionPool::close); + assertTrue( + "Pool close did not reach session.forceClose()", closeStarted.await(5, TimeUnit.SECONDS)); + + Future borrowFuture = + executor.submit(() -> concreteSessionPool.getPooledTableSession()); + try { + borrowFuture.get(2, TimeUnit.SECONDS); + Assert.fail("Expected the borrow to fail after the pool closed"); + } catch (TimeoutException e) { + Assert.fail("Pool close held the monitor while closing a session"); + } catch (ExecutionException e) { + assertTrue( + "Expected IoTDBConnectionException but got " + e.getCause(), + e.getCause() instanceof IoTDBConnectionException); + } + allowClose.countDown(); + closeFuture.get(5, TimeUnit.SECONDS); + Mockito.verify(session).forceClose(); + } finally { + allowClose.countDown(); + tableSession.close(); + executor.shutdownNow(); + } + } + + @Test + public void testRestoreFailureKeepsPoolCapacity() throws Exception { + Session replacementSession = Mockito.mock(Session.class); + SessionPool concreteSessionPool = new MockReplacementSessionPool(replacementSession); + ConcurrentLinkedDeque queue = new ConcurrentLinkedDeque<>(); + queue.add(session); + Whitebox.setInternalState(concreteSessionPool, "queue", queue); + Whitebox.setInternalState(concreteSessionPool, "size", 1); + Mockito.when(session.getSqlDialect()).thenReturn(concreteSessionPool.sqlDialect + "_borrowed"); + Mockito.doThrow(new StatementExecutionException("restore failed")) + .when(session) + .executeNonQueryStatement(anyString()); try { - sessionPool.sortTablet(tablet); - Assert.fail("Expected sortTablet to throw"); - } catch (IllegalArgumentException expected) { - // expected + concreteSessionPool.getPooledTableSession().close(); + + assertEquals(1, concreteSessionPool.currentAvailableSize()); + assertEquals(0, concreteSessionPool.currentOccupiedSize()); + assertEquals( + 1, ((Integer) Whitebox.getInternalState(concreteSessionPool, "size")).intValue()); + assertEquals( + replacementSession, + ((ConcurrentLinkedDeque) + Whitebox.getInternalState(concreteSessionPool, "queue")) + .peek()); + Mockito.verify(session).close(); + } finally { + concreteSessionPool.close(); } + } - assertEquals( - 1, - ((ConcurrentLinkedDeque) Whitebox.getInternalState(sessionPool, "queue")).size()); + @Test + public void testConnectionFailureReleasesCapacityAndClosesWrapper() throws Exception { + Session replacementSession = Mockito.mock(Session.class); + SessionPool concreteSessionPool = new MockReplacementSessionPool(replacementSession); + ConcurrentLinkedDeque queue = new ConcurrentLinkedDeque<>(); + queue.add(session); + Whitebox.setInternalState(concreteSessionPool, "queue", queue); + Whitebox.setInternalState(concreteSessionPool, "size", 1); + Mockito.doThrow(new IoTDBConnectionException("connection failed")) + .when(session) + .executeNonQueryStatement("fail"); + ITableSession tableSession = concreteSessionPool.getPooledTableSession(); + try { + try { + tableSession.executeNonQueryStatement("fail"); + Assert.fail("Expected the first operation to fail"); + } catch (IoTDBConnectionException expected) { + // expected + } + + try { + tableSession.executeNonQueryStatement("retry"); + Assert.fail("Expected using the failed wrapper to fail"); + } catch (IoTDBConnectionException expected) { + // expected + } + + assertEquals(1, concreteSessionPool.currentAvailableSize()); + assertEquals(0, concreteSessionPool.currentOccupiedSize()); + assertEquals( + 1, ((Integer) Whitebox.getInternalState(concreteSessionPool, "size")).intValue()); + assertEquals( + replacementSession, + ((ConcurrentLinkedDeque) + Whitebox.getInternalState(concreteSessionPool, "queue")) + .peek()); + Mockito.verify(session).close(); + } finally { + concreteSessionPool.close(); + } } @Test(timeout = 10_000) @@ -1304,7 +1420,9 @@ public void testTableSessionBorrowRacingWithPoolClose() throws Exception { borrowFuture.get(); Assert.fail("Expected the borrow to fail after the pool closed"); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof IoTDBConnectionException); + assertTrue( + "Expected IoTDBConnectionException but got " + e.getCause(), + e.getCause() instanceof IoTDBConnectionException); } Mockito.verify(session).close(); } finally { @@ -1316,13 +1434,14 @@ public void testTableSessionBorrowRacingWithPoolClose() throws Exception { public void testTableSessionReleaseRacingWithPoolClose() throws Exception { SessionPool concreteSessionPool = (SessionPool) sessionPool; Mockito.when(session.getSqlDialect()).thenReturn(concreteSessionPool.sqlDialect); + concreteSessionPool.database = "database"; CountDownLatch aboutToRelease = new CountDownLatch(1); CountDownLatch continueRelease = new CountDownLatch(1); Mockito.when(session.getDatabase()) .thenAnswer( ignored -> { aboutToRelease.countDown(); - assertTrue(continueRelease.await(5, TimeUnit.SECONDS)); + assertTrue("Release latch timed out", continueRelease.await(5, TimeUnit.SECONDS)); return null; }); ITableSession tableSession = concreteSessionPool.getPooledTableSession(); @@ -1334,7 +1453,8 @@ public void testTableSessionReleaseRacingWithPoolClose() throws Exception { tableSession.close(); return null; }); - assertTrue(aboutToRelease.await(5, TimeUnit.SECONDS)); + assertTrue( + "Wrapper did not reach release boundary", aboutToRelease.await(5, TimeUnit.SECONDS)); // The wrapper has observed an open pool and is paused immediately before releaseSession(). concreteSessionPool.close(); From 2b002f7b0fb0c33e356a61c76b520bc608c7018f Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Mon, 16 Nov 2026 15:42:34 +0800 Subject: [PATCH 7/9] Reject tree query leases after pool close Prevent tree-model result queries from registering a session in occupied after the pool has closed. --- .../iotdb/session/pool/SessionPool.java | 6 ++- .../iotdb/session/pool/SessionPoolTest.java | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java index dc24af2bcb743..dd840a21b91a3 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java @@ -879,7 +879,11 @@ boolean isPoolClosed() { } } - private void occupy(ISession session) { + private synchronized void occupy(ISession session) throws IoTDBConnectionException { + if (closed) { + closeSession(session); + throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); + } occupied.put(session, session); } diff --git a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java index 3f25f671dd849..1a05b5f4e9e8a 100644 --- a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java +++ b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java @@ -1402,6 +1402,44 @@ public void testConnectionFailureReleasesCapacityAndClosesWrapper() throws Excep } } + @Test(timeout = 10_000) + public void testTreeQueryDoesNotEnterOccupiedAfterPoolClose() throws Exception { + SessionPool concreteSessionPool = (SessionPool) sessionPool; + CountDownLatch queryStarted = new CountDownLatch(1); + CountDownLatch continueQuery = new CountDownLatch(1); + Mockito.when(session.executeQueryStatement("blocked")) + .thenAnswer( + ignored -> { + queryStarted.countDown(); + assertTrue("Query latch timed out", continueQuery.await(5, TimeUnit.SECONDS)); + return Mockito.mock(SessionDataSet.class); + }); + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + Future queryFuture = + executor.submit(() -> concreteSessionPool.executeQueryStatement("blocked")); + assertTrue("Query did not start", queryStarted.await(5, TimeUnit.SECONDS)); + + concreteSessionPool.close(); + continueQuery.countDown(); + + try { + queryFuture.get(); + Assert.fail("Expected the query to fail after the pool closed"); + } catch (ExecutionException e) { + assertTrue( + "Expected IoTDBConnectionException but got " + e.getCause(), + e.getCause() instanceof IoTDBConnectionException); + } + assertEquals(0, concreteSessionPool.currentAvailableSize()); + assertEquals(0, concreteSessionPool.currentOccupiedSize()); + Mockito.verify(session, Mockito.atLeastOnce()).close(); + } finally { + continueQuery.countDown(); + executor.shutdownNow(); + } + } + @Test(timeout = 10_000) public void testTableSessionBorrowRacingWithPoolClose() throws Exception { SessionPool concreteSessionPool = (SessionPool) sessionPool; From cb53a75a3d62f77603364f31cc4e4ab9421bd30c Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Mon, 16 Nov 2026 15:49:54 +0800 Subject: [PATCH 8/9] Fix session pool test compilation --- .../src/test/java/org/apache/iotdb/session/SessionTest.java | 2 +- .../java/org/apache/iotdb/session/pool/SessionPoolTest.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) 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 9e951bbc62a3b..c9160f82cb18c 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 @@ -178,7 +178,7 @@ public void testCloseDoesNotCloseBorrowedNodeSupplier() throws Exception { public void testForceCloseDoesNotWaitForCloseSessionRpc() { Whitebox.setInternalState(session, "isClosed", false); - session.forceClose(); + ((Session) session).forceClose(); Mockito.verify(sessionConnection).forceCloseTransport(); Mockito.verify(sessionConnection, Mockito.never()).close(); diff --git a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java index 1a05b5f4e9e8a..bcbf1c094b08e 100644 --- a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java +++ b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java @@ -87,6 +87,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; From f87aaff1df9f38312cd14fdf387c177fb09c2fe4 Mon Sep 17 00:00:00 2001 From: shuwenwei Date: Mon, 16 Nov 2026 16:08:52 +0800 Subject: [PATCH 9/9] Declare checked exception in force close test --- .../src/test/java/org/apache/iotdb/session/SessionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c9160f82cb18c..0b145e3ce8d8f 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 @@ -175,7 +175,7 @@ public void testCloseDoesNotCloseBorrowedNodeSupplier() throws Exception { } @Test - public void testForceCloseDoesNotWaitForCloseSessionRpc() { + public void testForceCloseDoesNotWaitForCloseSessionRpc() throws IoTDBConnectionException { Whitebox.setInternalState(session, "isClosed", false); ((Session) session).forceClose();