Search in sources :

Example 1 with WindowPane

use of org.apache.samza.operators.windows.WindowPane in project samza by apache.

the class TestWindowOperator method testTumblingWindowsAccumulatingMode.

@Test
public void testTumblingWindowsAccumulatingMode() throws Exception {
    StreamApplication sgb = new KeyedTumblingWindowStreamApplication(AccumulationMode.ACCUMULATING, 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(), 7);
    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(), 4);
    Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(2));
    Assert.assertEquals((windowPanes.get(3).getMessage()).size(), 4);
}
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 2 with WindowPane

use of org.apache.samza.operators.windows.WindowPane in project samza by apache.

the class TestOperatorSpecs method testCreateWindowOperatorWithRelaxedTypes.

@Test
public void testCreateWindowOperatorWithRelaxedTypes() throws Exception {
    Function<TestMessageEnvelope, String> keyExtractor = m -> m.getKey();
    FoldLeftFunction<TestMessageEnvelope, Integer> aggregator = (m, c) -> c + 1;
    Supplier<Integer> initialValue = () -> 0;
    //instantiate a window using reflection
    WindowInternal<TestInputMessageEnvelope, String, Integer> window = new WindowInternal(null, initialValue, aggregator, keyExtractor, null, WindowType.TUMBLING);
    MessageStreamImpl<WindowPane<String, Integer>> mockWndOut = mock(MessageStreamImpl.class);
    WindowOperatorSpec spec = OperatorSpecs.createWindowOperatorSpec(window, mockWndOut, 1);
    assertEquals(spec.getWindow(), window);
    assertEquals(spec.getWindow().getKeyExtractor(), keyExtractor);
    assertEquals(spec.getWindow().getFoldLeftFunction(), aggregator);
    // make sure that the functions with relaxed types work as expected
    TestInputMessageEnvelope inputMsg = new TestInputMessageEnvelope("test-input-key1", "test-value-1", 23456L, "input-id-1");
    assertEquals("test-input-key1", spec.getWindow().getKeyExtractor().apply(inputMsg));
    assertEquals(1, spec.getWindow().getFoldLeftFunction().apply(inputMsg, 0));
}
Also used : PartialJoinFunction(org.apache.samza.operators.functions.PartialJoinFunction) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Function(java.util.function.Function) Supplier(java.util.function.Supplier) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) MessageCollector(org.apache.samza.task.MessageCollector) MessageStreamImpl(org.apache.samza.operators.MessageStreamImpl) SystemStream(org.apache.samza.system.SystemStream) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) WindowType(org.apache.samza.operators.windows.internal.WindowType) MessageType(org.apache.samza.operators.data.MessageType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) TaskCoordinator(org.apache.samza.task.TaskCoordinator) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) Matchers.any(org.mockito.Matchers.any) List(java.util.List) SinkFunction(org.apache.samza.operators.functions.SinkFunction) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) TestMessageStreamImplUtil(org.apache.samza.operators.TestMessageStreamImplUtil) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) FoldLeftFunction(org.apache.samza.operators.functions.FoldLeftFunction) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) WindowPane(org.apache.samza.operators.windows.WindowPane) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) Test(org.junit.Test)

Example 3 with WindowPane

use of org.apache.samza.operators.windows.WindowPane in project samza by apache.

the class TestOperatorSpecs method testCreateWindowOperator.

@Test
public void testCreateWindowOperator() throws Exception {
    Function<TestMessageEnvelope, String> keyExtractor = m -> "globalkey";
    FoldLeftFunction<TestMessageEnvelope, Integer> aggregator = (m, c) -> c + 1;
    Supplier<Integer> initialValue = () -> 0;
    //instantiate a window using reflection
    WindowInternal window = new WindowInternal(null, initialValue, aggregator, keyExtractor, null, WindowType.TUMBLING);
    MessageStreamImpl<WindowPane<String, Integer>> mockWndOut = mock(MessageStreamImpl.class);
    WindowOperatorSpec spec = OperatorSpecs.<TestMessageEnvelope, String, Integer>createWindowOperatorSpec(window, mockWndOut, 1);
    assertEquals(spec.getWindow(), window);
    assertEquals(spec.getWindow().getKeyExtractor(), keyExtractor);
    assertEquals(spec.getWindow().getFoldLeftFunction(), aggregator);
}
Also used : PartialJoinFunction(org.apache.samza.operators.functions.PartialJoinFunction) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Function(java.util.function.Function) Supplier(java.util.function.Supplier) WindowPane(org.apache.samza.operators.windows.WindowPane) ArrayList(java.util.ArrayList) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) MessageCollector(org.apache.samza.task.MessageCollector) MessageStreamImpl(org.apache.samza.operators.MessageStreamImpl) SystemStream(org.apache.samza.system.SystemStream) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) WindowType(org.apache.samza.operators.windows.internal.WindowType) MessageType(org.apache.samza.operators.data.MessageType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) TaskCoordinator(org.apache.samza.task.TaskCoordinator) StreamGraphImpl(org.apache.samza.operators.StreamGraphImpl) Matchers.any(org.mockito.Matchers.any) List(java.util.List) SinkFunction(org.apache.samza.operators.functions.SinkFunction) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) TestMessageStreamImplUtil(org.apache.samza.operators.TestMessageStreamImplUtil) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) FoldLeftFunction(org.apache.samza.operators.functions.FoldLeftFunction) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) WindowInternal(org.apache.samza.operators.windows.internal.WindowInternal) WindowPane(org.apache.samza.operators.windows.WindowPane) Test(org.junit.Test)

