Search in sources :

Example 1 with Trigger

use of org.apache.samza.operators.triggers.Trigger in project samza by apache.

the class TestWindowOperatorSpec method testTriggerIntervalWithSingleTimeTrigger.

@Test
public void testTriggerIntervalWithSingleTimeTrigger() {
    Trigger defaultTrigger = Triggers.timeSinceFirstMessage(Duration.ofMillis(150));
    Trigger earlyTrigger = Triggers.repeat(Triggers.count(5));
    WindowInternal window = new WindowInternal(defaultTrigger, null, null, null, null, WindowType.SESSION);
    window.setEarlyTrigger(earlyTrigger);
    WindowOperatorSpec spec = new WindowOperatorSpec(window, new MessageStreamImpl(null), 0);
    Assert.assertEquals(spec.getDefaultTriggerMs(), 150);
}
Also used : MessageStreamImpl(org.apache.samza.operators.MessageStreamImpl) Trigger(org.apache.samza.operators.triggers.Trigger) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) Test(org.junit.Test)

Example 2 with Trigger

use of org.apache.samza.operators.triggers.Trigger in project samza by apache.

the class TestWindowOperatorSpec method testTriggerIntervalWithNestedTimeTriggers.

@Test
public void testTriggerIntervalWithNestedTimeTriggers() {
    Trigger defaultTrigger = Triggers.timeSinceFirstMessage(Duration.ofMillis(150));
    Trigger lateTrigger = Triggers.any(Triggers.count(6), Triggers.timeSinceFirstMessage(Duration.ofMillis(15)));
    Trigger earlyTrigger = Triggers.repeat(Triggers.any(Triggers.count(23), Triggers.timeSinceFirstMessage(Duration.ofMillis(15)), Triggers.any(Triggers.any(Triggers.count(6), Triggers.timeSinceFirstMessage(Duration.ofMillis(15)), Triggers.timeSinceFirstMessage(Duration.ofMillis(25)), Triggers.timeSinceLastMessage(Duration.ofMillis(15))))));
    WindowInternal window = new WindowInternal(defaultTrigger, null, null, null, null, WindowType.SESSION);
    window.setEarlyTrigger(earlyTrigger);
    window.setLateTrigger(lateTrigger);
    WindowOperatorSpec spec = new WindowOperatorSpec(window, new MessageStreamImpl(null), 0);
    Assert.assertEquals(spec.getDefaultTriggerMs(), 5);
}
Also used : MessageStreamImpl(org.apache.samza.operators.MessageStreamImpl) Trigger(org.apache.samza.operators.triggers.Trigger) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) Test(org.junit.Test)

Example 3 with Trigger

use of org.apache.samza.operators.triggers.Trigger 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)

Example 4 with Trigger

use of org.apache.samza.operators.triggers.Trigger in project samza by apache.

the class TestWindowOperator method testCancellationOfAnyTrigger.

@Test
public void testCancellationOfAnyTrigger() throws Exception {
    StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.ACCUMULATING, Duration.ofSeconds(1), Triggers.any(Triggers.count(2), Triggers.timeSinceFirstMessage(Duration.ofMillis(500))));
    TestClock testClock = new TestClock();
    StreamOperatorTask task = new StreamOperatorTask(sgb, runner, testClock);
    task.init(config, taskContext);
    List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
    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);
    //assert that the count trigger fired
    Assert.assertEquals(windowPanes.size(), 1);
    //advance the timer to enable the triggering of the inner timeSinceFirstMessage trigger
    testClock.advanceTime(Duration.ofMillis(500));
    //assert that the triggering of the count trigger cancelled the inner timeSinceFirstMessage trigger
    Assert.assertEquals(windowPanes.size(), 1);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    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(), 2);
    Assert.assertEquals(windowPanes.get(1).getFiringType(), FiringType.DEFAULT);
    Assert.assertEquals(windowPanes.get(1).getKey().getKey(), new Integer(1));
    Assert.assertEquals(windowPanes.get(1).getKey().getPaneId(), "0");
    Assert.assertEquals((windowPanes.get(1).getMessage()).size(), 5);
    task.process(new IntegerEnvelope(1), messageCollector, taskCoordinator);
    //advance timer by 500 millis to enable the inner timeSinceFirstMessage trigger
    testClock.advanceTime(Duration.ofMillis(500));
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 3);
    Assert.assertEquals(windowPanes.get(2).getFiringType(), FiringType.EARLY);
    Assert.assertEquals(windowPanes.get(2).getKey().getKey(), new Integer(1));
    Assert.assertEquals(windowPanes.get(2).getKey().getPaneId(), "1000");
    //advance timer by > 500 millis to enable the default trigger
    testClock.advanceTime(Duration.ofMillis(900));
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 4);
    Assert.assertEquals(windowPanes.get(3).getFiringType(), FiringType.DEFAULT);
    Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(1));
    Assert.assertEquals(windowPanes.get(3).getKey().getPaneId(), "1000");
}
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)

Aggregations

Trigger (org.apache.samza.operators.triggers.Trigger)4 Test (org.junit.Test)4 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Function (java.util.function.Function)2 Assert (junit.framework.Assert)2 Partition (org.apache.samza.Partition)2 StreamApplication (org.apache.samza.application.StreamApplication)2 Config (org.apache.samza.config.Config)2 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)2 MessageStreamImpl (org.apache.samza.operators.MessageStreamImpl)2 FiringType (org.apache.samza.operators.triggers.FiringType)2 Triggers (org.apache.samza.operators.triggers.Triggers)2 AccumulationMode (org.apache.samza.operators.windows.AccumulationMode)2 WindowPane (org.apache.samza.operators.windows.WindowPane)2 Windows (org.apache.samza.operators.windows.Windows)2