Example usage for io.netty.util.concurrent Future cause

List of usage examples for io.netty.util.concurrent Future cause

Introduction

In this page you can find the example usage for io.netty.util.concurrent Future cause.

Prototype

Throwable cause();

Source Link

Document

Returns the cause of the failed I/O operation if the I/O operation has failed.

Usage

From source file:org.redisson.connection.ConnectionEntry.java

License:Apache License

public Future<RedisConnection> connect(final MasterSlaveServersConfig config) {
    final Promise<RedisConnection> connectionFuture = client.getBootstrap().group().next().newPromise();
    Future<RedisConnection> future = client.connectAsync();
    future.addListener(new FutureListener<RedisConnection>() {
        @Override/*ww  w .  j  ava2s.com*/
        public void operationComplete(Future<RedisConnection> future) throws Exception {
            if (!future.isSuccess()) {
                connectionFuture.tryFailure(future.cause());
                return;
            }
            RedisConnection conn = future.getNow();
            log.debug("new connection created: {}", conn);

            FutureConnectionListener<RedisConnection> listener = new FutureConnectionListener<RedisConnection>(
                    connectionFuture, conn);
            connectListener.onConnect(config, nodeType, listener);
            listener.executeCommands();

            addReconnectListener(config, conn);
        }

    });
    return connectionFuture;
}

From source file:org.redisson.connection.ConnectionEntry.java

License:Apache License

public Future<RedisPubSubConnection> connectPubSub(final MasterSlaveServersConfig config) {
    final Promise<RedisPubSubConnection> connectionFuture = client.getBootstrap().group().next().newPromise();
    Future<RedisPubSubConnection> future = client.connectPubSubAsync();
    future.addListener(new FutureListener<RedisPubSubConnection>() {
        @Override/*from  w w w  .j  a  va2  s.co  m*/
        public void operationComplete(Future<RedisPubSubConnection> future) throws Exception {
            if (!future.isSuccess()) {
                connectionFuture.tryFailure(future.cause());
                return;
            }
            RedisPubSubConnection conn = future.getNow();
            log.debug("new pubsub connection created: {}", conn);

            FutureConnectionListener<RedisPubSubConnection> listener = new FutureConnectionListener<RedisPubSubConnection>(
                    connectionFuture, conn);
            connectListener.onConnect(config, nodeType, listener);
            listener.executeCommands();

            addReconnectListener(config, conn);
        }
    });
    return connectionFuture;
}

From source file:org.redisson.connection.CountListener.java

License:Apache License

@Override
public void operationComplete(Future<Void> future) throws Exception {
    if (!future.isSuccess()) {
        res.tryFailure(future.cause());
        return;// w w w . j  av a 2 s.c om
    }
    if (counter.decrementAndGet() == 0) {
        res.trySuccess(null);
    }
}

From source file:org.redisson.connection.DNSMonitor.java

License:Apache License

private void monitorMasters(AtomicInteger counter) {
    for (Entry<RedisURI, InetSocketAddress> entry : masters.entrySet()) {
        Future<InetSocketAddress> resolveFuture = resolver.resolve(
                InetSocketAddress.createUnresolved(entry.getKey().getHost(), entry.getKey().getPort()));
        resolveFuture.addListener(new FutureListener<InetSocketAddress>() {
            @Override/*from www . j av  a  2  s .  c  om*/
            public void operationComplete(Future<InetSocketAddress> future) throws Exception {
                if (counter.decrementAndGet() == 0) {
                    monitorDnsChange();
                }

                if (!future.isSuccess()) {
                    log.error("Unable to resolve " + entry.getKey().getHost(), future.cause());
                    return;
                }

                InetSocketAddress currentMasterAddr = entry.getValue();
                InetSocketAddress newMasterAddr = future.getNow();
                if (!newMasterAddr.getAddress().equals(currentMasterAddr.getAddress())) {
                    log.info("Detected DNS change. Master {} has changed ip from {} to {}", entry.getKey(),
                            currentMasterAddr.getAddress().getHostAddress(),
                            newMasterAddr.getAddress().getHostAddress());
                    MasterSlaveEntry masterSlaveEntry = connectionManager.getEntry(currentMasterAddr);
                    if (masterSlaveEntry == null) {
                        if (connectionManager instanceof SingleConnectionManager) {
                            log.error(
                                    "Unable to find master entry for {}. Multiple IP bindings for single hostname supported only in Redisson PRO!",
                                    currentMasterAddr);
                        } else {
                            log.error("Unable to find master entry for {}", currentMasterAddr);
                        }
                        return;
                    }
                    masterSlaveEntry.changeMaster(newMasterAddr, entry.getKey());
                    masters.put(entry.getKey(), newMasterAddr);
                }
            }
        });
    }
}

From source file:org.redisson.connection.DNSMonitor.java

License:Apache License

