Search in sources :

Example 46 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class StreamKafkaPTest method when_partitionAddedWhileJobDown_then_consumedFromBeginning.

@Test
public void when_partitionAddedWhileJobDown_then_consumedFromBeginning() throws Exception {
    IList<Entry<Integer, String>> sinkList = instance().getList("sinkList");
    Pipeline p = Pipeline.create();
    Properties properties = properties();
    properties.setProperty("auto.offset.reset", "latest");
    p.readFrom(KafkaSources.<Integer, String>kafka(properties, topic1Name)).withoutTimestamps().writeTo(Sinks.list(sinkList));
    Job job = instance().getJet().newJob(p, new JobConfig().setProcessingGuarantee(EXACTLY_ONCE));
    assertTrueEventually(() -> {
        // This might add multiple `0` events to the topic - we need to do this because the source starts from
        // the latest position and we don't exactly know when it starts, so we try repeatedly
        kafkaTestSupport.produce(topic1Name, 0, "0").get();
        assertFalse(sinkList.isEmpty());
        assertEquals(entry(0, "0"), sinkList.get(0));
    });
    job.suspend();
    assertJobStatusEventually(job, JobStatus.SUSPENDED);
    // Note that the job might not have consumed all the zeroes from the topic at this point
    // When
    kafkaTestSupport.setPartitionCount(topic1Name, INITIAL_PARTITION_COUNT + 2);
    // We produce to a partition that didn't exist during the previous job execution.
    // The job must start reading the new partition from the beginning, otherwise it would miss this item.
    Entry<Integer, String> event = produceEventToNewPartition(INITIAL_PARTITION_COUNT);
    job.resume();
    // All events after the resume will be loaded: the non-consumed zeroes, and the possibly multiple
    // events added in produceEventToNewPartition(). But they must include the event added to the new partition.
    assertTrueEventually(() -> assertThat(sinkList).contains(event));
}
Also used : Entry(java.util.Map.Entry) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) Properties(java.util.Properties) Job(com.hazelcast.jet.Job) JobConfig(com.hazelcast.jet.config.JobConfig) Pipeline(com.hazelcast.jet.pipeline.Pipeline) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 47 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class StreamKafkaPTest method when_autoOffsetResetLatest_then_doesNotReadOldMessages.

@Test
public void when_autoOffsetResetLatest_then_doesNotReadOldMessages() throws Exception {
    IList<Entry<Integer, String>> sinkList = instance().getList("sinkList");
    Pipeline p = Pipeline.create();
    Properties properties = properties();
    properties.setProperty("auto.offset.reset", "latest");
    p.readFrom(KafkaSources.<Integer, String>kafka(properties, topic1Name)).withoutTimestamps().writeTo(Sinks.list(sinkList));
    kafkaTestSupport.produce(topic1Name, 0, "0").get();
    instance().getJet().newJob(p);
    assertTrueAllTheTime(() -> assertTrue(sinkList.isEmpty()), 2);
}
Also used : Entry(java.util.Map.Entry) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) Properties(java.util.Properties) Pipeline(com.hazelcast.jet.pipeline.Pipeline) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 48 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class StreamKafkaPTest method integrationTest.

private void integrationTest(ProcessingGuarantee guarantee) throws Exception {
    int messageCount = 20;
    HazelcastInstance[] instances = new HazelcastInstance[2];
    Arrays.setAll(instances, i -> createHazelcastInstance());
    Pipeline p = Pipeline.create();
    p.readFrom(KafkaSources.kafka(properties(), topic1Name, topic2Name)).withoutTimestamps().writeTo(Sinks.list("sink"));
    JobConfig config = new JobConfig();
    config.setProcessingGuarantee(guarantee);
    config.setSnapshotIntervalMillis(500);
    Job job = instances[0].getJet().newJob(p, config);
    sleepSeconds(3);
    for (int i = 0; i < messageCount; i++) {
        kafkaTestSupport.produce(topic1Name, i, Integer.toString(i));
        kafkaTestSupport.produce(topic2Name, i - messageCount, Integer.toString(i - messageCount));
    }
    IList<Object> list = instances[0].getList("sink");
    assertTrueEventually(() -> {
        assertEquals(messageCount * 2, list.size());
        for (int i = 0; i < messageCount; i++) {
            Entry<Integer, String> entry1 = createEntry(i);
            Entry<Integer, String> entry2 = createEntry(i - messageCount);
            assertTrue("missing entry: " + entry1, list.contains(entry1));
            assertTrue("missing entry: " + entry2, list.contains(entry2));
        }
    }, 15);
    if (guarantee != ProcessingGuarantee.NONE) {
        // wait until a new snapshot appears
        JobRepository jr = new JobRepository(instances[0]);
        long currentMax = jr.getJobExecutionRecord(job.getId()).snapshotId();
        assertTrueEventually(() -> {
            JobExecutionRecord jobExecutionRecord = jr.getJobExecutionRecord(job.getId());
            assertNotNull("jobExecutionRecord == null", jobExecutionRecord);
            long newMax = jobExecutionRecord.snapshotId();
            assertTrue("no snapshot produced", newMax > currentMax);
            System.out.println("snapshot " + newMax + " found, previous was " + currentMax);
        });
        // Bring down one member. Job should restart and drain additional items (and maybe
        // some of the previous duplicately).
        instances[1].getLifecycleService().terminate();
        Thread.sleep(500);
        for (int i = messageCount; i < 2 * messageCount; i++) {
            kafkaTestSupport.produce(topic1Name, i, Integer.toString(i));
            kafkaTestSupport.produce(topic2Name, i - messageCount, Integer.toString(i - messageCount));
        }
        assertTrueEventually(() -> {
            assertTrue("Not all messages were received", list.size() >= messageCount * 4);
            for (int i = 0; i < 2 * messageCount; i++) {
                Entry<Integer, String> entry1 = createEntry(i);
                Entry<Integer, String> entry2 = createEntry(i - messageCount);
                assertTrue("missing entry: " + entry1.toString(), list.contains(entry1));
                assertTrue("missing entry: " + entry2.toString(), list.contains(entry2));
            }
        }, 10);
    }
    assertFalse(job.getFuture().isDone());
    // cancel the job
    job.cancel();
    assertTrueEventually(() -> assertTrue(job.getFuture().isDone()));
}
Also used : JobRepository(com.hazelcast.jet.impl.JobRepository) JobExecutionRecord(com.hazelcast.jet.impl.JobExecutionRecord) JobConfig(com.hazelcast.jet.config.JobConfig) Pipeline(com.hazelcast.jet.pipeline.Pipeline) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Job(com.hazelcast.jet.Job)

