Search in sources :

Example 1 with HealthReport

use of io.smallrye.reactive.messaging.health.HealthReport in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkTest method testABeanProducingMessagesSentToKafka.

@Test
public void testABeanProducingMessagesSentToKafka() {
    runApplication(getKafkaSinkConfigForProducingBean(), ProducingBean.class);
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics("output", 10, Duration.ofSeconds(10));
    await().until(this::isReady);
    assertThat(consumed.awaitCompletion(Duration.ofMinutes(1)).count()).isEqualTo(10);
    HealthReport liveness = getHealth().getLiveness();
    HealthReport readiness = getHealth().getReadiness();
    assertThat(liveness.isOk()).isTrue();
    assertThat(readiness.isOk()).isTrue();
    assertThat(liveness.getChannels()).hasSize(1);
    assertThat(readiness.getChannels()).hasSize(1);
    assertThat(liveness.getChannels().get(0).getChannel()).isEqualTo("output");
    assertThat(readiness.getChannels().get(0).getChannel()).isEqualTo("output");
    KafkaClientService service = get(KafkaClientService.class);
    assertThat(service.getProducer("output")).isNotNull();
    assertThat(service.getProducer("missing")).isNull();
    assertThatThrownBy(() -> service.getProducer(null)).isInstanceOf(NullPointerException.class);
    assertThat(service.getProducerChannels()).containsExactly("output");
}
Also used : HealthReport(io.smallrye.reactive.messaging.health.HealthReport) Test(org.junit.jupiter.api.Test)

Example 2 with HealthReport

use of io.smallrye.reactive.messaging.health.HealthReport in project smallrye-reactive-messaging by smallrye.

the class KafkaSinkWithLegacyMetadataTest method testABeanProducingMessagesSentToKafka.

@Test
public void testABeanProducingMessagesSentToKafka() {
    runApplication(getKafkaSinkConfigForProducingBean(), ProducingBean.class);
    ConsumerTask<String, Integer> consumed = companion.consumeIntegers().fromTopics(topic, 10, Duration.ofSeconds(10));
    await().until(this::isReady);
    assertThat(consumed.awaitCompletion(Duration.ofMinutes(1)).count()).isEqualTo(10);
    HealthReport liveness = getHealth().getLiveness();
    HealthReport readiness = getHealth().getReadiness();
    assertThat(liveness.isOk()).isTrue();
    assertThat(readiness.isOk()).isTrue();
    assertThat(liveness.getChannels()).hasSize(1);
    assertThat(readiness.getChannels()).hasSize(1);
    assertThat(liveness.getChannels().get(0).getChannel()).isEqualTo("output");
    assertThat(readiness.getChannels().get(0).getChannel()).isEqualTo("output");
    KafkaClientService service = get(KafkaClientService.class);
    assertThat(service.getProducer("output")).isNotNull();
    assertThat(service.getProducer("missing")).isNull();
    assertThatThrownBy(() -> service.getProducer(null)).isInstanceOf(NullPointerException.class);
}
Also used : HealthReport(io.smallrye.reactive.messaging.health.HealthReport) Test(org.junit.jupiter.api.Test)

Example 3 with HealthReport

use of io.smallrye.reactive.messaging.health.HealthReport in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceTest method testABeanConsumingTheKafkaMessages.

@Test
public void testABeanConsumingTheKafkaMessages() {
    String topic = UUID.randomUUID().toString();
    String group = UUID.randomUUID().toString();
    ConsumptionBean bean = run(myKafkaSourceConfig(topic, group));
    List<Integer> list = bean.getResults();
    assertThat(list).isEmpty();
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> list.size() >= 10);
    assertThat(list).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    List<KafkaRecord<String, Integer>> messages = bean.getKafkaMessages();
    messages.forEach(m -> {
        assertThat(m.getTopic()).isEqualTo(topic);
        assertThat(m.getTimestamp()).isAfter(Instant.EPOCH);
        assertThat(m.getPartition()).isGreaterThan(-1);
    });
    HealthReport liveness = getHealth().getLiveness();
    HealthReport readiness = getHealth().getReadiness();
    assertThat(liveness.isOk()).isTrue();
    assertThat(readiness.isOk()).isTrue();
    assertThat(liveness.getChannels()).hasSize(1);
    assertThat(readiness.getChannels()).hasSize(1);
    assertThat(liveness.getChannels().get(0).getChannel()).isEqualTo("data");
    assertThat(readiness.getChannels().get(0).getChannel()).isEqualTo("data");
    KafkaClientService service = get(KafkaClientService.class);
    assertThat(service.getConsumer("data")).isNotNull();
    assertThat(service.getConsumer("missing")).isNull();
    assertThatThrownBy(() -> service.getConsumer(null)).isInstanceOf(NullPointerException.class);
    assertThat(service.getConsumerChannels()).containsExactly("data");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) Test(org.junit.jupiter.api.Test)

