Search in sources :

Example 1 with CompletableFutureWrapper

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

the class RedissonBaseLock method evalWriteAsync.

protected <T> RFuture<T> evalWriteAsync(String key, Codec codec, RedisCommand<T> evalCommandType, String script, List<Object> keys, Object... params) {
    MasterSlaveEntry entry = commandExecutor.getConnectionManager().getEntry(getRawName());
    int availableSlaves = entry.getAvailableSlaves();
    CommandBatchService executorService = createCommandBatchService(availableSlaves);
    RFuture<T> result = executorService.evalWriteAsync(key, codec, evalCommandType, script, keys, params);
    if (commandExecutor instanceof CommandBatchService) {
        return result;
    }
    RFuture<BatchResult<?>> future = executorService.executeAsync();
    CompletionStage<T> f = future.handle((res, ex) -> {
        if (ex != null) {
            throw new CompletionException(ex);
        }
        if (commandExecutor.getConnectionManager().getCfg().isCheckLockSyncedSlaves() && res.getSyncedSlaves() < availableSlaves) {
            throw new CompletionException(new IllegalStateException("Only " + res.getSyncedSlaves() + " of " + availableSlaves + " slaves were synced"));
        }
        return result.getNow();
    });
    return new CompletableFutureWrapper<>(f);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) CommandBatchService(org.redisson.command.CommandBatchService) BatchResult(org.redisson.api.BatchResult)

Example 2 with CompletableFutureWrapper

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

the class RedissonBucket method removeListenerAsync.

@Override
public RFuture<Void> removeListenerAsync(int listenerId) {
    RPatternTopic setTopic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, "__keyevent@*:set");
    RFuture<Void> f1 = setTopic.removeListenerAsync(listenerId);
    RFuture<Void> f2 = super.removeListenerAsync(listenerId);
    CompletableFuture<Void> f = CompletableFuture.allOf(f1.toCompletableFuture(), f2.toCompletableFuture());
    return new CompletableFutureWrapper<>(f);
}
Also used : RPatternTopic(org.redisson.api.RPatternTopic) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper)

Example 3 with CompletableFutureWrapper

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

the class RedissonBaseAdder method sumAsync.

public RFuture<T> sumAsync(long timeout, TimeUnit timeUnit) {
    String id = generateId();
    RFuture<Long> future = topic.publishAsync(SUM_MSG + ":" + id);
    RSemaphore semaphore = getSemaphore(id);
    CompletionStage<T> f = future.thenCompose(r -> tryAcquire(semaphore, timeout, timeUnit, r.intValue())).thenCompose(r -> getAndDeleteAsync(id)).thenCompose(r -> semaphore.deleteAsync().thenApply(res -> r));
    return new CompletableFutureWrapper<>(f);
}
Also used : RFuture(org.redisson.api.RFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBufUtil(io.netty.buffer.ByteBufUtil) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) Logger(org.slf4j.Logger) StringCodec(org.redisson.client.codec.StringCodec) java.util.concurrent(java.util.concurrent) RTopic(org.redisson.api.RTopic) LoggerFactory(org.slf4j.LoggerFactory) RSemaphore(org.redisson.api.RSemaphore) RedissonClient(org.redisson.api.RedissonClient) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RSemaphore(org.redisson.api.RSemaphore)

Example 4 with CompletableFutureWrapper

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

the class RedissonBaseAdder method sumAsync.

public RFuture<T> sumAsync() {
    String id = generateId();
    RFuture<Long> future = topic.publishAsync(SUM_MSG + ":" + id);
    RSemaphore semaphore = getSemaphore(id);
    CompletionStage<T> f = future.thenCompose(r -> semaphore.acquireAsync(r.intValue())).thenCompose(r -> getAndDeleteAsync(id)).thenCompose(r -> semaphore.deleteAsync().thenApply(res -> r));
    return new CompletableFutureWrapper<>(f);
}
Also used : RFuture(org.redisson.api.RFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBufUtil(io.netty.buffer.ByteBufUtil) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) Logger(org.slf4j.Logger) StringCodec(org.redisson.client.codec.StringCodec) java.util.concurrent(java.util.concurrent) RTopic(org.redisson.api.RTopic) LoggerFactory(org.slf4j.LoggerFactory) RSemaphore(org.redisson.api.RSemaphore) RedissonClient(org.redisson.api.RedissonClient) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RSemaphore(org.redisson.api.RSemaphore)

