Search in sources :

Example 26 with RFuture

use of org.redisson.api.RFuture in project redisson by redisson.

the class CommandAsyncService method allAsync.

private <T, R> RFuture<R> allAsync(boolean readOnlyMode, RedisCommand<T> command, final SlotCallback<T, R> callback, Object... params) {
    final RPromise<R> mainPromise = connectionManager.newPromise();
    final Set<MasterSlaveEntry> nodes = connectionManager.getEntrySet();
    final AtomicInteger counter = new AtomicInteger(nodes.size());
    FutureListener<T> listener = new FutureListener<T>() {

        @Override
        public void operationComplete(Future<T> future) throws Exception {
            if (!future.isSuccess()) {
                mainPromise.tryFailure(future.cause());
                return;
            }
            if (callback != null) {
                callback.onSlotResult(future.getNow());
            }
            if (counter.decrementAndGet() == 0) {
                if (callback != null) {
                    mainPromise.trySuccess(callback.onFinish());
                } else {
                    mainPromise.trySuccess(null);
                }
            }
        }
    };
    for (MasterSlaveEntry entry : nodes) {
        RPromise<T> promise = connectionManager.newPromise();
        promise.addListener(listener);
        async(readOnlyMode, new NodeSource(entry), connectionManager.getCodec(), command, params, promise, 0);
    }
    return mainPromise;
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) ChannelFutureListener(io.netty.channel.ChannelFutureListener) NodeSource(org.redisson.connection.NodeSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RFuture(org.redisson.api.RFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future)

Example 27 with RFuture

use of org.redisson.api.RFuture in project redisson by redisson.

the class MasterSlaveEntry method initSlaveBalancer.

public List<RFuture<Void>> initSlaveBalancer(Collection<URL> disconnectedNodes) {
    boolean freezeMasterAsSlave = !config.getSlaveAddresses().isEmpty() && config.getReadMode() == ReadMode.SLAVE && disconnectedNodes.size() < config.getSlaveAddresses().size();
    List<RFuture<Void>> result = new LinkedList<RFuture<Void>>();
    RFuture<Void> f = addSlave(config.getMasterAddress().getHost(), config.getMasterAddress().getPort(), freezeMasterAsSlave, NodeType.MASTER);
    result.add(f);
    for (URL address : config.getSlaveAddresses()) {
        f = addSlave(address.getHost(), address.getPort(), disconnectedNodes.contains(address), NodeType.SLAVE);
        result.add(f);
    }
    return result;
}
Also used : RFuture(org.redisson.api.RFuture) LinkedList(java.util.LinkedList) URL(java.net.URL)

Example 28 with RFuture

use of org.redisson.api.RFuture in project redisson by redisson.

the class ReplicatedConnectionManager method connect.

private RFuture<RedisConnection> connect(BaseMasterSlaveServersConfig<?> cfg, final URL addr) {
    RedisConnection connection = nodeConnections.get(addr);
    if (connection != null) {
        return newSucceededFuture(connection);
    }
    RedisClient client = createClient(addr.getHost(), addr.getPort(), cfg.getConnectTimeout(), cfg.getRetryInterval() * cfg.getRetryAttempts());
    final RPromise<RedisConnection> result = newPromise();
    RFuture<RedisConnection> future = client.connectAsync();
    future.addListener(new FutureListener<RedisConnection>() {

        @Override
        public void operationComplete(Future<RedisConnection> future) throws Exception {
            if (!future.isSuccess()) {
                result.tryFailure(future.cause());
                return;
            }
            RedisConnection connection = future.getNow();
            RPromise<RedisConnection> promise = newPromise();
            connectListener.onConnect(promise, connection, null, config);
            promise.addListener(new FutureListener<RedisConnection>() {

                @Override
                public void operationComplete(Future<RedisConnection> future) throws Exception {
                    if (!future.isSuccess()) {
                        result.tryFailure(future.cause());
                        return;
                    }
                    RedisConnection connection = future.getNow();
                    if (connection.isActive()) {
                        nodeConnections.put(addr, connection);
                        result.trySuccess(connection);
                    } else {
                        connection.closeAsync();
                        result.tryFailure(new RedisException("Connection to " + connection.getRedisClient().getAddr() + " is not active!"));
                    }
                }
            });
        }
    });
    return result;
}
Also used : RedisClient(org.redisson.client.RedisClient) FutureListener(io.netty.util.concurrent.FutureListener) RPromise(org.redisson.misc.RPromise) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) RFuture(org.redisson.api.RFuture) Future(io.netty.util.concurrent.Future) RedisException(org.redisson.client.RedisException) RedisException(org.redisson.client.RedisException) RedisConnectionException(org.redisson.client.RedisConnectionException) RedisConnection(org.redisson.client.RedisConnection)

Example 29 with RFuture

use of org.redisson.api.RFuture in project redisson by redisson.

the class RedissonBatchTest method testOrdering.

@Test
public void testOrdering() throws InterruptedException {
    ExecutorService e = Executors.newFixedThreadPool(16);
    final RBatch batch = redisson.createBatch();
    final AtomicLong index = new AtomicLong(-1);
    final List<RFuture<Long>> futures = new CopyOnWriteArrayList<>();
    for (int i = 0; i < 500; i++) {
        futures.add(null);
    }
    for (int i = 0; i < 500; i++) {
        final int j = i;
        e.execute(new Runnable() {

            @Override
            public void run() {
                synchronized (RedissonBatchTest.this) {
                    int i = (int) index.incrementAndGet();
                    int ind = j % 3;
                    RFuture<Long> f1 = batch.getAtomicLong("test" + ind).addAndGetAsync(j);
                    futures.set(i, f1);
                }
            }
        });
    }
    e.shutdown();
    Assert.assertTrue(e.awaitTermination(30, TimeUnit.SECONDS));
    List<?> s = batch.execute();
    int i = 0;
    for (Object element : s) {
        RFuture<Long> a = futures.get(i);
        Assert.assertEquals(a.getNow(), element);
        i++;
    }
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) RBatch(org.redisson.api.RBatch) ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

RFuture (org.redisson.api.RFuture)29 FutureListener (io.netty.util.concurrent.FutureListener)23 Future (io.netty.util.concurrent.Future)21 ArrayList (java.util.ArrayList)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Timeout (io.netty.util.Timeout)7 TimerTask (io.netty.util.TimerTask)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 RedisConnection (org.redisson.client.RedisConnection)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 RedisConnectionException (org.redisson.client.RedisConnectionException)6 RedisException (org.redisson.client.RedisException)6 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)6 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Collection (java.util.Collection)4 RPromise (org.redisson.misc.RPromise)4 ChannelFuture (io.netty.channel.ChannelFuture)3 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 URL (java.net.URL)3