/**
* return an idle session
*/privatesynchronizedNebulaSessiongetSession()throwsClientServerIncompatibleException,AuthFailedException,IOErrorException,BindSpaceFailedException{int retry = sessionPoolConfig.getRetryConnectTimes();while(retry-->=0){// if there are idle sessions, get session from queueif(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 poolif(sessionList.size()< maxSessionSize){returncreateSessionObject(SessionState.USED);}// there's no available session, wait for SessionPoolConfig.getWaitTime and re-gettry{Thread.sleep(sessionPoolConfig.getWaitTime());}catch(InterruptedException e){
log.error("getSession error when wait for idle sessions, ", e);thrownewRuntimeException(e);}}// if session size is equal to max size and no idle session here, throw exceptionthrownewRuntimeException("no extra session available");}
可以调整从sessionList中随机取出nebulaSession
/**
* return an idle session
*/privatesynchronizedNebulaSessiongetSession()throwsClientServerIncompatibleException,AuthFailedException,IOErrorException,BindSpaceFailedException{int retry = sessionPoolConfig.getRetryConnectTimes();while(retry-->=0){// if there are idle sessions, get session from queueif(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 poolif(sessionList.size()< maxSessionSize){returncreateSessionObject(SessionState.USED);}// there's no available session, wait for SessionPoolConfig.getWaitTime and re-gettry{Thread.sleep(sessionPoolConfig.getWaitTime());}catch(InterruptedException e){
log.error("getSession error when wait for idle sessions, ", e);thrownewRuntimeException(e);}}// if session size is equal to max size and no idle session here, throw exceptionthrownewRuntimeException("no extra session available");}