6 package com.vesoft.nebula.client.graph.net;
8 import com.vesoft.nebula.client.graph.NebulaPoolConfig;
9 import com.vesoft.nebula.client.graph.data.HostAddress;
10 import com.vesoft.nebula.client.graph.exception.AuthFailedException;
11 import com.vesoft.nebula.client.graph.exception.ClientServerIncompatibleException;
12 import com.vesoft.nebula.client.graph.exception.IOErrorException;
13 import com.vesoft.nebula.client.graph.exception.InvalidConfigException;
14 import com.vesoft.nebula.client.graph.exception.NotValidConnectionException;
15 import java.io.Serializable;
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.atomic.AtomicBoolean;
21 import org.apache.commons.pool2.impl.AbandonedConfig;
22 import org.apache.commons.pool2.impl.BaseObjectPoolConfig;
23 import org.apache.commons.pool2.impl.GenericObjectPool;
24 import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
30 private static final long serialVersionUID = 6226487268001127885L;
32 private GenericObjectPool<SyncConnection> objectPool =
null;
34 private final Logger log = LoggerFactory.getLogger(this.getClass());
36 private int waitTime = 0;
37 private AtomicBoolean hasInit =
new AtomicBoolean(
false);
38 private AtomicBoolean isClosed =
new AtomicBoolean(
false);
41 if (config.getIdleTime() < 0) {
43 "Config idleTime:" + config.getIdleTime() +
" is illegal");
46 if (config.getMaxConnSize() <= 0) {
48 "Config maxConnSize:" + config.getMaxConnSize() +
" is illegal");
51 if (config.getMinConnSize() < 0 || config.getMinConnSize() > config.getMaxConnSize()) {
53 "Config minConnSize:" + config.getMinConnSize() +
" is illegal");
56 if (config.getTimeout() < 0) {
58 "Config timeout:" + config.getTimeout() +
" is illegal");
61 if (config.getWaitTime() < 0) {
63 "Config waitTime:" + config.getWaitTime() +
" is illegal");
66 if (config.getMinClusterHealthRate() < 0) {
68 "Config minClusterHealthRate:" + config.getMinClusterHealthRate()
86 this.waitTime = config.getWaitTime();
87 this.loadBalancer = config.isEnableSsl()
89 config.getMinClusterHealthRate(), config.isUseHttp2(), config.getCustomHeaders())
91 config.getMinClusterHealthRate(),config.isUseHttp2(), config.getCustomHeaders());
93 this.objectPool =
new GenericObjectPool<>(objectPool);
94 GenericObjectPoolConfig objConfig =
new GenericObjectPoolConfig();
95 objConfig.setMinIdle(config.getMinConnSize());
96 objConfig.setMaxIdle(config.getMaxConnSize());
97 objConfig.setMaxTotal(config.getMaxConnSize());
98 objConfig.setTestOnBorrow(
true);
99 objConfig.setTestOnReturn(
true);
100 objConfig.setTestOnCreate(
true);
101 objConfig.setTestWhileIdle(
true);
102 objConfig.setTimeBetweenEvictionRunsMillis(config.getIntervalIdle() <= 0
103 ? BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS
104 : config.getIntervalIdle());
105 objConfig.setSoftMinEvictableIdleTimeMillis(config.getIdleTime() <= 0
106 ? BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS
107 : config.getIdleTime());
108 this.objectPool.setConfig(objConfig);
110 AbandonedConfig abandonedConfig =
new AbandonedConfig();
111 abandonedConfig.setRemoveAbandonedOnBorrow(
true);
112 this.objectPool.setAbandonedConfig(abandonedConfig);
113 return objectPool.init();
120 if (isClosed.get()) {
124 this.loadBalancer.close();
125 this.objectPool.close();
141 checkNoInitAndClosed();
144 connection = getConnection();
145 AuthResult authResult = connection.authenticate(userName, password);
146 return new Session(connection, authResult,
this, reconnect);
147 }
catch (Exception e) {
150 if (connection !=
null) {
162 checkNoInitAndClosed();
163 return objectPool.getNumActive();
171 checkNoInitAndClosed();
172 return objectPool.getNumIdle();
180 checkNoInitAndClosed();
181 return objectPool.getNumWaiters();
189 checkNoInitAndClosed();
200 checkNoInitAndClosed();
202 objectPool.invalidateObject(connection);
203 }
catch (Exception e) {
204 log.error(
"Set invalidate object failed");
213 checkNoInitAndClosed();
214 objectPool.returnObject(connection);
218 checkNoInitAndClosed();
220 return objectPool.borrowObject(waitTime);
221 }
catch (Exception e) {
222 throw new NotValidConnectionException(e.getMessage());
226 private void checkNoInit() throws RuntimeException {
227 if (!hasInit.get()) {
228 throw new RuntimeException(
229 "The pool has not been initialized, please initialize it first.");
233 private void checkInit() throws RuntimeException {
235 throw new RuntimeException(
236 "The pool has already been initialized. "
237 +
"Please do not initialize the pool repeatedly.");
241 private void checkNoInitAndClosed() throws RuntimeException {
246 private void checkClosed() throws RuntimeException {
247 if (isClosed.get()) {
248 throw new RuntimeException(
"The pool has closed. Couldn't use again.");
int getActiveConnNum()
Get the number of connections was used by users.
int getIdleConnNum()
Get the number of free connections in the pool.
void updateServerStatus()
Update the services' status when the connection is broken, it is called by Session and NebulaPool.
void setInvalidateConnection(SyncConnection connection)
Set the connection is invalidate, and the object pool will destroy it.
Session getSession(String userName, String password, boolean reconnect)
get a session from the NebulaPool
int getWaitersNum()
Get the number of waits in a waiting get connection.
void close()
close the pool, all connections will be closed
boolean init(List< HostAddress > addresses, NebulaPoolConfig config)
void returnConnection(SyncConnection connection)
Return the connection to object pool.
The Session is an object that operates with nebula-graph.