Search in sources :

Example 1 with CoordinationUtils

use of org.apache.samza.coordinator.CoordinationUtils in project samza by apache.

the class TestLocalApplicationRunner method prepareTestForRunId.

private void prepareTestForRunId() throws Exception {
    coordinationUtils = mock(CoordinationUtils.class);
    DistributedLock lock = mock(DistributedLock.class);
    when(lock.lock(anyObject())).thenReturn(true);
    when(coordinationUtils.getLock(anyString())).thenReturn(lock);
    clusterMembership = mock(ClusterMembership.class);
    when(clusterMembership.getNumberOfProcessors()).thenReturn(1);
    when(coordinationUtils.getClusterMembership()).thenReturn(clusterMembership);
    metadataStore = mock(ZkMetadataStore.class);
    when(metadataStore.get(any())).thenReturn(null);
    PowerMockito.whenNew(ZkMetadataStore.class).withAnyArguments().thenReturn(metadataStore);
    ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc = ApplicationDescriptorUtil.getAppDescriptor(mockApp, config);
    runner = spy(new LocalApplicationRunner(mockApp, config, coordinationUtils));
    localPlanner = spy(new LocalJobPlanner(appDesc, coordinationUtils, "FAKE_UID", "FAKE_RUNID"));
    doReturn(localPlanner).when(runner).getPlanner();
    StreamProcessor sp = mock(StreamProcessor.class);
    CoordinatorStreamStore coordinatorStreamStore = mock(CoordinatorStreamStore.class);
    doReturn(sp).when(runner).createStreamProcessor(anyObject(), anyObject(), anyObject(), anyObject(), any(CoordinatorStreamStore.class));
    doReturn(coordinatorStreamStore).when(runner).createCoordinatorStreamStore(any(Config.class));
}
Also used : DistributedLock(org.apache.samza.coordinator.DistributedLock) StreamProcessor(org.apache.samza.processor.StreamProcessor) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) ClusterMembership(org.apache.samza.coordinator.ClusterMembership) MapConfig(org.apache.samza.config.MapConfig) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) JobConfig(org.apache.samza.config.JobConfig) ApplicationConfig(org.apache.samza.config.ApplicationConfig) LocalJobPlanner(org.apache.samza.execution.LocalJobPlanner) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) CoordinationUtils(org.apache.samza.coordinator.CoordinationUtils)

Example 2 with CoordinationUtils

use of org.apache.samza.coordinator.CoordinationUtils in project samza by apache.

the class LocalApplicationRunner method getCoordinationUtils.

private static Optional<CoordinationUtils> getCoordinationUtils(Config config) {
    if (!isAppModeBatch(config)) {
        return Optional.empty();
    }
    JobCoordinatorConfig jcConfig = new JobCoordinatorConfig(config);
    CoordinationUtils coordinationUtils = jcConfig.getCoordinationUtilsFactory().getCoordinationUtils(CoordinationConstants.APPLICATION_RUNNER_PATH_SUFFIX, PROCESSOR_ID, config);
    return Optional.ofNullable(coordinationUtils);
}
Also used : JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) CoordinationUtils(org.apache.samza.coordinator.CoordinationUtils)

Example 3 with CoordinationUtils

use of org.apache.samza.coordinator.CoordinationUtils in project samza by apache.

the class LocalApplicationRunner method getPlanner.

/**
 * @return LocalJobPlanner created
 */
@VisibleForTesting
LocalJobPlanner getPlanner() {
    boolean isAppModeBatch = new ApplicationConfig(appDesc.getConfig()).getAppMode() == ApplicationConfig.ApplicationMode.BATCH;
    if (!isAppModeBatch) {
        return new LocalJobPlanner(appDesc, PROCESSOR_ID);
    }
    CoordinationUtils coordinationUtils = this.coordinationUtils.orElse(null);
    String runId = this.runId.orElse(null);
    return new LocalJobPlanner(appDesc, coordinationUtils, PROCESSOR_ID, runId);
}
Also used : ApplicationConfig(org.apache.samza.config.ApplicationConfig) LocalJobPlanner(org.apache.samza.execution.LocalJobPlanner) CoordinationUtils(org.apache.samza.coordinator.CoordinationUtils) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with CoordinationUtils

use of org.apache.samza.coordinator.CoordinationUtils 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 5 with CoordinationUtils

use of org.apache.samza.coordinator.CoordinationUtils in project samza by apache.

the class TestLocalApplicationRunner method testStreamCreationWithCoordination.

