Search in sources :

Example 1 with ConfirmationStatus

use of com.rabbitmq.stream.ConfirmationStatus in project rabbitmq-stream-java-client by rabbitmq.

the class StreamProducerTest method producerShouldBeClosedWhenStreamIsDeleted.

@ParameterizedTest
@ValueSource(ints = { 1, 7 })
void producerShouldBeClosedWhenStreamIsDeleted(int subEntrySize, TestInfo info) throws Exception {
    Level initialLogLevel = TestUtils.newLoggerLevel(ProducersCoordinator.class, Level.DEBUG);
    try {
        String s = streamName(info);
        environment.streamCreator().stream(s).create();
        StreamProducer producer = (StreamProducer) environment.producerBuilder().subEntrySize(subEntrySize).stream(s).build();
        AtomicInteger published = new AtomicInteger(0);
        AtomicInteger confirmed = new AtomicInteger(0);
        AtomicInteger errored = new AtomicInteger(0);
        Set<Number> errorCodes = ConcurrentHashMap.newKeySet();
        AtomicBoolean continuePublishing = new AtomicBoolean(true);
        Thread publishThread = new Thread(() -> {
            ConfirmationHandler confirmationHandler = confirmationStatus -> {
                if (confirmationStatus.isConfirmed()) {
                    confirmed.incrementAndGet();
                } else {
                    errored.incrementAndGet();
                    errorCodes.add(confirmationStatus.getCode());
                }
            };
            while (continuePublishing.get()) {
                try {
                    producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler);
                    published.incrementAndGet();
                } catch (StreamException e) {
                // OK
                }
            }
        });
        publishThread.start();
        Thread.sleep(1000L);
        assertThat(producer.isOpen()).isTrue();
        environment.deleteStream(s);
        waitAtMost(() -> !producer.isOpen());
        continuePublishing.set(false);
        waitAtMost(() -> !errorCodes.isEmpty(), () -> "The producer should have received negative publish confirms");
    } finally {
        TestUtils.newLoggerLevel(ProducersCoordinator.class, initialLogLevel);
    }
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) SortedSet(java.util.SortedSet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IntConsumer(java.util.function.IntConsumer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) TreeSet(java.util.TreeSet) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProducerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ProducerInfo) Duration(java.time.Duration) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) Host(com.rabbitmq.stream.Host) ValueSource(org.junit.jupiter.params.provider.ValueSource) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Status(com.rabbitmq.stream.impl.StreamProducer.Status) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Compression(com.rabbitmq.stream.compression.Compression) Producer(com.rabbitmq.stream.Producer) Collectors(java.util.stream.Collectors) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Level(ch.qos.logback.classic.Level) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Level(ch.qos.logback.classic.Level) StreamException(com.rabbitmq.stream.StreamException) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ConfirmationStatus

use of com.rabbitmq.stream.ConfirmationStatus in project rabbitmq-stream-java-client by rabbitmq.

the class StreamProducerTest method subEntryBatchesSentCompressedShouldBeConsumedProperly.

