|
|
|
@ -104,39 +104,51 @@ abstract class ConnectionPool<T extends RedisConnection> {
|
|
|
|
|
initPromise.tryFailure(cause);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RFuture<T> promise = createConnection(entry);
|
|
|
|
|
promise.addListener(new FutureListener<T>() {
|
|
|
|
|
|
|
|
|
|
acquireConnection(entry, new Runnable() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void operationComplete(Future<T> future) throws Exception {
|
|
|
|
|
if (future.isSuccess()) {
|
|
|
|
|
T conn = future.getNow();
|
|
|
|
|
releaseConnection(entry, conn);
|
|
|
|
|
}
|
|
|
|
|
public void run() {
|
|
|
|
|
RFuture<T> promise = createConnection(entry, null);
|
|
|
|
|
promise.addListener(new FutureListener<T>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void operationComplete(Future<T> future) throws Exception {
|
|
|
|
|
if (future.isSuccess()) {
|
|
|
|
|
T conn = future.getNow();
|
|
|
|
|
|
|
|
|
|
releaseConnection(entry);
|
|
|
|
|
releaseConnection(entry, conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
releaseConnection(entry);
|
|
|
|
|
|
|
|
|
|
int value = initializedConnections.decrementAndGet();
|
|
|
|
|
if (value == 0) {
|
|
|
|
|
log.info("{} connections initialized for {}", minimumIdleSize, entry.getClient().getAddr());
|
|
|
|
|
if (!initPromise.trySuccess(null)) {
|
|
|
|
|
throw new IllegalStateException();
|
|
|
|
|
}
|
|
|
|
|
} else if (value > 0 && !initPromise.isDone()) {
|
|
|
|
|
if (requests.incrementAndGet() <= minimumIdleSize) {
|
|
|
|
|
createConnection(checkFreezed, requests, entry, initPromise, minimumIdleSize, initializedConnections);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int value = initializedConnections.decrementAndGet();
|
|
|
|
|
if (value == 0) {
|
|
|
|
|
log.info("{} connections initialized for {}", minimumIdleSize, entry.getClient().getAddr());
|
|
|
|
|
if (!initPromise.trySuccess(null)) {
|
|
|
|
|
throw new IllegalStateException();
|
|
|
|
|
}
|
|
|
|
|
} else if (value > 0 && !initPromise.isDone()) {
|
|
|
|
|
if (requests.incrementAndGet() <= minimumIdleSize) {
|
|
|
|
|
createConnection(checkFreezed, requests, entry, initPromise, minimumIdleSize, initializedConnections);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void acquireConnection(ClientConnectionsEntry entry, Runnable runnable) {
|
|
|
|
|
entry.acquireConnection(runnable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract int getMinimumIdleSize(ClientConnectionsEntry entry);
|
|
|
|
@ -148,27 +160,35 @@ abstract class ConnectionPool<T extends RedisConnection> {
|
|
|
|
|
public RFuture<T> get() {
|
|
|
|
|
for (int j = entries.size() - 1; j >= 0; j--) {
|
|
|
|
|
ClientConnectionsEntry entry = getEntry();
|
|
|
|
|
if (!entry.isFreezed() && tryAcquireConnection(entry)) {
|
|
|
|
|
return connectTo(entry);
|
|
|
|
|
if (!entry.isFreezed()
|
|
|
|
|
&& tryAcquireConnection(entry)) {
|
|
|
|
|
final RPromise<T> result = connectionManager.newPromise();
|
|
|
|
|
acquireConnection(entry, new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
connectTo(entry, result);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<InetSocketAddress> zeroConnectionsAmount = new LinkedList<InetSocketAddress>();
|
|
|
|
|
|
|
|
|
|
List<InetSocketAddress> failedAttempts = new LinkedList<InetSocketAddress>();
|
|
|
|
|
List<InetSocketAddress> freezed = new LinkedList<InetSocketAddress>();
|
|
|
|
|
for (ClientConnectionsEntry entry : entries) {
|
|
|
|
|
if (entry.isFreezed()) {
|
|
|
|
|
freezed.add(entry.getClient().getAddr());
|
|
|
|
|
} else {
|
|
|
|
|
zeroConnectionsAmount.add(entry.getClient().getAddr());
|
|
|
|
|
failedAttempts.add(entry.getClient().getAddr());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder errorMsg = new StringBuilder(getClass().getSimpleName() + " exhausted! ");
|
|
|
|
|
StringBuilder errorMsg = new StringBuilder(getClass().getSimpleName() + " no available Redis entries. ");
|
|
|
|
|
if (!freezed.isEmpty()) {
|
|
|
|
|
errorMsg.append(" Disconnected hosts: " + freezed);
|
|
|
|
|
}
|
|
|
|
|
if (!zeroConnectionsAmount.isEmpty()) {
|
|
|
|
|
errorMsg.append(" Hosts with fully busy connections: " + zeroConnectionsAmount);
|
|
|
|
|
if (!failedAttempts.isEmpty()) {
|
|
|
|
|
errorMsg.append(" Hosts disconnected due to `failedAttempts` limit reached: " + failedAttempts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RedisConnectionException exception = new RedisConnectionException(errorMsg.toString());
|
|
|
|
@ -178,7 +198,9 @@ abstract class ConnectionPool<T extends RedisConnection> {
|
|
|
|
|
public RFuture<T> get(ClientConnectionsEntry entry) {
|
|
|
|
|
if (((entry.getNodeType() == NodeType.MASTER && entry.getFreezeReason() == FreezeReason.SYSTEM) || !entry.isFreezed())
|
|
|
|
|
&& tryAcquireConnection(entry)) {
|
|
|
|
|
return connectTo(entry);
|
|
|
|
|
RPromise<T> result = connectionManager.newPromise();
|
|
|
|
|
connectTo(entry, result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RedisConnectionException exception = new RedisConnectionException(
|
|
|
|
@ -187,7 +209,7 @@ abstract class ConnectionPool<T extends RedisConnection> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected boolean tryAcquireConnection(ClientConnectionsEntry entry) {
|
|
|
|
|
return entry.getFailedAttempts() < config.getFailedAttempts() && entry.tryAcquireConnection();
|
|
|
|
|
return entry.getFailedAttempts() < config.getFailedAttempts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected T poll(ClientConnectionsEntry entry) {
|
|
|
|
@ -198,21 +220,29 @@ abstract class ConnectionPool<T extends RedisConnection> {
|
|
|
|
|
return (RFuture<T>) entry.connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RFuture<T> connectTo(ClientConnectionsEntry entry) {
|
|
|
|
|
private void connectTo(ClientConnectionsEntry entry, RPromise<T> promise) {
|
|
|
|
|
T conn = poll(entry);
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
if (!conn.isActive()) {
|
|
|
|
|
return promiseFailure(entry, conn);
|
|
|
|
|
promiseFailure(entry, promise, conn);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return promiseSuccessful(entry, conn);
|
|
|
|
|
entry.resetFailedAttempts();
|
|
|
|
|
promise.trySuccess(conn);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return createConnection(entry);
|
|
|
|
|
createConnection(entry, promise);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RFuture<T> createConnection(final ClientConnectionsEntry entry) {
|
|
|
|
|
final RPromise<T> promise = connectionManager.newPromise();
|
|
|
|
|
private RFuture<T> createConnection(final ClientConnectionsEntry entry, RPromise<T> ppromise) {
|
|
|
|
|
final RPromise<T> promise;
|
|
|
|
|
if (ppromise != null) {
|
|
|
|
|
promise = ppromise;
|
|
|
|
|
} else {
|
|
|
|
|
promise = connectionManager.newPromise();
|
|
|
|
|
}
|
|
|
|
|
RFuture<T> connFuture = connect(entry);
|
|
|
|
|
connFuture.addListener(new FutureListener<T>() {
|
|
|
|
|
@Override
|
|
|
|
@ -242,11 +272,6 @@ abstract class ConnectionPool<T extends RedisConnection> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RFuture<T> promiseSuccessful(ClientConnectionsEntry entry, T conn) {
|
|
|
|
|
entry.resetFailedAttempts();
|
|
|
|
|
return (RFuture<T>) conn.getAcquireFuture();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void promiseFailure(ClientConnectionsEntry entry, RPromise<T> promise, Throwable cause) {
|
|
|
|
|
if (entry.incFailedAttempts() == config.getFailedAttempts()) {
|
|
|
|
|
checkForReconnect(entry);
|
|
|
|
@ -274,23 +299,6 @@ abstract class ConnectionPool<T extends RedisConnection> {
|
|
|
|
|
promise.tryFailure(cause);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RFuture<T> promiseFailure(ClientConnectionsEntry entry, T conn) {
|
|
|
|
|
int attempts = entry.incFailedAttempts();
|
|
|
|
|
if (attempts == config.getFailedAttempts()) {
|
|
|
|
|
conn.closeAsync();
|
|
|
|
|
checkForReconnect(entry);
|
|
|
|
|
} else if (attempts < config.getFailedAttempts()) {
|
|
|
|
|
releaseConnection(entry, conn);
|
|
|
|
|
} else {
|
|
|
|
|
conn.closeAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
releaseConnection(entry);
|
|
|
|
|
|
|
|
|
|
RedisConnectionException cause = new RedisConnectionException(conn + " is not active!");
|
|
|
|
|
return connectionManager.newFailedFuture(cause);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkForReconnect(ClientConnectionsEntry entry) {
|
|
|
|
|
if (entry.getNodeType() == NodeType.SLAVE) {
|
|
|
|
|
masterSlaveEntry.slaveDown(entry.getClient().getAddr().getHostName(),
|
|
|
|
|