Example 4 with WindowPane

use of org.apache.samza.operators.windows.WindowPane in project samza by apache.

the class TestWindowOperator method testSessionWindowsDiscardingMode.

@Test
public void testSessionWindowsDiscardingMode() throws Exception {
    OperatorSpecGraph sgb = this.getKeyedSessionWindowStreamGraph(AccumulationMode.DISCARDING, Duration.ofMillis(500)).getOperatorSpecGraph();
    TestClock testClock = new TestClock();
    List<WindowPane<Integer, Collection<IntegerEnvelope>>> windowPanes = new ArrayList<>();
    StreamOperatorTask task = new StreamOperatorTask(sgb, testClock);
    task.init(this.context);
    MessageCollector messageCollector = envelope -> windowPanes.add((WindowPane<Integer, Collection<IntegerEnvelope>>) envelope.getMessage());
    task.processAsync(new IntegerEnvelope(1), messageCollector, taskCoordinator, taskCallback);
    task.processAsync(new IntegerEnvelope(1), messageCollector, taskCoordinator, taskCallback);
    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.processAsync(new IntegerEnvelope(2), messageCollector, taskCoordinator, taskCallback);
    task.processAsync(new IntegerEnvelope(2), messageCollector, taskCoordinator, taskCallback);
    task.processAsync(new IntegerEnvelope(3), messageCollector, taskCoordinator, taskCallback);
    task.processAsync(new IntegerEnvelope(3), messageCollector, taskCoordinator, taskCallback);
    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.processAsync(new IntegerEnvelope(2), messageCollector, taskCoordinator, taskCallback);
    task.processAsync(new IntegerEnvelope(2), messageCollector, taskCoordinator, taskCallback);
    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) 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) OperatorSpecGraph(org.apache.samza.operators.OperatorSpecGraph) 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 5 with WindowPane

use of org.apache.samza.operators.windows.WindowPane in project samza by apache.

the class TestWindowOperator method testTumblingWindowsAccumulatingMode.

@Test
public void testTumblingWindowsAccumulatingMode() throws Exception {
    OperatorSpecGraph sgb = this.getKeyedTumblingWindowStreamGraph(AccumulationMode.ACCUMULATING, 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());
    integers.forEach(n -> task.processAsync(new IntegerEnvelope(n), messageCollector, taskCoordinator, taskCallback));
    testClock.advanceTime(Duration.ofSeconds(1));
    task.window(messageCollector, taskCoordinator);
    Assert.assertEquals(windowPanes.size(), 7);
    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(), 4);
    Assert.assertEquals(windowPanes.get(3).getKey().getKey(), new Integer(2));
    Assert.assertEquals((windowPanes.get(3).getMessage()).size(), 4);
}
Also used : TestClock(org.apache.samza.testUtils.TestClock) 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) OperatorSpecGraph(org.apache.samza.operators.OperatorSpecGraph) 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

WindowPane (org.apache.samza.operators.windows.WindowPane)26 Duration (java.time.Duration)24 Windows (org.apache.samza.operators.windows.Windows)24 Collection (java.util.Collection)23 StreamApplication (org.apache.samza.application.StreamApplication)23 Config (org.apache.samza.config.Config)23 Test (org.junit.Test)23 Mockito.mock (org.mockito.Mockito.mock)23 Mockito.when (org.mockito.Mockito.when)23 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Triggers (org.apache.samza.operators.triggers.Triggers)22 OutgoingMessageEnvelope (org.apache.samza.system.OutgoingMessageEnvelope)22 SystemStream (org.apache.samza.system.SystemStream)22 MessageCollector (org.apache.samza.task.MessageCollector)22 TaskCoordinator (org.apache.samza.task.TaskCoordinator)22 ImmutableList (com.google.common.collect.ImmutableList)21 AccumulationMode (org.apache.samza.operators.windows.AccumulationMode)21 ImmutableSet (com.google.common.collect.ImmutableSet)20 Partition (org.apache.samza.Partition)20