use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project zuliasearch by zuliaio.
the class ZuliaPool method execute.
public <R extends Result> R execute(BaseCommand<R> command) throws Exception {
int tries = 0;
while (true) {
ZuliaConnection zuliaConnection;
Node selectedNode = null;
try {
if (routingEnabled && (indexRouting != null)) {
if (command instanceof ShardRoutableCommand) {
ShardRoutableCommand rc = (ShardRoutableCommand) command;
selectedNode = indexRouting.getNode(rc.getIndexName(), rc.getUniqueId());
} else if (command instanceof SingleIndexRoutableCommand) {
SingleIndexRoutableCommand sirc = (SingleIndexRoutableCommand) command;
selectedNode = indexRouting.getRandomNode(sirc.getIndexName());
} else if (command instanceof MultiIndexRoutableCommand) {
MultiIndexRoutableCommand mirc = (MultiIndexRoutableCommand) command;
selectedNode = indexRouting.getRandomNode(mirc.getIndexNames());
}
}
if (selectedNode == null) {
// stop array index out bounds on updates without locking
List<Node> tempList = nodes;
if (tempList.isEmpty()) {
throw new IOException("There are no active nodes");
}
int randomNodeIndex = (int) (Math.random() * tempList.size());
selectedNode = tempList.get(randomNodeIndex);
}
String nodeKey = getNodeKey(selectedNode);
if (command instanceof RESTCommand) {
int restPort = selectedNode.getRestPort();
if (selectedNode.getRestPort() == 0) {
Node fullSelectedNode = nodeKeyToNode.get(nodeKey);
if (fullSelectedNode != null) {
restPort = fullSelectedNode.getRestPort();
} else {
LOG.warning("Failed to find rest port for <" + nodeKey + "> using default");
restPort = ZuliaConstants.DEFAULT_REST_SERVICE_PORT;
}
}
int finalRestPort = restPort;
final String finalServer = selectedNode.getServerAddress();
ZuliaRESTClient restClient = zuliaRestPoolMap.computeIfAbsent(nodeKey, s -> new ZuliaRESTClient(finalServer, finalRestPort));
return ((RESTCommand<R>) command).execute(restClient);
} else {
GrpcCommand<R> grpcCommand = (GrpcCommand<R>) command;
final Node finalSelectedNode = selectedNode;
GenericObjectPool<ZuliaConnection> nodePool = zuliaConnectionPoolMap.computeIfAbsent(nodeKey, (String key) -> {
//
GenericObjectPoolConfig<ZuliaConnection> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxTotal(maxConnections);
return new GenericObjectPool<>(new ZuliaConnectionFactory(finalSelectedNode, compressedConnection), poolConfig);
});
boolean valid = true;
zuliaConnection = nodePool.borrowObject();
R r;
try {
r = grpcCommand.executeTimed(zuliaConnection);
return r;
} catch (StatusRuntimeException e) {
if (!Status.INVALID_ARGUMENT.equals(e.getStatus())) {
valid = false;
}
Metadata trailers = e.getTrailers();
if (trailers.containsKey(MetaKeys.ERROR_KEY)) {
String errorMessage = trailers.get(MetaKeys.ERROR_KEY);
if (!Status.INVALID_ARGUMENT.equals(e.getStatus())) {
throw new Exception(grpcCommand.getClass().getSimpleName() + ": " + errorMessage);
} else {
throw new IllegalArgumentException(grpcCommand.getClass().getSimpleName() + ":" + errorMessage);
}
} else {
throw e;
}
} finally {
if (valid) {
nodePool.returnObject(zuliaConnection);
} else {
nodePool.invalidateObject(zuliaConnection);
}
}
}
} catch (Exception e) {
System.err.println(e.getClass().getSimpleName() + ":" + e.getMessage());
if (tries >= retries) {
throw e;
}
tries++;
}
}
}
use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project ksan by infinistor.
the class MariaDB method init.
@Override
public void init(String dbUrl, String dbPort, String dbName, String userName, String passwd, int poolSize) throws GWException {
try {
Class.forName(GWConstants.JDBC_MARIADB_DRIVER);
String jdbcUrl = GWConstants.MARIADB_URL + dbUrl + GWConstants.COLON + dbPort + GWConstants.SLASH + dbName + GWConstants.MARIADB_OPTIONS;
ConnectionFactory connFactory = new DriverManagerConnectionFactory(jdbcUrl, userName, passwd);
PoolableConnectionFactory poolableConnFactory = new PoolableConnectionFactory(connFactory, null);
poolableConnFactory.setValidationQuery(GWConstants.MARIADB_VALIDATION_QUERY);
GenericObjectPoolConfig<PoolableConnection> poolConfig = new GenericObjectPoolConfig<PoolableConnection>();
Duration timeBetweenEvictionRuns = Duration.ofMinutes(60);
poolConfig.setTimeBetweenEvictionRuns(timeBetweenEvictionRuns);
poolConfig.setTestWhileIdle(true);
poolConfig.setMinIdle(poolSize / 2);
poolConfig.setMaxTotal(poolSize);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestWhileIdle(true);
GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnFactory, poolConfig);
poolableConnFactory.setPool(connectionPool);
Class.forName(GWConstants.DBCP2_DRIVER);
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(GWConstants.JDBC_DRIVER_DBCP);
driver.registerPool(GWConstants.CONNECTION_POOL, connectionPool);
} catch (ClassNotFoundException e) {
throw new RuntimeException(GWConstants.LOG_MARIA_DB_FAIL_TO_LOAD_DRIVER, e);
} catch (SQLException e) {
throw new RuntimeException(GWConstants.LOG_MARIA_DB_FAIL_TO_LOAD_DRIVER, e);
}
createDB(dbName, userName, passwd);
createTable();
loadUser();
}
use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project today-infrastructure by TAKETODAY.
the class CommonsPool2TargetSource method createObjectPool.
/**
* Subclasses can override this if they want to return a specific Commons pool.
* They should apply any configuration properties to the pool here.
* <p>Default is a GenericObjectPool instance with the given pool size.
*
* @return an empty Commons {@code ObjectPool}.
* @see GenericObjectPool
* @see #setMaxSize
*/
protected ObjectPool createObjectPool() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(getMaxSize());
config.setMaxIdle(getMaxIdle());
config.setMinIdle(getMinIdle());
config.setMaxWaitMillis(getMaxWait());
config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
config.setBlockWhenExhausted(isBlockWhenExhausted());
return new GenericObjectPool(this, config);
}
use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project FROST-Server by FraunhoferIOSB.
the class ConnectionUtils method setupPoolingDriver.
/**
* Set up a connection pool. The driver used in the connection URI should
* already be loaded using
* Class.forName("org.apache.commons.dbcp2.PoolingDriver"); After calling
* this you can use "jdbc:apache:commons:dbcp:FROST-Pool" to connect.
*
* @param name The name of the pool to create.
* @param connectURI The URL of the database to connect to.
* @param username The username to use when connecting to the database.
* @param password The password to use when connecting to the database.
* @throws ClassNotFoundException If the PoolingDriver is not on the
* classpath.
* @throws SQLException If the dbcp driver could not be loaded.
*/
public static void setupPoolingDriver(String name, String connectURI, String username, String password) throws ClassNotFoundException, SQLException {
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
Class.forName("org.apache.commons.dbcp2.PoolingDriver");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
driver.registerPool(name, connectionPool);
}
use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project varsql by varsqlinfo.
the class ConnectionDBCP2 method createDataSource.
@Override
public void createDataSource(ConnectionInfo connInfo) throws ConnectionFactoryException {
logger.info("!!!!!!!!!!!!!!!!! connection create start !!!!!!!!!!!!!!!!!!!!!!!!!!!");
try {
String poolName = connInfo.getConnid();
Properties properties = setConnectionOption(connInfo);
Driver dbDriver = JdbcDriverLoader.getInstance().load(connInfo.getJdbcDriverInfo());
ConnectionFactory connectionFactory;
if (dbDriver == null) {
connectionFactory = new DriverManagerConnectionFactory(connInfo.getUrl(), properties);
} else {
connectionFactory = new DriverConnectionFactory(dbDriver, connInfo.getUrl(), properties);
}
// DBCP가 커넥션 풀에 커넥션을 보관할때 사용하는 PoolableConnectionFactory 생성
// 실제로 내부적으로 커넥션을 담고있고 커넥션을 관리하는데 기능을 제공한다. ex)커넥션을 close하면 종료하지 않고 커넥션 풀에 반환
PoolableConnectionFactory poolableConnFactory = new PoolableConnectionFactory(connectionFactory, null);
poolableConnFactory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
// 커넥션이 유효한지 확인할때 사용하는 쿼리를 설정한다.
poolableConnFactory.setValidationQuery(connInfo.getValidationQuery());
// 커넥션 풀의 설정 정보를 생성한다.
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
// 유휴 커넥션 검사 주기
poolConfig.setTimeBetweenEvictionRuns(Duration.ofMillis(1000L * 60L * 1L));
if (connInfo.isTestWhileIdle()) {
poolConfig.setTestWhileIdle(true);
}
// 커넥션 최소갯수 설정
poolConfig.setMinIdle(connInfo.getMinIdle());
// 커넥션 최대 갯수 설정
poolConfig.setMaxTotal(connInfo.getMaxActive());
// 커넥션 풀 생성. 인자로는 위에서 생성한 PoolabeConnectionFactory와 GenericObjectPoolConfig를 사용한다.
GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(poolableConnFactory, poolConfig);
// PoolabeConnectionFactory에도 커넥션 풀을 연결
poolableConnFactory.setPool(connectionPool);
// 위에서 커넥션 풀 드라이버에 생성한 커넥션 풀을 등룍한다. 이름은 cp이다.
driver.registerPool(poolName, connectionPool);
logger.debug("poolName : {}", poolName);
logger.debug("poolConfig : {}", poolConfig);
} catch (Exception e) {
throw new ConnectionFactoryException(e.getMessage(), e);
}
logger.info("!!!!!!!!!!!!!!!!! connection create end !!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
Aggregations