Search in sources :

Example 11 with TaskCallback

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

the class TestWindowOperator method testEndOfStreamFlushesWithNoTriggerFirings.

@Test
public void testEndOfStreamFlushesWithNoTriggerFirings() 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);
    task.processAsync(new IntegerEnvelope(1), messageCollector, taskCoordinator, taskCallback);
    task.processAsync(new IntegerEnvelope(1), messageCollector, taskCoordinator, taskCallback);
    final IncomingMessageEnvelope endOfStream = IncomingMessageEnvelope.buildEndOfStreamEnvelope(new SystemStreamPartition("kafka", "integers", new Partition(0)));
    task.processAsync(endOfStream, messageCollector, taskCoordinator, taskCallback);
    Assert.assertEquals(windowPanes.size(), 1);
    Assert.assertEquals(windowPanes.get(0).getMessage().size(), 4);
    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)

Example 12 with TaskCallback

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

the class TestJoinOperator method joinRetainsMatchedMessages.

@Test
public void joinRetainsMatchedMessages() throws Exception {
    StreamApplicationDescriptorImpl streamAppDesc = this.getTestJoinStreamGraph(new TestJoinFunction());
    StreamOperatorTask sot = createStreamOperatorTask(new SystemClock(), streamAppDesc);
    List<Integer> output = new ArrayList<>();
    MessageCollector messageCollector = envelope -> output.add((Integer) envelope.getMessage());
    // push messages to first stream
    numbers.forEach(n -> sot.processAsync(new FirstStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    // push messages to second stream with same key
    numbers.forEach(n -> sot.processAsync(new SecondStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    int outputSum = output.stream().reduce(0, (s, m) -> s + m);
    assertEquals(110, outputSum);
    output.clear();
    // push messages to first stream with same keys once again.
    numbers.forEach(n -> sot.processAsync(new FirstStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    int newOutputSum = output.stream().reduce(0, (s, m) -> s + m);
    // should produce the same output as before
    assertEquals(110, newOutputSum);
}
Also used : StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) GenericSystemDescriptor(org.apache.samza.system.descriptors.GenericSystemDescriptor) TaskModel(org.apache.samza.job.model.TaskModel) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) GenericInputDescriptor(org.apache.samza.system.descriptors.GenericInputDescriptor) ArrayList(java.util.ArrayList) StreamTestUtils(org.apache.samza.testUtils.StreamTestUtils) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Matchers.eq(org.mockito.Matchers.eq) MockContext(org.apache.samza.context.MockContext) Duration(java.time.Duration) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) IntegerSerde(org.apache.samza.serializers.IntegerSerde) TaskCallback(org.apache.samza.task.TaskCallback) ImmutableSet(com.google.common.collect.ImmutableSet) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Clock(org.apache.samza.util.Clock) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) SamzaException(org.apache.samza.SamzaException) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Mockito.verify(org.mockito.Mockito.verify) Context(org.apache.samza.context.Context) TimestampedValueSerde(org.apache.samza.operators.impl.store.TimestampedValueSerde) Matchers.any(org.mockito.Matchers.any) List(java.util.List) SystemClock(org.apache.samza.util.SystemClock) TestClock(org.apache.samza.testUtils.TestClock) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) TestInMemoryStore(org.apache.samza.operators.impl.store.TestInMemoryStore) Config(org.apache.samza.config.Config) KVSerde(org.apache.samza.serializers.KVSerde) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) SystemClock(org.apache.samza.util.SystemClock) StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with TaskCallback

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

the class TestJoinOperator method joinRetainsLatestMessageForKey.