Example 4 with HealthReport

use of io.smallrye.reactive.messaging.health.HealthReport in project smallrye-reactive-messaging by smallrye.

the class BatchCommitStrategiesTest method testThrottledStrategyWithTooManyUnackedMessages.

@Test
void testThrottledStrategyWithTooManyUnackedMessages() {
    MapBasedConfig config = commonConfiguration().with("client.id", UUID.randomUUID().toString()).with("commit-strategy", "throttled").with("auto.offset.reset", "earliest").with("health-enabled", true).with("throttled.unprocessed-record-max-age.ms", 1000).with("auto.commit.interval.ms", 100);
    String group = UUID.randomUUID().toString();
    source = new KafkaSource<>(vertx, group, new KafkaConnectorIncomingConfiguration(config), getConsumerRebalanceListeners(), CountKafkaCdiEvents.noCdiEvents, getDeserializationFailureHandlers(), -1);
    injectMockConsumer(source, consumer);
    List<Message<?>> list = new CopyOnWriteArrayList<>();
    source.getBatchStream().subscribe().with(list::add);
    TopicPartition p0 = new TopicPartition(TOPIC, 0);
    TopicPartition p1 = new TopicPartition(TOPIC, 1);
    Map<TopicPartition, Long> offsets = new HashMap<>();
    offsets.put(p0, 0L);
    offsets.put(p1, 5L);
    consumer.updateBeginningOffsets(offsets);
    consumer.schedulePollTask(() -> {
        consumer.rebalance(offsets.keySet());
        source.getCommitHandler().partitionsAssigned(offsets.keySet());
    });
    for (int i = 0; i < 500; i++) {
        int j = i;
        consumer.schedulePollTask(() -> {
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 0, j, "k", "v0-" + j));
            consumer.addRecord(new ConsumerRecord<>(TOPIC, 1, j, "r", "v1-" + j));
        });
    }
    // Expected number of messages: 500 messages in each partition minus the [0..5) messages from p1
    int expected = 500 * 2 - 5;
    await().until(() -> list.stream().map(IncomingKafkaRecordBatch.class::cast).mapToLong(r -> r.getRecords().size()).sum() == expected);
    // Ack first 10 records
    int count = 0;
    for (Message<?> message : list) {
        assertThat(message.getMetadata(IncomingKafkaRecordBatchMetadata.class)).isPresent();
        if (count < 10) {
            message.ack().toCompletableFuture().join();
            count++;
        }
    }
    // wait until health check is not ok
    await().until(() -> {
        HealthReport.HealthReportBuilder builder = HealthReport.builder();
        source.isAlive(builder);
        HealthReport r = builder.build();
        return !r.isOk();
    });
    // build the health check again and get the report message
    HealthReport.HealthReportBuilder builder = HealthReport.builder();
    source.isAlive(builder);
    String message = builder.build().getChannels().get(0).getMessage();
    assertThat(message).containsAnyOf("my-topic-0", "my-topic-1");
}
Also used : org.apache.kafka.clients.consumer(org.apache.kafka.clients.consumer) BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) io.smallrye.reactive.messaging.kafka(io.smallrye.reactive.messaging.kafka) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) MockKafkaUtils.injectMockConsumer(io.smallrye.reactive.messaging.kafka.base.MockKafkaUtils.injectMockConsumer) TypeLiteral(javax.enterprise.util.TypeLiteral) Duration(java.time.Duration) WeldTestBase(io.smallrye.reactive.messaging.kafka.base.WeldTestBase) Instance(javax.enterprise.inject.Instance) TopicPartition(org.apache.kafka.common.TopicPartition) Awaitility.await(org.awaitility.Awaitility.await) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) Vertx(io.vertx.mutiny.core.Vertx) ApplicationScoped(javax.enterprise.context.ApplicationScoped) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) IncomingKafkaRecordBatchMetadata(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordBatchMetadata) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Identifier(io.smallrye.common.annotation.Identifier) Message(org.eclipse.microprofile.reactive.messaging.Message) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) TopicPartition(org.apache.kafka.common.TopicPartition) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 5 with HealthReport

use of io.smallrye.reactive.messaging.health.HealthReport in project smallrye-reactive-messaging by smallrye.

the class KafkaCommitHandlerTest method testSourceWithThrottledAndRebalance.

