Search in sources :

Example 76 with GenericObjectPool

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++;
        }
    }
}
Also used : ZuliaRESTClient(io.zulia.client.rest.ZuliaRESTClient) Node(io.zulia.message.ZuliaBase.Node) GrpcCommand(io.zulia.client.command.base.GrpcCommand) Metadata(io.grpc.Metadata) RESTCommand(io.zulia.client.command.base.RESTCommand) IOException(java.io.IOException) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) MultiIndexRoutableCommand(io.zulia.client.command.base.MultiIndexRoutableCommand) ShardRoutableCommand(io.zulia.client.command.base.ShardRoutableCommand) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) StatusRuntimeException(io.grpc.StatusRuntimeException) SingleIndexRoutableCommand(io.zulia.client.command.base.SingleIndexRoutableCommand)

Example 77 with GenericObjectPool

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();
}
Also used : DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) SQLException(java.sql.SQLException) Duration(java.time.Duration) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 78 with GenericObjectPool

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);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 79 with GenericObjectPool

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);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 80 with GenericObjectPool

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 !!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
Also used : DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) Driver(java.sql.Driver) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) ConnectionFactoryException(com.varsql.core.exception.ConnectionFactoryException) SQLException(java.sql.SQLException) ConnectionFactoryException(com.varsql.core.exception.ConnectionFactoryException) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) DriverConnectionFactory(org.apache.commons.dbcp2.DriverConnectionFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) DriverConnectionFactory(org.apache.commons.dbcp2.DriverConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)79 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)32 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)27 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)23 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)19 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)16 Test (org.junit.jupiter.api.Test)13 Properties (java.util.Properties)11 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)9 SQLException (java.sql.SQLException)8 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)8 Connection (java.sql.Connection)7 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Bean (org.springframework.context.annotation.Bean)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 IOException (java.io.IOException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 Test (org.junit.Test)3 TimeInterval (com.adaptris.util.TimeInterval)2 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)2