@Test
public void joinRetainsLatestMessageForKey() throws Exception {
    StreamApplicationDescriptorImpl streamAppDesc = this.getTestJoinStreamGraph(new TestJoinFunction());
    StreamOperatorTask sot = createStreamOperatorTask(new SystemClock(), streamAppDesc);
    List<Integer> output = new ArrayList<>();
    MessageCollector messageCollector = envelope -> output.add((Integer) envelope.getMessage());
    // push messages to first stream
    numbers.forEach(n -> sot.processAsync(new FirstStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    // push messages to first stream again with same keys but different values
    numbers.forEach(n -> sot.processAsync(new FirstStreamIME(n, 2 * n), messageCollector, taskCoordinator, taskCallback));
    // push messages to second stream with same key
    numbers.forEach(n -> sot.processAsync(new SecondStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    int outputSum = output.stream().reduce(0, (s, m) -> s + m);
    // should use latest messages in the first stream
    assertEquals(165, outputSum);
}
Also used : StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) GenericSystemDescriptor(org.apache.samza.system.descriptors.GenericSystemDescriptor) TaskModel(org.apache.samza.job.model.TaskModel) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) GenericInputDescriptor(org.apache.samza.system.descriptors.GenericInputDescriptor) ArrayList(java.util.ArrayList) StreamTestUtils(org.apache.samza.testUtils.StreamTestUtils) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Matchers.eq(org.mockito.Matchers.eq) MockContext(org.apache.samza.context.MockContext) Duration(java.time.Duration) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) IntegerSerde(org.apache.samza.serializers.IntegerSerde) TaskCallback(org.apache.samza.task.TaskCallback) ImmutableSet(com.google.common.collect.ImmutableSet) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Clock(org.apache.samza.util.Clock) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) SamzaException(org.apache.samza.SamzaException) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Mockito.verify(org.mockito.Mockito.verify) Context(org.apache.samza.context.Context) TimestampedValueSerde(org.apache.samza.operators.impl.store.TimestampedValueSerde) Matchers.any(org.mockito.Matchers.any) List(java.util.List) SystemClock(org.apache.samza.util.SystemClock) TestClock(org.apache.samza.testUtils.TestClock) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) TestInMemoryStore(org.apache.samza.operators.impl.store.TestInMemoryStore) Config(org.apache.samza.config.Config) KVSerde(org.apache.samza.serializers.KVSerde) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) SystemClock(org.apache.samza.util.SystemClock) StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with TaskCallback

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

the class TestJoinOperator method joinReverse.

@Test
public void joinReverse() throws Exception {
    StreamApplicationDescriptorImpl streamAppDesc = this.getTestJoinStreamGraph(new TestJoinFunction());
    StreamOperatorTask sot = createStreamOperatorTask(new SystemClock(), streamAppDesc);
    List<Integer> output = new ArrayList<>();
    MessageCollector messageCollector = envelope -> output.add((Integer) envelope.getMessage());
    // push messages to second stream
    numbers.forEach(n -> sot.processAsync(new SecondStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    // push messages to first stream with same keys
    numbers.forEach(n -> sot.processAsync(new FirstStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    int outputSum = output.stream().reduce(0, (s, m) -> s + m);
    assertEquals(110, outputSum);
}
Also used : StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) GenericSystemDescriptor(org.apache.samza.system.descriptors.GenericSystemDescriptor) TaskModel(org.apache.samza.job.model.TaskModel) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) GenericInputDescriptor(org.apache.samza.system.descriptors.GenericInputDescriptor) ArrayList(java.util.ArrayList) StreamTestUtils(org.apache.samza.testUtils.StreamTestUtils) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Matchers.eq(org.mockito.Matchers.eq) MockContext(org.apache.samza.context.MockContext) Duration(java.time.Duration) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) IntegerSerde(org.apache.samza.serializers.IntegerSerde) TaskCallback(org.apache.samza.task.TaskCallback) ImmutableSet(com.google.common.collect.ImmutableSet) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Clock(org.apache.samza.util.Clock) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) SamzaException(org.apache.samza.SamzaException) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Mockito.verify(org.mockito.Mockito.verify) Context(org.apache.samza.context.Context) TimestampedValueSerde(org.apache.samza.operators.impl.store.TimestampedValueSerde) Matchers.any(org.mockito.Matchers.any) List(java.util.List) SystemClock(org.apache.samza.util.SystemClock) TestClock(org.apache.samza.testUtils.TestClock) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) TestInMemoryStore(org.apache.samza.operators.impl.store.TestInMemoryStore) Config(org.apache.samza.config.Config) KVSerde(org.apache.samza.serializers.KVSerde) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) SystemClock(org.apache.samza.util.SystemClock) StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with TaskCallback

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

