예제 #1
0
  public RedisPubSubConnection nextPubSubConnection() {
    clientsEmpty.awaitUninterruptibly();
    List<SubscribesConnectionEntry> clientsCopy =
        new ArrayList<SubscribesConnectionEntry>(clients.values());
    while (true) {
      if (clientsCopy.isEmpty()) {
        throw new RedisConnectionException("Slave subscribe-connection pool gets exhausted!");
      }

      int index = getIndex(clientsCopy);
      SubscribesConnectionEntry entry = clientsCopy.get(index);

      if (entry.isFreezed() || !entry.getSubscribeConnectionsSemaphore().tryAcquire()) {
        clientsCopy.remove(index);
      } else {
        try {
          RedisPubSubConnection conn = entry.pollFreeSubscribeConnection();
          if (conn != null) {
            return conn;
          }
          return entry.connectPubSub(config);
        } catch (RedisConnectionException e) {
          entry.getSubscribeConnectionsSemaphore().release();
          // TODO connection scoring
          log.warn("Can't connect to {}, trying next connection!", entry.getClient().getAddr());
          clientsCopy.remove(index);
        }
      }
    }
  }
예제 #2
0
 public synchronized void unfreeze(String host, int port) {
   InetSocketAddress addr = new InetSocketAddress(host, port);
   for (SubscribesConnectionEntry connectionEntry : clients.values()) {
     if (!connectionEntry.getClient().getAddr().equals(addr)) {
       continue;
     }
     connectionEntry.setFreezed(false);
     clientsEmpty.open();
     return;
   }
   throw new IllegalStateException("Can't find " + addr + " in slaves!");
 }
예제 #3
0
  public synchronized Collection<RedisPubSubConnection> freeze(String host, int port) {
    InetSocketAddress addr = new InetSocketAddress(host, port);
    for (SubscribesConnectionEntry connectionEntry : clients.values()) {
      if (connectionEntry.isFreezed() || !connectionEntry.getClient().getAddr().equals(addr)) {
        continue;
      }

      log.debug("{} freezed", addr);
      connectionEntry.setFreezed(true);

      // close all connections
      while (true) {
        RedisConnection connection = connectionEntry.getConnections().poll();
        if (connection == null) {
          break;
        }
        connection.closeAsync();
      }

      // close all pub/sub connections
      while (true) {
        RedisPubSubConnection connection = connectionEntry.pollFreeSubscribeConnection();
        if (connection == null) {
          break;
        }
        connection.closeAsync();
      }

      boolean allFreezed = true;
      for (SubscribesConnectionEntry entry : clients.values()) {
        if (!entry.isFreezed()) {
          allFreezed = false;
          break;
        }
      }
      if (allFreezed) {
        clientsEmpty.close();
      }

      List<RedisPubSubConnection> list =
          new ArrayList<RedisPubSubConnection>(connectionEntry.getAllSubscribeConnections());
      connectionEntry.getAllSubscribeConnections().clear();
      return list;
    }

    return Collections.emptyList();
  }
예제 #4
0
  public RedisConnection nextConnection() {
    clientsEmpty.awaitUninterruptibly();
    List<SubscribesConnectionEntry> clientsCopy =
        new ArrayList<SubscribesConnectionEntry>(clients.values());
    while (true) {
      if (clientsCopy.isEmpty()) {
        throw new RedisConnectionException("Slave connection pool gets exhausted!");
      }

      int index = getIndex(clientsCopy);
      SubscribesConnectionEntry entry = clientsCopy.get(index);

      RedisConnection conn = retrieveConnection(entry);
      if (conn == null) {
        clientsCopy.remove(index);
      } else {
        return conn;
      }
    }
  }
예제 #5
0
 public synchronized void add(SubscribesConnectionEntry entry) {
   clients.put(entry.getClient(), entry);
   if (!entry.isFreezed()) {
     clientsEmpty.open();
   }
 }