Search in sources :

Example 1 with ZkMetadataStore

use of org.apache.samza.zk.ZkMetadataStore in project samza by apache.

the class TestLocalJobPlanner method createLocalJobPlanner.

private LocalJobPlanner createLocalJobPlanner(ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc) throws Exception {
    CoordinationUtils coordinationUtils = mock(CoordinationUtils.class);
    DistributedLock distributedLock = mock(DistributedLock.class);
    when(distributedLock.lock(anyObject())).thenReturn(true);
    when(coordinationUtils.getLock(anyString())).thenReturn(distributedLock);
    ZkMetadataStore zkMetadataStore = mock(ZkMetadataStore.class);
    when(zkMetadataStore.get(any())).thenReturn(null);
    PowerMockito.whenNew(ZkMetadataStore.class).withAnyArguments().thenReturn(zkMetadataStore);
    return spy(new LocalJobPlanner(appDesc, coordinationUtils, "FAKE_UID", "FAKE_RUNID"));
}
Also used : DistributedLock(org.apache.samza.coordinator.DistributedLock) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) CoordinationUtils(org.apache.samza.coordinator.CoordinationUtils)

Example 2 with ZkMetadataStore

use of org.apache.samza.zk.ZkMetadataStore in project samza by apache.

the class TestZkLocalApplicationRunner method setUp.

