Search in sources :

Example 6 with Command

use of io.lettuce.core.protocol.Command in project lettuce-core by lettuce-io.

the class CommandSegmentCommandFactory method createCommand.

@Override
public RedisCommand<Object, Object, Object> createCommand(Object[] parameters) {
    MethodParametersAccessor parametersAccessor = new CodecAwareMethodParametersAccessor(new DefaultMethodParametersAccessor(commandMethod.getParameters(), parameters), typeContext);
    CommandArgs<Object, Object> args = new CommandArgs<>(redisCodec);
    CommandOutput<Object, Object, ?> output = outputFactory.create(redisCodec);
    Command<Object, Object, ?> command = new Command<>(this.segments.getCommandType(), output, args);
    parameterBinder.bind(args, redisCodec, segments, parametersAccessor);
    return (Command) command;
}
Also used : MethodParametersAccessor(io.lettuce.core.dynamic.parameter.MethodParametersAccessor) CommandArgs(io.lettuce.core.protocol.CommandArgs) RedisCommand(io.lettuce.core.protocol.RedisCommand) Command(io.lettuce.core.protocol.Command)

Example 7 with Command

use of io.lettuce.core.protocol.Command in project lettuce-core by lettuce-io.

the class ReactiveBackpressurePropagationUnitTests method writeCommandAndCancelInTheMiddle.

@Test
void writeCommandAndCancelInTheMiddle() throws Exception {
    Command<String, String, List<String>> lrange = new Command<>(CommandType.LRANGE, new ValueListOutput<>(StringCodec.UTF8));
    RedisPublisher<String, String, String> publisher = new RedisPublisher<>(lrange, statefulConnection, true, ImmediateEventExecutor.INSTANCE);
    CountDownLatch pressureArrived = new CountDownLatch(1);
    CountDownLatch buildPressure = new CountDownLatch(1);
    CountDownLatch waitForPressureReduced = new CountDownLatch(2);
    Disposable cancellation = Flux.from(publisher).limitRate(2).publishOn(Schedulers.single()).doOnNext(s -> {
        try {
            pressureArrived.countDown();
            buildPressure.await();
        } catch (InterruptedException e) {
        }
        waitForPressureReduced.countDown();
    }).subscribe();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    // produce some back pressure
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.arrayHeader(4)));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("one")));
    pressureArrived.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("two")));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("three")));
    assertThat(embeddedChannel.config().isAutoRead()).isFalse();
    cancellation.dispose();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    // allow processing
    buildPressure.countDown();
    // emit the last item
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("four")));
    // done
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) ClientResources(io.lettuce.core.resource.ClientResources) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) Disposable(reactor.core.Disposable) StringCodec(io.lettuce.core.codec.StringCodec) CommandType(io.lettuce.core.protocol.CommandType) Mock(org.mockito.Mock) CommandLatencyCollector(io.lettuce.core.metrics.CommandLatencyCollector) StatefulConnection(io.lettuce.core.api.StatefulConnection) Command(io.lettuce.core.protocol.Command) Unpooled(io.netty.buffer.Unpooled) LocalAddress(io.netty.channel.local.LocalAddress) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ImmediateEventExecutor(io.netty.util.concurrent.ImmediateEventExecutor) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Tracing(io.lettuce.core.tracing.Tracing) CommandHandler(io.lettuce.core.protocol.CommandHandler) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Endpoint(io.lettuce.core.protocol.Endpoint) ValueListOutput(io.lettuce.core.output.ValueListOutput) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) RedisCommand(io.lettuce.core.protocol.RedisCommand) Command(io.lettuce.core.protocol.Command) RedisCommand(io.lettuce.core.protocol.RedisCommand) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 8 with Command

use of io.lettuce.core.protocol.Command in project lettuce-core by lettuce-io.

the class ReactiveBackpressurePropagationUnitTests method writeCommand.