private void monitorSlaves(AtomicInteger counter) {
    for (Entry<RedisURI, InetSocketAddress> entry : slaves.entrySet()) {
        Future<InetSocketAddress> resolveFuture = resolver.resolve(
                InetSocketAddress.createUnresolved(entry.getKey().getHost(), entry.getKey().getPort()));
        resolveFuture.addListener(new FutureListener<InetSocketAddress>() {
            @Override/*from  w  ww. j  ava2s .c om*/
            public void operationComplete(Future<InetSocketAddress> future) throws Exception {
                if (counter.decrementAndGet() == 0) {
                    monitorDnsChange();
                }

                if (!future.isSuccess()) {
                    log.error("Unable to resolve " + entry.getKey().getHost(), future.cause());
                    return;
                }

                InetSocketAddress currentSlaveAddr = entry.getValue();
                InetSocketAddress newSlaveAddr = future.getNow();
                if (!newSlaveAddr.getAddress().equals(currentSlaveAddr.getAddress())) {
                    log.info("Detected DNS change. Slave {} has changed ip from {} to {}",
                            entry.getKey().getHost(), currentSlaveAddr.getAddress().getHostAddress(),
                            newSlaveAddr.getAddress().getHostAddress());
                    for (MasterSlaveEntry masterSlaveEntry : connectionManager.getEntrySet()) {
                        if (!masterSlaveEntry.hasSlave(currentSlaveAddr)) {
                            continue;
                        }

                        if (masterSlaveEntry.hasSlave(newSlaveAddr)) {
                            masterSlaveEntry.slaveUp(newSlaveAddr, FreezeReason.MANAGER);
                            masterSlaveEntry.slaveDown(currentSlaveAddr, FreezeReason.MANAGER);
                        } else {
                            RFuture<Void> addFuture = masterSlaveEntry.addSlave(newSlaveAddr, entry.getKey());
                            addFuture.onComplete((res, e) -> {
                                if (e != null) {
                                    log.error("Can't add slave: " + newSlaveAddr, e);
                                    return;
                                }

                                masterSlaveEntry.slaveDown(currentSlaveAddr, FreezeReason.MANAGER);
                            });
                        }
                        break;
                    }
                    slaves.put(entry.getKey(), newSlaveAddr);
                }
            }
        });
    }
}

From source file:org.redisson.connection.FutureConnectionListener.java

License:Apache License

@Override
public void operationComplete(Future<Object> future) throws Exception {
    if (!future.isSuccess()) {
        connection.closeAsync();// ww w. j  av a2s .  com
        connectionPromise.tryFailure(future.cause());
        return;
    }
    if (commandsCounter.decrementAndGet() == 0) {
        connectionPromise.trySuccess(connection);
    }
}

From source file:org.redisson.connection.MasterSlaveConnectionManager.java

License:Apache License

private <V, T> void writeAllAsync(final int slot, final AsyncOperation<V, T> asyncOperation,
        final AtomicInteger counter, final Promise<T> mainPromise, final int attempt) {
    final Promise<T> promise = getGroup().next().newPromise();
    final AtomicReference<RedisException> ex = new AtomicReference<RedisException>();

    TimerTask timerTask = new TimerTask() {
        @Override//  w  w  w . j ava2s  . c o m
        public void run(Timeout timeout) throws Exception {
            if (promise.isDone()) {
                return;
            }
            if (attempt == config.getRetryAttempts()) {
                promise.setFailure(ex.get());
                return;
            }
            promise.cancel(true);

            int count = attempt + 1;
            writeAllAsync(slot, asyncOperation, counter, mainPromise, count);
        }
    };

    try {
        RedisConnection<Object, V> connection = connectionWriteOp(slot);
        RedisAsyncConnection<Object, V> async = connection.getAsync();
        asyncOperation.execute(promise, async);

        ex.set(new RedisTimeoutException());
        Timeout timeout = timer.newTimeout(timerTask, config.getTimeout(), TimeUnit.MILLISECONDS);
        promise.addListener(createReleaseWriteListener(slot, connection, timeout));
    } catch (RedisConnectionException e) {
        ex.set(e);
        timer.newTimeout(timerTask, config.getRetryInterval(), TimeUnit.MILLISECONDS);
    }
    promise.addListener(new FutureListener<T>() {
        @Override
        public void operationComplete(Future<T> future) throws Exception {
            if (future.isCancelled()) {
                return;
            }

            if (future.cause() instanceof RedisMovedException) {
                RedisMovedException ex = (RedisMovedException) future.cause();
                writeAllAsync(ex.getSlot(), asyncOperation, counter, mainPromise, attempt);
                return;
            }

            if (future.isSuccess()) {
                if (counter.decrementAndGet() == 0 && !mainPromise.isDone()) {
                    mainPromise.setSuccess(future.getNow());
                }
            } else {
                mainPromise.setFailure(future.cause());
            }
        }
    });
}

From source file:org.redisson.connection.MasterSlaveConnectionManager.java

License:Apache License