Example 49 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class WriteKafkaPTest method testWriteToSpecificPartitions.

@Test
public void testWriteToSpecificPartitions() {
    String localTopic = topic;
    Pipeline p = Pipeline.create();
    p.readFrom(Sources.map(sourceIMap)).writeTo(KafkaSinks.kafka(properties, e -> new ProducerRecord<>(localTopic, e.getKey(), e.getKey(), e.getValue())));
    instance().getJet().newJob(p).join();
    kafkaTestSupport.assertTopicContentsEventually(topic, sourceIMap, true);
}
Also used : AbstractProcessor(com.hazelcast.jet.core.AbstractProcessor) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Traverser(com.hazelcast.jet.Traverser) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) Processor(com.hazelcast.jet.core.Processor) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) ArrayList(java.util.ArrayList) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Util.entry(com.hazelcast.jet.Util.entry) Duration(java.time.Duration) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Collections.singletonMap(java.util.Collections.singletonMap) KafkaSinks(com.hazelcast.jet.kafka.KafkaSinks) Job(com.hazelcast.jet.Job) Before(org.junit.Before) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) TestInbox(com.hazelcast.jet.core.test.TestInbox) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) Pipeline(com.hazelcast.jet.pipeline.Pipeline) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) JobConfig(com.hazelcast.jet.config.JobConfig) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Traversers(com.hazelcast.jet.Traversers) Category(org.junit.experimental.categories.Category) Sources(com.hazelcast.jet.pipeline.Sources) List(java.util.List) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Entry(java.util.Map.Entry) HOURS(java.util.concurrent.TimeUnit.HOURS) SinkStressTestUtil(com.hazelcast.jet.impl.connector.SinkStressTestUtil) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) Sink(com.hazelcast.jet.pipeline.Sink) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Pipeline(com.hazelcast.jet.pipeline.Pipeline) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 50 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class WriteKafkaPTest method testWriteToTopic.

@Test
public void testWriteToTopic() {
    Pipeline p = Pipeline.create();
    p.readFrom(Sources.map(sourceIMap)).writeTo(KafkaSinks.kafka(properties, topic));
    instance().getJet().newJob(p).join();
    kafkaTestSupport.assertTopicContentsEventually(topic, sourceIMap, false);
}
Also used : Pipeline(com.hazelcast.jet.pipeline.Pipeline) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

Pipeline (com.hazelcast.jet.pipeline.Pipeline)379 Test (org.junit.Test)300 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)142 QuickTest (com.hazelcast.test.annotation.QuickTest)142 Job (com.hazelcast.jet.Job)125 Sinks (com.hazelcast.jet.pipeline.Sinks)107 Category (org.junit.experimental.categories.Category)100 HazelcastInstance (com.hazelcast.core.HazelcastInstance)94 JobConfig (com.hazelcast.jet.config.JobConfig)86 Assert.assertEquals (org.junit.Assert.assertEquals)73 List (java.util.List)72 NightlyTest (com.hazelcast.test.annotation.NightlyTest)65 Before (org.junit.Before)64 Entry (java.util.Map.Entry)61 TestSources (com.hazelcast.jet.pipeline.test.TestSources)58 Assert.assertTrue (org.junit.Assert.assertTrue)50 Sources (com.hazelcast.jet.pipeline.Sources)49 IOException (java.io.IOException)48 BeforeClass (org.junit.BeforeClass)48 SimpleTestInClusterSupport (com.hazelcast.jet.SimpleTestInClusterSupport)42