Search in sources :

Example 1 with ApplicationRunner

use of org.apache.samza.runtime.ApplicationRunner in project samza by apache.

the class TestMessageStreamImpl method testPartitionBy.

@Test
public void testPartitionBy() {
    Map<String, String> map = new HashMap<>();
    map.put(JobConfig.JOB_DEFAULT_SYSTEM(), "testsystem");
    Config config = new MapConfig(map);
    ApplicationRunner runner = ApplicationRunner.fromConfig(config);
    StreamGraphImpl streamGraph = new StreamGraphImpl(runner, config);
    MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(streamGraph);
    Function<TestMessageEnvelope, String> keyExtractorFunc = m -> "222";
    inputStream.partitionBy(keyExtractorFunc);
    assertTrue(streamGraph.getInputStreams().size() == 1);
    assertTrue(streamGraph.getOutputStreams().size() == 1);
    Collection<OperatorSpec> subs = inputStream.getRegisteredOperatorSpecs();
    assertEquals(subs.size(), 1);
    OperatorSpec<TestMessageEnvelope> partitionByOp = subs.iterator().next();
    assertTrue(partitionByOp instanceof SinkOperatorSpec);
    assertNull(partitionByOp.getNextStream());
    ((SinkOperatorSpec) partitionByOp).getSinkFn().apply(new TestMessageEnvelope("111", "test", 1000), envelope -> assertTrue(envelope.getPartitionKey().equals("222")), null);
}
Also used : StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) HashMap(java.util.HashMap) Function(java.util.function.Function) MapFunction(org.apache.samza.operators.functions.MapFunction) ArrayList(java.util.ArrayList) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) PartialJoinOperatorSpec(org.apache.samza.operators.spec.PartialJoinOperatorSpec) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) MapConfig(org.apache.samza.config.MapConfig) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) FilterFunction(org.apache.samza.operators.functions.FilterFunction) TaskContext(org.apache.samza.task.TaskContext) MessageType(org.apache.samza.operators.data.MessageType) TestExtOutputMessageEnvelope(org.apache.samza.operators.data.TestExtOutputMessageEnvelope) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) SinkFunction(org.apache.samza.operators.functions.SinkFunction) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) JobConfig(org.apache.samza.config.JobConfig) MapConfig(org.apache.samza.config.MapConfig) Config(org.apache.samza.config.Config) StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) PartialJoinOperatorSpec(org.apache.samza.operators.spec.PartialJoinOperatorSpec) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MapConfig(org.apache.samza.config.MapConfig) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) Test(org.junit.Test)

Example 2 with ApplicationRunner

use of org.apache.samza.runtime.ApplicationRunner in project samza by apache.

the class TestJobGraphJsonGenerator method test.

