Search in sources :

Example 41 with RFuture

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);
}
Also used : RedisException(org.redisson.client.RedisException) RPromise(org.redisson.misc.RPromise) java.util(java.util) NodeType(org.redisson.api.NodeType) Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) MessageDigest(java.security.MessageDigest) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClient(org.redisson.client.RedisClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) RFuture(org.redisson.api.RFuture) NodeSource(org.redisson.connection.NodeSource) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) RedissonReference(org.redisson.RedissonReference) SlotCallback(org.redisson.SlotCallback) Logger(org.slf4j.Logger) LRUCacheMap(org.redisson.cache.LRUCacheMap) ConnectionManager(org.redisson.connection.ConnectionManager) RedissonPromise(org.redisson.misc.RedissonPromise) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RedisCommands(org.redisson.client.protocol.RedisCommands) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteBufUtil(io.netty.buffer.ByteBufUtil) RedisRedirectException(org.redisson.client.RedisRedirectException) CompletionStage(java.util.concurrent.CompletionStage) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) Entry(java.util.Map.Entry) RedisCommand(org.redisson.client.protocol.RedisCommand) RedissonObjectBuilder(org.redisson.liveobject.core.RedissonObjectBuilder) RFuture(org.redisson.api.RFuture) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RedisCommand(org.redisson.client.protocol.RedisCommand)

Example 42 with RFuture

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();
                });
            }
        });
    });
}
Also used : NodeType(org.redisson.api.NodeType) RedisConnection(org.redisson.client.RedisConnection) org.redisson.config(org.redisson.config) Logger(org.slf4j.Logger) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) AsyncCountDownLatch(org.redisson.misc.AsyncCountDownLatch) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) RedisURI(org.redisson.misc.RedisURI) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClient(org.redisson.client.RedisClient) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) RedisConnectionException(org.redisson.client.RedisConnectionException) RedisCommands(org.redisson.client.protocol.RedisCommands) RFuture(org.redisson.api.RFuture) TimeUnit(java.util.concurrent.TimeUnit) CompletionStage(java.util.concurrent.CompletionStage) FreezeReason(org.redisson.connection.ClientConnectionsEntry.FreezeReason) Map(java.util.Map) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) RedisConnection(org.redisson.client.RedisConnection)

Example 43 with RFuture

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;
}
Also used : HashValue(org.redisson.misc.HashValue) RPromise(org.redisson.misc.RPromise) TransactionalOperation(org.redisson.transaction.operation.TransactionalOperation) Arrays(java.util.Arrays) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) UnlinkOperation(org.redisson.transaction.operation.UnlinkOperation) RedissonMultiLock(org.redisson.RedissonMultiLock) HashMap(java.util.HashMap) Hash(org.redisson.misc.Hash) org.redisson.transaction.operation.map(org.redisson.transaction.operation.map) RedisClient(org.redisson.client.RedisClient) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RFuture(org.redisson.api.RFuture) RMap(org.redisson.api.RMap) BigDecimal(java.math.BigDecimal) RedissonObject(org.redisson.RedissonObject) ByteBuf(io.netty.buffer.ByteBuf) ScanResult(org.redisson.ScanResult) TouchOperation(org.redisson.transaction.operation.TouchOperation) RLock(org.redisson.api.RLock) Map(java.util.Map) RedissonMap(org.redisson.RedissonMap) MapScanResult(org.redisson.client.protocol.decoder.MapScanResult) Iterator(java.util.Iterator) Collection(java.util.Collection) RedissonPromise(org.redisson.misc.RedissonPromise) Set(java.util.Set) DeleteOperation(org.redisson.transaction.operation.DeleteOperation) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) Entry(java.util.Map.Entry) RedissonPromise(org.redisson.misc.RedissonPromise) NumberConvertor(org.redisson.client.protocol.convertor.NumberConvertor) HashValue(org.redisson.misc.HashValue) BigDecimal(java.math.BigDecimal)

Example 44 with RFuture

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);
        });
    });
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicReference(java.util.concurrent.atomic.AtomicReference) RFuture(org.redisson.api.RFuture)

Aggregations

RFuture (org.redisson.api.RFuture)44 FutureListener (io.netty.util.concurrent.FutureListener)19 Future (io.netty.util.concurrent.Future)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)12 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 StringCodec (org.redisson.client.codec.StringCodec)11 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)11 RPromise (org.redisson.misc.RPromise)11 RedissonPromise (org.redisson.misc.RedissonPromise)11 ArrayList (java.util.ArrayList)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 RedisClient (org.redisson.client.RedisClient)10 RedisCommands (org.redisson.client.protocol.RedisCommands)10 java.util.concurrent (java.util.concurrent)9 CommandAsyncExecutor (org.redisson.command.CommandAsyncExecutor)9 Timeout (io.netty.util.Timeout)8 java.util (java.util)8