@Test
public void testSourceWithThrottledAndRebalance() {
    companion.topics().createAndWait(topic, 2);
    MapBasedConfig config1 = newCommonConfigForSource().with("client.id", UUID.randomUUID().toString()).with("group.id", "test-source-with-throttled-latest-processed-commit").with("value.deserializer", IntegerDeserializer.class.getName()).with("commit-strategy", "throttled").with("throttled.unprocessed-record-max-age.ms", 1);
    MapBasedConfig config2 = newCommonConfigForSource().with("client.id", UUID.randomUUID().toString()).with("group.id", "test-source-with-throttled-latest-processed-commit").with("value.deserializer", IntegerDeserializer.class.getName()).with("commit-strategy", "throttled").with("throttled.unprocessed-record-max-age.ms", 1);
    KafkaConnectorIncomingConfiguration ic1 = new KafkaConnectorIncomingConfiguration(config1);
    KafkaConnectorIncomingConfiguration ic2 = new KafkaConnectorIncomingConfiguration(config2);
    source = new KafkaSource<>(vertx, "test-source-with-throttled-latest-processed-commit", ic1, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    KafkaSource<String, Integer> source2 = new KafkaSource<>(vertx, "test-source-with-throttled-latest-processed-commit", ic2, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages1 = Collections.synchronizedList(new ArrayList<>());
    source.getStream().subscribe().with(m -> {
        m.ack();
        messages1.add(m);
    });
    await().until(() -> source.getConsumer().getAssignments().await().indefinitely().size() == 2);
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, Integer.toString(i % 2), i), 10000);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 10);
    List<Message<?>> messages2 = Collections.synchronizedList(new ArrayList<>());
    source2.getStream().subscribe().with(m -> {
        m.ack();
        messages2.add(m);
    });
    await().until(() -> source2.getConsumer().getAssignments().await().indefinitely().size() == 1 && source.getConsumer().getAssignments().await().indefinitely().size() == 1);
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, Integer.toString(i % 2), i), 10000);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() + messages2.size() >= 10000);
    await().atMost(2, TimeUnit.MINUTES).untilAsserted(() -> {
        TopicPartition tp1 = new TopicPartition(topic, 0);
        TopicPartition tp2 = new TopicPartition(topic, 1);
        Map<TopicPartition, OffsetAndMetadata> result = companion.consumerGroups().offsets("test-source-with-throttled-latest-processed-commit", Arrays.asList(tp1, tp2));
        assertNotNull(result.get(tp1));
        assertNotNull(result.get(tp2));
        assertEquals(result.get(tp1).offset() + result.get(tp2).offset(), 20000);
    });
    await().atMost(2, TimeUnit.MINUTES).untilAsserted(() -> {
        HealthReport.HealthReportBuilder healthReportBuilder = HealthReport.builder();
        source.isAlive(healthReportBuilder);
        HealthReport build = healthReportBuilder.build();
        boolean ok = build.isOk();
        if (!ok) {
            build.getChannels().forEach(ci -> System.out.println(ci.getChannel() + " - " + ci.getMessage()));
        }
        assertTrue(ok);
    });
    source2.closeQuietly();
}
Also used : IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) Message(org.eclipse.microprofile.reactive.messaging.Message) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) Test(org.junit.jupiter.api.Test)

Aggregations

HealthReport (io.smallrye.reactive.messaging.health.HealthReport)16 Test (org.junit.jupiter.api.Test)16 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)7 Message (org.eclipse.microprofile.reactive.messaging.Message)6 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)5 TopicPartition (org.apache.kafka.common.TopicPartition)4 KafkaConnectorIncomingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration)2 IncomingKafkaRecordMetadata (io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata)2 KafkaSource (io.smallrye.reactive.messaging.kafka.impl.KafkaSource)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 UnsatisfiedResolutionException (javax.enterprise.inject.UnsatisfiedResolutionException)2 RepeatedTest (org.junit.jupiter.api.RepeatedTest)2 Identifier (io.smallrye.common.annotation.Identifier)1 io.smallrye.reactive.messaging.kafka (io.smallrye.reactive.messaging.kafka)1 IncomingKafkaRecordBatchMetadata (io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordBatchMetadata)1 MockKafkaUtils.injectMockConsumer (io.smallrye.reactive.messaging.kafka.base.MockKafkaUtils.injectMockConsumer)1 WeldTestBase (io.smallrye.reactive.messaging.kafka.base.WeldTestBase)1 ProducerTask (io.smallrye.reactive.messaging.kafka.companion.ProducerTask)1 Vertx (io.vertx.mutiny.core.Vertx)1