@Test
void writeCommand() throws Exception {
    Command<String, String, List<String>> lrange = new Command<>(CommandType.LRANGE, new ValueListOutput<>(StringCodec.UTF8));
    RedisPublisher<String, String, String> publisher = new RedisPublisher<>((Command) lrange, statefulConnection, true, ImmediateEventExecutor.INSTANCE);
    CountDownLatch pressureArrived = new CountDownLatch(1);
    CountDownLatch buildPressure = new CountDownLatch(1);
    CountDownLatch waitForPressureReduced = new CountDownLatch(2);
    CountDownLatch waitForWorkCompleted = new CountDownLatch(4);
    Flux.from(publisher).limitRate(2).publishOn(Schedulers.single()).doOnNext(s -> {
        try {
            pressureArrived.countDown();
            buildPressure.await();
        } catch (InterruptedException e) {
        }
        waitForPressureReduced.countDown();
        waitForWorkCompleted.countDown();
    }).subscribe();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    // produce some back pressure
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.arrayHeader(4)));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("one")));
    pressureArrived.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("two")));
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("three")));
    assertThat(embeddedChannel.config().isAutoRead()).isFalse();
    // allow processing
    buildPressure.countDown();
    // wait until processing caught up
    waitForPressureReduced.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
    // emit the last item
    embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("four")));
    // done
    waitForWorkCompleted.await();
    assertThat(embeddedChannel.config().isAutoRead()).isTrue();
}
Also used : ClientResources(io.lettuce.core.resource.ClientResources) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) Disposable(reactor.core.Disposable) StringCodec(io.lettuce.core.codec.StringCodec) CommandType(io.lettuce.core.protocol.CommandType) Mock(org.mockito.Mock) CommandLatencyCollector(io.lettuce.core.metrics.CommandLatencyCollector) StatefulConnection(io.lettuce.core.api.StatefulConnection) Command(io.lettuce.core.protocol.Command) Unpooled(io.netty.buffer.Unpooled) LocalAddress(io.netty.channel.local.LocalAddress) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ImmediateEventExecutor(io.netty.util.concurrent.ImmediateEventExecutor) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Tracing(io.lettuce.core.tracing.Tracing) CommandHandler(io.lettuce.core.protocol.CommandHandler) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Endpoint(io.lettuce.core.protocol.Endpoint) ValueListOutput(io.lettuce.core.output.ValueListOutput) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) RedisCommand(io.lettuce.core.protocol.RedisCommand) Command(io.lettuce.core.protocol.Command) RedisCommand(io.lettuce.core.protocol.RedisCommand) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 9 with Command

use of io.lettuce.core.protocol.Command in project lettuce-core by lettuce-io.

the class BraveTracingUnitTests method shouldCustomizeSpan.

@Test
void shouldCustomizeSpan() {
    BraveTracing tracing = BraveTracing.builder().tracing(clientTracing).spanCustomizer((command, span) -> span.tag("cmd", command.getType().name())).build();
    BraveTracing.BraveSpan span = (BraveTracing.BraveSpan) tracing.getTracerProvider().getTracer().nextSpan();
    span.start(new AsyncCommand<>(new Command<>(CommandType.AUTH, null)));
    MutableSpan braveSpan = ReflectionTestUtils.getField(span.getSpan(), "state");
    Object[] tags = ReflectionTestUtils.getField(braveSpan, "tags");
    assertThat(tags).contains("cmd", "AUTH");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Tracing(brave.Tracing) Tracer(brave.Tracer) CommandType(io.lettuce.core.protocol.CommandType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Span(zipkin2.Span) CurrentTraceContext(brave.propagation.CurrentTraceContext) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Command(io.lettuce.core.protocol.Command) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) List(java.util.List) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) BeforeAll(org.junit.jupiter.api.BeforeAll) MutableSpan(brave.handler.MutableSpan) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) Tag(brave.Tag) Queue(java.util.Queue) TestSupport(io.lettuce.core.TestSupport) ReflectionTestUtils(io.lettuce.test.ReflectionTestUtils) MutableSpan(brave.handler.MutableSpan) Command(io.lettuce.core.protocol.Command) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) Test(org.junit.jupiter.api.Test)

Aggregations

Command (io.lettuce.core.protocol.Command)9 RedisCommand (io.lettuce.core.protocol.RedisCommand)7 Test (org.junit.jupiter.api.Test)5 CommandType (io.lettuce.core.protocol.CommandType)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 StringCodec (io.lettuce.core.codec.StringCodec)3 ClientResources (io.lettuce.core.resource.ClientResources)3 Tracing (io.lettuce.core.tracing.Tracing)3 Unpooled (io.netty.buffer.Unpooled)3 List (java.util.List)3 Assertions (org.assertj.core.api.Assertions)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 ArgumentMatchers (org.mockito.ArgumentMatchers)3 Mock (org.mockito.Mock)3 Mockito (org.mockito.Mockito)3 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)3 StatefulConnection (io.lettuce.core.api.StatefulConnection)2 CommandLatencyCollector (io.lettuce.core.metrics.CommandLatencyCollector)2 ValueListOutput (io.lettuce.core.output.ValueListOutput)2 AsyncCommand (io.lettuce.core.protocol.AsyncCommand)2