Search in sources :

Example 11 with MasterSlaveEntry

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

the class CommandBatchService method executeAsyncVoid.

private RFuture<Void> executeAsyncVoid(boolean noResult) {
    if (executed) {
        throw new IllegalStateException("Batch already executed!");
    }
    if (commands.isEmpty()) {
        return connectionManager.newSucceededFuture(null);
    }
    if (noResult) {
        for (Entry entry : commands.values()) {
            RPromise<Object> s = connectionManager.newPromise();
            BatchCommandData commandData = new BatchCommandData(s, null, RedisCommands.CLIENT_REPLY, new Object[] { "OFF" }, index.incrementAndGet());
            entry.getCommands().addFirst(commandData);
            RPromise<Object> s1 = connectionManager.newPromise();
            BatchCommandData commandData1 = new BatchCommandData(s1, null, RedisCommands.CLIENT_REPLY, new Object[] { "ON" }, index.incrementAndGet());
            entry.getCommands().add(commandData1);
        }
    }
    executed = true;
    RPromise<Void> voidPromise = connectionManager.newPromise();
    voidPromise.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            commands = null;
        }
    });
    AtomicInteger slots = new AtomicInteger(commands.size());
    for (java.util.Map.Entry<MasterSlaveEntry, Entry> e : commands.entrySet()) {
        execute(e.getValue(), new NodeSource(e.getKey()), voidPromise, slots, 0, true);
    }
    return voidPromise;
}
Also used : BatchCommandData(org.redisson.client.protocol.BatchCommandData) RedisMovedException(org.redisson.client.RedisMovedException) RedisAskException(org.redisson.client.RedisAskException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) NodeSource(org.redisson.connection.NodeSource) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 12 with MasterSlaveEntry

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

the class ClusterConnectionManager method checkSlotsMigration.

private void checkSlotsMigration(Collection<ClusterPartition> newPartitions, String nodes) {
    Set<ClusterPartition> currentPartitions = getLastPartitions();
    for (ClusterPartition currentPartition : currentPartitions) {
        for (ClusterPartition newPartition : newPartitions) {
            if (!currentPartition.getNodeId().equals(newPartition.getNodeId()) || // skip master change case
            !currentPartition.getMasterAddr().equals(newPartition.getMasterAddr())) {
                continue;
            }
            Set<Integer> addedSlots = new HashSet<Integer>(newPartition.getSlots());
            addedSlots.removeAll(currentPartition.getSlots());
            currentPartition.addSlots(addedSlots);
            MasterSlaveEntry entry = getEntry(currentPartition.getMasterAddr());
            for (Integer slot : addedSlots) {
                entry.addSlotRange(slot);
                addEntry(slot, entry);
                lastPartitions.put(slot, currentPartition);
            }
            if (!addedSlots.isEmpty()) {
                log.info("{} slots added to {}", addedSlots.size(), currentPartition.getMasterAddr());
            }
            Set<Integer> removedSlots = new HashSet<Integer>(currentPartition.getSlots());
            removedSlots.removeAll(newPartition.getSlots());
            for (Integer removeSlot : removedSlots) {
                if (lastPartitions.remove(removeSlot, currentPartition)) {
                    entry.removeSlotRange(removeSlot);
                    removeMaster(removeSlot);
                }
            }
            currentPartition.removeSlots(removedSlots);
            if (!removedSlots.isEmpty()) {
                log.info("{} slots removed from {}", removedSlots.size(), currentPartition.getMasterAddr());
            }
            break;
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) HashSet(java.util.HashSet)

Example 13 with MasterSlaveEntry

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

the class CommandAsyncService method readAllAsync.

@Override
public <T, R> RFuture<Collection<R>> readAllAsync(RedisCommand<T> command, Object... params) {
    final RPromise<Collection<R>> mainPromise = connectionManager.newPromise();
    final Set<MasterSlaveEntry> nodes = connectionManager.getEntrySet();
    final List<R> results = new ArrayList<R>();
    final AtomicInteger counter = new AtomicInteger(nodes.size());
    FutureListener<R> listener = new FutureListener<R>() {

        @Override
        public void operationComplete(Future<R> future) throws Exception {
            if (!future.isSuccess()) {
                mainPromise.tryFailure(future.cause());
                return;
            }
            R result = future.getNow();
            if (result instanceof Collection) {
                synchronized (results) {
                    results.addAll((Collection) result);
                }
            } else {
                synchronized (results) {
                    results.add(result);
                }
            }
            if (counter.decrementAndGet() == 0 && !mainPromise.isDone()) {
                mainPromise.trySuccess(results);
            }
        }
    };
    for (MasterSlaveEntry entry : nodes) {
        RPromise<R> promise = connectionManager.newPromise();
        promise.addListener(listener);
        async(true, new NodeSource(entry), connectionManager.getCodec(), command, params, promise, 0);
    }
    return mainPromise;
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ArrayList(java.util.ArrayList) NodeSource(org.redisson.connection.NodeSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) Collection(java.util.Collection) RFuture(org.redisson.api.RFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future)

Example 14 with MasterSlaveEntry

use of org.redisson.connection.MasterSlaveEntry 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 15 with MasterSlaveEntry

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

the class CommandAsyncService method retryReadRandomAsync.

private <R, T> void retryReadRandomAsync(final RedisCommand<T> command, final RPromise<R> mainPromise, final List<MasterSlaveEntry> nodes, final Object... params) {
    final RPromise<R> attemptPromise = connectionManager.newPromise();
    attemptPromise.addListener(new FutureListener<R>() {

        @Override
        public void operationComplete(Future<R> future) throws Exception {
            if (future.isSuccess()) {
                if (future.getNow() == null) {
                    if (nodes.isEmpty()) {
                        mainPromise.trySuccess(null);
                    } else {
                        retryReadRandomAsync(command, mainPromise, nodes, params);
                    }
                } else {
                    mainPromise.trySuccess(future.getNow());
                }
            } else {
                mainPromise.tryFailure(future.cause());
            }
        }
    });
    MasterSlaveEntry entry = nodes.remove(0);
    async(true, new NodeSource(entry), connectionManager.getCodec(), command, params, attemptPromise, 0);
}
Also used : NodeSource(org.redisson.connection.NodeSource) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisAskException(org.redisson.client.RedisAskException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisException(org.redisson.client.RedisException) RedisMovedException(org.redisson.client.RedisMovedException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedissonShutdownException(org.redisson.RedissonShutdownException)

Aggregations

MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 NodeSource (org.redisson.connection.NodeSource)7 Future (io.netty.util.concurrent.Future)6 FutureListener (io.netty.util.concurrent.FutureListener)6 ArrayList (java.util.ArrayList)6 RFuture (org.redisson.api.RFuture)6 ChannelFuture (io.netty.channel.ChannelFuture)3 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 Collection (java.util.Collection)3 RedisAskException (org.redisson.client.RedisAskException)3 RedisException (org.redisson.client.RedisException)3 RedisLoadingException (org.redisson.client.RedisLoadingException)3 RedisMovedException (org.redisson.client.RedisMovedException)3 RedisTimeoutException (org.redisson.client.RedisTimeoutException)3 RedisTryAgainException (org.redisson.client.RedisTryAgainException)3 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)3 HashSet (java.util.HashSet)2 List (java.util.List)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2