@Test
public void test() throws Exception {
    /**
     * the graph looks like the following. number of partitions in parentheses. quotes indicate expected value.
     *
     *                               input1 (64) -> map -> join -> output1 (8)
     *                                                       |
     *          input2 (16) -> partitionBy ("64") -> filter -|
     *                                                       |
     * input3 (32) -> filter -> partitionBy ("64") -> map -> join -> output2 (16)
     *
     */
    Map<String, String> configMap = new HashMap<>();
    configMap.put(JobConfig.JOB_NAME(), "test-app");
    configMap.put(JobConfig.JOB_DEFAULT_SYSTEM(), "test-system");
    Config config = new MapConfig(configMap);
    StreamSpec input1 = new StreamSpec("input1", "input1", "system1");
    StreamSpec input2 = new StreamSpec("input2", "input2", "system2");
    StreamSpec input3 = new StreamSpec("input3", "input3", "system2");
    StreamSpec output1 = new StreamSpec("output1", "output1", "system1");
    StreamSpec output2 = new StreamSpec("output2", "output2", "system2");
    ApplicationRunner runner = mock(ApplicationRunner.class);
    when(runner.getStreamSpec("input1")).thenReturn(input1);
    when(runner.getStreamSpec("input2")).thenReturn(input2);
    when(runner.getStreamSpec("input3")).thenReturn(input3);
    when(runner.getStreamSpec("output1")).thenReturn(output1);
    when(runner.getStreamSpec("output2")).thenReturn(output2);
    // intermediate streams used in tests
    when(runner.getStreamSpec("test-app-1-partition_by-0")).thenReturn(new StreamSpec("test-app-1-partition_by-0", "test-app-1-partition_by-0", "default-system"));
    when(runner.getStreamSpec("test-app-1-partition_by-1")).thenReturn(new StreamSpec("test-app-1-partition_by-1", "test-app-1-partition_by-1", "default-system"));
    when(runner.getStreamSpec("test-app-1-partition_by-4")).thenReturn(new StreamSpec("test-app-1-partition_by-4", "test-app-1-partition_by-4", "default-system"));
    // set up external partition count
    Map<String, Integer> system1Map = new HashMap<>();
    system1Map.put("input1", 64);
    system1Map.put("output1", 8);
    Map<String, Integer> system2Map = new HashMap<>();
    system2Map.put("input2", 16);
    system2Map.put("input3", 32);
    system2Map.put("output2", 16);
    Map<String, SystemAdmin> systemAdmins = new HashMap<>();
    SystemAdmin systemAdmin1 = createSystemAdmin(system1Map);
    SystemAdmin systemAdmin2 = createSystemAdmin(system2Map);
    systemAdmins.put("system1", systemAdmin1);
    systemAdmins.put("system2", systemAdmin2);
    StreamManager streamManager = new StreamManager(systemAdmins);
    StreamGraphImpl streamGraph = new StreamGraphImpl(runner, config);
    BiFunction mockBuilder = mock(BiFunction.class);
    MessageStream m1 = streamGraph.getInputStream("input1", mockBuilder).map(m -> m);
    MessageStream m2 = streamGraph.getInputStream("input2", mockBuilder).partitionBy(m -> "haha").filter(m -> true);
    MessageStream m3 = streamGraph.getInputStream("input3", mockBuilder).filter(m -> true).partitionBy(m -> "hehe").map(m -> m);
    Function mockFn = mock(Function.class);
    OutputStream<Object, Object, Object> outputStream1 = streamGraph.getOutputStream("output1", mockFn, mockFn);
    OutputStream<Object, Object, Object> outputStream2 = streamGraph.getOutputStream("output2", mockFn, mockFn);
    m1.join(m2, mock(JoinFunction.class), Duration.ofHours(2)).sendTo(outputStream1);
    m2.sink((message, collector, coordinator) -> {
    });
    m3.join(m2, mock(JoinFunction.class), Duration.ofHours(1)).sendTo(outputStream2);
    ExecutionPlanner planner = new ExecutionPlanner(config, streamManager);
    ExecutionPlan plan = planner.plan(streamGraph);
    String json = plan.getPlanAsJson();
    System.out.println(json);
    // deserialize
    ObjectMapper mapper = new ObjectMapper();
    JobGraphJsonGenerator.JobGraphJson nodes = mapper.readValue(json, JobGraphJsonGenerator.JobGraphJson.class);
    assertTrue(nodes.jobs.get(0).operatorGraph.inputStreams.size() == 5);
    assertTrue(nodes.jobs.get(0).operatorGraph.operators.size() == 13);
    assertTrue(nodes.sourceStreams.size() == 3);
    assertTrue(nodes.sinkStreams.size() == 2);
    assertTrue(nodes.intermediateStreams.size() == 2);
}
Also used : ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) Function(java.util.function.Function) TestExecutionPlanner.createSystemAdmin(org.apache.samza.execution.TestExecutionPlanner.createSystemAdmin) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) Duration(java.time.Duration) Map(java.util.Map) SystemAdmin(org.apache.samza.system.SystemAdmin) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) OutputStream(org.apache.samza.operators.OutputStream) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) MessageStream(org.apache.samza.operators.MessageStream) Mockito.mock(org.mockito.Mockito.mock) StreamSpec(org.apache.samza.system.StreamSpec) HashMap(java.util.HashMap) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) BiFunction(java.util.function.BiFunction) JoinFunction(org.apache.samza.operators.functions.JoinFunction) Function(java.util.function.Function) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) BiFunction(java.util.function.BiFunction) MessageStream(org.apache.samza.operators.MessageStream) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) MapConfig(org.apache.samza.config.MapConfig) TestExecutionPlanner.createSystemAdmin(org.apache.samza.execution.TestExecutionPlanner.createSystemAdmin) SystemAdmin(org.apache.samza.system.SystemAdmin) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 3 with ApplicationRunner

