Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,27 +692,9 @@ public synchronized void close() throws IoTDBConnectionException {
return;
}

Set<SessionConnection> 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;
Expand All @@ -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) {
Expand All @@ -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<SessionConnection> getSessionConnections() {
Set<SessionConnection> 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) {
Expand Down Expand Up @@ -3810,10 +3825,15 @@ private boolean checkSorted(List<Long> 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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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])) {
Expand Down
Loading