/**
* return an idle session
*/
private synchronized NebulaSession getSession() throws ClientServerIncompatibleException,
AuthFailedException, IOErrorException, BindSpaceFailedException {
int retry = sessionPoolConfig.getRetryConnectTimes();
while (retry-- >= 0) {
// if there are idle sessions, get session from queue
if (idleSessionSize.get() > 0) {
for (NebulaSession nebulaSession : sessionList) {
if (nebulaSession.isIdleAndSetUsed()) {
int currentIdleSessionSize = idleSessionSize.decrementAndGet();
log.info("ng session {} {}", currentIdleSessionSize, nebulaSession.getSessionID());
return nebulaSession;
}
}
}
// if session size is less than max size, get session from pool
if (sessionList.size() < maxSessionSize) {
return createSessionObject(SessionState.USED);
}
// there's no available session, wait for SessionPoolConfig.getWaitTime and re-get
try {
Thread.sleep(sessionPoolConfig.getWaitTime());
} catch (InterruptedException e) {
log.error("getSession error when wait for idle sessions, ", e);
throw new RuntimeException(e);
}
}
// if session size is equal to max size and no idle session here, throw exception
throw new RuntimeException("no extra session available");
}
可以调整从sessionList中随机取出nebulaSession
java复制代码
/**
* return an idle session
*/
private synchronized NebulaSession getSession() throws ClientServerIncompatibleException,
AuthFailedException, IOErrorException, BindSpaceFailedException {
int retry = sessionPoolConfig.getRetryConnectTimes();
while (retry-- >= 0) {
// if there are idle sessions, get session from queue
if (idleSessionSize.get() > 0) {
int[] randomInts = RandomUtil.randomInts(sessionList.size());
for (int randomInt : randomInts) {
NebulaSession nebulaSession = sessionList.get(randomInt);
if (nebulaSession.isIdleAndSetUsed()) {
int currentIdleSessionSize = idleSessionSize.decrementAndGet();
log.debug("ng session {} {}", currentIdleSessionSize, nebulaSession.getSessionID());
return nebulaSession;
}
}
}
// if session size is less than max size, get session from pool
if (sessionList.size() < maxSessionSize) {
return createSessionObject(SessionState.USED);
}
// there's no available session, wait for SessionPoolConfig.getWaitTime and re-get
try {
Thread.sleep(sessionPoolConfig.getWaitTime());
} catch (InterruptedException e) {
log.error("getSession error when wait for idle sessions, ", e);
throw new RuntimeException(e);
}
}
// if session size is equal to max size and no idle session here, throw exception
throw new RuntimeException("no extra session available");
}