@Test
void subEntryBatchesSentCompressedShouldBeConsumedProperly() {
    int messagePerProducer = 10000;
    int messageCount = Compression.values().length * messagePerProducer;
    CountDownLatch publishLatch = new CountDownLatch(messageCount);
    ConfirmationHandler confirmationHandler = confirmationStatus -> publishLatch.countDown();
    AtomicInteger messageIndex = new AtomicInteger(0);
    Set<String> publishedBodies = ConcurrentHashMap.newKeySet(messageCount);
    for (Compression compression : Compression.values()) {
        Producer producer = environment.producerBuilder().stream(stream).subEntrySize(100).compression(compression).build();
        IntStream.range(0, messagePerProducer).forEach(i -> {
            String body = "compression " + compression.name() + " message " + messageIndex.getAndIncrement();
            producer.send(producer.messageBuilder().addData(body.getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler);
            publishedBodies.add(body);
        });
    }
    assertThat(latchAssert(publishLatch)).completes();
    Set<String> consumedBodies = ConcurrentHashMap.newKeySet(messageCount);
    CountDownLatch consumeLatch = new CountDownLatch(messageCount);
    environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> {
        consumedBodies.add(new String(message.getBodyAsBinary(), StandardCharsets.UTF_8));
        consumeLatch.countDown();
    }).build();
    assertThat(latchAssert(consumeLatch)).completes();
    assertThat(consumedBodies).isNotEmpty().hasSameSizeAs(publishedBodies);
    publishedBodies.forEach(publishBody -> assertThat(consumedBodies.contains(publishBody)).isTrue());
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) SortedSet(java.util.SortedSet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IntConsumer(java.util.function.IntConsumer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) TreeSet(java.util.TreeSet) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProducerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ProducerInfo) Duration(java.time.Duration) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) Host(com.rabbitmq.stream.Host) ValueSource(org.junit.jupiter.params.provider.ValueSource) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Status(com.rabbitmq.stream.impl.StreamProducer.Status) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Compression(com.rabbitmq.stream.compression.Compression) Producer(com.rabbitmq.stream.Producer) Collectors(java.util.stream.Collectors) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Level(ch.qos.logback.classic.Level) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) Compression(com.rabbitmq.stream.compression.Compression) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Producer(com.rabbitmq.stream.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ConfirmationStatus

use of com.rabbitmq.stream.ConfirmationStatus in project rabbitmq-stream-java-client by rabbitmq.

the class StreamProducerTest method shouldRecoverAfterConnectionIsKilled.

@ParameterizedTest
@ValueSource(ints = { 1, 10 })
@TestUtils.DisabledIfRabbitMqCtlNotSet
void shouldRecoverAfterConnectionIsKilled(int subEntrySize) throws Exception {
    Producer producer = environment.producerBuilder().subEntrySize(subEntrySize).stream(stream).build();
    AtomicInteger published = new AtomicInteger(0);
    AtomicInteger confirmed = new AtomicInteger(0);
    AtomicInteger errored = new AtomicInteger(0);
    AtomicBoolean canPublish = new AtomicBoolean(true);
    Thread publishThread = new Thread(() -> {
        ConfirmationHandler confirmationHandler = confirmationStatus -> {
            if (confirmationStatus.isConfirmed()) {
                confirmed.incrementAndGet();
            } else {
                errored.incrementAndGet();
            }
        };
        while (true) {
            try {
                if (canPublish.get()) {
                    producer.send(producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build(), confirmationHandler);
                    published.incrementAndGet();
                } else {
                    Thread.sleep(500);
                }
            } catch (InterruptedException | StreamException e) {
            // OK
            }
        }
    });
    publishThread.start();
    Thread.sleep(1000L);
    Host.killConnection("rabbitmq-stream-producer-0");
    waitAtMost(10, () -> ((StreamProducer) producer).status() == Status.NOT_AVAILABLE);
    canPublish.set(false);
    assertThat(confirmed.get()).isPositive();
    waitAtMost(5, () -> confirmed.get() + errored.get() == published.get(), () -> String.format("confirmed %d / errored %d / published %d, %d + %d = %d != %d, difference %d", confirmed.get(), errored.get(), published.get(), confirmed.get(), errored.get(), (confirmed.get() + errored.get()), published.get(), (published.get() - (confirmed.get() + errored.get()))));
    assertThat(confirmed.get() + errored.get()).isEqualTo(published.get());
    waitAtMost(10, () -> ((StreamProducer) producer).status() == StreamProducer.Status.RUNNING);
    int confirmedAfterUnavailability = confirmed.get();
    int errorAfterUnavailability = errored.get();
    canPublish.set(true);
    waitAtMost(10, () -> confirmed.get() > confirmedAfterUnavailability * 2);
    assertThat(errored.get()).isEqualTo(errorAfterUnavailability);
    canPublish.set(false);
    publishThread.interrupt();
    waitAtMost(10, () -> confirmed.get() + errored.get() == published.get());
    CountDownLatch consumeLatch = new CountDownLatch(confirmed.get());
    environment.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((offset, message) -> {
        consumeLatch.countDown();
    }).build();
    assertThat(consumeLatch.await(10, TimeUnit.SECONDS)).isTrue();
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) SortedSet(java.util.SortedSet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IntConsumer(java.util.function.IntConsumer) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) TreeSet(java.util.TreeSet) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProducerInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.ProducerInfo) Duration(java.time.Duration) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) Host(com.rabbitmq.stream.Host) ValueSource(org.junit.jupiter.params.provider.ValueSource) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Status(com.rabbitmq.stream.impl.StreamProducer.Status) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Compression(com.rabbitmq.stream.compression.Compression) Producer(com.rabbitmq.stream.Producer) Collectors(java.util.stream.Collectors) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Level(ch.qos.logback.classic.Level) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Producer(com.rabbitmq.stream.Producer) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) StreamException(com.rabbitmq.stream.StreamException) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ConfirmationStatus

