|
35 | 35 | import org.apache.samza.config.Config; |
36 | 36 | import org.apache.samza.config.ConfigException; |
37 | 37 | import org.apache.samza.config.JobConfig; |
| 38 | +import org.apache.samza.config.JobCoordinatorConfig; |
38 | 39 | import org.apache.samza.config.MapConfig; |
39 | 40 | import org.apache.samza.context.ExternalContext; |
40 | 41 | import org.apache.samza.coordinator.ClusterMembership; |
41 | 42 | import org.apache.samza.coordinator.CoordinationConstants; |
42 | 43 | import org.apache.samza.coordinator.CoordinationUtils; |
43 | 44 | import org.apache.samza.coordinator.DistributedLock; |
| 45 | +import org.apache.samza.coordinator.metadatastore.CoordinatorStreamMetadataStoreFactory; |
44 | 46 | import org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore; |
45 | 47 | import org.apache.samza.execution.LocalJobPlanner; |
46 | 48 | import org.apache.samza.job.ApplicationStatus; |
| 49 | +import org.apache.samza.metadatastore.InMemoryMetadataStore; |
| 50 | +import org.apache.samza.metadatastore.InMemoryMetadataStoreFactory; |
| 51 | +import org.apache.samza.metadatastore.MetadataStore; |
47 | 52 | import org.apache.samza.metadatastore.MetadataStoreFactory; |
| 53 | +import org.apache.samza.metrics.MetricsRegistry; |
48 | 54 | import org.apache.samza.processor.StreamProcessor; |
| 55 | +import org.apache.samza.standalone.PassthroughJobCoordinatorFactory; |
| 56 | +import org.apache.samza.system.SystemAdmins; |
49 | 57 | import org.apache.samza.task.IdentityStreamTask; |
50 | 58 | import org.apache.samza.zk.ZkMetadataStore; |
51 | 59 | import org.apache.samza.zk.ZkMetadataStoreFactory; |
@@ -476,24 +484,94 @@ private void prepareTestForRunId() throws Exception { |
476 | 484 | doReturn(coordinatorStreamStore).when(runner).createCoordinatorStreamStore(any(Config.class)); |
477 | 485 | } |
478 | 486 |
|
| 487 | + /** |
| 488 | + * Default metadata store factory should be null if no job coordinator system defined and the default |
| 489 | + * ZkJobCoordinator is used. |
| 490 | + */ |
479 | 491 | @Test |
480 | | - public void testGetMetadataStoreFactoryWithoutJobCoordinatorSystem() { |
| 492 | + public void testGetCoordinatorStreamStoreFactoryWithoutJobCoordinatorSystem() { |
481 | 493 | MetadataStoreFactory metadataStoreFactory = |
482 | | - LocalApplicationRunner.getMetadataStoreFactory(new JobConfig(new MapConfig())); |
| 494 | + LocalApplicationRunner.getDefaultCoordinatorStreamStoreFactory(new JobConfig(new MapConfig())); |
483 | 495 | assertNull(metadataStoreFactory); |
484 | 496 | } |
485 | 497 |
|
| 498 | + /** |
| 499 | + * Default metadata store factory should not be null if job coordinator system defined and the default |
| 500 | + * ZkJobCoordinator is used. |
| 501 | + */ |
486 | 502 | @Test |
487 | | - public void testGetMetadataStoreFactoryWithJobCoordinatorSystem() { |
| 503 | + public void testGetCoordinatorStreamStoreFactoryWithJobCoordinatorSystem() { |
488 | 504 | MetadataStoreFactory metadataStoreFactory = |
489 | | - LocalApplicationRunner.getMetadataStoreFactory(new JobConfig(new MapConfig(ImmutableMap.of(JobConfig.JOB_COORDINATOR_SYSTEM, "test-system")))); |
| 505 | + LocalApplicationRunner.getDefaultCoordinatorStreamStoreFactory(new JobConfig(new MapConfig(ImmutableMap.of(JobConfig.JOB_COORDINATOR_SYSTEM, "test-system")))); |
490 | 506 | assertNotNull(metadataStoreFactory); |
491 | 507 | } |
492 | 508 |
|
| 509 | + /** |
| 510 | + * Default metadata store factory should not be null if default system defined and the default |
| 511 | + * ZkJobCoordinator is used. |
| 512 | + */ |
493 | 513 | @Test |
494 | | - public void testGetMetadataStoreFactoryWithDefaultSystem() { |
| 514 | + public void testGetCoordinatorStreamStoreFactoryWithDefaultSystem() { |
495 | 515 | MetadataStoreFactory metadataStoreFactory = |
496 | | - LocalApplicationRunner.getMetadataStoreFactory(new JobConfig(new MapConfig(ImmutableMap.of(JobConfig.JOB_DEFAULT_SYSTEM, "test-system")))); |
| 516 | + LocalApplicationRunner.getDefaultCoordinatorStreamStoreFactory(new JobConfig(new MapConfig(ImmutableMap.of(JobConfig.JOB_DEFAULT_SYSTEM, "test-system")))); |
497 | 517 | assertNotNull(metadataStoreFactory); |
498 | 518 | } |
| 519 | + |
| 520 | + /** |
| 521 | + * Default metadata store factory be null if job coordinator system or default system defined and a non ZkJobCoordinator |
| 522 | + * job coordinator is used. |
| 523 | + */ |
| 524 | + @Test |
| 525 | + public void testGetCoordinatorStreamStoreFactoryWithNonZkJobCoordinator() { |
| 526 | + MapConfig mapConfig = new MapConfig( |
| 527 | + ImmutableMap.of( |
| 528 | + JobConfig.JOB_DEFAULT_SYSTEM, "test-system", |
| 529 | + JobCoordinatorConfig.JOB_COORDINATOR_FACTORY, PassthroughJobCoordinatorFactory.class.getName())); |
| 530 | + MetadataStoreFactory metadataStoreFactory = |
| 531 | + LocalApplicationRunner.getDefaultCoordinatorStreamStoreFactory(new JobConfig(mapConfig)); |
| 532 | + assertNull(metadataStoreFactory); |
| 533 | + } |
| 534 | + |
| 535 | + /** |
| 536 | + * Underlying coordinator stream should be created if using CoordinatorStreamMetadataStoreFactory |
| 537 | + * @throws Exception |
| 538 | + */ |
| 539 | + @Test |
| 540 | + public void testCreateCoordinatorStreamWithCoordinatorFactory() throws Exception { |
| 541 | + CoordinatorStreamStore coordinatorStreamStore = mock(CoordinatorStreamStore.class); |
| 542 | + CoordinatorStreamMetadataStoreFactory coordinatorStreamMetadataStoreFactory = mock(CoordinatorStreamMetadataStoreFactory.class); |
| 543 | + doReturn(coordinatorStreamStore).when(coordinatorStreamMetadataStoreFactory).getMetadataStore(anyString(), any(Config.class), any( |
| 544 | + MetricsRegistry.class)); |
| 545 | + SystemAdmins systemAdmins = mock(SystemAdmins.class); |
| 546 | + PowerMockito.whenNew(SystemAdmins.class).withAnyArguments().thenReturn(systemAdmins); |
| 547 | + LocalApplicationRunner localApplicationRunner = |
| 548 | + spy(new LocalApplicationRunner(mockApp, config, coordinatorStreamMetadataStoreFactory)); |
| 549 | + |
| 550 | + // create store only if successful in creating the underlying coordinator stream |
| 551 | + doReturn(true).when(localApplicationRunner).createUnderlyingCoordinatorStream(eq(config)); |
| 552 | + assertEquals(coordinatorStreamStore, localApplicationRunner.createCoordinatorStreamStore(config)); |
| 553 | + verify(localApplicationRunner).createUnderlyingCoordinatorStream(eq(config)); |
| 554 | + |
| 555 | + // do not create store if creating the underlying coordinator stream fails |
| 556 | + doReturn(false).when(localApplicationRunner).createUnderlyingCoordinatorStream(eq(config)); |
| 557 | + assertNull(localApplicationRunner.createCoordinatorStreamStore(config)); |
| 558 | + } |
| 559 | + |
| 560 | + /** |
| 561 | + * Underlying coordinator stream should not be created if not using CoordinatorStreamMetadataStoreFactory |
| 562 | + * @throws Exception |
| 563 | + */ |
| 564 | + @Test |
| 565 | + public void testCreateCoordinatorStreamWithoutCoordinatorFactory() throws Exception { |
| 566 | + SystemAdmins systemAdmins = mock(SystemAdmins.class); |
| 567 | + PowerMockito.whenNew(SystemAdmins.class).withAnyArguments().thenReturn(systemAdmins); |
| 568 | + LocalApplicationRunner localApplicationRunner = |
| 569 | + spy(new LocalApplicationRunner(mockApp, config, new InMemoryMetadataStoreFactory())); |
| 570 | + doReturn(false).when(localApplicationRunner).createUnderlyingCoordinatorStream(eq(config)); |
| 571 | + MetadataStore coordinatorStreamStore = localApplicationRunner.createCoordinatorStreamStore(config); |
| 572 | + assertTrue(coordinatorStreamStore instanceof InMemoryMetadataStore); |
| 573 | + |
| 574 | + // creating underlying coordinator stream should not be called for other coordinator stream metadata store types. |
| 575 | + verify(localApplicationRunner, never()).createUnderlyingCoordinatorStream(eq(config)); |
| 576 | + } |
499 | 577 | } |
0 commit comments