Minimal connections amount initialization fixed. #375

pull/395/head
Nikita 9 years ago
parent 01be3348b5
commit f843275b7f

@ -79,38 +79,54 @@ abstract class ConnectionPool<T extends RedisConnection> {
} }
final AtomicInteger initializedConnections = new AtomicInteger(minimumIdleSize); final AtomicInteger initializedConnections = new AtomicInteger(minimumIdleSize);
for (int i = 0; i < minimumIdleSize; i++) { int startAmount = Math.min(50, minimumIdleSize);
if ((checkFreezed && entry.isFreezed()) || !tryAcquireConnection(entry)) { final AtomicInteger requests = new AtomicInteger(startAmount);
Throwable cause = new RedisConnectionException( for (int i = 0; i < startAmount; i++) {
"Can't init enough connections amount! Only " + (minimumIdleSize - initializedConnections.get()) + " from " + minimumIdleSize + " were initialized. Server: " createConnection(checkFreezed, requests, entry, initPromise, minimumIdleSize, initializedConnections);
+ entry.getClient().getAddr()); }
initPromise.tryFailure(cause); }
return;
}
Future<T> promise = connectTo(entry); private void createConnection(final boolean checkFreezed, final AtomicInteger requests, final ClientConnectionsEntry entry, final Promise<Void> initPromise,
promise.addListener(new FutureListener<T>() { final int minimumIdleSize, final AtomicInteger initializedConnections) {
@Override
public void operationComplete(Future<T> future) throws Exception { if ((checkFreezed && entry.isFreezed()) || !tryAcquireConnection(entry)) {
if (future.isSuccess()) { Throwable cause = new RedisConnectionException(
T conn = future.getNow(); "Can't init enough connections amount! Only " + (minimumIdleSize - initializedConnections.get()) + " from " + minimumIdleSize + " were initialized. Server: "
releaseConnection(entry, conn); + entry.getClient().getAddr());
} initPromise.tryFailure(cause);
releaseConnection(entry); return;
if (!future.isSuccess()) { }
Throwable cause = new RedisConnectionException(
"Can't init enough connections amount! Only " + (minimumIdleSize - initializedConnections.get()) + " from " + minimumIdleSize + " were initialized. Server: " Future<T> promise = createConnection(entry);
+ entry.getClient().getAddr(), future.cause()); promise.addListener(new FutureListener<T>() {
initPromise.tryFailure(cause); @Override
return; public void operationComplete(Future<T> future) throws Exception {
} if (future.isSuccess()) {
T conn = future.getNow();
releaseConnection(entry, conn);
}
releaseConnection(entry);
if (!future.isSuccess()) {
Throwable cause = new RedisConnectionException(
"Can't init enough connections amount! Only " + (minimumIdleSize - initializedConnections.get()) + " from " + minimumIdleSize + " were initialized. Server: "
+ entry.getClient().getAddr(), future.cause());
initPromise.tryFailure(cause);
return;
}
if (initializedConnections.decrementAndGet() == 0) { int value = initializedConnections.decrementAndGet();
initPromise.setSuccess(null); if (value == 0) {
log.info("{} connections initialized for {}", minimumIdleSize, entry.getClient().getAddr());
initPromise.setSuccess(null);
} else if (value > 0 && !initPromise.isDone()) {
if (requests.incrementAndGet() <= minimumIdleSize) {
createConnection(checkFreezed, requests, entry, initPromise, minimumIdleSize, initializedConnections);
} }
} }
}); }
} });
} }
protected abstract int getMinimumIdleSize(ClientConnectionsEntry entry); protected abstract int getMinimumIdleSize(ClientConnectionsEntry entry);
@ -137,14 +153,13 @@ abstract class ConnectionPool<T extends RedisConnection> {
} }
} }
StringBuilder errorMsg = new StringBuilder("Connection pool exhausted! All connections are busy. "); StringBuilder errorMsg = new StringBuilder("Connection pool exhausted! All connections are busy. Try to increase connection pool size.");
if (!freezed.isEmpty()) { // if (!freezed.isEmpty()) {
errorMsg.append(" disconnected hosts: " + freezed); // errorMsg.append(" Disconnected hosts: " + freezed);
} // }
if (!zeroConnectionsAmount.isEmpty()) { if (!zeroConnectionsAmount.isEmpty()) {
errorMsg.append(" hosts with fully busy connections: " + zeroConnectionsAmount); errorMsg.append(" Hosts with fully busy connections: " + zeroConnectionsAmount);
} }
errorMsg.append(" Try to increase connection pool size.");
RedisConnectionException exception = new RedisConnectionException(errorMsg.toString()); RedisConnectionException exception = new RedisConnectionException(errorMsg.toString());
return connectionManager.newFailedFuture(exception); return connectionManager.newFailedFuture(exception);
@ -183,6 +198,10 @@ abstract class ConnectionPool<T extends RedisConnection> {
return promiseSuccessful(entry, conn); return promiseSuccessful(entry, conn);
} }
return createConnection(entry);
}
private Future<T> createConnection(final ClientConnectionsEntry entry) {
final Promise<T> promise = connectionManager.newPromise(); final Promise<T> promise = connectionManager.newPromise();
Future<T> connFuture = connect(entry); Future<T> connFuture = connect(entry);
connFuture.addListener(new FutureListener<T>() { connFuture.addListener(new FutureListener<T>() {

Loading…
Cancel
Save