Search in sources :

Example 1 with ConnectionEntry

use of org.redisson.command.CommandBatchService.ConnectionEntry in project redisson by redisson.

the class RedisQueuedBatchExecutor method sendCommand.

@Override
protected void sendCommand(CompletableFuture<R> attemptPromise, RedisConnection connection) {
    MasterSlaveEntry msEntry = getEntry(source);
    ConnectionEntry connectionEntry = connections.get(msEntry);
    boolean syncSlaves = options.getSyncSlaves() > 0;
    if (source.getRedirect() == Redirect.ASK) {
        List<CommandData<?, ?>> list = new ArrayList<>(2);
        CompletableFuture<Void> promise = new CompletableFuture<>();
        list.add(new CommandData<>(promise, codec, RedisCommands.ASKING, new Object[] {}));
        if (connectionEntry.isFirstCommand()) {
            list.add(new CommandData<>(promise, codec, RedisCommands.MULTI, new Object[] {}));
            connectionEntry.setFirstCommand(false);
        }
        list.add(new CommandData<>(attemptPromise, codec, command, params));
        CompletableFuture<Void> main = new CompletableFuture<>();
        writeFuture = connection.send(new CommandsData(main, list, true, syncSlaves));
    } else {
        if (log.isDebugEnabled()) {
            log.debug("acquired connection for command {} and params {} from slot {} using node {}... {}", command, LogHelper.toString(params), source, connection.getRedisClient().getAddr(), connection);
        }
        if (connectionEntry.isFirstCommand()) {
            List<CommandData<?, ?>> list = new ArrayList<>(2);
            list.add(new CommandData<>(new CompletableFuture<>(), codec, RedisCommands.MULTI, new Object[] {}));
            list.add(new CommandData<>(attemptPromise, codec, command, params));
            CompletableFuture<Void> main = new CompletableFuture<>();
            writeFuture = connection.send(new CommandsData(main, list, true, syncSlaves));
            connectionEntry.setFirstCommand(false);
        } else {
            if (RedisCommands.EXEC.getName().equals(command.getName())) {
                Entry entry = commands.get(msEntry);
                List<CommandData<?, ?>> list = new ArrayList<>();
                if (options.isSkipResult()) {
                    list.add(new CommandData<>(new CompletableFuture<>(), codec, RedisCommands.CLIENT_REPLY, new Object[] { "OFF" }));
                }
                list.add(new CommandData<>(attemptPromise, codec, command, params));
                if (options.isSkipResult()) {
                    list.add(new CommandData<>(new CompletableFuture<>(), codec, RedisCommands.CLIENT_REPLY, new Object[] { "ON" }));
                }
                if (options.getSyncSlaves() > 0) {
                    BatchCommandData<?, ?> waitCommand = new BatchCommandData(RedisCommands.WAIT, new Object[] { this.options.getSyncSlaves(), this.options.getSyncTimeout() }, index.incrementAndGet());
                    list.add(waitCommand);
                    entry.getCommands().add(waitCommand);
                }
                CompletableFuture<Void> main = new CompletableFuture<>();
                writeFuture = connection.send(new CommandsData(main, list, new ArrayList(entry.getCommands()), options.isSkipResult(), false, true, syncSlaves));
            } else {
                CompletableFuture<Void> main = new CompletableFuture<>();
                List<CommandData<?, ?>> list = new ArrayList<>();
                list.add(new CommandData<>(attemptPromise, codec, command, params));
                writeFuture = connection.send(new CommandsData(main, list, true, syncSlaves));
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionEntry(org.redisson.command.CommandBatchService.ConnectionEntry) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) Entry(org.redisson.command.CommandBatchService.Entry) ConnectionEntry(org.redisson.command.CommandBatchService.ConnectionEntry) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry)

Example 2 with ConnectionEntry

use of org.redisson.command.CommandBatchService.ConnectionEntry in project redisson by redisson.

the class RedisQueuedBatchExecutor method getConnection.

@Override
protected CompletableFuture<RedisConnection> getConnection() {
    MasterSlaveEntry msEntry = getEntry(source);
    ConnectionEntry entry = connections.get(msEntry);
    if (entry == null) {
        entry = new ConnectionEntry();
        ConnectionEntry oldEntry = connections.putIfAbsent(msEntry, entry);
        if (oldEntry != null) {
            entry = oldEntry;
        }
    }
    if (entry.getConnectionFuture() != null) {
        return entry.getConnectionFuture();
    }
    synchronized (this) {
        if (entry.getConnectionFuture() != null) {
            return entry.getConnectionFuture();
        }
        CompletableFuture<RedisConnection> connectionFuture;
        if (this.options.getExecutionMode() == ExecutionMode.REDIS_WRITE_ATOMIC) {
            connectionFuture = connectionManager.connectionWriteOp(source, null);
        } else {
            connectionFuture = connectionManager.connectionReadOp(source, null);
        }
        connectionFuture.toCompletableFuture().join();
        entry.setConnectionFuture(connectionFuture);
        entry.setCancelCallback(() -> {
            handleError(connectionFuture, new CancellationException());
        });
        return connectionFuture;
    }
}
Also used : ConnectionEntry(org.redisson.command.CommandBatchService.ConnectionEntry) CancellationException(java.util.concurrent.CancellationException) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisConnection(org.redisson.client.RedisConnection)

Aggregations

ConnectionEntry (org.redisson.command.CommandBatchService.ConnectionEntry)2 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)2 ArrayList (java.util.ArrayList)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 RedisConnection (org.redisson.client.RedisConnection)1 Entry (org.redisson.command.CommandBatchService.Entry)1