Search in sources :

Example 21 with StreamOperatorTask

use of org.apache.samza.task.StreamOperatorTask in project samza by apache.

the class WatermarkIntegrationTest method getTaskOperationGraphs.

Map<String, StreamOperatorTask> getTaskOperationGraphs(MockLocalApplicationRunner runner) throws Exception {
    StreamProcessor processor = runner.getProcessors().iterator().next();
    SamzaContainer container = TestStreamProcessorUtil.getContainer(processor);
    Map<TaskName, TaskInstance> taskInstances = JavaConverters.mapAsJavaMapConverter(container.getTaskInstances()).asJava();
    Map<String, StreamOperatorTask> tasks = new HashMap<>();
    for (Map.Entry<TaskName, TaskInstance> entry : taskInstances.entrySet()) {
        StreamOperatorTask task = (StreamOperatorTask) entry.getValue().task();
        tasks.put(entry.getKey().getTaskName(), task);
    }
    return tasks;
}
Also used : TaskInstance(org.apache.samza.container.TaskInstance) StreamProcessor(org.apache.samza.processor.StreamProcessor) TaskName(org.apache.samza.container.TaskName) HashMap(java.util.HashMap) TestStreamOperatorTask(org.apache.samza.task.TestStreamOperatorTask) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) Map(java.util.Map) HashMap(java.util.HashMap) SamzaContainer(org.apache.samza.container.SamzaContainer)

Example 22 with StreamOperatorTask

use of org.apache.samza.task.StreamOperatorTask in project samza by apache.

the class TestWindowOperator method testTumblingWindowsDiscardingMode.

@Test
public void testTumblingWindowsDiscardingMode() throws Exception {
    StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.DISCARDING, Duration.ofSeconds(1), Triggers.repeat(Triggers.count(2)));
    List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
    TestClock testClock = new TestClock();
    StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
    task.init(config, taskContext);
    MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
    integers.forEach(n -> task.process(new IntegerEnvelope(n), messageCollector, taskCoordinator));
    testClock.advanceTime(Duration.ofSeconds(1));
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 5);
    Assert.assertEquals(windowPanes.get(0).getKey().getKey(), new Integer(1));
    Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
    Assert.assertEquals(windowPanes.get(1).getKey().getKey(), new Integer(2));
    Assert.assertEquals((windowPanes.get(1).getMessage()).size(), 2);
    Assert.assertEquals(windowPanes.get(2).getKey().getKey(), new Integer(1));
    Assert.assertEquals((windowPanes.get(2).getMessage()).size(), 2);
    Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(2));
    Assert.assertEquals((windowPanes.get(3).getMessage()).size(), 2);
    Assert.assertEquals(windowPanes.get(4).getKey().getKey(), new Integer(3));
    Assert.assertEquals((windowPanes.get(4).getMessage()).size(), 1);
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Function(java.util.function.Function) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) Trigger(org.apache.samza.operators.triggers.Trigger) ImmutableList(com.google.common.collect.ImmutableList) AccumulationMode(org.apache.samza.operators.windows.AccumulationMode) MessageCollector(org.apache.samza.task.MessageCollector) FiringType(org.apache.samza.operators.triggers.FiringType) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Before(org.junit.Before) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) Windows(org.apache.samza.operators.windows.Windows) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Collection(java.util.Collection) Partition(org.apache.samza.Partition) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Triggers(org.apache.samza.operators.triggers.Triggers) List(java.util.List) TestClock(org.apache.samza.testUtils.TestClock) Assert(junit.framework.Assert) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamApplication(org.apache.samza.application.StreamApplication) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Collection(java.util.Collection) WindowPane(org.apache.samza.operators.windows.WindowPane) Test(org.junit.Test)

Example 23 with StreamOperatorTask

use of org.apache.samza.task.StreamOperatorTask in project samza by apache.

the class TestWindowOperator method testSessionWindowsDiscardingMode.