@Test
public void testStreamCreationWithCoordination() throws Exception {
    Map<String, String> config = new HashMap<>();
    LocalApplicationRunner runner = new LocalApplicationRunner(new MapConfig(config));
    StreamApplication app = mock(StreamApplication.class);
    doNothing().when(app).init(anyObject(), anyObject());
    ExecutionPlanner planner = mock(ExecutionPlanner.class);
    Field plannerField = runner.getClass().getSuperclass().getDeclaredField("planner");
    plannerField.setAccessible(true);
    plannerField.set(runner, planner);
    StreamManager streamManager = mock(StreamManager.class);
    Field streamManagerField = runner.getClass().getSuperclass().getDeclaredField("streamManager");
    streamManagerField.setAccessible(true);
    streamManagerField.set(runner, streamManager);
    ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
    ExecutionPlan plan = new ExecutionPlan() {

        @Override
        public List<JobConfig> getJobConfigs() {
            return Collections.emptyList();
        }

        @Override
        public List<StreamSpec> getIntermediateStreams() {
            return Collections.singletonList(new StreamSpec("test-stream", "test-stream", "test-system"));
        }

        @Override
        public String getPlanAsJson() throws Exception {
            return "";
        }
    };
    when(planner.plan(anyObject())).thenReturn(plan);
    LocalApplicationRunner spy = spy(runner);
    CoordinationUtils coordinationUtils = mock(CoordinationUtils.class);
    LeaderElector leaderElector = new LeaderElector() {

        private LeaderElectorListener leaderElectorListener;

        @Override
        public void setLeaderElectorListener(LeaderElectorListener listener) {
            this.leaderElectorListener = listener;
        }

        @Override
        public void tryBecomeLeader() {
            leaderElectorListener.onBecomingLeader();
        }

        @Override
        public void resignLeadership() {
        }

        @Override
        public boolean amILeader() {
            return false;
        }
    };
    Latch latch = new Latch() {

        boolean done = false;

        @Override
        public void await(long timeout, TimeUnit tu) throws TimeoutException {
            // in this test, latch is released before wait
            assertTrue(done);
        }

        @Override
        public void countDown() {
            done = true;
        }
    };
    when(coordinationUtils.getLeaderElector()).thenReturn(leaderElector);
    when(coordinationUtils.getLatch(anyInt(), anyString())).thenReturn(latch);
    doReturn(coordinationUtils).when(spy).createCoordinationUtils();
    try {
        spy.run(app);
    } catch (Throwable t) {
        //no jobs exception
        assertNotNull(t);
    }
    verify(streamManager).createStreams(captor.capture());
    List<StreamSpec> streamSpecs = captor.getValue();
    assertEquals(streamSpecs.size(), 1);
    assertEquals(streamSpecs.get(0).getId(), "test-stream");
}
Also used : StreamSpec(org.apache.samza.system.StreamSpec) HashMap(java.util.HashMap) StreamApplication(org.apache.samza.application.StreamApplication) ExecutionPlanner(org.apache.samza.execution.ExecutionPlanner) Matchers.anyString(org.mockito.Matchers.anyString) JobConfig(org.apache.samza.config.JobConfig) Field(java.lang.reflect.Field) ExecutionPlan(org.apache.samza.execution.ExecutionPlan) StreamManager(org.apache.samza.execution.StreamManager) LeaderElectorListener(org.apache.samza.coordinator.LeaderElectorListener) LeaderElector(org.apache.samza.coordinator.LeaderElector) Latch(org.apache.samza.coordinator.Latch) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) MapConfig(org.apache.samza.config.MapConfig) CoordinationUtils(org.apache.samza.coordinator.CoordinationUtils) Test(org.junit.Test)

Aggregations

CoordinationUtils (org.apache.samza.coordinator.CoordinationUtils)7 DistributedLock (org.apache.samza.coordinator.DistributedLock)4 JobConfig (org.apache.samza.config.JobConfig)3 JobCoordinatorConfig (org.apache.samza.config.JobCoordinatorConfig)3 LocalJobPlanner (org.apache.samza.execution.LocalJobPlanner)3 ZkMetadataStore (org.apache.samza.zk.ZkMetadataStore)3 List (java.util.List)2 ApplicationConfig (org.apache.samza.config.ApplicationConfig)2 Config (org.apache.samza.config.Config)2 MapConfig (org.apache.samza.config.MapConfig)2 StreamSpec (org.apache.samza.system.StreamSpec)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 StreamApplication (org.apache.samza.application.StreamApplication)1 StreamApplicationDescriptorImpl (org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl)1 ClusterMembership (org.apache.samza.coordinator.ClusterMembership)1