use of org.apache.samza.runtime.ApplicationRunner in project samza by apache.

the class TestJoinOperator method createStreamOperatorTask.

private StreamOperatorTask createStreamOperatorTask(Clock clock, StreamApplication app) throws Exception {
    ApplicationRunner runner = mock(ApplicationRunner.class);
    when(runner.getStreamSpec("instream")).thenReturn(new StreamSpec("instream", "instream", "insystem"));
    when(runner.getStreamSpec("instream2")).thenReturn(new StreamSpec("instream2", "instream2", "insystem2"));
    TaskContext taskContext = mock(TaskContext.class);
    when(taskContext.getSystemStreamPartitions()).thenReturn(ImmutableSet.of(new SystemStreamPartition("insystem", "instream", new Partition(0)), new SystemStreamPartition("insystem2", "instream2", new Partition(0))));
    when(taskContext.getMetricsRegistry()).thenReturn(new MetricsRegistryMap());
    Config config = mock(Config.class);
    StreamOperatorTask sot = new StreamOperatorTask(app, runner, clock);
    sot.init(config, taskContext);
    return sot;
}
Also used : StreamSpec(org.apache.samza.system.StreamSpec) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Partition(org.apache.samza.Partition) TaskContext(org.apache.samza.task.TaskContext) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) Config(org.apache.samza.config.Config) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition)

Example 4 with ApplicationRunner

use of org.apache.samza.runtime.ApplicationRunner in project samza by apache.

the class TestStreamGraphImpl method testGetOutputStream.

@Test
public void testGetOutputStream() {
    ApplicationRunner mockRunner = mock(ApplicationRunner.class);
    Config mockConfig = mock(Config.class);
    StreamSpec testStreamSpec = new StreamSpec("test-stream-1", "physical-stream-1", "test-system");
    when(mockRunner.getStreamSpec("test-stream-1")).thenReturn(testStreamSpec);
    class MyMessageType extends MessageType {

        public final String outputId;

        public MyMessageType(String value, long eventTime, String outputId) {
            super(value, eventTime);
            this.outputId = outputId;
        }
    }
    StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
    Function<TestMessageEnvelope, String> xKeyExtractor = x -> x.getKey();
    Function<TestMessageEnvelope, MyMessageType> xMsgExtractor = x -> new MyMessageType(x.getMessage().getValue(), x.getMessage().getEventTime(), "test-output-id-1");
    OutputStream<String, MyMessageType, TestInputMessageEnvelope> mOutputStream = graph.getOutputStream("test-stream-1", xKeyExtractor, xMsgExtractor);
    assertEquals(graph.getOutputStreams().get(testStreamSpec), mOutputStream);
    assertTrue(mOutputStream instanceof OutputStreamInternalImpl);
    assertEquals(((OutputStreamInternalImpl) mOutputStream).getKeyExtractor(), xKeyExtractor);
    assertEquals(((OutputStreamInternalImpl) mOutputStream).getMsgExtractor(), xMsgExtractor);
    TestInputMessageEnvelope xInputMsg = new TestInputMessageEnvelope("test-key-1", "test-msg-1", 33333L, "input-id-1");
    assertEquals(((OutputStreamInternalImpl<String, MyMessageType, TestInputMessageEnvelope>) mOutputStream).getKeyExtractor().apply(xInputMsg), "test-key-1");
    assertEquals(((OutputStreamInternalImpl<String, MyMessageType, TestInputMessageEnvelope>) mOutputStream).getMsgExtractor().apply(xInputMsg).getValue(), "test-msg-1");
    assertEquals(((OutputStreamInternalImpl<String, MyMessageType, TestInputMessageEnvelope>) mOutputStream).getMsgExtractor().apply(xInputMsg).getEventTime(), 33333L);
    assertEquals(((OutputStreamInternalImpl<String, MyMessageType, TestInputMessageEnvelope>) mOutputStream).getMsgExtractor().apply(xInputMsg).outputId, "test-output-id-1");
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) Config(org.apache.samza.config.Config) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StreamSpec(org.apache.samza.system.StreamSpec) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Example 5 with ApplicationRunner

use of org.apache.samza.runtime.ApplicationRunner in project samza by apache.