private <V, T> void writeAsync(final int slot, final AsyncOperation<V, T> asyncOperation,
        final Promise<T> mainPromise, final int attempt) {
    final Promise<T> promise = getGroup().next().newPromise();
    final AtomicReference<RedisException> ex = new AtomicReference<RedisException>();

    TimerTask timerTask = new TimerTask() {
        @Override/* w  ww  .jav a 2  s  .  c o m*/
        public void run(Timeout timeout) throws Exception {
            if (promise.isDone()) {
                return;
            }
            if (attempt == config.getRetryAttempts()) {
                promise.setFailure(ex.get());
                return;
            }
            promise.cancel(true);

            int count = attempt + 1;
            writeAsync(slot, asyncOperation, mainPromise, count);
        }
    };

    try {
        RedisConnection<Object, V> connection = connectionWriteOp(slot);
        RedisAsyncConnection<Object, V> async = connection.getAsync();
        log.debug("writeAsync for slot {} using {}", slot, connection.getRedisClient().getAddr());
        asyncOperation.execute(promise, async);

        ex.set(new RedisTimeoutException());
        Timeout timeout = timer.newTimeout(timerTask, config.getTimeout(), TimeUnit.MILLISECONDS);
        promise.addListener(createReleaseWriteListener(slot, connection, timeout));
    } catch (RedisConnectionException e) {
        ex.set(e);
        timer.newTimeout(timerTask, config.getRetryInterval(), TimeUnit.MILLISECONDS);
    }
    promise.addListener(new FutureListener<T>() {
        @Override
        public void operationComplete(Future<T> future) throws Exception {
            if (future.isCancelled()) {
                return;
            }

            if (future.cause() instanceof RedisMovedException) {
                RedisMovedException ex = (RedisMovedException) future.cause();
                writeAsync(ex.getSlot(), asyncOperation, mainPromise, attempt);
                return;
            }

            if (future.isSuccess()) {
                mainPromise.setSuccess(future.getNow());
            } else {
                mainPromise.setFailure(future.cause());
            }
        }
    });
}

From source file:org.redisson.connection.MasterSlaveConnectionManager.java

License:Apache License

private <V, T> void readAsync(final int slot, final AsyncOperation<V, T> asyncOperation,
        final Promise<T> mainPromise, final int attempt) {
    final Promise<T> promise = getGroup().next().newPromise();
    final AtomicReference<RedisException> ex = new AtomicReference<RedisException>();

    TimerTask timerTask = new TimerTask() {
        @Override/*from w  w w  .j a v  a2s. com*/
        public void run(Timeout timeout) throws Exception {
            if (promise.isDone()) {
                return;
            }
            if (attempt == config.getRetryAttempts()) {
                promise.setFailure(ex.get());
                return;
            }
            promise.cancel(true);

            int count = attempt + 1;
            readAsync(slot, asyncOperation, mainPromise, count);
        }
    };

    try {
        RedisConnection<Object, V> connection = connectionReadOp(slot);
        RedisAsyncConnection<Object, V> async = connection.getAsync();
        log.debug("readAsync for slot {} using {}", slot, connection.getRedisClient().getAddr());
        asyncOperation.execute(promise, async);

        ex.set(new RedisTimeoutException());
        Timeout timeout = timer.newTimeout(timerTask, config.getTimeout(), TimeUnit.MILLISECONDS);
        promise.addListener(createReleaseReadListener(slot, connection, timeout));
    } catch (RedisConnectionException e) {
        ex.set(e);
        timer.newTimeout(timerTask, config.getRetryInterval(), TimeUnit.MILLISECONDS);
    }
    promise.addListener(new FutureListener<T>() {
        @Override
        public void operationComplete(Future<T> future) throws Exception {
            if (future.isCancelled()) {
                return;
            }
            // TODO cancel timeout

            if (future.cause() instanceof RedisMovedException) {
                RedisMovedException ex = (RedisMovedException) future.cause();
                readAsync(ex.getSlot(), asyncOperation, mainPromise, attempt);
                return;
            }

            if (future.isSuccess()) {
                mainPromise.setSuccess(future.getNow());
            } else {
                mainPromise.setFailure(future.cause());
            }
        }
    });
}

From source file:org.redisson.connection.pool.ConnectionPool.java

License:Apache License

private void createConnection(final boolean checkFreezed, final AtomicInteger requests,
        final ClientConnectionsEntry entry, final RPromise<Void> initPromise, final int minimumIdleSize,
        final AtomicInteger initializedConnections) {

    if ((checkFreezed && entry.isFreezed()) || !tryAcquireConnection(entry)) {
        Throwable cause = new RedisConnectionException("Can't init enough connections amount! Only "
                + (minimumIdleSize - initializedConnections.get()) + " from " + minimumIdleSize
                + " were initialized. Server: " + entry.getClient().getAddr());
        initPromise.tryFailure(cause);/*from   ww  w  . j a v  a  2s . c om*/
        return;
    }

    acquireConnection(entry, new Runnable() {

        @Override
        public void run() {
            RPromise<T> promise = connectionManager.newPromise();
            createConnection(entry, promise);
            promise.addListener(new FutureListener<T>() {
                @Override
                public void operationComplete(Future<T> future) throws Exception {
                    if (future.isSuccess()) {
                        T conn = future.getNow();

                        releaseConnection(entry, conn);
                    }

                    releaseConnection(entry);

                    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);
                        }
                    }
                }
            });
        }
    });

}