@Override
public void setUp() {
    super.setUp();
    String uniqueTestId = UUID.randomUUID().toString();
    testStreamAppName = String.format("test-app-name-%s", uniqueTestId);
    testStreamAppId = String.format("test-app-id-%s", uniqueTestId);
    inputKafkaTopic = String.format("test-input-topic-%s", uniqueTestId);
    outputKafkaTopic = String.format("test-output-topic-%s", uniqueTestId);
    inputSinglePartitionKafkaTopic = String.format("test-input-single-partition-topic-%s", uniqueTestId);
    outputSinglePartitionKafkaTopic = String.format("test-output-single-partition-topic-%s", uniqueTestId);
    // Set up stream application config map with the given testStreamAppName, testStreamAppId and test kafka system
    // TODO: processorId should typically come up from a processorID generator as processor.id will be deprecated in 0.14.0+
    Map<String, String> configMap = buildStreamApplicationConfigMap(testStreamAppName, testStreamAppId, false, Optional.empty());
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[0]);
    applicationConfig1 = new ApplicationConfig(new MapConfig(configMap));
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[1]);
    applicationConfig2 = new ApplicationConfig(new MapConfig(configMap));
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[2]);
    applicationConfig3 = new ApplicationConfig(new MapConfig(configMap));
    ZkClient zkClient = new ZkClient(zkConnect(), ZK_CONNECTION_TIMEOUT_MS, ZK_CONNECTION_TIMEOUT_MS, new ZkStringSerializer());
    ZkKeyBuilder zkKeyBuilder = new ZkKeyBuilder(ZkJobCoordinatorFactory.getJobCoordinationZkPath(applicationConfig1));
    zkUtils = new ZkUtils(zkKeyBuilder, zkClient, ZK_CONNECTION_TIMEOUT_MS, ZK_SESSION_TIMEOUT_MS, new NoOpMetricsRegistry());
    zkUtils.connect();
    topicToPartitionCount = ImmutableMap.of(inputSinglePartitionKafkaTopic, 1, outputSinglePartitionKafkaTopic, 1, inputKafkaTopic, NUM_PARTITION, outputKafkaTopic, NUM_PARTITION);
    List<NewTopic> newTopics = ImmutableList.of(inputKafkaTopic, outputKafkaTopic, inputSinglePartitionKafkaTopic, outputSinglePartitionKafkaTopic).stream().map(topic -> new NewTopic(topic, topicToPartitionCount.get(topic), (short) 1)).collect(Collectors.toList());
    assertTrue("Encountered errors during test setup. Failed to create topics.", createTopics(newTopics));
    zkMetadataStore = new ZkMetadataStore(zkUtils.getKeyBuilder().getRootPath(), new MapConfig(configMap), new NoOpMetricsRegistry());
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) TaskModel(org.apache.samza.job.model.TaskModel) StoreProperties(org.apache.samza.storage.StoreProperties) Map(java.util.Map) StorageEngine(org.apache.samza.storage.StorageEngine) StandaloneTestUtils(org.apache.samza.test.StandaloneTestUtils) MapConfig(org.apache.samza.config.MapConfig) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) StorageConfig(org.apache.samza.config.StorageConfig) ImmutableSet(com.google.common.collect.ImmutableSet) TaskName(org.apache.samza.container.TaskName) ZkStringSerializer(org.apache.samza.zk.ZkStringSerializer) ImmutableMap(com.google.common.collect.ImmutableMap) TaskApplication(org.apache.samza.application.TaskApplication) ZkClient(org.I0Itec.zkclient.ZkClient) Set(java.util.Set) UUID(java.util.UUID) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MetadataStoreFactory(org.apache.samza.metadatastore.MetadataStoreFactory) Assert.assertFalse(org.junit.Assert.assertFalse) IntegrationTestHarness(org.apache.samza.test.harness.IntegrationTestHarness) Optional(java.util.Optional) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) ApplicationStatus(org.apache.samza.job.ApplicationStatus) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JobModelUtil(org.apache.samza.job.model.JobModelUtil) ZkKeyBuilder(org.apache.samza.zk.ZkKeyBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) ClusterManagerConfig(org.apache.samza.config.ClusterManagerConfig) Serde(org.apache.samza.serializers.Serde) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MetadataStore(org.apache.samza.metadatastore.MetadataStore) ImmutableList(com.google.common.collect.ImmutableList) MessageCollector(org.apache.samza.task.MessageCollector) ZkUtils(org.apache.samza.zk.ZkUtils) Timeout(org.junit.rules.Timeout) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ApplicationRunners(org.apache.samza.runtime.ApplicationRunners) ExpectedException(org.junit.rules.ExpectedException) JobModel(org.apache.samza.job.model.JobModel) LocalApplicationRunner(org.apache.samza.runtime.LocalApplicationRunner) ZkJobCoordinatorFactory(org.apache.samza.zk.ZkJobCoordinatorFactory) Logger(org.slf4j.Logger) TaskConfig(org.apache.samza.config.TaskConfig) JobContext(org.apache.samza.context.JobContext) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) NewTopic(org.apache.kafka.clients.admin.NewTopic) Partition(org.apache.samza.Partition) ContainerContext(org.apache.samza.context.ContainerContext) Assert.assertTrue(org.junit.Assert.assertTrue) CoordinatorStreamValueSerde(org.apache.samza.coordinator.stream.CoordinatorStreamValueSerde) Test(org.junit.Test) ZkConfig(org.apache.samza.config.ZkConfig) Maps(com.google.common.collect.Maps) File(java.io.File) SamzaException(org.apache.samza.SamzaException) TestKafkaEvent(org.apache.samza.test.util.TestKafkaEvent) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TaskMode(org.apache.samza.job.model.TaskMode) MockStorageEngine(org.apache.samza.storage.MockStorageEngine) Rule(org.junit.Rule) ReflectionUtil(org.apache.samza.util.ReflectionUtil) ContainerModel(org.apache.samza.job.model.ContainerModel) StorageEngineFactory(org.apache.samza.storage.StorageEngineFactory) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ZkUtils(org.apache.samza.zk.ZkUtils) ZkKeyBuilder(org.apache.samza.zk.ZkKeyBuilder) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) NewTopic(org.apache.kafka.clients.admin.NewTopic) MapConfig(org.apache.samza.config.MapConfig) ZkStringSerializer(org.apache.samza.zk.ZkStringSerializer)

Example 3 with ZkMetadataStore

use of org.apache.samza.zk.ZkMetadataStore in project samza by apache.

the class TestZkLocalApplicationRunner method shouldStopNewProcessorsJoiningGroupWhenNumContainersIsGreaterThanNumTasks.