use of com.rabbitmq.stream.ConfirmationStatus in project rabbitmq-stream-java-client by rabbitmq.

the class StreamProducer method send.

@Override
public void send(Message message, ConfirmationHandler confirmationHandler) {
    if (confirmationHandler == null) {
        confirmationHandler = NO_OP_CONFIRMATION_HANDLER;
    }
    try {
        if (canSend()) {
            if (this.blockOnMaxUnconfirmed) {
                unconfirmedMessagesSemaphore.acquire();
                doSend(message, confirmationHandler);
            } else {
                if (unconfirmedMessagesSemaphore.tryAcquire(this.enqueueTimeoutMs, TimeUnit.MILLISECONDS)) {
                    doSend(message, confirmationHandler);
                } else {
                    confirmationHandler.handle(new ConfirmationStatus(message, false, CODE_MESSAGE_ENQUEUEING_FAILED));
                }
            }
        } else {
            failPublishing(message, confirmationHandler);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new StreamException("Interrupted while waiting to accumulate outbound message", e);
    }
}
Also used : ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) StreamException(com.rabbitmq.stream.StreamException)

Example 5 with ConfirmationStatus

use of com.rabbitmq.stream.ConfirmationStatus in project rabbitmq-stream-java-client by rabbitmq.

the class SuperStreamProducer method send.

@Override
public void send(Message message, ConfirmationHandler confirmationHandler) {
    List<String> streams = this.routingStrategy.route(message, superStreamMetadata);
    if (streams.isEmpty()) {
        confirmationHandler.handle(new ConfirmationStatus(message, false, Constants.CODE_NO_ROUTE_FOUND));
    } else {
        for (String stream : streams) {
            Producer producer = producers.computeIfAbsent(stream, stream1 -> {
                Producer p = producerBuilder.duplicate().stream(stream1).build();
                return p;
            });
            producer.send(message, confirmationHandler);
        }
    }
}
Also used : ConfirmationStatus(com.rabbitmq.stream.ConfirmationStatus) Producer(com.rabbitmq.stream.Producer)

Aggregations

ConfirmationStatus (com.rabbitmq.stream.ConfirmationStatus)10 Producer (com.rabbitmq.stream.Producer)9 StreamException (com.rabbitmq.stream.StreamException)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Level (ch.qos.logback.classic.Level)7 BackOffDelayPolicy (com.rabbitmq.stream.BackOffDelayPolicy)7 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)7 Constants (com.rabbitmq.stream.Constants)7 Environment (com.rabbitmq.stream.Environment)7 EnvironmentBuilder (com.rabbitmq.stream.EnvironmentBuilder)7 Host (com.rabbitmq.stream.Host)7 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)7 Compression (com.rabbitmq.stream.compression.Compression)7 ProducerInfo (com.rabbitmq.stream.impl.MonitoringTestUtils.ProducerInfo)7 Status (com.rabbitmq.stream.impl.StreamProducer.Status)7 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)7 TestUtils.localhost (com.rabbitmq.stream.impl.TestUtils.localhost)7 TestUtils.streamName (com.rabbitmq.stream.impl.TestUtils.streamName)7 TestUtils.waitAtMost (com.rabbitmq.stream.impl.TestUtils.waitAtMost)7