Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix to release the invalid session for SessionPool #499

Merged
merged 5 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,8 +69,12 @@ public ResultSet executeWithParameter(String stmt, Map<String, Object> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -259,4 +261,67 @@ public void testThreadSafe() {
assert failedCount.get() == 0;
}

@Test
public void testReleaseBrokenSession() {
List<HostAddress> 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();
}
}
}

}