Search in sources :

Example 16 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonScript method evalShaAsync.

@Override
public <R> RFuture<R> evalShaAsync(String key, Mode mode, String shaDigest, ReturnType returnType, List<Object> keys, Object... values) {
    RedisCommand command = new RedisCommand(returnType.getCommand(), "EVALSHA");
    if (mode == Mode.READ_ONLY && commandExecutor.isEvalShaROSupported()) {
        RedisCommand cmd = new RedisCommand(returnType.getCommand(), "EVALSHA_RO");
        RFuture<R> f = commandExecutor.evalReadAsync(key, codec, cmd, shaDigest, keys, encode(Arrays.asList(values), codec).toArray());
        CompletableFuture<R> result = new CompletableFuture<>();
        f.whenComplete((r, e) -> {
            if (e != null && e.getMessage().startsWith("ERR unknown command")) {
                commandExecutor.setEvalShaROSupported(false);
                RFuture<R> s = evalShaAsync(key, mode, shaDigest, returnType, keys, values);
                commandExecutor.transfer(s.toCompletableFuture(), result);
                return;
            }
            commandExecutor.transfer(f.toCompletableFuture(), result);
        });
        return new CompletableFutureWrapper<>(result);
    }
    return commandExecutor.evalWriteAsync(key, codec, command, shaDigest, keys, encode(Arrays.asList(values), codec).toArray());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RedisCommand(org.redisson.client.protocol.RedisCommand)

Example 17 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedisConnection method async.

public <T, R> RFuture<R> async(long timeout, Codec encoder, RedisCommand<T> command, Object... params) {
    CompletableFuture<R> promise = new CompletableFuture<>();
    if (timeout == -1) {
        timeout = redisClient.getCommandTimeout();
    }
    if (redisClient.getEventLoopGroup().isShuttingDown()) {
        RedissonShutdownException cause = new RedissonShutdownException("Redisson is shutdown");
        return RedissonPromise.newFailedFuture(cause);
    }
    Timeout scheduledFuture = redisClient.getTimer().newTimeout(t -> {
        RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout for command: " + LogHelper.toString(command, params) + ", Redis client: " + redisClient);
        promise.completeExceptionally(ex);
    }, timeout, TimeUnit.MILLISECONDS);
    promise.whenComplete((res, e) -> {
        scheduledFuture.cancel();
    });
    ChannelFuture writeFuture = send(new CommandData<>(promise, encoder, command, params));
    writeFuture.addListener((ChannelFutureListener) future -> {
        if (!future.isSuccess()) {
            promise.completeExceptionally(future.cause());
        }
    });
    return new CompletableFutureWrapper<>(promise);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AttributeKey(io.netty.util.AttributeKey) Timeout(io.netty.util.Timeout) Logger(org.slf4j.Logger) Codec(org.redisson.client.codec.Codec) CommandsQueue(org.redisson.client.handler.CommandsQueue) org.redisson.client.protocol(org.redisson.client.protocol) RedissonPromise(org.redisson.misc.RedissonPromise) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) Deque(java.util.Deque) RedissonShutdownException(org.redisson.RedissonShutdownException) CommandsQueuePubSub(org.redisson.client.handler.CommandsQueuePubSub) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) RFuture(org.redisson.api.RFuture) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Queue(java.util.Queue) LogHelper(org.redisson.misc.LogHelper) CompletableFuture(java.util.concurrent.CompletableFuture) Timeout(io.netty.util.Timeout) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RedissonShutdownException(org.redisson.RedissonShutdownException)

Example 18 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedisClient method shutdownAsync.

public RFuture<Void> shutdownAsync() {
    shutdown = true;
    CompletableFuture<Void> result = new CompletableFuture<>();
    if (channels.isEmpty() || config.getGroup().isShuttingDown()) {
        shutdown(result);
        return new CompletableFutureWrapper<>(result);
    }
    ChannelGroupFuture channelsFuture = channels.newCloseFuture();
    channelsFuture.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess()) {
                result.completeExceptionally(future.cause());
                return;
            }
            shutdown(result);
        }
    });
    for (Channel channel : channels) {
        RedisConnection connection = RedisConnection.getFrom(channel);
        if (connection != null) {
            connection.closeAsync();
        }
    }
    return new CompletableFutureWrapper<>(result);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ChannelGroupFuture(io.netty.channel.group.ChannelGroupFuture) EpollDatagramChannel(io.netty.channel.epoll.EpollDatagramChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) UnknownHostException(java.net.UnknownHostException)

Example 19 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class CommandAsyncService method pollFromAnyAsync.

@Override
public <V> RFuture<V> pollFromAnyAsync(String name, Codec codec, RedisCommand<Object> command, long secondsTimeout, String... queueNames) {
    if (connectionManager.isClusterMode() && queueNames.length > 0) {
        AtomicReference<Iterator<String>> ref = new AtomicReference<>();
        List<String> names = new ArrayList<>();
        names.add(name);
        names.addAll(Arrays.asList(queueNames));
        ref.set(names.iterator());
        AtomicLong counter = new AtomicLong(secondsTimeout);
        CompletionStage<V> result = poll(codec, ref, names, counter, command);
        return new CompletableFutureWrapper<>(result);
    } else {
        List<Object> params = new ArrayList<Object>(queueNames.length + 1);
        params.add(name);
        params.addAll(Arrays.asList(queueNames));
        params.add(secondsTimeout);
        return writeAsync(name, codec, command, params.toArray());
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 20 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class CommandAsyncService method readAllAsync.

@Override
public <T, R> RFuture<Collection<R>> readAllAsync(Codec codec, RedisCommand<T> command, Object... params) {
    Collection<MasterSlaveEntry> nodes = connectionManager.getEntrySet();
    List<CompletableFuture<?>> futures = new ArrayList<>();
    for (MasterSlaveEntry entry : nodes) {
        RFuture<Object> f = async(true, new NodeSource(entry), codec, command, params, true, false);
        futures.add(f.toCompletableFuture());
    }
    CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
    CompletableFuture<Collection<R>> resFuture = future.thenApply(r -> {
        List<R> results = new ArrayList<>();
        for (CompletableFuture<?> f : futures) {
            Object res = f.getNow(null);
            if (res instanceof Collection) {
                results.addAll((Collection) res);
            } else {
                results.add((R) res);
            }
        }
        return results;
    });
    return new CompletableFutureWrapper<>(resFuture);
}
Also used : NodeSource(org.redisson.connection.NodeSource) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry)

Aggregations

CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)38 ByteBuf (io.netty.buffer.ByteBuf)16 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 RFuture (org.redisson.api.RFuture)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 StringCodec (org.redisson.client.codec.StringCodec)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 ByteBufUtil (io.netty.buffer.ByteBufUtil)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 CommandAsyncExecutor (org.redisson.command.CommandAsyncExecutor)5 Timeout (io.netty.util.Timeout)4 java.util.concurrent (java.util.concurrent)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 RSemaphore (org.redisson.api.RSemaphore)4 RTopic (org.redisson.api.RTopic)4 RedissonClient (org.redisson.api.RedissonClient)4 RedisCommand (org.redisson.client.protocol.RedisCommand)4 TimerTask (io.netty.util.TimerTask)3