/**
 * sspGrouper is set to GroupBySystemStreamPartitionFactory.
 * Run a stream application(appRunner1) consuming messages from input topic(effectively one container).
 *
 * In the callback triggered by appRunner1 after processing a message, bring up an another stream application(appRunner2).
 *
 * Assertions:
 *           A) JobModel generated before and after the addition of appRunner2 should be equal.
 *           B) Second stream application(appRunner2) should not join the group and process any message.
 */
@Test
public void shouldStopNewProcessorsJoiningGroupWhenNumContainersIsGreaterThanNumTasks() throws InterruptedException {
    // Set up kafka topics.
    publishKafkaEvents(inputSinglePartitionKafkaTopic, 0, NUM_KAFKA_EVENTS * 2, PROCESSOR_IDS[0]);
    // Configuration, verification variables
    MapConfig testConfig = new MapConfig(ImmutableMap.of(JobConfig.SSP_GROUPER_FACTORY, "org.apache.samza.container.grouper.stream.GroupBySystemStreamPartitionFactory", JobConfig.JOB_DEBOUNCE_TIME_MS, "10", ClusterManagerConfig.JOB_HOST_AFFINITY_ENABLED, "true"));
    // Declared as final array to update it from streamApplication callback(Variable should be declared final to access in lambda block).
    final JobModel[] previousJobModel = new JobModel[1];
    final String[] previousJobModelVersion = new String[1];
    AtomicBoolean hasSecondProcessorJoined = new AtomicBoolean(false);
    final CountDownLatch secondProcessorRegistered = new CountDownLatch(1);
    zkUtils.subscribeToProcessorChange((parentPath, currentChilds) -> {
        // When appRunner2 with id: PROCESSOR_IDS[1] is registered, run processing message in appRunner1.
        if (currentChilds.contains(PROCESSOR_IDS[1])) {
            secondProcessorRegistered.countDown();
        }
    });
    // Set up stream app appRunner2.
    CountDownLatch processedMessagesLatch = new CountDownLatch(NUM_KAFKA_EVENTS);
    Config localTestConfig2 = new MapConfig(applicationConfig2, testConfig);
    ApplicationRunner appRunner2 = ApplicationRunners.getApplicationRunner(TestStreamApplication.getInstance(TEST_SYSTEM, ImmutableList.of(inputSinglePartitionKafkaTopic), outputSinglePartitionKafkaTopic, processedMessagesLatch, null, null, localTestConfig2), localTestConfig2);
    // Callback handler for appRunner1.
    TestStreamApplication.StreamApplicationCallback callback = m -> {
        if (hasSecondProcessorJoined.compareAndSet(false, true)) {
            previousJobModelVersion[0] = zkUtils.getJobModelVersion();
            previousJobModel[0] = JobModelUtil.readJobModel(previousJobModelVersion[0], zkMetadataStore);
            executeRun(appRunner2, localTestConfig2);
            try {
                // Wait for appRunner2 to register with zookeeper.
                secondProcessorRegistered.await();
            } catch (InterruptedException e) {
            }
        }
    };
    CountDownLatch kafkaEventsConsumedLatch = new CountDownLatch(NUM_KAFKA_EVENTS * 2);
    // Set up stream app appRunner1.
    Config localTestConfig1 = new MapConfig(applicationConfig1, testConfig);
    ApplicationRunner appRunner1 = ApplicationRunners.getApplicationRunner(TestStreamApplication.getInstance(TEST_SYSTEM, ImmutableList.of(inputSinglePartitionKafkaTopic), outputSinglePartitionKafkaTopic, null, callback, kafkaEventsConsumedLatch, localTestConfig1), localTestConfig1);
    executeRun(appRunner1, localTestConfig1);
    kafkaEventsConsumedLatch.await();
    String currentJobModelVersion = zkUtils.getJobModelVersion();
    JobModel updatedJobModel = JobModelUtil.readJobModel(currentJobModelVersion, zkMetadataStore);
    // Job model before and after the addition of second stream processor should be the same.
    assertEquals(previousJobModel[0], updatedJobModel);
    assertEquals(new MapConfig(), updatedJobModel.getConfig());
    assertEquals(NUM_KAFKA_EVENTS, processedMessagesLatch.getCount());
    appRunner2.kill();
    appRunner2.waitForFinish();
    assertEquals(appRunner2.status(), ApplicationStatus.UnsuccessfulFinish);
    appRunner1.kill();
    appRunner1.waitForFinish();
    assertEquals(appRunner1.status(), ApplicationStatus.SuccessfulFinish);
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) TaskModel(org.apache.samza.job.model.TaskModel) StoreProperties(org.apache.samza.storage.StoreProperties) Map(java.util.Map) StorageEngine(org.apache.samza.storage.StorageEngine) StandaloneTestUtils(org.apache.samza.test.StandaloneTestUtils) MapConfig(org.apache.samza.config.MapConfig) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) StorageConfig(org.apache.samza.config.StorageConfig) ImmutableSet(com.google.common.collect.ImmutableSet) TaskName(org.apache.samza.container.TaskName) ZkStringSerializer(org.apache.samza.zk.ZkStringSerializer) ImmutableMap(com.google.common.collect.ImmutableMap) TaskApplication(org.apache.samza.application.TaskApplication) ZkClient(org.I0Itec.zkclient.ZkClient) Set(java.util.Set) UUID(java.util.UUID) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MetadataStoreFactory(org.apache.samza.metadatastore.MetadataStoreFactory) Assert.assertFalse(org.junit.Assert.assertFalse) IntegrationTestHarness(org.apache.samza.test.harness.IntegrationTestHarness) Optional(java.util.Optional) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) ApplicationStatus(org.apache.samza.job.ApplicationStatus) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JobModelUtil(org.apache.samza.job.model.JobModelUtil) ZkKeyBuilder(org.apache.samza.zk.ZkKeyBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) ClusterManagerConfig(org.apache.samza.config.ClusterManagerConfig) Serde(org.apache.samza.serializers.Serde) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MetadataStore(org.apache.samza.metadatastore.MetadataStore) ImmutableList(com.google.common.collect.ImmutableList) MessageCollector(org.apache.samza.task.MessageCollector) ZkUtils(org.apache.samza.zk.ZkUtils) Timeout(org.junit.rules.Timeout) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ApplicationRunners(org.apache.samza.runtime.ApplicationRunners) ExpectedException(org.junit.rules.ExpectedException) JobModel(org.apache.samza.job.model.JobModel) LocalApplicationRunner(org.apache.samza.runtime.LocalApplicationRunner) ZkJobCoordinatorFactory(org.apache.samza.zk.ZkJobCoordinatorFactory) Logger(org.slf4j.Logger) TaskConfig(org.apache.samza.config.TaskConfig) JobContext(org.apache.samza.context.JobContext) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) NewTopic(org.apache.kafka.clients.admin.NewTopic) Partition(org.apache.samza.Partition) ContainerContext(org.apache.samza.context.ContainerContext) Assert.assertTrue(org.junit.Assert.assertTrue) CoordinatorStreamValueSerde(org.apache.samza.coordinator.stream.CoordinatorStreamValueSerde) Test(org.junit.Test) ZkConfig(org.apache.samza.config.ZkConfig) Maps(com.google.common.collect.Maps) File(java.io.File) SamzaException(org.apache.samza.SamzaException) TestKafkaEvent(org.apache.samza.test.util.TestKafkaEvent) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TaskMode(org.apache.samza.job.model.TaskMode) MockStorageEngine(org.apache.samza.storage.MockStorageEngine) Rule(org.junit.Rule) ReflectionUtil(org.apache.samza.util.ReflectionUtil) ContainerModel(org.apache.samza.job.model.ContainerModel) StorageEngineFactory(org.apache.samza.storage.StorageEngineFactory) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) 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) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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 4 with ZkMetadataStore

