From 540b1d723bb81957551d47eff40a256948531d1a Mon Sep 17 00:00:00 2001 From: Anqi Date: Mon, 19 Dec 2022 12:14:22 +0800 Subject: [PATCH 1/4] fix to release the invalid session for SessionPool --- .../nebula/client/graph/NebulaSession.java | 12 +++- .../client/graph/net/TestSessionPool.java | 63 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) 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/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..ca2eee063 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,65 @@ 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"); + 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); + + 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); + runtime.exec("docker start nebula-docker-compose_graphd1_1") + .waitFor(5, TimeUnit.SECONDS); + TimeUnit.SECONDS.sleep(5); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } From 71de42c979c2948e7dbed60d256c40c57e7f982a Mon Sep 17 00:00:00 2001 From: Anqi Date: Mon, 19 Dec 2022 18:52:57 +0800 Subject: [PATCH 2/4] add unit for config --- .../java/com/vesoft/nebula/client/graph/SessionPoolConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From caa03888f7362c9953190de6928a057040481e42 Mon Sep 17 00:00:00 2001 From: Anqi Date: Mon, 19 Dec 2022 18:53:06 +0800 Subject: [PATCH 3/4] update test --- .../com/vesoft/nebula/client/graph/net/TestSessionPool.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 ca2eee063..00439dfb3 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 @@ -267,6 +267,7 @@ public void testReleaseBrokenSession() { 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(); @@ -291,6 +292,9 @@ public void testReleaseBrokenSession() { p.waitFor(5, TimeUnit.SECONDS); ProcessUtil.printProcessStatus(cmd, p); + // sleep 6 seconds to process the healthy check schedule task + Thread.sleep(6); + for (int i = 0; i < 10; i++) { try { ResultSet resultSet = sessionPool.execute("SHOW SPACES;"); @@ -313,8 +317,6 @@ public void testReleaseBrokenSession() { try { runtime.exec("docker start nebula-docker-compose_graphd0_1") .waitFor(5, TimeUnit.SECONDS); - runtime.exec("docker start nebula-docker-compose_graphd1_1") - .waitFor(5, TimeUnit.SECONDS); TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); From bacd66adee5fcdb8dbc3cd5b9e95459a967bf7f1 Mon Sep 17 00:00:00 2001 From: Anqi Date: Tue, 20 Dec 2022 09:22:27 +0800 Subject: [PATCH 4/4] update test --- .../com/vesoft/nebula/client/graph/net/TestSessionPool.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 00439dfb3..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 @@ -293,7 +293,7 @@ public void testReleaseBrokenSession() { ProcessUtil.printProcessStatus(cmd, p); // sleep 6 seconds to process the healthy check schedule task - Thread.sleep(6); + Thread.sleep(6000); for (int i = 0; i < 10; i++) { try {