Search in sources :

Example 26 with KafkaConnectorIncomingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration in project smallrye-reactive-messaging by smallrye.

the class KafkaCommitHandlerTest method testSourceWithAutoCommitDisabled.

@Test
public void testSourceWithAutoCommitDisabled() throws ExecutionException, InterruptedException, TimeoutException {
    MapBasedConfig config = newCommonConfigForSource().with("group.id", "test-source-with-auto-commit-disabled").with("value.deserializer", IntegerDeserializer.class.getName()).with("commit-strategy", "latest");
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, "test-source-with-auto-commit-disabled", ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages = Collections.synchronizedList(new ArrayList<>());
    source.getStream().subscribe().with(messages::add);
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 10);
    assertThat(messages.stream().map(m -> ((KafkaRecord<String, Integer>) m).getPayload()).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    Message<?> last = messages.get(messages.size() - 1);
    last.ack().toCompletableFuture().get(2, TimeUnit.MINUTES);
    await().ignoreExceptions().untilAsserted(() -> {
        TopicPartition topicPartition = new TopicPartition(topic, 0);
        OffsetAndMetadata offset = companion.consumerGroups().offsets("test-source-with-auto-commit-disabled", topicPartition);
        assertNotNull(offset);
        assertEquals(10L, offset.offset());
    });
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) CountKafkaCdiEvents(io.smallrye.reactive.messaging.kafka.CountKafkaCdiEvents) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TopicPartition(org.apache.kafka.common.TopicPartition) Awaitility.await(org.awaitility.Awaitility.await) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) UUID(java.util.UUID) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) KafkaRecord(io.smallrye.reactive.messaging.kafka.KafkaRecord) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Optional(java.util.Optional) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) Collections(java.util.Collections) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord) Message(org.eclipse.microprofile.reactive.messaging.Message) KafkaRecord(io.smallrye.reactive.messaging.kafka.KafkaRecord) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord) 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)

Example 27 with KafkaConnectorIncomingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration in project smallrye-reactive-messaging by smallrye.

the class KafkaCommitHandlerTest method testSourceWithThrottledLatestProcessedCommitEnabled.

@Test
public void testSourceWithThrottledLatestProcessedCommitEnabled() {
    MapBasedConfig config = newCommonConfigForSource().with("client.id", UUID.randomUUID().toString()).with("group.id", "test-source-with-throttled-latest-processed-commit").with("auto.offset.reset", "earliest").with("value.deserializer", IntegerDeserializer.class.getName()).with("commit-strategy", "throttled").with("throttled.unprocessed-record-max-age.ms", 100);
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, "test-source-with-throttled-latest-processed-commit", ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages = Collections.synchronizedList(new ArrayList<>());
    source.getStream().subscribe().with(messages::add);
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 10);
    assertThat(messages.stream().map(m -> ((KafkaRecord<String, Integer>) m).getPayload()).collect(Collectors.toList())).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    await().atMost(2, TimeUnit.MINUTES).ignoreExceptions().untilAsserted(() -> {
        // we must keep acking to eventually induce a commit
        messages.forEach(Message::ack);
        TopicPartition topicPartition = new TopicPartition(topic, 0);
        OffsetAndMetadata offset = companion.consumerGroups().offsets("test-source-with-throttled-latest-processed-commit", topicPartition);
        assertNotNull(offset);
        assertEquals(10L, offset.offset());
    });
    await().atMost(2, TimeUnit.MINUTES).untilAsserted(() -> {
        HealthReport.HealthReportBuilder healthReportBuilder = HealthReport.builder();
        source.isAlive(healthReportBuilder);
        assertTrue(healthReportBuilder.build().isOk());
    });
}
Also used : IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaCompanionTestBase(io.smallrye.reactive.messaging.kafka.base.KafkaCompanionTestBase) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HealthReport(io.smallrye.reactive.messaging.health.HealthReport) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) ArrayList(java.util.ArrayList) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) CountKafkaCdiEvents(io.smallrye.reactive.messaging.kafka.CountKafkaCdiEvents) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TopicPartition(org.apache.kafka.common.TopicPartition) Awaitility.await(org.awaitility.Awaitility.await) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) UUID(java.util.UUID) UnsatisfiedInstance(io.smallrye.reactive.messaging.kafka.base.UnsatisfiedInstance) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Message(org.eclipse.microprofile.reactive.messaging.Message) AfterEach(org.junit.jupiter.api.AfterEach) KafkaRecord(io.smallrye.reactive.messaging.kafka.KafkaRecord) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Optional(java.util.Optional) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) KafkaSource(io.smallrye.reactive.messaging.kafka.impl.KafkaSource) Collections(java.util.Collections) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord) Message(org.eclipse.microprofile.reactive.messaging.Message) KafkaRecord(io.smallrye.reactive.messaging.kafka.KafkaRecord) IncomingKafkaRecord(io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord) 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)

