/** * Create a new instance of {@link ApplicationDescriptorImpl} based on {@link SamzaApplication} and {@link Config} * * @param app an implementation of {@link SamzaApplication}. The {@code app} has to have a proper fully-qualified class name. * @param config the {@link Config} for the application * @return the {@link ApplicationDescriptorImpl} instance containing the processing logic and the config */ public static ApplicationDescriptorImpl<? extends ApplicationDescriptor> getAppDescriptor(SamzaApplication app, Config config) { if (app instanceof StreamApplication) { return new StreamApplicationDescriptorImpl((StreamApplication) app, config); } if (app instanceof TaskApplication) { return new TaskApplicationDescriptorImpl((TaskApplication) app, config); } throw new IllegalArgumentException(String.format("User application class %s is not supported. Only StreamApplication " + "and TaskApplication are supported.", app.getClass().getName())); }
/** * Creates a {@link TaskFactory} based on {@link ApplicationDescriptorImpl} * * @param appDesc {@link ApplicationDescriptorImpl} for this application * @return {@link TaskFactory} object defined by {@code appDesc} */ public static TaskFactory getTaskFactory(ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc) { if (appDesc instanceof TaskApplicationDescriptorImpl) { return ((TaskApplicationDescriptorImpl) appDesc).getTaskFactory(); } else if (appDesc instanceof StreamApplicationDescriptorImpl) { return (StreamTaskFactory) () -> new StreamOperatorTask( ((StreamApplicationDescriptorImpl) appDesc).getOperatorSpecGraph()); } throw new IllegalArgumentException(String.format("ApplicationDescriptorImpl has to be either TaskApplicationDescriptorImpl or " + "StreamApplicationDescriptorImpl. class %s is not supported", appDesc.getClass().getName())); }
@Override public TaskApplicationDescriptor withInputStream(InputDescriptor inputDescriptor) { // TODO: SAMZA-1841: need to add to the broadcast streams if inputDescriptor is for a broadcast stream addInputDescriptor(inputDescriptor); getOrCreateStreamSerdes(inputDescriptor.getStreamId(), inputDescriptor.getSerde()); return this; }
@Override public TaskApplicationDescriptor withOutputStream(OutputDescriptor outputDescriptor) { addOutputDescriptor(outputDescriptor); getOrCreateStreamSerdes(outputDescriptor.getStreamId(), outputDescriptor.getSerde()); return this; }
@Override public TaskApplicationDescriptor withTable(TableDescriptor tableDescriptor) { addTableDescriptor(tableDescriptor); if (tableDescriptor instanceof LocalTableDescriptor) { LocalTableDescriptor localTableDescriptor = (LocalTableDescriptor) tableDescriptor; getOrCreateTableSerdes(localTableDescriptor.getTableId(), localTableDescriptor.getSerde()); } return this; }
@Test public void testWithTaskFactory() { TaskFactory mockTf = mock(TaskFactory.class); TaskApplication testApp = appDesc -> appDesc.withTaskFactory(mockTf); TaskApplicationDescriptorImpl appDesc = new TaskApplicationDescriptorImpl(testApp, config); assertEquals(appDesc.getTaskFactory(), mockTf); }
@Test public void testNoApplicationTaskContextFactory() { TaskApplication testApp = appDesc -> { }; TaskApplicationDescriptorImpl appSpec = new TaskApplicationDescriptorImpl(testApp, mock(Config.class)); assertEquals(appSpec.getApplicationTaskContextFactory(), Optional.empty()); }
@Test public void testNoApplicationContainerContextFactory() { TaskApplication testApp = appDesc -> { }; TaskApplicationDescriptorImpl appSpec = new TaskApplicationDescriptorImpl(testApp, mock(Config.class)); assertEquals(appSpec.getApplicationContainerContextFactory(), Optional.empty()); }
@Test public void testAddOutputStreams() { TaskApplication testApp = appDesc -> { mockOutputs.forEach(appDesc::withOutputStream); }; TaskApplicationDescriptorImpl appDesc = new TaskApplicationDescriptorImpl(testApp, config); assertEquals(mockOutputs.toArray(), appDesc.getOutputDescriptors().values().toArray()); }
@Test public void testAddInputStreams() { TaskApplication testApp = appDesc -> { mockInputs.forEach(appDesc::withInputStream); }; TaskApplicationDescriptorImpl appDesc = new TaskApplicationDescriptorImpl(testApp, config); assertEquals(mockInputs.toArray(), appDesc.getInputDescriptors().values().toArray()); }
@Test public void testAddTables() { TaskApplication testApp = appDesc -> { mockTables.forEach(appDesc::withTable); }; TaskApplicationDescriptorImpl appDesc = new TaskApplicationDescriptorImpl(testApp, config); assertEquals(mockTables, appDesc.getTableDescriptors()); }
@Test public void testConstructor() { TaskApplication mockApp = mock(TaskApplication.class); TaskApplicationDescriptorImpl appDesc = new TaskApplicationDescriptorImpl(mockApp, config); verify(mockApp).describe(appDesc); assertEquals(config, appDesc.getConfig()); }
@Test public void testProcessorLifecycleListener() { ProcessorLifecycleListenerFactory mockFactory = mock(ProcessorLifecycleListenerFactory.class); TaskApplication testApp = appDesc -> { appDesc.withProcessorLifecycleListenerFactory(mockFactory); }; TaskApplicationDescriptorImpl appDesc = new TaskApplicationDescriptorImpl(testApp, config); assertEquals(appDesc.getProcessorLifecycleListenerFactory(), mockFactory); } }
TaskApplicationDescriptorImpl taskAppDesc = new TaskApplicationDescriptorImpl(getTaskApplication(), mockConfig); taskAppDesc.withTable(mockTableDescriptor); taskAppDesc.withInputStream(inputSystemDescriptor.getInputDescriptor("sideInput1", defaultSerde)); configureJobNode(taskAppDesc);
@Override public TaskApplicationDescriptor withOutputStream(OutputDescriptor outputDescriptor) { addOutputDescriptor(outputDescriptor); getOrCreateStreamSerdes(outputDescriptor.getStreamId(), outputDescriptor.getSerde()); return this; }
@Override public TaskApplicationDescriptor withTable(TableDescriptor tableDescriptor) { addTableDescriptor(tableDescriptor); BaseTableDescriptor baseTableDescriptor = (BaseTableDescriptor) tableDescriptor; getOrCreateTableSerdes(baseTableDescriptor.getTableId(), baseTableDescriptor.getSerde()); return this; }
@Test public void testTaskClassOnly() { Map<String, String> configMap = new HashMap<>(); configMap.put(TaskConfig.TASK_CLASS(), MockStreamTask.class.getName()); Config config = new MapConfig(configMap); SamzaApplication app = ApplicationUtil.fromConfig(config); assertTrue(app instanceof TaskApplication); TaskApplicationDescriptorImpl appSpec = new TaskApplicationDescriptorImpl((TaskApplication) app, config); assertTrue(appSpec.getTaskFactory().createInstance() instanceof MockStreamTask); }
@Test public void testApplicationTaskContextFactory() { ApplicationTaskContextFactory factory = mock(ApplicationTaskContextFactory.class); TaskApplication testApp = appDesc -> appDesc.withApplicationTaskContextFactory(factory); TaskApplicationDescriptorImpl appSpec = new TaskApplicationDescriptorImpl(testApp, mock(Config.class)); assertEquals(appSpec.getApplicationTaskContextFactory(), Optional.of(factory)); }
@Test public void testApplicationContainerContextFactory() { ApplicationContainerContextFactory factory = mock(ApplicationContainerContextFactory.class); TaskApplication testApp = appDesc -> appDesc.withApplicationContainerContextFactory(factory); TaskApplicationDescriptorImpl appSpec = new TaskApplicationDescriptorImpl(testApp, mock(Config.class)); assertEquals(appSpec.getApplicationContainerContextFactory(), Optional.of(factory)); }
@Override public TaskApplicationDescriptor withOutputStream(OutputDescriptor outputDescriptor) { addOutputDescriptor(outputDescriptor); getOrCreateStreamSerdes(outputDescriptor.getStreamId(), outputDescriptor.getSerde()); return this; }