|
|
@ -82,8 +82,7 @@ public class ConnectionPool<T extends RedisConnection> {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Promise<T> promise = connectionManager.newPromise();
|
|
|
|
Future<T> promise = connectTo(entry);
|
|
|
|
connect(entry, promise);
|
|
|
|
|
|
|
|
promise.addListener(new FutureListener<T>() {
|
|
|
|
promise.addListener(new FutureListener<T>() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void operationComplete(Future<T> future) throws Exception {
|
|
|
|
public void operationComplete(Future<T> future) throws Exception {
|
|
|
@ -118,9 +117,7 @@ public class ConnectionPool<T extends RedisConnection> {
|
|
|
|
for (int j = entries.size() - 1; j >= 0; j--) {
|
|
|
|
for (int j = entries.size() - 1; j >= 0; j--) {
|
|
|
|
ClientConnectionsEntry entry = getEntry();
|
|
|
|
ClientConnectionsEntry entry = getEntry();
|
|
|
|
if (!entry.isFreezed() && tryAcquireConnection(entry)) {
|
|
|
|
if (!entry.isFreezed() && tryAcquireConnection(entry)) {
|
|
|
|
Promise<T> promise = connectionManager.newPromise();
|
|
|
|
return connectTo(entry);
|
|
|
|
connect(entry, promise);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -132,9 +129,7 @@ public class ConnectionPool<T extends RedisConnection> {
|
|
|
|
public Future<T> get(ClientConnectionsEntry entry) {
|
|
|
|
public Future<T> get(ClientConnectionsEntry entry) {
|
|
|
|
if (((entry.getNodeType() == NodeType.MASTER && entry.getFreezeReason() == FreezeReason.SYSTEM) || !entry.isFreezed())
|
|
|
|
if (((entry.getNodeType() == NodeType.MASTER && entry.getFreezeReason() == FreezeReason.SYSTEM) || !entry.isFreezed())
|
|
|
|
&& tryAcquireConnection(entry)) {
|
|
|
|
&& tryAcquireConnection(entry)) {
|
|
|
|
Promise<T> promise = connectionManager.newPromise();
|
|
|
|
return connectTo(entry);
|
|
|
|
connect(entry, promise);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RedisConnectionException exception = new RedisConnectionException(
|
|
|
|
RedisConnectionException exception = new RedisConnectionException(
|
|
|
@ -154,18 +149,17 @@ public class ConnectionPool<T extends RedisConnection> {
|
|
|
|
return (Future<T>) entry.connect(config);
|
|
|
|
return (Future<T>) entry.connect(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void connect(final ClientConnectionsEntry entry, final Promise<T> promise) {
|
|
|
|
private Future<T> connectTo(final ClientConnectionsEntry entry) {
|
|
|
|
T conn = poll(entry);
|
|
|
|
T conn = poll(entry);
|
|
|
|
if (conn != null) {
|
|
|
|
if (conn != null) {
|
|
|
|
if (!conn.isActive()) {
|
|
|
|
if (!conn.isActive()) {
|
|
|
|
promiseFailure(entry, promise, conn);
|
|
|
|
return promiseFailure(entry, conn);
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
promiseSuccessful(entry, promise, conn);
|
|
|
|
return promiseSuccessful(entry, conn);
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -186,9 +180,10 @@ public class ConnectionPool<T extends RedisConnection> {
|
|
|
|
promiseSuccessful(entry, promise, conn);
|
|
|
|
promiseSuccessful(entry, promise, conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void promiseSuccessful(final ClientConnectionsEntry entry, final Promise<T> promise, T conn) {
|
|
|
|
private void promiseSuccessful(ClientConnectionsEntry entry, Promise<T> promise, T conn) {
|
|
|
|
entry.resetFailedAttempts();
|
|
|
|
entry.resetFailedAttempts();
|
|
|
|
if (!promise.trySuccess(conn)) {
|
|
|
|
if (!promise.trySuccess(conn)) {
|
|
|
|
releaseConnection(entry, conn);
|
|
|
|
releaseConnection(entry, conn);
|
|
|
@ -196,6 +191,11 @@ public class ConnectionPool<T extends RedisConnection> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Future<T> promiseSuccessful(ClientConnectionsEntry entry, T conn) {
|
|
|
|
|
|
|
|
entry.resetFailedAttempts();
|
|
|
|
|
|
|
|
return connectionManager.newSucceededFuture(conn);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void promiseFailure(ClientConnectionsEntry entry, Promise<T> promise, Throwable cause) {
|
|
|
|
private void promiseFailure(ClientConnectionsEntry entry, Promise<T> promise, Throwable cause) {
|
|
|
|
if (entry.incFailedAttempts() == config.getFailedAttempts()) {
|
|
|
|
if (entry.incFailedAttempts() == config.getFailedAttempts()) {
|
|
|
|
checkForReconnect(entry);
|
|
|
|
checkForReconnect(entry);
|
|
|
@ -218,6 +218,20 @@ public class ConnectionPool<T extends RedisConnection> {
|
|
|
|
promise.tryFailure(cause);
|
|
|
|
promise.tryFailure(cause);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Future<T> promiseFailure(ClientConnectionsEntry entry, T conn) {
|
|
|
|
|
|
|
|
int attempts = entry.incFailedAttempts();
|
|
|
|
|
|
|
|
if (attempts == config.getFailedAttempts()) {
|
|
|
|
|
|
|
|
checkForReconnect(entry);
|
|
|
|
|
|
|
|
} else if (attempts < config.getFailedAttempts()) {
|
|
|
|
|
|
|
|
releaseConnection(entry, conn);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
releaseConnection(entry);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RedisConnectionException cause = new RedisConnectionException(conn + " is not active!");
|
|
|
|
|
|
|
|
return connectionManager.newFailedFuture(cause);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void checkForReconnect(ClientConnectionsEntry entry) {
|
|
|
|
private void checkForReconnect(ClientConnectionsEntry entry) {
|
|
|
|
if (entry.getNodeType() == NodeType.SLAVE) {
|
|
|
|
if (entry.getNodeType() == NodeType.SLAVE) {
|
|
|
|
connectionManager.slaveDown(masterSlaveEntry, entry.getClient().getAddr().getHostName(),
|
|
|
|
connectionManager.slaveDown(masterSlaveEntry, entry.getClient().getAddr().getHostName(),
|
|
|
|