Example 28 with KafkaConnectorIncomingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithCloudEventsTest method testReceivingStructuredCloudEventsWithByteArrayDeserializer.

@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithByteArrayDeserializer() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.deserializer", ByteArrayDeserializer.class.getName());
    config.put("channel-name", topic);
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages = new ArrayList<>();
    source.getStream().subscribe().with(messages::add);
    companion.produce(String.class, JsonObject.class).fromRecords(new ProducerRecord<>(topic, null, null, "key", new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "foo").put("data", new JsonObject().put("name", "neo")), Collections.singletonList(new RecordHeader("content-type", "application/cloudevents+json; charset=utf-8".getBytes()))));
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 1);
    Message<?> message = messages.get(0);
    IncomingKafkaCloudEventMetadata<String, JsonObject> metadata = message.getMetadata(IncomingKafkaCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNotNull();
    assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
    assertThat(metadata.getType()).isEqualTo("type");
    assertThat(metadata.getId()).isEqualTo("id");
    assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
    assertThat(metadata.getSubject()).hasValue("foo");
    assertThat(metadata.getData().getString("name")).isEqualTo("neo");
    // Extensions
    assertThat(metadata.getKey()).isEqualTo("key");
    // Rule 3.1 - partitionkey attribute
    assertThat(metadata.<String>getExtension("partitionkey")).hasValue("key");
    assertThat(metadata.getTopic()).isEqualTo(topic);
    assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
    assertThat(((JsonObject) message.getPayload()).getString("name")).isEqualTo("neo");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) JsonObject(io.vertx.core.json.JsonObject) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) ByteArrayDeserializer(org.apache.kafka.common.serialization.ByteArrayDeserializer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 29 with KafkaConnectorIncomingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration in project smallrye-reactive-messaging by smallrye.

the class ClientTestBase method createSourceSeekToBeginning.

public KafkaSource<Integer, String> createSourceSeekToBeginning() {
    String groupId = UUID.randomUUID().toString();
    MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic);
    SingletonInstance<KafkaConsumerRebalanceListener> listeners = new SingletonInstance<>(groupId, getKafkaConsumerRebalanceListenerAwaitingAssignationAndSeekToBeginning());
    source = new KafkaSource<>(vertx, groupId, new KafkaConnectorIncomingConfiguration(config), listeners, CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), 0);
    return source;
}
Also used : KafkaConsumerRebalanceListener(io.smallrye.reactive.messaging.kafka.KafkaConsumerRebalanceListener) SingletonInstance(io.smallrye.reactive.messaging.kafka.base.SingletonInstance) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)

Example 30 with KafkaConnectorIncomingConfiguration

use of io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration in project smallrye-reactive-messaging by smallrye.

the class HighLatencyTest method testHighLatency.

@Test
public void testHighLatency() throws InterruptedException, IOException {
    MapBasedConfig config = newCommonConfigForSource().with("value.deserializer", IntegerDeserializer.class.getName()).with(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 6000).with(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG, 100).with("retry", true).with("retry-attempts", 100).with("retry-max-wait", 30);
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<KafkaRecord<?, ?>> messages1 = new ArrayList<>();
    source.getStream().subscribe().with(messages1::add);
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 10);
    toxics().latency("latency", ToxicDirection.UPSTREAM, 6000 + 1000);
    // session timeout + a bit more just in case.
    Thread.sleep(6000 + 2000);
    toxics().get("latency").remove();
    companion.produceIntegers().usingGenerator(i -> new ProducerRecord<>(topic, 10 + i), 10);
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages1.size() >= 20);
    assertThat(messages1.size()).isGreaterThanOrEqualTo(20);
}
Also used : ArrayList(java.util.ArrayList) KafkaRecord(io.smallrye.reactive.messaging.kafka.KafkaRecord) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) MapBasedConfig(io.smallrye.reactive.messaging.test.common.config.MapBasedConfig) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaConnectorIncomingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration)37 Test (org.junit.jupiter.api.Test)31 Message (org.eclipse.microprofile.reactive.messaging.Message)28 ArrayList (java.util.ArrayList)27 MapBasedConfig (io.smallrye.reactive.messaging.test.common.config.MapBasedConfig)20 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)18 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)17 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)17 IncomingKafkaCloudEventMetadata (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata)14 TopicPartition (org.apache.kafka.common.TopicPartition)13 JsonObject (io.vertx.core.json.JsonObject)12 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)10 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)9 KafkaSource (io.smallrye.reactive.messaging.kafka.impl.KafkaSource)8 HealthReport (io.smallrye.reactive.messaging.health.HealthReport)7 CountKafkaCdiEvents (io.smallrye.reactive.messaging.kafka.CountKafkaCdiEvents)6 IncomingKafkaRecord (io.smallrye.reactive.messaging.kafka.IncomingKafkaRecord)6 Collectors (java.util.stream.Collectors)6 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6