diff --git a/client/src/main/java/com/vesoft/nebula/client/graph/NebulaSession.java b/client/src/main/java/com/vesoft/nebula/client/graph/NebulaSession.java index 99ffb8eb3..0d2e0ef9f 100644 --- a/client/src/main/java/com/vesoft/nebula/client/graph/NebulaSession.java +++ b/client/src/main/java/com/vesoft/nebula/client/graph/NebulaSession.java @@ -15,11 +15,15 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class NebulaSession implements Serializable { private static final long serialVersionUID = -88438249377120255L; + private final Logger log = LoggerFactory.getLogger(this.getClass()); + private long sessionID; private int timezoneOffset; private SyncConnection connection; @@ -65,8 +69,12 @@ public ResultSet executeWithParameter(String stmt, Map parameter } public void release() { - connection.signout(sessionID); - connection.close(); + try { + connection.signout(sessionID); + connection.close(); + } catch (Exception e) { + log.warn("release session failed, " + e.getMessage()); + } connection = null; } } diff --git a/client/src/main/java/com/vesoft/nebula/client/graph/SessionPoolConfig.java b/client/src/main/java/com/vesoft/nebula/client/graph/SessionPoolConfig.java index 7e5f603ff..8ba20fd11 100644 --- a/client/src/main/java/com/vesoft/nebula/client/graph/SessionPoolConfig.java +++ b/client/src/main/java/com/vesoft/nebula/client/graph/SessionPoolConfig.java @@ -32,7 +32,7 @@ public class SessionPoolConfig implements Serializable { // must be less than NebulaGraph's session_idle_timeout_secs, unit: second private int cleanTime = 3600; - // The healthCheckTime for schedule check the health of session + // The healthCheckTime for schedule check the health of session, unit: second private int healthCheckTime = 600; // The wait time to get idle connection, unit ms diff --git a/client/src/test/java/com/vesoft/nebula/client/graph/net/TestSessionPool.java b/client/src/test/java/com/vesoft/nebula/client/graph/net/TestSessionPool.java index e69c2a422..a8b6d223d 100644 --- a/client/src/test/java/com/vesoft/nebula/client/graph/net/TestSessionPool.java +++ b/client/src/test/java/com/vesoft/nebula/client/graph/net/TestSessionPool.java @@ -14,12 +14,14 @@ import com.vesoft.nebula.client.graph.exception.BindSpaceFailedException; import com.vesoft.nebula.client.graph.exception.ClientServerIncompatibleException; import com.vesoft.nebula.client.graph.exception.IOErrorException; +import com.vesoft.nebula.client.util.ProcessUtil; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -259,4 +261,67 @@ public void testThreadSafe() { assert failedCount.get() == 0; } + @Test + public void testReleaseBrokenSession() { + List addresses = Arrays.asList(new HostAddress(ip, 9669), + new HostAddress(ip, 9670), new HostAddress(ip, 9671)); + SessionPoolConfig config = new SessionPoolConfig(addresses, "space_for_session_pool", + "root", "nebula"); + config.setHealthCheckTime(5); + SessionPool sessionPool = new SessionPool(config); + assert sessionPool.init(); + + Runtime runtime = Runtime.getRuntime(); + try { + for (int i = 0; i < 10; i++) { + try { + ResultSet resultSet = sessionPool.execute("SHOW SPACES;"); + if (!resultSet.isSucceeded()) { + System.out.println("show spaces failed, ErrorCode:" + + resultSet.getErrorCode() + " ErrorMessageļ¼š" + + resultSet.getErrorMessage()); + } + } catch (Exception e) { + e.printStackTrace(); + assert false; + } + } + + String cmd = "docker stop nebula-docker-compose_graphd0_1"; + Process p = runtime.exec(cmd); + p.waitFor(5, TimeUnit.SECONDS); + ProcessUtil.printProcessStatus(cmd, p); + + // sleep 6 seconds to process the healthy check schedule task + Thread.sleep(6000); + + for (int i = 0; i < 10; i++) { + try { + ResultSet resultSet = sessionPool.execute("SHOW SPACES;"); + if (!resultSet.isSucceeded()) { + System.out.println("show spaces failed, ErrorCode:" + + resultSet.getErrorCode() + " ErrorMessageļ¼š" + + resultSet.getErrorMessage()); + } + } catch (Exception e) { + e.printStackTrace(); + assert false; + } + } + + sessionPool.close(); + } catch (Exception e) { + e.printStackTrace(); + Assert.assertFalse(e.getMessage(), false); + } finally { + try { + runtime.exec("docker start nebula-docker-compose_graphd0_1") + .waitFor(5, TimeUnit.SECONDS); + TimeUnit.SECONDS.sleep(5); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }