Search in sources :

Example 6 with MasterSlaveEntry

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

the class RedissonKeys method deleteAsync.

@Override
public RFuture<Long> deleteAsync(String... keys) {
    if (!commandExecutor.getConnectionManager().isClusterMode()) {
        return commandExecutor.writeAsync(null, RedisCommands.DEL, keys);
    }
    Map<MasterSlaveEntry, List<String>> range2key = new HashMap<MasterSlaveEntry, List<String>>();
    for (String key : keys) {
        int slot = commandExecutor.getConnectionManager().calcSlot(key);
        for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
            List<String> list = range2key.get(entry);
            if (list == null) {
                list = new ArrayList<String>();
                range2key.put(entry, list);
            }
            list.add(key);
        }
    }
    final RPromise<Long> result = commandExecutor.getConnectionManager().newPromise();
    final AtomicReference<Throwable> failed = new AtomicReference<Throwable>();
    final AtomicLong count = new AtomicLong();
    final AtomicLong executed = new AtomicLong(range2key.size());
    FutureListener<List<?>> listener = new FutureListener<List<?>>() {

        @Override
        public void operationComplete(Future<List<?>> future) throws Exception {
            if (future.isSuccess()) {
                List<Long> result = (List<Long>) future.get();
                for (Long res : result) {
                    count.addAndGet(res);
                }
            } else {
                failed.set(future.cause());
            }
            checkExecution(result, failed, count, executed);
        }
    };
    for (Entry<MasterSlaveEntry, List<String>> entry : range2key.entrySet()) {
        // executes in batch due to CROSSLOT error
        CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
        for (String key : entry.getValue()) {
            executorService.writeAsync(entry.getKey(), null, RedisCommands.DEL, key);
        }
        RFuture<List<?>> future = executorService.executeAsync();
        future.addListener(listener);
    }
    return result;
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) HashMap(java.util.HashMap) CommandBatchService(org.redisson.command.CommandBatchService) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicLong(java.util.concurrent.atomic.AtomicLong) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) AtomicLong(java.util.concurrent.atomic.AtomicLong) RFuture(org.redisson.api.RFuture) Future(io.netty.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with MasterSlaveEntry

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

the class RedissonKeys method getKeysByPattern.

public Iterable<String> getKeysByPattern(final String pattern, final int count) {
    List<Iterable<String>> iterables = new ArrayList<Iterable<String>>();
    for (final MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
        Iterable<String> iterable = new Iterable<String>() {

            @Override
            public Iterator<String> iterator() {
                return createKeysIterator(entry, pattern, count);
            }
        };
        iterables.add(iterable);
    }
    return new CompositeIterable<String>(iterables);
}
Also used : CompositeIterable(org.redisson.misc.CompositeIterable) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) ArrayList(java.util.ArrayList) CompositeIterable(org.redisson.misc.CompositeIterable)

Example 8 with MasterSlaveEntry

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

the class RedissonNode method retrieveAdresses.

private void retrieveAdresses() {
    ConnectionManager connectionManager = ((Redisson) redisson).getConnectionManager();
    for (MasterSlaveEntry entry : connectionManager.getEntrySet()) {
        RFuture<RedisConnection> readFuture = entry.connectionReadOp(null);
        if (readFuture.awaitUninterruptibly((long) connectionManager.getConfig().getConnectTimeout()) && readFuture.isSuccess()) {
            RedisConnection connection = readFuture.getNow();
            entry.releaseRead(connection);
            remoteAddress = (InetSocketAddress) connection.getChannel().remoteAddress();
            localAddress = (InetSocketAddress) connection.getChannel().localAddress();
            return;
        }
        RFuture<RedisConnection> writeFuture = entry.connectionWriteOp(null);
        if (writeFuture.awaitUninterruptibly((long) connectionManager.getConfig().getConnectTimeout()) && writeFuture.isSuccess()) {
            RedisConnection connection = writeFuture.getNow();
            entry.releaseWrite(connection);
            remoteAddress = (InetSocketAddress) connection.getChannel().remoteAddress();
            localAddress = (InetSocketAddress) connection.getChannel().localAddress();
            return;
        }
    }
}
Also used : ConnectionManager(org.redisson.connection.ConnectionManager) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisConnection(org.redisson.client.RedisConnection)

Example 9 with MasterSlaveEntry

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

the class CommandAsyncService method getNodeSource.

private NodeSource getNodeSource(String key) {
    int slot = connectionManager.calcSlot(key);
    MasterSlaveEntry entry = connectionManager.getEntry(slot);
    return new NodeSource(entry);
}
Also used : NodeSource(org.redisson.connection.NodeSource) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry)

Example 10 with MasterSlaveEntry

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

the class CommandBatchService method executeAsync.

public RFuture<List<?>> executeAsync() {
    if (executed) {
        throw new IllegalStateException("Batch already executed!");
    }
    if (commands.isEmpty()) {
        return connectionManager.newSucceededFuture(null);
    }
    executed = true;
    RPromise<Void> voidPromise = connectionManager.newPromise();
    final RPromise<List<?>> promise = connectionManager.newPromise();
    voidPromise.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess()) {
                promise.tryFailure(future.cause());
                commands = null;
                return;
            }
            List<BatchCommandData> entries = new ArrayList<BatchCommandData>();
            for (Entry e : commands.values()) {
                entries.addAll(e.getCommands());
            }
            Collections.sort(entries);
            List<Object> result = new ArrayList<Object>(entries.size());
            for (BatchCommandData<?, ?> commandEntry : entries) {
                Object entryResult = commandEntry.getPromise().getNow();
                if (isRedissonReferenceSupportEnabled() && entryResult instanceof RedissonReference) {
                    result.add(redisson != null ? RedissonObjectFactory.<Object>fromReference(redisson, (RedissonReference) entryResult) : RedissonObjectFactory.<Object>fromReference(redissonReactive, (RedissonReference) entryResult));
                } else {
                    result.add(entryResult);
                }
            }
            promise.trySuccess(result);
            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, false);
    }
    return promise;
}
Also used : RedissonReference(org.redisson.RedissonReference) 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) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentMap(java.util.concurrent.ConcurrentMap)

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