This is an automated email from the ASF dual-hosted git repository. shuwenwei pushed a commit to branch fix/table-session-pool-lifecycle in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit a6598aecc0a692ea41f1cec9a2d01ff75159e921 Author: shuwenwei <[email protected]> AuthorDate: Thu Nov 12 16:53:00 2026 +0800 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. --- .../session/pool/IoTDBTableModelSessionPoolIT.java | 30 +++++ .../org/apache/iotdb/session/pool/SessionPool.java | 129 +++++++++++++++------ .../iotdb/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 3e2cba94c4c..31d13362faa 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 class IoTDBTableModelSessionPoolIT { 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 9585d65c390..b5f9f8ea292 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 @@ public class SessionPool implements ISessionPool { // 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 @@ public class SessionPool implements ISessionPool { 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 @@ public class SessionPool implements ISessionPool { } 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 class SessionPool implements ISessionPool { @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 @@ public class SessionPool implements ISessionPool { /** 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 class SessionPool implements ISessionPool { this.availableNodes = null; } LOGGER.info(SessionMessages.CLOSING_SESSION_POOL); - this.closed = true; - queue.clear(); - occupied.clear(); } @Override @@ -876,18 +929,20 @@ public class SessionPool implements ISessionPool { 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 @@ public class SessionPool implements ISessionPool { 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 @@ public class SessionPool implements ISessionPool { } 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 4cf7ca980f3..e05585d5982 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.Logger; 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 class TableSessionWrapper implements ITableSession { 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 class TableSessionWrapper implements ITableSession { 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 class TableSessionWrapper implements ITableSession { 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 class TableSessionWrapper implements ITableSession { 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; } }
