Search in sources :

Example 1 with ConnectionFuture

use of io.lettuce.core.ConnectionFuture in project LinkAgent by shulieTech.

the class LettuceDefaultConnectionMediator method getPreformanceClient.

public ConnectionFuture getPreformanceClient() {
    if (isUpdate.compareAndSet(true, false)) {
        ConnectionFuture dynamic = (ConnectionFuture) LettuceFactory.getFactory().dynamic(businessRedisClient);
        performanceRedisClient = dynamic;
    }
    return performanceRedisClient;
}
Also used : ConnectionFuture(io.lettuce.core.ConnectionFuture)

Example 2 with ConnectionFuture

use of io.lettuce.core.ConnectionFuture in project easeagent by megaease.

the class CommonRedisClientInterceptorTest method processRedisClient.

@Test
public void processRedisClient() throws SocketException {
    CommonRedisClientInterceptor commonRedisClientInterceptor = new CommonRedisClientInterceptor();
    RedisClient redisClient = RedisClient.create(RedisURI.create("127.0.0.1", 111));
    DatagramSocket s = new DatagramSocket(0);
    ConnectionFuture connectionFuture = ConnectionFuture.completed(new InetSocketAddress(s.getLocalPort()), "test");
    MethodInfo methodInfo = MethodInfo.builder().invoker(redisClient).retValue(connectionFuture).build();
    commonRedisClientInterceptor.processRedisClient(methodInfo, EaseAgent.getContext());
    assertTrue(methodInfo.getRetValue() instanceof ConnectionFutureWrapper);
    redisClient.shutdown();
}
Also used : RedisClient(io.lettuce.core.RedisClient) DatagramSocket(java.net.DatagramSocket) ConnectionFuture(io.lettuce.core.ConnectionFuture) InetSocketAddress(java.net.InetSocketAddress) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Example 3 with ConnectionFuture

use of io.lettuce.core.ConnectionFuture in project lettuce-core by lettuce-io.

the class PooledClusterConnectionProvider method getWriteConnection.

private CompletableFuture<StatefulRedisConnection<K, V>> getWriteConnection(int slot) {
    // avoid races when reconfiguring partitions.
    CompletableFuture<StatefulRedisConnection<K, V>> writer;
    synchronized (stateLock) {
        writer = writers[slot];
    }
    if (writer == null) {
        RedisClusterNode partition = partitions.getPartitionBySlot(slot);
        if (partition == null) {
            clusterEventListener.onUncoveredSlot(slot);
            return Futures.failed(new PartitionSelectorException("Cannot determine a partition for slot " + slot + ".", partitions.clone()));
        }
        // Use always host and port for slot-oriented operations. We don't want to get reconnected on a different
        // host because the nodeId can be handled by a different host.
        RedisURI uri = partition.getUri();
        ConnectionKey key = new ConnectionKey(Intent.WRITE, uri.getHost(), uri.getPort());
        ConnectionFuture<StatefulRedisConnection<K, V>> future = getConnectionAsync(key);
        return future.thenApply(connection -> {
            synchronized (stateLock) {
                if (writers[slot] == null) {
                    writers[slot] = CompletableFuture.completedFuture(connection);
                }
            }
            return connection;
        }).toCompletableFuture();
    }
    return writer;
}
Also used : Arrays(java.util.Arrays) Partitions(io.lettuce.core.cluster.models.partitions.Partitions) CompletableFuture(java.util.concurrent.CompletableFuture) StatefulConnection(io.lettuce.core.api.StatefulConnection) RedisNodeDescription(io.lettuce.core.models.role.RedisNodeDescription) Function(java.util.function.Function) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) ConnectionKey(io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey) Futures(io.lettuce.core.internal.Futures) ArrayList(java.util.ArrayList) LettuceAssert(io.lettuce.core.internal.LettuceAssert) OrderingReadFromAccessor(io.lettuce.core.OrderingReadFromAccessor) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) PushMessage(io.lettuce.core.api.push.PushMessage) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RedisFuture(io.lettuce.core.RedisFuture) AsyncConnectionProvider(io.lettuce.core.internal.AsyncConnectionProvider) Iterator(java.util.Iterator) RedisException(io.lettuce.core.RedisException) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) Exceptions(io.lettuce.core.internal.Exceptions) Collection(java.util.Collection) RedisClusterPushListener(io.lettuce.core.cluster.api.push.RedisClusterPushListener) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) RedisCodec(io.lettuce.core.codec.RedisCodec) RedisURI(io.lettuce.core.RedisURI) List(java.util.List) RedisConnectionException(io.lettuce.core.RedisConnectionException) HostAndPort(io.lettuce.core.internal.HostAndPort) ReadFrom(io.lettuce.core.ReadFrom) InternalLogger(io.netty.util.internal.logging.InternalLogger) ConnectionFuture(io.lettuce.core.ConnectionFuture) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisURI(io.lettuce.core.RedisURI) ConnectionKey(io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection)