Example 5 with CompletableFutureWrapper

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

the class RedissonKeys method deleteByPatternAsync.

@Override
public RFuture<Long> deleteByPatternAsync(String pattern) {
    if (commandExecutor instanceof CommandBatchService || commandExecutor instanceof CommandReactiveBatchService || commandExecutor instanceof CommandRxBatchService) {
        if (getConnectionManager().isClusterMode()) {
            throw new IllegalStateException("This method doesn't work in batch for Redis cluster mode. For Redis cluster execute it as non-batch method");
        }
        return commandExecutor.evalWriteAsync((String) null, null, RedisCommands.EVAL_LONG, "local keys = redis.call('keys', ARGV[1]) " + "local n = 0 " + "for i=1, #keys,5000 do " + "n = n + redis.call('del', unpack(keys, i, math.min(i+4999, table.getn(keys)))) " + "end " + "return n;", Collections.emptyList(), pattern);
    }
    int batchSize = 500;
    List<CompletableFuture<Long>> futures = new ArrayList<>();
    for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
        commandExecutor.getConnectionManager().getExecutor().execute(() -> {
            long count = 0;
            try {
                Iterator<String> keysIterator = createKeysIterator(entry, RedisCommands.SCAN, pattern, batchSize);
                List<String> keys = new ArrayList<>();
                while (keysIterator.hasNext()) {
                    String key = keysIterator.next();
                    keys.add(key);
                    if (keys.size() % batchSize == 0) {
                        count += delete(keys.toArray(new String[0]));
                        keys.clear();
                    }
                }
                if (!keys.isEmpty()) {
                    count += delete(keys.toArray(new String[0]));
                    keys.clear();
                }
                RFuture<Long> future = RedissonPromise.newSucceededFuture(count);
                futures.add(future.toCompletableFuture());
            } catch (Exception e) {
                CompletableFuture<Long> future = new CompletableFuture<>();
                future.completeExceptionally(e);
                futures.add(future);
            }
        });
    }
    CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
    CompletableFuture<Long> res = future.handle((r, e) -> {
        long cc = futures.stream().filter(f -> f.isDone()).mapToLong(f -> f.getNow(0L)).sum();
        if (e != null) {
            if (cc > 0) {
                RedisException ex = new RedisException(cc + " keys have been deleted. But one or more nodes has an error", e);
                throw new CompletionException(ex);
            } else {
                throw new CompletionException(e);
            }
        }
        return cc;
    });
    return new CompletableFutureWrapper<>(res);
}
Also used : RedisException(org.redisson.client.RedisException) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) java.util(java.util) StringCodec(org.redisson.client.codec.StringCodec) CommandBatchService(org.redisson.command.CommandBatchService) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClient(org.redisson.client.RedisClient) CompositeIterable(org.redisson.misc.CompositeIterable) RFuture(org.redisson.api.RFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) CommandRxBatchService(org.redisson.rx.CommandRxBatchService) StreamSupport(java.util.stream.StreamSupport) RObject(org.redisson.api.RObject) CommandReactiveBatchService(org.redisson.reactive.CommandReactiveBatchService) RType(org.redisson.api.RType) ConnectionManager(org.redisson.connection.ConnectionManager) RedissonPromise(org.redisson.misc.RedissonPromise) CompletionException(java.util.concurrent.CompletionException) RedissonBaseIterator(org.redisson.iterator.RedissonBaseIterator) RKeys(org.redisson.api.RKeys) RedisCommands(org.redisson.client.protocol.RedisCommands) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisCommand(org.redisson.client.protocol.RedisCommand) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException) CompletionException(java.util.concurrent.CompletionException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) CommandRxBatchService(org.redisson.rx.CommandRxBatchService) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) CompletionException(java.util.concurrent.CompletionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) RedisException(org.redisson.client.RedisException) CommandReactiveBatchService(org.redisson.reactive.CommandReactiveBatchService)

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