Search in sources :

Example 1 with ConnectionManager

use of org.redisson.connection.ConnectionManager in project redisson by redisson.

the class RedissonNode method retrieveAddresses.

private void retrieveAddresses() {
    ConnectionManager connectionManager = ((Redisson) redisson).getConnectionManager();
    for (MasterSlaveEntry entry : connectionManager.getEntrySet()) {
        CompletionStage<RedisConnection> readFuture = entry.connectionReadOp(null);
        RedisConnection readConnection = null;
        try {
            readConnection = readFuture.toCompletableFuture().get(connectionManager.getConfig().getConnectTimeout(), TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (Exception e) {
        // skip
        }
        if (readConnection != null) {
            entry.releaseRead(readConnection);
            remoteAddress = (InetSocketAddress) readConnection.getChannel().remoteAddress();
            localAddress = (InetSocketAddress) readConnection.getChannel().localAddress();
            return;
        }
        CompletionStage<RedisConnection> writeFuture = entry.connectionWriteOp(null);
        RedisConnection writeConnection = null;
        try {
            writeConnection = writeFuture.toCompletableFuture().get(connectionManager.getConfig().getConnectTimeout(), TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (Exception e) {
        // skip
        }
        if (writeConnection != null) {
            entry.releaseWrite(writeConnection);
            remoteAddress = (InetSocketAddress) writeConnection.getChannel().remoteAddress();
            localAddress = (InetSocketAddress) writeConnection.getChannel().localAddress();
            return;
        }
    }
}
Also used : ConnectionManager(org.redisson.connection.ConnectionManager) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) IOException(java.io.IOException) RedisConnection(org.redisson.client.RedisConnection)

Example 2 with ConnectionManager

use of org.redisson.connection.ConnectionManager in project redisson by redisson.

the class RedissonNode method retrieveAdresses.

private void retrieveAdresses() {
    ConnectionManager connectionManager = ((Redisson) redisson).getConnectionManager();
    for (MasterSlaveEntry entry : connectionManager.getEntrySet()) {
        RFuture<RedisConnection> readFuture = entry.connectionReadOp(null);
        if (readFuture.awaitUninterruptibly((long) connectionManager.getConfig().getConnectTimeout()) && readFuture.isSuccess()) {
            RedisConnection connection = readFuture.getNow();
            entry.releaseRead(connection);
            remoteAddress = (InetSocketAddress) connection.getChannel().remoteAddress();
            localAddress = (InetSocketAddress) connection.getChannel().localAddress();
            return;
        }
        RFuture<RedisConnection> writeFuture = entry.connectionWriteOp(null);
        if (writeFuture.awaitUninterruptibly((long) connectionManager.getConfig().getConnectTimeout()) && writeFuture.isSuccess()) {
            RedisConnection connection = writeFuture.getNow();
            entry.releaseWrite(connection);
            remoteAddress = (InetSocketAddress) connection.getChannel().remoteAddress();
            localAddress = (InetSocketAddress) connection.getChannel().localAddress();
            return;
        }
    }
}
Also used : ConnectionManager(org.redisson.connection.ConnectionManager) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisConnection(org.redisson.client.RedisConnection)

Example 3 with ConnectionManager

use of org.redisson.connection.ConnectionManager in project redisson by redisson.

the class CommandAsyncService method evalAsync.

private <T, R> RFuture<R> evalAsync(NodeSource nodeSource, boolean readOnlyMode, Codec codec, RedisCommand<T> evalCommandType, String script, List<Object> keys, boolean noRetry, Object... params) {
    if (isEvalCacheActive() && evalCommandType.getName().equals("EVAL")) {
        CompletableFuture<R> mainPromise = new CompletableFuture<>();
        Object[] pps = copy(params);
        CompletableFuture<R> promise = new CompletableFuture<>();
        String sha1 = calcSHA(script);
        RedisCommand cmd;
        if (readOnlyMode && evalShaROSupported.get()) {
            cmd = new RedisCommand(evalCommandType, "EVALSHA_RO");
        } else {
            cmd = new RedisCommand(evalCommandType, "EVALSHA");
        }
        List<Object> args = new ArrayList<Object>(2 + keys.size() + params.length);
        args.add(sha1);
        args.add(keys.size());
        args.addAll(keys);
        args.addAll(Arrays.asList(params));
        RedisExecutor<T, R> executor = new RedisExecutor<>(readOnlyMode, nodeSource, codec, cmd, args.toArray(), promise, false, connectionManager, objectBuilder, referenceType, noRetry);
        executor.execute();
        promise.whenComplete((res, e) -> {
            if (e != null) {
                if (e.getMessage().startsWith("ERR unknown command")) {
                    evalShaROSupported.set(false);
                    free(pps);
                    RFuture<R> future = evalAsync(nodeSource, readOnlyMode, codec, evalCommandType, script, keys, noRetry, params);
                    transfer(future.toCompletableFuture(), mainPromise);
                } else if (e.getMessage().startsWith("NOSCRIPT")) {
                    RFuture<String> loadFuture = loadScript(executor.getRedisClient(), script);
                    loadFuture.whenComplete((r, ex) -> {
                        if (ex != null) {
                            free(pps);
                            mainPromise.completeExceptionally(ex);
                            return;
                        }
                        List<Object> newargs = new ArrayList<Object>(2 + keys.size() + params.length);
                        newargs.add(sha1);
                        newargs.add(keys.size());
                        newargs.addAll(keys);
                        newargs.addAll(Arrays.asList(pps));
                        NodeSource ns = nodeSource;
                        if (ns.getRedisClient() == null) {
                            ns = new NodeSource(nodeSource, executor.getRedisClient());
                        }
                        RFuture<R> future = async(readOnlyMode, ns, codec, cmd, newargs.toArray(), false, noRetry);
                        transfer(future.toCompletableFuture(), mainPromise);
                    });
                } else {
                    free(pps);
                    mainPromise.completeExceptionally(e);
                }
                return;
            }
            free(pps);
            mainPromise.complete(res);
        });
        return new CompletableFutureWrapper<>(mainPromise);
    }
    List<Object> args = new ArrayList<Object>(2 + keys.size() + params.length);
    args.add(script);
    args.add(keys.size());
    args.addAll(keys);
    args.addAll(Arrays.asList(params));
    return async(readOnlyMode, nodeSource, codec, evalCommandType, args.toArray(), false, noRetry);
}
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) NodeSource(org.redisson.connection.NodeSource) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RedisCommand(org.redisson.client.protocol.RedisCommand)

Example 4 with ConnectionManager

use of org.redisson.connection.ConnectionManager 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)

Aggregations

ConnectionManager (org.redisson.connection.ConnectionManager)4 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)4 IOException (java.io.IOException)3 ByteBuf (io.netty.buffer.ByteBuf)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)2 ByteBufUtil (io.netty.buffer.ByteBufUtil)2 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)2 MessageDigest (java.security.MessageDigest)2 java.util (java.util)2 Entry (java.util.Map.Entry)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 BiConsumer (java.util.function.BiConsumer)2 Collectors (java.util.stream.Collectors)2 RedissonReference (org.redisson.RedissonReference)2