the class TestStreamGraphImpl method testGetIntermediateStream.

@Test
public void testGetIntermediateStream() {
    ApplicationRunner mockRunner = mock(ApplicationRunner.class);
    Config mockConfig = mock(Config.class);
    StreamSpec testStreamSpec = new StreamSpec("myJob-i001-test-stream-1", "physical-stream-1", "test-system");
    when(mockRunner.getStreamSpec("myJob-i001-test-stream-1")).thenReturn(testStreamSpec);
    when(mockConfig.get(JobConfig.JOB_NAME())).thenReturn("myJob");
    when(mockConfig.get(JobConfig.JOB_ID(), "1")).thenReturn("i001");
    class MyMessageType extends MessageType {

        public final String outputId;

        public MyMessageType(String value, long eventTime, String outputId) {
            super(value, eventTime);
            this.outputId = outputId;
        }
    }
    StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
    Function<TestMessageEnvelope, String> xKeyExtractor = x -> x.getKey();
    Function<TestMessageEnvelope, MyMessageType> xMsgExtractor = x -> new MyMessageType(x.getMessage().getValue(), x.getMessage().getEventTime(), "test-output-id-1");
    BiFunction<String, MessageType, TestInputMessageEnvelope> xMsgBuilder = (k, v) -> new TestInputMessageEnvelope(k, v.getValue(), v.getEventTime(), "input-id-1");
    MessageStream<TestMessageEnvelope> mIntermediateStream = graph.getIntermediateStream("test-stream-1", xKeyExtractor, xMsgExtractor, xMsgBuilder);
    assertEquals(graph.getOutputStreams().get(testStreamSpec), mIntermediateStream);
    assertTrue(mIntermediateStream instanceof IntermediateStreamInternalImpl);
    assertEquals(((IntermediateStreamInternalImpl) mIntermediateStream).getKeyExtractor(), xKeyExtractor);
    assertEquals(((IntermediateStreamInternalImpl) mIntermediateStream).getMsgExtractor(), xMsgExtractor);
    assertEquals(((IntermediateStreamInternalImpl) mIntermediateStream).getMsgBuilder(), xMsgBuilder);
    TestMessageEnvelope xInputMsg = new TestMessageEnvelope("test-key-1", "test-msg-1", 33333L);
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getKeyExtractor().apply(xInputMsg), "test-key-1");
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgExtractor().apply(xInputMsg).getValue(), "test-msg-1");
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgExtractor().apply(xInputMsg).getEventTime(), 33333L);
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgBuilder().apply("test-key-1", new MyMessageType("test-msg-1", 33333L, "test-output-id-1")).getKey(), "test-key-1");
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgBuilder().apply("test-key-1", new MyMessageType("test-msg-1", 33333L, "test-output-id-1")).getMessage().getValue(), "test-msg-1");
    assertEquals(((IntermediateStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mIntermediateStream).getMsgBuilder().apply("test-key-1", new MyMessageType("test-msg-1", 33333L, "test-output-id-1")).getMessage().getEventTime(), 33333L);
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) Config(org.apache.samza.config.Config) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StreamSpec(org.apache.samza.system.StreamSpec) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Aggregations

Config (org.apache.samza.config.Config)8 ApplicationRunner (org.apache.samza.runtime.ApplicationRunner)8 JobConfig (org.apache.samza.config.JobConfig)7 Test (org.junit.Test)7 Function (java.util.function.Function)6 StreamSpec (org.apache.samza.system.StreamSpec)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 Mockito.mock (org.mockito.Mockito.mock)6 Mockito.when (org.mockito.Mockito.when)6 BiFunction (java.util.function.BiFunction)5 MessageType (org.apache.samza.operators.data.MessageType)5 TestInputMessageEnvelope (org.apache.samza.operators.data.TestInputMessageEnvelope)5 TestMessageEnvelope (org.apache.samza.operators.data.TestMessageEnvelope)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 InputStreamInternalImpl (org.apache.samza.operators.stream.InputStreamInternalImpl)4 IntermediateStreamInternalImpl (org.apache.samza.operators.stream.IntermediateStreamInternalImpl)4 OutputStreamInternalImpl (org.apache.samza.operators.stream.OutputStreamInternalImpl)4 Duration (java.time.Duration)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2