public ConcurrentHClientPool(CassandraHost host) throws SQLException { this.cassandraHost = host; ds = new CassandraDataSource( cassandraHost.getHost(), cassandraHost.getPort(), cassandraHost.getKeyspaceName(), cassandraHost.getUser(), cassandraHost.getPassword()); availableConnectionQueue = new ArrayBlockingQueue<CassandraConnectionHandle>(cassandraHost.getMaxActive(), true); // This counter can be offset by as much as the number of threads. activeConnectionCount = new AtomicInteger(0); realActiveConnectionCount = new AtomicInteger(0); numBlocked = new AtomicInteger(); active = new AtomicBoolean(true); maxWaitTimeWhenExhausted = cassandraHost.getMaxWaitTimeWhenExhausted() < 0 ? 0 : cassandraHost.getMaxWaitTimeWhenExhausted(); for (int i = 0; i < cassandraHost.getMaxActive() / 3; i++) { availableConnectionQueue.add(createConnection()); } if (log.isDebugEnabled()) { log.debug( "Concurrent Host pool started with {} active clients; max: {} exhausted wait: {}", new Object[] {getNumIdle(), cassandraHost.getMaxActive(), maxWaitTimeWhenExhausted}); } }
@Override public CassandraConnectionHandle borrowClient() throws SQLException { if (!active.get()) { throw new HPoolInnactiveException("Attempt to borrow on in-active pool: " + getName()); } CassandraConnectionHandle conn = availableConnectionQueue.poll(); int currentActiveClients = activeConnectionCount.incrementAndGet(); try { if (conn == null) { if (currentActiveClients <= cassandraHost.getMaxActive()) { conn = createConnection(); } else { // We can't grow so let's wait for a connection to become available. conn = waitForConnection(); } } if (conn == null) { // Abnormal situation. throw new HectorException( "HConnectionManager returned a null client after aquisition - are we shutting down?"); } } catch (RuntimeException e) { activeConnectionCount.decrementAndGet(); throw e; } catch (SQLException e) { activeConnectionCount.decrementAndGet(); throw e; } realActiveConnectionCount.incrementAndGet(); return conn; }
@Override public int getMaxActive() { return cassandraHost.getMaxActive(); }
@Override public int getNumBeforeExhausted() { return cassandraHost.getMaxActive() - realActiveConnectionCount.get(); }