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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations