Search in sources :

Example 6 with TestKafkaEvent

use of org.apache.samza.test.util.TestKafkaEvent in project samza by apache.

the class TestZkLocalApplicationRunner method testRollingUpgradeOfStreamApplicationsShouldGenerateSameJobModel.

@Test
public void testRollingUpgradeOfStreamApplicationsShouldGenerateSameJobModel() throws Exception {
    // Set up kafka topics.
    publishKafkaEvents(inputKafkaTopic, 0, NUM_KAFKA_EVENTS, PROCESSOR_IDS[0]);
    Map<String, String> configMap = buildStreamApplicationConfigMap(testStreamAppName, testStreamAppId, false, Optional.empty());
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[0]);
    Config applicationConfig1 = new MapConfig(configMap);
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[1]);
    Config applicationConfig2 = new MapConfig(configMap);
    List<TestKafkaEvent> messagesProcessed = new ArrayList<>();
    TestStreamApplication.StreamApplicationCallback streamApplicationCallback = messagesProcessed::add;
    // Create StreamApplication from configuration.
    CountDownLatch kafkaEventsConsumedLatch = new CountDownLatch(NUM_KAFKA_EVENTS);
    CountDownLatch processedMessagesLatch1 = new CountDownLatch(1);
    CountDownLatch processedMessagesLatch2 = new CountDownLatch(1);
    ApplicationRunner appRunner1 = ApplicationRunners.getApplicationRunner(TestStreamApplication.getInstance(TEST_SYSTEM, ImmutableList.of(inputKafkaTopic), outputKafkaTopic, processedMessagesLatch1, null, kafkaEventsConsumedLatch, applicationConfig1), applicationConfig1);
    ApplicationRunner appRunner2 = ApplicationRunners.getApplicationRunner(TestStreamApplication.getInstance(TEST_SYSTEM, ImmutableList.of(inputKafkaTopic), outputKafkaTopic, processedMessagesLatch2, null, kafkaEventsConsumedLatch, applicationConfig2), applicationConfig2);
    // Run stream application.
    executeRun(appRunner1, applicationConfig1);
    executeRun(appRunner2, applicationConfig2);
    processedMessagesLatch1.await();
    processedMessagesLatch2.await();
    // Read job model before rolling upgrade.
    String jobModelVersion = zkUtils.getJobModelVersion();
    JobModel jobModel = JobModelUtil.readJobModel(jobModelVersion, zkMetadataStore);
    appRunner1.kill();
    appRunner1.waitForFinish();
    int lastProcessedMessageId = -1;
    for (TestKafkaEvent message : messagesProcessed) {
        lastProcessedMessageId = Math.max(lastProcessedMessageId, Integer.parseInt(message.getEventData()));
    }
    messagesProcessed.clear();
    assertEquals(ApplicationStatus.SuccessfulFinish, appRunner1.status());
    processedMessagesLatch1 = new CountDownLatch(1);
    publishKafkaEvents(inputKafkaTopic, NUM_KAFKA_EVENTS, 2 * NUM_KAFKA_EVENTS, PROCESSOR_IDS[0]);
    ApplicationRunner appRunner3 = ApplicationRunners.getApplicationRunner(TestStreamApplication.getInstance(TEST_SYSTEM, ImmutableList.of(inputKafkaTopic), outputKafkaTopic, processedMessagesLatch1, null, kafkaEventsConsumedLatch, applicationConfig1), applicationConfig1);
    executeRun(appRunner3, applicationConfig1);
    processedMessagesLatch1.await();
    // Read new job model after rolling upgrade.
    String newJobModelVersion = zkUtils.getJobModelVersion();
    JobModel newJobModel = JobModelUtil.readJobModel(newJobModelVersion, zkMetadataStore);
    assertEquals(Integer.parseInt(jobModelVersion) + 1, Integer.parseInt(newJobModelVersion));
    assertEquals(jobModel.getContainers(), newJobModel.getContainers());
    appRunner2.kill();
    appRunner2.waitForFinish();
    assertEquals(ApplicationStatus.SuccessfulFinish, appRunner2.status());
    appRunner3.kill();
    appRunner3.waitForFinish();
    assertEquals(ApplicationStatus.SuccessfulFinish, appRunner3.status());
}
Also used : MapConfig(org.apache.samza.config.MapConfig) StorageConfig(org.apache.samza.config.StorageConfig) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) JobConfig(org.apache.samza.config.JobConfig) ClusterManagerConfig(org.apache.samza.config.ClusterManagerConfig) ApplicationConfig(org.apache.samza.config.ApplicationConfig) TaskConfig(org.apache.samza.config.TaskConfig) ZkConfig(org.apache.samza.config.ZkConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) TestKafkaEvent(org.apache.samza.test.util.TestKafkaEvent) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) LocalApplicationRunner(org.apache.samza.runtime.LocalApplicationRunner) JobModel(org.apache.samza.job.model.JobModel) MapConfig(org.apache.samza.config.MapConfig) Test(org.junit.Test)

Example 7 with TestKafkaEvent

use of org.apache.samza.test.util.TestKafkaEvent in project samza by apache.

the class TestZkLocalApplicationRunner method publishKafkaEvents.

private void publishKafkaEvents(String topic, int startIndex, int endIndex, String streamProcessorId) {
    int partitionCount = topicToPartitionCount.getOrDefault(topic, 1);
    for (int eventIndex = startIndex; eventIndex < endIndex; eventIndex++) {
        try {
            LOGGER.info("Publish kafka event with index : {} for stream processor: {}.", eventIndex, streamProcessorId);
            producer.send(new ProducerRecord(topic, eventIndex % partitionCount, null, new TestKafkaEvent(streamProcessorId, String.valueOf(eventIndex)).toString().getBytes()));
        } catch (Exception e) {
            LOGGER.error("Publishing to kafka topic: {} resulted in exception: {}.", new Object[] { topic, e });
            throw new SamzaException(e);
        }
    }
}
Also used : TestKafkaEvent(org.apache.samza.test.util.TestKafkaEvent) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) SamzaException(org.apache.samza.SamzaException) ExpectedException(org.junit.rules.ExpectedException) SamzaException(org.apache.samza.SamzaException)

Aggregations

TestKafkaEvent (org.apache.samza.test.util.TestKafkaEvent)7 SamzaException (org.apache.samza.SamzaException)6 ExpectedException (org.junit.rules.ExpectedException)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ApplicationRunner (org.apache.samza.runtime.ApplicationRunner)5 Test (org.junit.Test)5 CoordinatorStreamStore (org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore)4 StartpointManager (org.apache.samza.startpoint.StartpointManager)4 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)4 TaskCallback (org.apache.samza.task.TaskCallback)4 TestTaskApplication (org.apache.samza.test.processor.TestTaskApplication)4 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)3 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ApplicationConfig (org.apache.samza.config.ApplicationConfig)1 ClusterManagerConfig (org.apache.samza.config.ClusterManagerConfig)1 Config (org.apache.samza.config.Config)1 JobConfig (org.apache.samza.config.JobConfig)1