@Test
public void testSessionWindowsDiscardingMode() throws Exception {
    StreamApplication sgb = new KeyedSessionWindowStreamApplication(AccumulationMode.DISCARDING, Duration.ofMillis(500));
    TestClock testClock = new TestClock();
    List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
    StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
    task.init(config, taskContext);
    MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    testClock.advanceTime(Duration.ofSeconds(1));
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 1);
    Assert.assertEquals(windowPanes.get(0).getKey().getPaneId(), "1");
    Assert.assertEquals(windowPanes.get(0).getKey().getKey(), new Integer(1));
    task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
    task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
    task.process(new IntegerEnvelope(3), messageCollector, taskCoordinator);
    task.process(new IntegerEnvelope(3), messageCollector, taskCoordinator);
    testClock.advanceTime(Duration.ofSeconds(1));
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 3);
    Assert.assertEquals(windowPanes.get(0).getKey().getPaneId(), "1");
    Assert.assertEquals(windowPanes.get(1).getKey().getPaneId(), "1001");
    Assert.assertEquals(windowPanes.get(2).getKey().getPaneId(), "1001");
    Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
    Assert.assertEquals((windowPanes.get(1).getMessage()).size(), 2);
    Assert.assertEquals((windowPanes.get(2).getMessage()).size(), 2);
    task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
    task.process(new IntegerEnvelope(2), messageCollector, taskCoordinator);
    testClock.advanceTime(Duration.ofSeconds(1));
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 4);
    Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(2));
    Assert.assertEquals(windowPanes.get(3).getKey().getPaneId(), "2001");
    Assert.assertEquals((windowPanes.get(3).getMessage()).size(), 2);
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Function(java.util.function.Function) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) Trigger(org.apache.samza.operators.triggers.Trigger) ImmutableList(com.google.common.collect.ImmutableList) AccumulationMode(org.apache.samza.operators.windows.AccumulationMode) MessageCollector(org.apache.samza.task.MessageCollector) FiringType(org.apache.samza.operators.triggers.FiringType) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Before(org.junit.Before) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) Windows(org.apache.samza.operators.windows.Windows) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Collection(java.util.Collection) Partition(org.apache.samza.Partition) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Triggers(org.apache.samza.operators.triggers.Triggers) List(java.util.List) TestClock(org.apache.samza.testUtils.TestClock) Assert(junit.framework.Assert) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamApplication(org.apache.samza.application.StreamApplication) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Collection(java.util.Collection) WindowPane(org.apache.samza.operators.windows.WindowPane) Test(org.junit.Test)

Example 24 with StreamOperatorTask

use of org.apache.samza.task.StreamOperatorTask in project samza by apache.

the class TestWindowOperator method testNonKeyedTumblingWindowsDiscardingMode.

@Test
public void testNonKeyedTumblingWindowsDiscardingMode() throws Exception {
    StreamApplication sgb = new TumblingWindowStreamApplication(AccumulationMode.DISCARDING, Duration.ofSeconds(1), Triggers.repeat(Triggers.count(1000)));
    List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
    TestClock testClock = new TestClock();
    StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
    task.init(config, taskContext);
    MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
    Assert.assertEquals(windowPanes.size(), 0);
    integers.forEach(n -> task.process(new IntegerEnvelope(n), messageCollector, taskCoordinator));
    Assert.assertEquals(windowPanes.size(), 0);
    testClock.advanceTime(Duration.ofSeconds(1));
    Assert.assertEquals(windowPanes.size(), 0);
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 1);
    Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 9);
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Function(java.util.function.Function) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) Trigger(org.apache.samza.operators.triggers.Trigger) ImmutableList(com.google.common.collect.ImmutableList) AccumulationMode(org.apache.samza.operators.windows.AccumulationMode) MessageCollector(org.apache.samza.task.MessageCollector) FiringType(org.apache.samza.operators.triggers.FiringType) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Before(org.junit.Before) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) Windows(org.apache.samza.operators.windows.Windows) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Collection(java.util.Collection) Partition(org.apache.samza.Partition) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Triggers(org.apache.samza.operators.triggers.Triggers) List(java.util.List) TestClock(org.apache.samza.testUtils.TestClock) Assert(junit.framework.Assert) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamApplication(org.apache.samza.application.StreamApplication) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Collection(java.util.Collection) WindowPane(org.apache.samza.operators.windows.WindowPane) Test(org.junit.Test)

