Search in sources :

Example 11 with ClientResources

use of io.lettuce.core.resource.ClientResources in project lettuce-core by lettuce-io.

the class RedisClusterClientCdiBean method create.

@Override
public RedisClusterClient create(CreationalContext<RedisClusterClient> creationalContext) {
    CreationalContext<RedisURI> uriCreationalContext = beanManager.createCreationalContext(redisURIBean);
    RedisURI redisURI = (RedisURI) beanManager.getReference(redisURIBean, RedisURI.class, uriCreationalContext);
    if (clientResourcesBean != null) {
        ClientResources clientResources = (ClientResources) beanManager.getReference(clientResourcesBean, ClientResources.class, uriCreationalContext);
        return RedisClusterClient.create(clientResources, redisURI);
    }
    return RedisClusterClient.create(redisURI);
}
Also used : RedisURI(io.lettuce.core.RedisURI) ClientResources(io.lettuce.core.resource.ClientResources)

Example 12 with ClientResources

use of io.lettuce.core.resource.ClientResources in project lettuce-core by lettuce-io.

the class ConnectionFailureIntegrationTests method pingOnConnectFailureShouldCloseConnection.

@Test
void pingOnConnectFailureShouldCloseConnection() throws Exception {
    AtomicReference<Channel> ref = new AtomicReference<>();
    ClientResources clientResources = ClientResources.builder().nettyCustomizer(new NettyCustomizer() {

        @Override
        public void afterChannelInitialized(Channel channel) {
            ref.set(channel);
        }
    }).build();
    // Cluster node with auth
    RedisURI redisUri = RedisURI.create(TestSettings.host(), 7385);
    RedisClient client = RedisClient.create(clientResources);
    client.setOptions(ClientOptions.builder().pingBeforeActivateConnection(true).build());
    try {
        client.connect(redisUri);
        fail("Missing Exception");
    } catch (Exception e) {
        Wait.untilEquals(false, ref.get()::isRegistered).waitOrTimeout();
        assertThat(ref.get().isOpen()).isFalse();
        Wait.untilEquals(false, () -> ref.get().isRegistered()).waitOrTimeout();
    } finally {
        FastShutdown.shutdown(client);
        FastShutdown.shutdown(clientResources);
    }
}
Also used : ClientResources(io.lettuce.core.resource.ClientResources) Channel(io.netty.channel.Channel) NettyCustomizer(io.lettuce.core.resource.NettyCustomizer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 13 with ClientResources

use of io.lettuce.core.resource.ClientResources in project lettuce-core by lettuce-io.

the class ConnectionFailureIntegrationTests method emitEventOnReconnectFailure.

@Test
void emitEventOnReconnectFailure() throws Exception {
    RandomResponseServer ts = getRandomResponseServer();
    Queue<Event> queue = new ConcurrentLinkedQueue<>();
    ClientResources clientResources = ClientResources.create();
    RedisURI redisUri = RedisURI.create(defaultRedisUri.toURI());
    RedisClient client = RedisClient.create(clientResources);
    client.setOptions(ClientOptions.builder().build());
    try {
        RedisAsyncCommandsImpl<String, String> connection = (RedisAsyncCommandsImpl<String, String>) client.connect(redisUri).async();
        ConnectionWatchdog connectionWatchdog = ConnectionTestUtil.getConnectionWatchdog(connection.getStatefulConnection());
        redisUri.setPort(TestSettings.nonexistentPort());
        client.getResources().eventBus().get().subscribe(queue::add);
        connection.quit();
        Wait.untilTrue(() -> !connection.getStatefulConnection().isOpen()).waitOrTimeout();
        connectionWatchdog.run(0);
        Delay.delay(Duration.ofMillis(500));
        connection.getStatefulConnection().close();
        assertThat(queue).isNotEmpty();
        List<ReconnectFailedEvent> failures = queue.stream().filter(ReconnectFailedEvent.class::isInstance).map(ReconnectFailedEvent.class::cast).sorted(Comparator.comparingInt(ReconnectFailedEvent::getAttempt)).collect(Collectors.toList());
        assertThat(failures.size()).isGreaterThanOrEqualTo(2);
        ReconnectFailedEvent failure1 = failures.get(0);
        assertThat(failure1.localAddress()).isEqualTo(LocalAddress.ANY);
        assertThat(failure1.remoteAddress()).isInstanceOf(InetSocketAddress.class);
        assertThat(failure1.getCause()).hasMessageContaining("Invalid first byte");
        assertThat(failure1.getAttempt()).isZero();
        ReconnectFailedEvent failure2 = failures.get(1);
        assertThat(failure2.localAddress()).isEqualTo(LocalAddress.ANY);
        assertThat(failure2.remoteAddress()).isInstanceOf(InetSocketAddress.class);
        assertThat(failure2.getCause()).hasMessageContaining("Invalid first byte");
        assertThat(failure2.getAttempt()).isOne();
    } finally {
        ts.shutdown();
        FastShutdown.shutdown(client);
        FastShutdown.shutdown(clientResources);
    }
}
Also used : ClientResources(io.lettuce.core.resource.ClientResources) ReconnectFailedEvent(io.lettuce.core.event.connection.ReconnectFailedEvent) RandomResponseServer(io.lettuce.test.server.RandomResponseServer) Event(io.lettuce.core.event.Event) ReconnectFailedEvent(io.lettuce.core.event.connection.ReconnectFailedEvent) Test(org.junit.jupiter.api.Test)

Example 14 with ClientResources

use of io.lettuce.core.resource.ClientResources in project lettuce-core by lettuce-io.

the class BraveTracingIntegrationTests method getAndSetWithTraceWithCommandArgsExcludedFromTags.

@Test
void getAndSetWithTraceWithCommandArgsExcludedFromTags() {
    ClientResources clientResources = ClientResources.builder().tracing(BraveTracing.builder().tracing(clientTracing).excludeCommandArgsFromSpanTags().build()).build();
    RedisClient client = RedisClient.create(clientResources, RedisURI.Builder.redis(host, port).build());
    ScopedSpan trace = clientTracing.tracer().startScopedSpan("foo");
    StatefulRedisConnection<String, String> connect = client.connect();
    connect.sync().set("foo", "bar");
    connect.sync().get("foo");
    Wait.untilTrue(() -> spans.size() > 2).waitOrTimeout();
    trace.finish();
    List<Span> spans = new ArrayList<>(BraveTracingIntegrationTests.spans);
    assertThat(spans.get(1).name()).isEqualTo("set");
    assertThat(spans.get(1).tags()).doesNotContainKey("redis.args");
    assertThat(spans.get(2).name()).isEqualTo("get");
    assertThat(spans.get(2).tags()).doesNotContainKey("redis.args");
    assertThat(spans.get(3).name()).isEqualTo("foo");
    FastShutdown.shutdown(client);
    FastShutdown.shutdown(clientResources);
}
Also used : RedisClient(io.lettuce.core.RedisClient) ClientResources(io.lettuce.core.resource.ClientResources) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources) ArrayList(java.util.ArrayList) ScopedSpan(brave.ScopedSpan) Span(zipkin2.Span) ScopedSpan(brave.ScopedSpan) Test(org.junit.jupiter.api.Test)

Aggregations

ClientResources (io.lettuce.core.resource.ClientResources)14 Test (org.junit.jupiter.api.Test)7 RedisURI (io.lettuce.core.RedisURI)6 DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)4 RedisClient (io.lettuce.core.RedisClient)2 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)2 NettyCustomizer (io.lettuce.core.resource.NettyCustomizer)2 Channel (io.netty.channel.Channel)2 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)2 ScopedSpan (brave.ScopedSpan)1 ClientOptions (io.lettuce.core.ClientOptions)1 ConnectionFuture (io.lettuce.core.ConnectionFuture)1 RedisConnectionException (io.lettuce.core.RedisConnectionException)1 RedisException (io.lettuce.core.RedisException)1 SocketOptions (io.lettuce.core.SocketOptions)1