@Override public Host getHost() { return pool.getHost(); }
@Override public Connection<CL> borrowConnection(Operation<CL, R> operation) throws ConnectionException { // find the pool with the least outstanding (i.e most idle) active connections Iterator<HostConnectionPool<CL>> iterator = this.pools.iterator(); HostConnectionPool eligible = iterator.next(); while (iterator.hasNext()) { HostConnectionPool<CL> candidate = iterator.next(); if (candidate.getIdleConnectionCount() > eligible.getIdleConnectionCount()) { eligible = candidate; } } return eligible.borrowConnection(waitDelta * waitMultiplier); }
private void closeConnection(Connection<CL> connection) { connection.getHostConnectionPool().closeConnection(connection); activeConnectionCount.decrementAndGet(); }
@Override public int compare(HostConnectionPool<?> p1, HostConnectionPool<?> p2) { return p1.getBusyConnectionCount() + p1.getBlockedThreadCount() - p2.getBusyConnectionCount() - p2.getBlockedThreadCount(); } };
@Override public void run() { try { open(); callback.success(This); } catch (Exception e) { pool.addLatencySample(TimeUnit.NANOSECONDS.convert(cpConfig.getSocketTimeout(), TimeUnit.MILLISECONDS), System.nanoTime()); callback.failure(This, ThriftConverter.ToConnectionPoolException(e)); } } });
while (iter.hasNext()) { HostConnectionPool<CL> pool = iter.next(); if (pool.isReconnecting()) { for (; pools.get(0).getScore() == 0.0 && first < pools.size(); first++); double scoreFirst = pools.get(first).getScore(); HostConnectionPool<CL> pool = pools.get(i); if ((pool.getScore() / scoreFirst) > getScoreThreshold()) { Collections.sort(pools, busyComparator); HostConnectionPool<CL> poolFirst = pools.get(0); int firstBusy = poolFirst.getBusyConnectionCount() - poolFirst.getBlockedThreadCount(); for (int i = pools.size() - 1; i >= keep; i--) { HostConnectionPool<CL> pool = pools.get(i); int busy = pool.getBusyConnectionCount() + pool.getBlockedThreadCount(); if ( (busy - firstBusy) > getBlockedThreshold()) {
@Override public Connection<CL> borrowConnection(Operation<CL, R> operation) throws ConnectionException { pool = null; connection = BagOfConnectionsConnectionPoolImpl.this.borrowConnection(operation); pool = connection.getHostConnectionPool(); return connection; }
protected boolean returnConnection(Connection<CL> connection) { if (connection != null) { if (connection.getHostConnectionPool().isReconnecting() || connection.getOperationCount() > config.getMaxOperationsPerConnection()) { closeConnection(connection); } else { ConnectionException ce = connection.getLastException(); if (ce != null && (ce instanceof IsDeadConnectionException || ce instanceof IsTimeoutException)) { closeConnection(connection); } else if (!this.idleConnections.offer(connection)) { closeConnection(connection); } else { this.monitor.incConnectionReturned(connection.getHostConnectionPool().getHost()); } } return true; } return false; }
Host existingHost = hosts.get(host).getHost(); if (existingHost.getTokenRanges().size() != host.getTokenRanges().size()) { existingHost.setTokenRanges(host.getTokenRanges()); rebuildPartitions(); pool.primeConnections(config.getInitConnsPerHost());
/** * Refresh the partition */ public synchronized void refresh() { List<HostConnectionPool<CL>> pools = Lists.newArrayList(); for (HostConnectionPool<CL> pool : this.pools) { if (!pool.isReconnecting()) { pools.add(pool); } } this.activePools.set(strategy.sortAndfilterPartition(pools, prioritize)); }
@Override public Connection<CL> borrowConnection(Operation<CL, R> operation) throws ConnectionException { pool = pools.get(getNextHostIndex()); return pool.borrowConnection(waitDelta * waitMultiplier); }
@Override public void run() { try { open(); callback.success(This); } catch (Exception e) { pool.addLatencySample(TimeUnit.NANOSECONDS.convert(cpConfig.getSocketTimeout(), TimeUnit.MILLISECONDS), System.nanoTime()); callback.failure(This, ThriftConverter.ToConnectionPoolException(e)); } } });
while (iter.hasNext()) { HostConnectionPool<CL> pool = iter.next(); if (pool.isReconnecting()) { for (; pools.get(0).getScore() == 0.0 && first < pools.size(); first++); double scoreFirst = pools.get(first).getScore(); HostConnectionPool<CL> pool = pools.get(i); if ((pool.getScore() / scoreFirst) > getScoreThreshold()) { Collections.sort(pools, busyComparator); HostConnectionPool<CL> poolFirst = pools.get(0); int firstBusy = poolFirst.getBusyConnectionCount() - poolFirst.getBlockedThreadCount(); for (int i = pools.size() - 1; i >= keep; i--) { HostConnectionPool<CL> pool = pools.get(i); int busy = pool.getBusyConnectionCount() + pool.getBlockedThreadCount(); if ( (busy - firstBusy) > getBlockedThreshold()) {
@Override public Connection<CL> borrowConnection(Operation<CL, R> operation) throws ConnectionException { pool = null; connection = BagOfConnectionsConnectionPoolImpl.this.borrowConnection(operation); pool = connection.getHostConnectionPool(); return connection; }
protected boolean returnConnection(Connection<CL> connection) { if (connection != null) { if (connection.getHostConnectionPool().isReconnecting() || connection.getOperationCount() > config.getMaxOperationsPerConnection()) { closeConnection(connection); } else { ConnectionException ce = connection.getLastException(); if (ce != null && (ce instanceof IsDeadConnectionException || ce instanceof IsTimeoutException)) { closeConnection(connection); } else if (!this.idleConnections.offer(connection)) { closeConnection(connection); } else { this.monitor.incConnectionReturned(connection.getHostConnectionPool().getHost()); } } return true; } return false; }
Host existingHost = hosts.get(host).getHost(); if (existingHost.getTokenRanges().size() != host.getTokenRanges().size()) { existingHost.setTokenRanges(host.getTokenRanges()); rebuildPartitions(); pool.primeConnections(config.getInitConnsPerHost());
@Override public int compare(HostConnectionPool<?> p1, HostConnectionPool<?> p2) { return p1.getBusyConnectionCount() + p1.getBlockedThreadCount() - p2.getBusyConnectionCount() - p2.getBlockedThreadCount(); } };
/** * @return boolean */ @Override public boolean isHostUp(Host host) { HostConnectionPool<CL> pool = hosts.get(host); if (pool != null) { return !pool.isReconnecting(); } return false; }
@Override public Connection<CL> borrowConnection(Operation<CL, R> operation) throws ConnectionException { pool = pools.get(getNextHostIndex()); return pool.borrowConnection(waitDelta * waitMultiplier); }