Пример #1
0
  private Future<T> createConnection(final ClientConnectionsEntry entry) {
    final Promise<T> promise = connectionManager.newPromise();
    Future<T> connFuture = connect(entry);
    connFuture.addListener(
        new FutureListener<T>() {
          @Override
          public void operationComplete(Future<T> future) throws Exception {
            if (!future.isSuccess()) {
              releaseConnection(entry);

              promiseFailure(entry, promise, future.cause());
              return;
            }

            T conn = future.getNow();
            if (!conn.isActive()) {
              promiseFailure(entry, promise, conn);
              return;
            }

            promiseSuccessful(entry, promise, conn);
          }
        });
    return promise;
  }
Пример #2
0
 public Future<RedisConnection> getConnection(InetSocketAddress addr) {
   ClientConnectionsEntry entry = addr2Entry.get(addr);
   if (entry != null) {
     return entries.get(entry);
   }
   RedisConnectionException exception =
       new RedisConnectionException("Can't find entry for " + addr);
   return connectionManager.newFailedFuture(exception);
 }
Пример #3
0
  public Future<T> get(ClientConnectionsEntry entry) {
    if (((entry.getNodeType() == NodeType.MASTER && entry.getFreezeReason() == FreezeReason.SYSTEM)
            || !entry.isFreezed())
        && tryAcquireConnection(entry)) {
      return connectTo(entry);
    }

    RedisConnectionException exception =
        new RedisConnectionException("Can't aquire connection to " + entry.getClient().getAddr());
    return connectionManager.newFailedFuture(exception);
  }
Пример #4
0
 public Future<Void> add(final ClientConnectionsEntry entry) {
   final Promise<Void> promise = connectionManager.newPromise();
   promise.addListener(
       new FutureListener<Void>() {
         @Override
         public void operationComplete(Future<Void> future) throws Exception {
           entries.add(entry);
         }
       });
   initConnections(entry, promise, true);
   return promise;
 }
Пример #5
0
  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);
  }
Пример #6
0
  public Future<T> get() {
    for (int j = entries.size() - 1; j >= 0; j--) {
      ClientConnectionsEntry entry = getEntry();
      if (!entry.isFreezed() && tryAcquireConnection(entry)) {
        return connectTo(entry);
      }
    }

    List<InetSocketAddress> zeroConnectionsAmount = 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());
      }
    }

    StringBuilder errorMsg;
    if (connectionManager.isClusterMode()) {
      errorMsg =
          new StringBuilder(
              "Connection pool exhausted! for slots: " + masterSlaveEntry.getSlotRanges());
    } else {
      errorMsg = new StringBuilder("Connection pool exhausted! ");
    }
    if (!freezed.isEmpty()) {
      errorMsg.append(" Disconnected hosts: " + freezed);
    }
    if (!zeroConnectionsAmount.isEmpty()) {
      errorMsg.append(" Hosts with fully busy connections: " + zeroConnectionsAmount);
    }

    RedisConnectionException exception = new RedisConnectionException(errorMsg.toString());
    return connectionManager.newFailedFuture(exception);
  }
Пример #7
0
 public void shutdownAsync() {
   for (ClientConnectionsEntry entry : addr2Entry.values()) {
     connectionManager.shutdownAsync(entry.getClient());
   }
 }
Пример #8
0
 @Override
 public void shutdown() {
   connectionManager.shutdown();
 }
Пример #9
0
  private void scheduleCheck(final ClientConnectionsEntry entry) {

    connectionManager.getConnectionEventsHub().fireDisconnect(entry.getClient().getAddr());

    connectionManager.newTimeout(
        new TimerTask() {
          @Override
          public void run(Timeout timeout) throws Exception {
            if (entry.getFreezeReason() != FreezeReason.RECONNECT || !entry.isFreezed()) {
              return;
            }

            Future<RedisConnection> connectionFuture = entry.getClient().connectAsync();
            connectionFuture.addListener(
                new FutureListener<RedisConnection>() {
                  @Override
                  public void operationComplete(Future<RedisConnection> future) throws Exception {
                    if (entry.getFreezeReason() != FreezeReason.RECONNECT || !entry.isFreezed()) {
                      return;
                    }

                    if (!future.isSuccess()) {
                      scheduleCheck(entry);
                      return;
                    }
                    final RedisConnection c = future.getNow();
                    if (!c.isActive()) {
                      c.closeAsync();
                      scheduleCheck(entry);
                      return;
                    }

                    final FutureListener<String> pingListener =
                        new FutureListener<String>() {
                          @Override
                          public void operationComplete(Future<String> future) throws Exception {
                            try {
                              if (entry.getFreezeReason() != FreezeReason.RECONNECT
                                  || !entry.isFreezed()) {
                                return;
                              }

                              if (future.isSuccess() && "PONG".equals(future.getNow())) {
                                entry.resetFailedAttempts();
                                Promise<Void> promise = connectionManager.newPromise();
                                promise.addListener(
                                    new FutureListener<Void>() {
                                      @Override
                                      public void operationComplete(Future<Void> future)
                                          throws Exception {
                                        if (entry.getNodeType() == NodeType.SLAVE) {
                                          masterSlaveEntry.slaveUp(
                                              entry.getClient().getAddr().getHostName(),
                                              entry.getClient().getAddr().getPort(),
                                              FreezeReason.RECONNECT);
                                          log.info(
                                              "slave {} successfully reconnected",
                                              entry.getClient().getAddr());
                                        } else {
                                          synchronized (entry) {
                                            if (entry.getFreezeReason() == FreezeReason.RECONNECT) {
                                              entry.setFreezed(false);
                                              entry.setFreezeReason(null);
                                              log.info(
                                                  "host {} successfully reconnected",
                                                  entry.getClient().getAddr());
                                            }
                                          }
                                        }
                                      }
                                    });
                                initConnections(entry, promise, false);
                              } else {
                                scheduleCheck(entry);
                              }
                            } finally {
                              c.closeAsync();
                            }
                          }
                        };

                    if (entry.getConfig().getPassword() != null) {
                      Future<Void> temp = c.async(RedisCommands.AUTH, config.getPassword());

                      FutureListener<Void> listener =
                          new FutureListener<Void>() {
                            @Override
                            public void operationComplete(Future<Void> future) throws Exception {
                              ping(c, pingListener);
                            }
                          };

                      temp.addListener(listener);
                    } else {
                      ping(c, pingListener);
                    }
                  }
                });
          }
        },
        config.getReconnectionTimeout(),
        TimeUnit.MILLISECONDS);
  }