the class TestJoinOperator method joinRetainsLatestMessageForKeyReverse.

@Test
public void joinRetainsLatestMessageForKeyReverse() throws Exception {
    StreamApplicationDescriptorImpl streamAppDesc = this.getTestJoinStreamGraph(new TestJoinFunction());
    StreamOperatorTask sot = createStreamOperatorTask(new SystemClock(), streamAppDesc);
    List<Integer> output = new ArrayList<>();
    MessageCollector messageCollector = envelope -> output.add((Integer) envelope.getMessage());
    // push messages to second stream
    numbers.forEach(n -> sot.processAsync(new SecondStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    // push messages to second stream again with same keys but different values
    numbers.forEach(n -> sot.processAsync(new SecondStreamIME(n, 2 * n), messageCollector, taskCoordinator, taskCallback));
    // push messages to first stream with same key
    numbers.forEach(n -> sot.processAsync(new FirstStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
    int outputSum = output.stream().reduce(0, (s, m) -> s + m);
    // should use latest messages in the second stream
    assertEquals(165, outputSum);
}
Also used : StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) GenericSystemDescriptor(org.apache.samza.system.descriptors.GenericSystemDescriptor) TaskModel(org.apache.samza.job.model.TaskModel) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) GenericInputDescriptor(org.apache.samza.system.descriptors.GenericInputDescriptor) ArrayList(java.util.ArrayList) StreamTestUtils(org.apache.samza.testUtils.StreamTestUtils) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Matchers.eq(org.mockito.Matchers.eq) MockContext(org.apache.samza.context.MockContext) Duration(java.time.Duration) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) IntegerSerde(org.apache.samza.serializers.IntegerSerde) TaskCallback(org.apache.samza.task.TaskCallback) ImmutableSet(com.google.common.collect.ImmutableSet) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Clock(org.apache.samza.util.Clock) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) SamzaException(org.apache.samza.SamzaException) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Mockito.verify(org.mockito.Mockito.verify) Context(org.apache.samza.context.Context) TimestampedValueSerde(org.apache.samza.operators.impl.store.TimestampedValueSerde) Matchers.any(org.mockito.Matchers.any) List(java.util.List) SystemClock(org.apache.samza.util.SystemClock) TestClock(org.apache.samza.testUtils.TestClock) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) TestInMemoryStore(org.apache.samza.operators.impl.store.TestInMemoryStore) Config(org.apache.samza.config.Config) KVSerde(org.apache.samza.serializers.KVSerde) Assert.assertEquals(org.junit.Assert.assertEquals) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) Mockito.mock(org.mockito.Mockito.mock) SystemClock(org.apache.samza.util.SystemClock) StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) StreamOperatorTask(org.apache.samza.task.StreamOperatorTask) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

TaskCallback (org.apache.samza.task.TaskCallback)33 Test (org.junit.Test)32 HashMap (java.util.HashMap)28 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)26 ImmutableSet (com.google.common.collect.ImmutableSet)22 IOException (java.io.IOException)22 Duration (java.time.Duration)22 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Map (java.util.Map)22 Partition (org.apache.samza.Partition)22 StreamApplicationDescriptorImpl (org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl)22 Config (org.apache.samza.config.Config)22 MapConfig (org.apache.samza.config.MapConfig)22 Context (org.apache.samza.context.Context)22 MockContext (org.apache.samza.context.MockContext)22 TaskModel (org.apache.samza.job.model.TaskModel)22 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)22 TestInMemoryStore (org.apache.samza.operators.impl.store.TestInMemoryStore)22 IntegerSerde (org.apache.samza.serializers.IntegerSerde)22