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/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 86d1106348046..88464408aaf3d 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; @@ -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 @@ -835,43 +849,84 @@ protected void putBack(ISession session) { closeSession(session); } - private void occupy(ISession 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 + */ + boolean releaseSession(ISession session, boolean reusable) { + boolean wasOccupied; + boolean replenish = false; + synchronized (this) { + wasOccupied = occupied.remove(session) != null; + if (reusable && !closed) { + if (!wasOccupied) { + return false; + } + putBack(session); + return true; + } + replenish = !closed; + } + + closeSession(session); + if (replenish) { + tryConstructNewSession(); + } + return wasOccupied; + } + + boolean isPoolClosed() { + synchronized (this) { + return closed; + } + } + + private synchronized void occupy(ISession session) throws IoTDBConnectionException { + if (closed) { + closeSession(session); + throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED); + } occupied.put(session, session); } /** close all connections in the pool */ @Override - public synchronized void close() { - for (ISession session : queue) { - try { - session.close(); - } catch (IoTDBConnectionException e) { - // do nothing - LOGGER.warn(CLOSE_THE_SESSION_FAILED, e); + 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(); } - for (ISession session : occupied.keySet()) { - try { - session.close(); - } catch (IoTDBConnectionException e) { - // do nothing - LOGGER.warn(CLOSE_THE_SESSION_FAILED, e); - } + + for (ISession session : idleSessions) { + closeSession(session); } - if (this.executorService != null) { - this.executorService.shutdown(); - this.executorService = null; + for (ISession session : borrowedSessions) { + forceCloseSession(session); } - if (availableNodes != null) { - if (!this.availableNodes.isCallerManaged()) { - this.availableNodes.close(); - } - this.availableNodes = null; + if (executorToShutdown != null) { + executorToShutdown.shutdown(); + } + if (nodesToClose != null && !nodesToClose.isCallerManaged()) { + nodesToClose.close(); } LOGGER.info(SessionMessages.CLOSING_SESSION_POOL); - this.closed = true; - queue.clear(); - occupied.clear(); } @Override @@ -884,21 +939,14 @@ public void closeResultSet(SessionDataSetWrapper wrapper) { } // Detach before returning the session, so closing an old wrapper cannot release a new query. wrapper.setSession(null); - session = occupied.remove(session); - } - if (session == null) { - return; } boolean putback = true; try { wrapper.getSessionDataSet().closeOperationHandle(); } catch (IoTDBConnectionException | StatementExecutionException e) { putback = false; - cleanSessionAndMayThrowConnectionException(session); } finally { - if (putback) { - putBack(session); - } + releaseSession(session, putback); } } @@ -949,10 +997,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 { - closeSession(session); - tryConstructNewSession(); + releaseSession(session, false); if (times == FINAL_RETRY) { throw new IoTDBConnectionException( String.format( @@ -964,9 +1023,12 @@ private void cleanSessionAndMayThrowConnectionException( } } + /** + * @deprecated Use the pool's internal release path. + */ + @Deprecated protected void cleanSessionAndMayThrowConnectionException(ISession session) { - closeSession(session); - tryConstructNewSession(); + releaseSession(session, false); } /** @@ -2992,10 +3054,8 @@ public List showPathsTemplateUsingOn(String templateName) } @Override - public void sortTablet(Tablet tablet) throws IoTDBConnectionException { - ISession session = getSession(); - session.sortTablet(tablet); - 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 38b927e386780..efff55393dcd6 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,15 +31,8 @@ import org.slf4j.LoggerFactory; import java.util.Objects; -import java.util.concurrent.atomic.AtomicBoolean; -/** - * 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); @@ -48,107 +41,121 @@ 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); } @Override public synchronized void insert(Tablet tablet) throws StatementExecutionException, IoTDBConnectionException { + Session currentSession = getOpenSession(); try { - session.insertRelationalTablet(tablet); + currentSession.insertRelationalTablet(tablet); } catch (IoTDBConnectionException e) { - sessionPool.cleanSessionAndMayThrowConnectionException(session); - closed.set(true); - session = null; - throw e; + throw releaseOnConnectionFailure(e); } } @Override public synchronized SessionDataSet executeQueryStatement(String sql) throws StatementExecutionException, IoTDBConnectionException { + Session currentSession = getOpenSession(); try { - return session.executeQueryStatement(sql); + return currentSession.executeQueryStatement(sql); } catch (IoTDBConnectionException e) { - sessionPool.cleanSessionAndMayThrowConnectionException(session); - closed.set(true); - session = null; - throw e; + throw releaseOnConnectionFailure(e); } } @Override public synchronized 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) { - sessionPool.cleanSessionAndMayThrowConnectionException(session); - closed.set(true); - session = null; - throw e; + throw releaseOnConnectionFailure(e); } } @Override public synchronized void executeNonQueryStatement(String sql) throws IoTDBConnectionException, StatementExecutionException { + Session currentSession = getOpenSession(); try { - session.executeNonQueryStatement(sql); + currentSession.executeNonQueryStatement(sql); } catch (IoTDBConnectionException e) { - sessionPool.cleanSessionAndMayThrowConnectionException(session); - closed.set(true); - session = null; - throw e; + throw releaseOnConnectionFailure(e); } } @Override public synchronized void close() throws IoTDBConnectionException { - if (!closed.compareAndSet(false, true)) { + if (closed) { return; } - - Session closingSession = session; + closed = true; + final Session sessionToRelease = session; session = null; + + if (sessionPool.isPoolClosed()) { + sessionPool.releaseSession(sessionToRelease, false); + return; + } + + boolean reusable = true; try { - if (!Objects.equals(closingSession.getSqlDialect(), sessionPool.sqlDialect)) { + if (!Objects.equals(sessionToRelease.getSqlDialect(), sessionPool.sqlDialect)) { try { - closingSession.executeNonQueryStatement("set sql_dialect=" + sessionPool.sqlDialect); + 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); - sessionPool.cleanSessionAndMayThrowConnectionException(closingSession); - return; + reusable = false; } } - if (!Objects.equals(closingSession.getDatabase(), sessionPool.database) + if (reusable + && !Objects.equals(sessionToRelease.getDatabase(), sessionPool.database) && sessionPool.database != null) { try { - closingSession.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); - sessionPool.cleanSessionAndMayThrowConnectionException(closingSession); - return; + reusable = false; } } - - sessionPool.putBack(closingSession); - } catch (IoTDBConnectionException e) { - sessionPool.cleanSessionAndMayThrowConnectionException(closingSession); + } catch (IoTDBConnectionException | RuntimeException e) { + sessionPool.releaseSession(sessionToRelease, false); throw e; } + + sessionPool.releaseSession(sessionToRelease, reusable); + } + + private synchronized Session getOpenSession() throws IoTDBConnectionException { + if (closed || session == null) { + throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN); + } + return session; + } + + private IoTDBConnectionException releaseOnConnectionFailure(IoTDBConnectionException exception) { + closed = true; + final Session failedSession = session; + session = null; + if (failedSession != null) { + sessionPool.releaseSession(failedSession, false); + } + return exception; } } 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..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 @@ -174,6 +174,17 @@ public void testCloseDoesNotCloseBorrowedNodeSupplier() throws Exception { Mockito.verify(availableNodes, Mockito.never()).close(); } + @Test + public void testForceCloseDoesNotWaitForCloseSessionRpc() throws IoTDBConnectionException { + Whitebox.setInternalState(session, "isClosed", false); + + ((Session) 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 d08625cb978c4..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 @@ -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,10 +75,19 @@ 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.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; 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; @@ -98,6 +108,67 @@ 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("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; + } + }; + } + } + @Test public void testBuilder() { SessionPool pool = @@ -1217,6 +1288,227 @@ public void testSortTablet() throws IoTDBConnectionException, StatementExecution ((ConcurrentLinkedDeque) Whitebox.getInternalState(sessionPool, "queue")).size()); } + @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 { + 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(); + } + } + + @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) + 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; + 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( + "Expected IoTDBConnectionException but got " + e.getCause(), + 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); + concreteSessionPool.database = "database"; + CountDownLatch aboutToRelease = new CountDownLatch(1); + CountDownLatch continueRelease = new CountDownLatch(1); + Mockito.when(session.getDatabase()) + .thenAnswer( + ignored -> { + aboutToRelease.countDown(); + assertTrue("Release latch timed out", continueRelease.await(5, TimeUnit.SECONDS)); + return null; + }); + ITableSession tableSession = concreteSessionPool.getPooledTableSession(); + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + Future closeFuture = + executor.submit( + () -> { + tableSession.close(); + return null; + }); + 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(); + 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");