Search in sources :

Example 1 with RedisClientEntry

use of org.redisson.connection.RedisClientEntry in project redisson by redisson.

the class RedisNodes method pingAll.

@Override
public boolean pingAll() {
    List<RedisClientEntry> clients = new ArrayList<RedisClientEntry>(connectionManager.getClients());
    final Map<RedisConnection, RFuture<String>> result = new ConcurrentHashMap<RedisConnection, RFuture<String>>(clients.size());
    final CountDownLatch latch = new CountDownLatch(clients.size());
    for (RedisClientEntry entry : clients) {
        RFuture<RedisConnection> f = entry.getClient().connectAsync();
        f.addListener(new FutureListener<RedisConnection>() {

            @Override
            public void operationComplete(Future<RedisConnection> future) throws Exception {
                if (future.isSuccess()) {
                    final RedisConnection c = future.getNow();
                    RPromise<RedisConnection> connectionFuture = connectionManager.newPromise();
                    connectionManager.getConnectListener().onConnect(connectionFuture, c, null, connectionManager.getConfig());
                    connectionFuture.addListener(new FutureListener<RedisConnection>() {

                        @Override
                        public void operationComplete(Future<RedisConnection> future) throws Exception {
                            RFuture<String> r = c.async(connectionManager.getConfig().getPingTimeout(), RedisCommands.PING);
                            result.put(c, r);
                            latch.countDown();
                        }
                    });
                } else {
                    latch.countDown();
                }
            }
        });
    }
    long time = System.currentTimeMillis();
    try {
        latch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    if (System.currentTimeMillis() - time >= connectionManager.getConfig().getConnectTimeout()) {
        for (Entry<RedisConnection, RFuture<String>> entry : result.entrySet()) {
            entry.getKey().closeAsync();
        }
        return false;
    }
    time = System.currentTimeMillis();
    boolean res = true;
    for (Entry<RedisConnection, RFuture<String>> entry : result.entrySet()) {
        RFuture<String> f = entry.getValue();
        f.awaitUninterruptibly();
        if (!"PONG".equals(f.getNow())) {
            res = false;
        }
        entry.getKey().closeAsync();
    }
    // true and no futures missed during client connection
    return res && result.size() == clients.size();
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) RPromise(org.redisson.misc.RPromise) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) CountDownLatch(java.util.concurrent.CountDownLatch) RFuture(org.redisson.api.RFuture) Future(io.netty.util.concurrent.Future) RedisClientEntry(org.redisson.connection.RedisClientEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RedisConnection(org.redisson.client.RedisConnection)

Aggregations

Future (io.netty.util.concurrent.Future)1 FutureListener (io.netty.util.concurrent.FutureListener)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 RFuture (org.redisson.api.RFuture)1 RedisConnection (org.redisson.client.RedisConnection)1 RedisClientEntry (org.redisson.connection.RedisClientEntry)1 RPromise (org.redisson.misc.RPromise)1