Example 25 with StreamOperatorTask

use of org.apache.samza.task.StreamOperatorTask in project samza by apache.

the class TestWindowOperator method testCancelationOfRepeatingNestedTriggers.

@Test
public void testCancelationOfRepeatingNestedTriggers() throws Exception {
    StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.ACCUMULATING, Duration.ofSeconds(1), Triggers.repeat(Triggers.any(Triggers.count(2), Triggers.timeSinceFirstMessage(Duration.ofMillis(500)))));
    List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
    MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
    TestClock testClock = new TestClock();
    StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
    task.init(config, taskContext);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    //assert that the count trigger fired
    Assert.assertEquals(windowPanes.size(), 1);
    //advance the timer to enable the potential triggering of the inner timeSinceFirstMessage trigger
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    testClock.advanceTime(Duration.ofMillis(500));
    //assert that the triggering of the count trigger cancelled the inner timeSinceFirstMessage trigger
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 2);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 3);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    //advance timer by 500 more millis to enable the default trigger
    testClock.advanceTime(Duration.ofMillis(500));
    task.window(messageCollector, taskCoordinator);
    //assert that the default trigger fired
    Assert.assertEquals(windowPanes.size(), 4);
}
Also used : StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Function(java.util.function.Function) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) Trigger(org.apache.samza.operators.triggers.Trigger) ImmutableList(com.google.common.collect.ImmutableList) AccumulationMode(org.apache.samza.operators.windows.AccumulationMode) MessageCollector(org.apache.samza.task.MessageCollector) FiringType(org.apache.samza.operators.triggers.FiringType) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Before(org.junit.Before) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) TaskContext(org.apache.samza.task.TaskContext) ImmutableSet(com.google.common.collect.ImmutableSet) Windows(org.apache.samza.operators.windows.Windows) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Collection(java.util.Collection) Partition(org.apache.samza.Partition) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Triggers(org.apache.samza.operators.triggers.Triggers) List(java.util.List) TestClock(org.apache.samza.testUtils.TestClock) Assert(junit.framework.Assert) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) TestClock(org.apache.samza.testUtils.TestClock) StreamApplication(org.apache.samza.application.StreamApplication) MessageCollector(org.apache.samza.task.MessageCollector) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) ArrayList(java.util.ArrayList) Collection(java.util.Collection) WindowPane(org.apache.samza.operators.windows.WindowPane) Test(org.junit.Test)

Aggregations

StreamOperatorTask (org.apache.samza.task.StreamOperatorTask)36 Partition (org.apache.samza.Partition)33 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)33 Test (org.junit.Test)33 Config (org.apache.samza.config.Config)32 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)32 MessageCollector (org.apache.samza.task.MessageCollector)32 ArrayList (java.util.ArrayList)31 List (java.util.List)31 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)31 OutgoingMessageEnvelope (org.apache.samza.system.OutgoingMessageEnvelope)31 ImmutableSet (com.google.common.collect.ImmutableSet)30 Duration (java.time.Duration)30 SystemStream (org.apache.samza.system.SystemStream)30 TaskCoordinator (org.apache.samza.task.TaskCoordinator)30 TestClock (org.apache.samza.testUtils.TestClock)30 Mockito.mock (org.mockito.Mockito.mock)30 Mockito.when (org.mockito.Mockito.when)30 HashMap (java.util.HashMap)25 Map (java.util.Map)24