Search in sources :

Example 1 with Triggers

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

the class TestWindowOperator method testEndOfStreamFlushesWithEarlyTriggerFirings.

@Test
public void testEndOfStreamFlushesWithEarlyTriggerFirings() throws Exception {
    OperatorSpecGraph sgb = this.getTumblingWindowStreamGraph(AccumulationMode.DISCARDING, Duration.ofSeconds(1), Triggers.repeat(Triggers.count(2))).getOperatorSpecGraph();
    List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
    TestClock testClock = new TestClock();
    StreamOperatorTask task = new StreamOperatorTask(sgb, testClock);
    task.init(this.context);
    MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
    Assert.assertEquals(windowPanes.size(), 0);
    List<Integer> integerList = ImmutableList.of(1, 2, 1, 2, 1);
    integerList.forEach(n -> task.processAsync(new IntegerEnvelope(n), messageCollector, taskCoordinator, taskCallback));
    // early triggers should emit (1,2) and (1,2) in the same window.
    Assert.assertEquals(windowPanes.size(), 2);
    testClock.advanceTime(Duration.ofSeconds(1));
    Assert.assertEquals(windowPanes.size(), 2);
    final IncomingMessageEnvelope endOfStream = IncomingMessageEnvelope.buildEndOfStreamEnvelope(new SystemStreamPartition("kafka", "integers", new Partition(0)));
    task.processAsync(endOfStream, messageCollector, taskCoordinator, taskCallback);
    // end of stream flushes the last entry (1)
    Assert.assertEquals(windowPanes.size(), 3);
    Assert.assertEquals((windowPanes.get(0).getMessage()).size(), 2);
    verify(taskCoordinator, times(1)).commit(TaskCoordinator.RequestScope.CURRENT_TASK);
    verify(taskCoordinator, times(1)).shutdown(TaskCoordinator.RequestScope.CURRENT_TASK);
}
Also used : StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) TaskModel(org.apache.samza.job.model.TaskModel) GenericInputDescriptor(org.apache.samza.system.descriptors.GenericInputDescriptor) OperatorSpecGraph(org.apache.samza.operators.OperatorSpecGraph) AccumulationMode(org.apache.samza.operators.windows.AccumulationMode) Duration(java.time.Duration) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) KV(org.apache.samza.operators.KV) TaskCallback(org.apache.samza.task.TaskCallback) ImmutableSet(com.google.common.collect.ImmutableSet) TaskName(org.apache.samza.container.TaskName) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Collection(java.util.Collection) Triggers(org.apache.samza.operators.triggers.Triggers) Context(org.apache.samza.context.Context) List(java.util.List) TestClock(org.apache.samza.testUtils.TestClock) TestInMemoryStore(org.apache.samza.operators.impl.store.TestInMemoryStore) Config(org.apache.samza.config.Config) KVSerde(org.apache.samza.serializers.KVSerde) StreamApplication(org.apache.samza.application.StreamApplication) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) GenericSystemDescriptor(org.apache.samza.system.descriptors.GenericSystemDescriptor) HashMap(java.util.HashMap) Serde(org.apache.samza.serializers.Serde) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ArrayList(java.util.ArrayList) MapFunction(org.apache.samza.operators.functions.MapFunction) WindowPane(org.apache.samza.operators.windows.WindowPane) Trigger(org.apache.samza.operators.triggers.Trigger) ImmutableList(com.google.common.collect.ImmutableList) MessageCollector(org.apache.samza.task.MessageCollector) FiringType(org.apache.samza.operators.triggers.FiringType) SystemStream(org.apache.samza.system.SystemStream) MockContext(org.apache.samza.context.MockContext) IntegerSerde(org.apache.samza.serializers.IntegerSerde) MessageStream(org.apache.samza.operators.MessageStream) Before(org.junit.Before) Windows(org.apache.samza.operators.windows.Windows) Partition(org.apache.samza.Partition) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Mockito.verify(org.mockito.Mockito.verify) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) TaskContextImpl(org.apache.samza.context.TaskContextImpl) Assert(org.junit.Assert) TimeSeriesKeySerde(org.apache.samza.operators.impl.store.TimeSeriesKeySerde) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Partition(org.apache.samza.Partition) OperatorSpecGraph(org.apache.samza.operators.OperatorSpecGraph) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) ArrayList(java.util.ArrayList) WindowPane(org.apache.samza.operators.windows.WindowPane) TestClock(org.apache.samza.testUtils.TestClock) MessageCollector(org.apache.samza.task.MessageCollector) Collection(java.util.Collection) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Partition (org.apache.samza.Partition)1 StreamApplication (org.apache.samza.application.StreamApplication)1 StreamApplicationDescriptorImpl (org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl)1 Config (org.apache.samza.config.Config)1 MapConfig (org.apache.samza.config.MapConfig)1 TaskName (org.apache.samza.container.TaskName)1 Context (org.apache.samza.context.Context)1 MockContext (org.apache.samza.context.MockContext)1 TaskContextImpl (org.apache.samza.context.TaskContextImpl)1 TaskModel (org.apache.samza.job.model.TaskModel)1 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)1