use of org.apache.samza.zk.ZkMetadataStore in project samza by apache.

the class TestZkLocalApplicationRunner method shouldUpdateJobModelWhenNewProcessorJoiningGroupUsingAllSspToSingleTaskGrouperFactory.

/**
 * sspGrouper is set to AllSspToSingleTaskGrouperFactory (All ssps from input kafka topic are mapped to a single task per container).
 * AllSspToSingleTaskGrouperFactory should be used only with high-level consumers which do the partition management
 * by themselves. Using the factory with the consumers that do not do the partition management will result in
 * each processor/task consuming all the messages from all the partitions.
 * Run a stream application(streamApp1) consuming messages from input topic(effectively one container).
 *
 * In the callback triggered by streamApp1 after processing a message, bring up an another stream application(streamApp2).
 *
 * Assertions:
 *           A) JobModel generated before and after the addition of streamApp2 should not be equal.
 *           B) Second stream application(streamApp2) should join the group and process all the messages.
 */
@Test
public void shouldUpdateJobModelWhenNewProcessorJoiningGroupUsingAllSspToSingleTaskGrouperFactory() throws InterruptedException {
    // Set up kafka topics.
    publishKafkaEvents(inputKafkaTopic, 0, NUM_KAFKA_EVENTS * 2, PROCESSOR_IDS[0]);
    // Configuration, verification variables
    MapConfig testConfig = new MapConfig(ImmutableMap.of(JobConfig.SSP_GROUPER_FACTORY, "org.apache.samza.container.grouper.stream.AllSspToSingleTaskGrouperFactory", JobConfig.JOB_DEBOUNCE_TIME_MS, "10", ClusterManagerConfig.HOST_AFFINITY_ENABLED, "false"));
    // Declared as final array to update it from streamApplication callback(Variable should be declared final to access in lambda block).
    final JobModel[] previousJobModel = new JobModel[1];
    final String[] previousJobModelVersion = new String[1];
    AtomicBoolean hasSecondProcessorJoined = new AtomicBoolean(false);
    final CountDownLatch secondProcessorRegistered = new CountDownLatch(1);
    zkUtils.subscribeToProcessorChange((parentPath, currentChilds) -> {
        // When appRunner2 with id: PROCESSOR_IDS[1] is registered, start processing message in appRunner1.
        if (currentChilds.contains(PROCESSOR_IDS[1])) {
            secondProcessorRegistered.countDown();
        }
    });
    // Set up appRunner2.
    CountDownLatch processedMessagesLatch = new CountDownLatch(NUM_KAFKA_EVENTS * 2);
    Config testAppConfig2 = new MapConfig(applicationConfig2, testConfig);
    ApplicationRunner appRunner2 = ApplicationRunners.getApplicationRunner(TestStreamApplication.getInstance(TEST_SYSTEM, ImmutableList.of(inputKafkaTopic), outputKafkaTopic, processedMessagesLatch, null, null, testAppConfig2), testAppConfig2);
    // Callback handler for appRunner1.
    TestStreamApplication.StreamApplicationCallback streamApplicationCallback = message -> {
        if (hasSecondProcessorJoined.compareAndSet(false, true)) {
            previousJobModelVersion[0] = zkUtils.getJobModelVersion();
            previousJobModel[0] = JobModelUtil.readJobModel(previousJobModelVersion[0], zkMetadataStore);
            executeRun(appRunner2, testAppConfig2);
            try {
                // Wait for appRunner2 to register with zookeeper.
                secondProcessorRegistered.await();
            } catch (InterruptedException e) {
            }
        }
    };
    // This is the latch for the messages received by appRunner1. Since appRunner1 is run first, it gets one event
    // redelivered due to re-balancing done by Zk after the appRunner2 joins (See the callback above).
    CountDownLatch kafkaEventsConsumedLatch = new CountDownLatch(NUM_KAFKA_EVENTS * 2 + 1);
    // Set up stream app appRunner1.
    Config testAppConfig1 = new MapConfig(applicationConfig1, testConfig);
    ApplicationRunner appRunner1 = ApplicationRunners.getApplicationRunner(TestStreamApplication.getInstance(TEST_SYSTEM, ImmutableList.of(inputKafkaTopic), outputKafkaTopic, null, streamApplicationCallback, kafkaEventsConsumedLatch, testAppConfig1), testAppConfig1);
    executeRun(appRunner1, testAppConfig1);
    kafkaEventsConsumedLatch.await();
    String currentJobModelVersion = zkUtils.getJobModelVersion();
    JobModel updatedJobModel = JobModelUtil.readJobModel(currentJobModelVersion, zkMetadataStore);
    // JobModelVersion check to verify that leader publishes new jobModel.
    assertTrue(Integer.parseInt(previousJobModelVersion[0]) < Integer.parseInt(currentJobModelVersion));
    // Job model before and after the addition of second stream processor should not be the same.
    assertTrue(!previousJobModel[0].equals(updatedJobModel));
    // Task names in the job model should be different but the set of partitions should be the same and each task name
    // should be assigned to a different container.
    assertEquals(new MapConfig(), previousJobModel[0].getConfig());
    assertEquals(previousJobModel[0].getContainers().get(PROCESSOR_IDS[0]).getTasks().size(), 1);
    assertEquals(new MapConfig(), updatedJobModel.getConfig());
    assertEquals(updatedJobModel.getContainers().get(PROCESSOR_IDS[0]).getTasks().size(), 1);
    assertEquals(updatedJobModel.getContainers().get(PROCESSOR_IDS[1]).getTasks().size(), 1);
    Map<TaskName, TaskModel> updatedTaskModelMap1 = updatedJobModel.getContainers().get(PROCESSOR_IDS[0]).getTasks();
    Map<TaskName, TaskModel> updatedTaskModelMap2 = updatedJobModel.getContainers().get(PROCESSOR_IDS[1]).getTasks();
    assertEquals(updatedTaskModelMap1.size(), 1);
    assertEquals(updatedTaskModelMap2.size(), 1);
    TaskModel taskModel1 = updatedTaskModelMap1.values().stream().findFirst().get();
    TaskModel taskModel2 = updatedTaskModelMap2.values().stream().findFirst().get();
    assertEquals(taskModel1.getSystemStreamPartitions(), taskModel2.getSystemStreamPartitions());
    assertFalse(taskModel1.getTaskName().getTaskName().equals(taskModel2.getTaskName().getTaskName()));
    processedMessagesLatch.await();
    assertEquals(ApplicationStatus.Running, appRunner2.status());
    appRunner1.kill();
    appRunner1.waitForFinish();
    appRunner2.kill();
    appRunner2.waitForFinish();
    assertEquals(ApplicationStatus.SuccessfulFinish, appRunner1.status());
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) TaskModel(org.apache.samza.job.model.TaskModel) StoreProperties(org.apache.samza.storage.StoreProperties) Map(java.util.Map) StorageEngine(org.apache.samza.storage.StorageEngine) StandaloneTestUtils(org.apache.samza.test.StandaloneTestUtils) MapConfig(org.apache.samza.config.MapConfig) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) StorageConfig(org.apache.samza.config.StorageConfig) ImmutableSet(com.google.common.collect.ImmutableSet) TaskName(org.apache.samza.container.TaskName) ZkStringSerializer(org.apache.samza.zk.ZkStringSerializer) ImmutableMap(com.google.common.collect.ImmutableMap) TaskApplication(org.apache.samza.application.TaskApplication) ZkClient(org.I0Itec.zkclient.ZkClient) Set(java.util.Set) UUID(java.util.UUID) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MetadataStoreFactory(org.apache.samza.metadatastore.MetadataStoreFactory) Assert.assertFalse(org.junit.Assert.assertFalse) IntegrationTestHarness(org.apache.samza.test.harness.IntegrationTestHarness) Optional(java.util.Optional) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) ApplicationStatus(org.apache.samza.job.ApplicationStatus) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JobModelUtil(org.apache.samza.job.model.JobModelUtil) ZkKeyBuilder(org.apache.samza.zk.ZkKeyBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) ClusterManagerConfig(org.apache.samza.config.ClusterManagerConfig) Serde(org.apache.samza.serializers.Serde) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MetadataStore(org.apache.samza.metadatastore.MetadataStore) ImmutableList(com.google.common.collect.ImmutableList) MessageCollector(org.apache.samza.task.MessageCollector) ZkUtils(org.apache.samza.zk.ZkUtils) Timeout(org.junit.rules.Timeout) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ApplicationRunners(org.apache.samza.runtime.ApplicationRunners) ExpectedException(org.junit.rules.ExpectedException) JobModel(org.apache.samza.job.model.JobModel) LocalApplicationRunner(org.apache.samza.runtime.LocalApplicationRunner) ZkJobCoordinatorFactory(org.apache.samza.zk.ZkJobCoordinatorFactory) Logger(org.slf4j.Logger) TaskConfig(org.apache.samza.config.TaskConfig) JobContext(org.apache.samza.context.JobContext) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) NewTopic(org.apache.kafka.clients.admin.NewTopic) Partition(org.apache.samza.Partition) ContainerContext(org.apache.samza.context.ContainerContext) Assert.assertTrue(org.junit.Assert.assertTrue) CoordinatorStreamValueSerde(org.apache.samza.coordinator.stream.CoordinatorStreamValueSerde) Test(org.junit.Test) ZkConfig(org.apache.samza.config.ZkConfig) Maps(com.google.common.collect.Maps) File(java.io.File) SamzaException(org.apache.samza.SamzaException) TestKafkaEvent(org.apache.samza.test.util.TestKafkaEvent) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TaskMode(org.apache.samza.job.model.TaskMode) MockStorageEngine(org.apache.samza.storage.MockStorageEngine) Rule(org.junit.Rule) ReflectionUtil(org.apache.samza.util.ReflectionUtil) ContainerModel(org.apache.samza.job.model.ContainerModel) StorageEngineFactory(org.apache.samza.storage.StorageEngineFactory) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) 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) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) LocalApplicationRunner(org.apache.samza.runtime.LocalApplicationRunner) TaskName(org.apache.samza.container.TaskName) JobModel(org.apache.samza.job.model.JobModel) MapConfig(org.apache.samza.config.MapConfig) TaskModel(org.apache.samza.job.model.TaskModel) Test(org.junit.Test)

