use of org.redisson.api.RFuture in project redisson by redisson.
the class CommandAsyncService method async.
public <V, R> RFuture<R> async(boolean readOnlyMode, NodeSource source, Codec codec, RedisCommand<V> command, Object[] params, boolean ignoreRedirect, boolean noRetry) {
if (readOnlyMode && command.getName().equals("SORT") && !sortRoSupported.get()) {
readOnlyMode = false;
} else if (readOnlyMode && command.getName().equals("SORT") && sortRoSupported.get()) {
RedisCommand cmd = new RedisCommand("SORT_RO", command.getReplayMultiDecoder());
CompletableFuture<R> mainPromise = createPromise();
RedisExecutor<V, R> executor = new RedisExecutor<>(readOnlyMode, source, codec, cmd, params, mainPromise, ignoreRedirect, connectionManager, objectBuilder, referenceType, noRetry);
executor.execute();
CompletableFuture<R> result = new CompletableFuture<>();
mainPromise.whenComplete((r, e) -> {
if (e != null && e.getMessage().startsWith("ERR unknown command")) {
sortRoSupported.set(false);
RFuture<R> future = async(false, source, codec, command, params, ignoreRedirect, noRetry);
transfer(future.toCompletableFuture(), result);
return;
}
transfer(mainPromise, result);
});
return new CompletableFutureWrapper<>(result);
}
CompletableFuture<R> mainPromise = createPromise();
RedisExecutor<V, R> executor = new RedisExecutor<>(readOnlyMode, source, codec, command, params, mainPromise, ignoreRedirect, connectionManager, objectBuilder, referenceType, noRetry);
executor.execute();
return new CompletableFutureWrapper<>(mainPromise);
}
use of org.redisson.api.RFuture in project redisson by redisson.
the class ReplicatedConnectionManager method checkNode.
private void checkNode(AsyncCountDownLatch latch, RedisURI uri, ReplicatedServersConfig cfg, Set<InetSocketAddress> slaveIPs) {
CompletionStage<RedisConnection> connectionFuture = connectToNode(cfg, uri, uri.getHost());
connectionFuture.whenComplete((connection, exc) -> {
if (exc != null) {
log.error(exc.getMessage(), exc);
latch.countDown();
return;
}
if (isShuttingDown()) {
return;
}
RFuture<Map<String, String>> result = connection.async(RedisCommands.INFO_REPLICATION);
result.whenComplete((r, ex) -> {
if (ex != null) {
log.error(ex.getMessage(), ex);
closeNodeConnection(connection);
latch.countDown();
return;
}
InetSocketAddress addr = connection.getRedisClient().getAddr();
Role role = Role.valueOf(r.get(ROLE_KEY));
if (Role.master.equals(role)) {
InetSocketAddress master = currentMaster.get();
if (master.equals(addr)) {
log.debug("Current master {} unchanged", master);
} else if (currentMaster.compareAndSet(master, addr)) {
CompletableFuture<RedisClient> changeFuture = changeMaster(singleSlotRange.getStartSlot(), uri);
changeFuture.exceptionally(e -> {
log.error("Unable to change master to " + addr, e);
currentMaster.compareAndSet(addr, master);
return null;
});
}
latch.countDown();
} else if (!config.checkSkipSlavesInit()) {
CompletableFuture<Void> f = slaveUp(addr, uri);
slaveIPs.add(addr);
f.whenComplete((res, e) -> {
latch.countDown();
});
}
});
});
}
use of org.redisson.api.RFuture in project redisson by redisson.
the class BaseTransactionalMap method addAndGetOperationAsync.
protected RFuture<V> addAndGetOperationAsync(K key, Number value) {
RPromise<V> result = new RedissonPromise<V>();
long threadId = Thread.currentThread().getId();
executeLocked(result, key, new Runnable() {
@Override
public void run() {
HashValue keyHash = toKeyHash(key);
MapEntry entry = state.get(keyHash);
if (entry != null) {
BigDecimal currentValue = BigDecimal.ZERO;
if (entry != MapEntry.NULL) {
currentValue = (BigDecimal) entry.getValue();
}
BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
state.put(keyHash, new MapEntry(key, res));
if (deleted != null) {
deleted = false;
}
NumberConvertor convertor = new NumberConvertor(value.getClass());
result.trySuccess((V) convertor.convert(res.toPlainString()));
return;
}
map.getAsync(key).onComplete((r, e) -> {
if (e != null) {
result.tryFailure(e);
return;
}
BigDecimal currentValue = new BigDecimal(r.toString());
BigDecimal res = currentValue.add(new BigDecimal(value.toString()));
operations.add(new MapAddAndGetOperation(map, key, value, transactionId, threadId));
state.put(keyHash, new MapEntry(key, res));
if (deleted != null) {
deleted = false;
}
NumberConvertor convertor = new NumberConvertor(value.getClass());
result.trySuccess((V) convertor.convert(res.toPlainString()));
});
}
});
return result;
}
use of org.redisson.api.RFuture in project redisson by redisson.
the class ElementsStream method takeElements.
public static <V> Flux<V> takeElements(Callable<RFuture<V>> callable) {
return Flux.create(emitter -> {
AtomicReference<RFuture<V>> futureRef = new AtomicReference<RFuture<V>>();
emitter.onRequest(n -> {
AtomicLong counter = new AtomicLong(n);
take(callable, emitter, counter, futureRef);
});
emitter.onDispose(() -> {
futureRef.get().cancel(true);
});
});
}
Aggregations