Example 4 with ConnectionFuture

use of io.lettuce.core.ConnectionFuture in project easeagent by megaease.

the class ConnectionFutureWrapperTest method thenRunAsync.

@Test
public void thenRunAsync() throws ExecutionException, InterruptedException {
    ConnectionFutureWrapper<DynamicFieldAccessorObj> connectionFutureWrapper = createOne();
    AtomicBoolean ran = new AtomicBoolean(false);
    ConnectionFuture connectionFuture = connectionFutureWrapper.thenRunAsync(() -> ran.set(true));
    connectionFuture.get();
    assertTrue(ran.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectionFuture(io.lettuce.core.ConnectionFuture) Test(org.junit.Test)

Example 5 with ConnectionFuture

use of io.lettuce.core.ConnectionFuture in project easeagent by megaease.

the class CommonRedisClientInterceptorTest method doAfter.

@Test
public void doAfter() throws SocketException {
    CommonRedisClientInterceptor commonRedisClientInterceptor = new CommonRedisClientInterceptor();
    RedisClusterClient redisClusterClient = RedisClusterClient.create(RedisURI.create("127.0.0.1", 111));
    MethodInfo methodInfo = MethodInfo.builder().invoker(redisClusterClient).retValue(null).throwable(new RuntimeException()).build();
    commonRedisClientInterceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertNull(methodInfo.getRetValue());
    CompletableFuture completableFuture = CompletableFuture.completedFuture("testCompletableFuture");
    methodInfo = MethodInfo.builder().invoker(redisClusterClient).retValue(completableFuture).build();
    commonRedisClientInterceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertTrue(methodInfo.getRetValue() instanceof CompletableFutureWrapper);
    RedisClient redisClient = RedisClient.create(RedisURI.create("127.0.0.1", 111));
    DatagramSocket s = new DatagramSocket(0);
    ConnectionFuture connectionFuture = ConnectionFuture.completed(new InetSocketAddress(s.getLocalPort()), "test");
    methodInfo = MethodInfo.builder().invoker(redisClient).retValue(connectionFuture).build();
    commonRedisClientInterceptor.doAfter(methodInfo, EaseAgent.getContext());
    assertTrue(methodInfo.getRetValue() instanceof ConnectionFutureWrapper);
    redisClient.shutdown();
    redisClusterClient.shutdown();
}
Also used : RedisClient(io.lettuce.core.RedisClient) CompletableFuture(java.util.concurrent.CompletableFuture) DatagramSocket(java.net.DatagramSocket) ConnectionFuture(io.lettuce.core.ConnectionFuture) InetSocketAddress(java.net.InetSocketAddress) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient) Test(org.junit.Test)

Aggregations

ConnectionFuture (io.lettuce.core.ConnectionFuture)8 RedisURI (io.lettuce.core.RedisURI)4 RedisClient (io.lettuce.core.RedisClient)3 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 Test (org.junit.Test)3 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)2 Futures (io.lettuce.core.internal.Futures)2 StatefulRedisPubSubConnection (io.lettuce.core.pubsub.StatefulRedisPubSubConnection)2 InternalLogger (io.netty.util.internal.logging.InternalLogger)2 InternalLoggerFactory (io.netty.util.internal.logging.InternalLoggerFactory)2 DatagramSocket (java.net.DatagramSocket)2 InetSocketAddress (java.net.InetSocketAddress)2 Arrays (java.util.Arrays)2 List (java.util.List)2 OrderingReadFromAccessor (io.lettuce.core.OrderingReadFromAccessor)1 ReadFrom (io.lettuce.core.ReadFrom)1 RedisChannelWriter (io.lettuce.core.RedisChannelWriter)1 RedisConnectionException (io.lettuce.core.RedisConnectionException)1