Example 5 with ZkMetadataStore

use of org.apache.samza.zk.ZkMetadataStore in project samza by apache.

the class TestLocalApplicationRunner method prepareTest.

private void prepareTest() throws Exception {
    CoordinationUtils coordinationUtils = mock(CoordinationUtils.class);
    DistributedLock distributedLock = mock(DistributedLock.class);
    when(distributedLock.lock(anyObject())).thenReturn(true);
    when(coordinationUtils.getLock(anyString())).thenReturn(distributedLock);
    ZkMetadataStore zkMetadataStore = mock(ZkMetadataStore.class);
    when(zkMetadataStore.get(any())).thenReturn(null);
    PowerMockito.whenNew(ZkMetadataStore.class).withAnyArguments().thenReturn(zkMetadataStore);
    ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc = ApplicationDescriptorUtil.getAppDescriptor(mockApp, config);
    localPlanner = spy(new LocalJobPlanner(appDesc, coordinationUtils, "FAKE_UID", "FAKE_RUNID"));
    runner = spy(new LocalApplicationRunner(mockApp, config, coordinationUtils));
    doReturn(localPlanner).when(runner).getPlanner();
}
Also used : DistributedLock(org.apache.samza.coordinator.DistributedLock) LocalJobPlanner(org.apache.samza.execution.LocalJobPlanner) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) CoordinationUtils(org.apache.samza.coordinator.CoordinationUtils)

Aggregations

ZkMetadataStore (org.apache.samza.zk.ZkMetadataStore)5 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 Maps (com.google.common.collect.Maps)3 Sets (com.google.common.collect.Sets)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Set (java.util.Set)3 UUID (java.util.UUID)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Collectors (java.util.stream.Collectors)3