use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestJoinOperator method joinRemovesExpiredMessages.
@Test
public void joinRemovesExpiredMessages() throws Exception {
TestClock testClock = new TestClock();
StreamApplicationDescriptorImpl streamAppDesc = this.getTestJoinStreamGraph(new TestJoinFunction());
StreamOperatorTask sot = createStreamOperatorTask(testClock, 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));
// 1 minute after ttl
testClock.advanceTime(JOIN_TTL.plus(Duration.ofMinutes(1)));
// should expire first stream messages
sot.window(messageCollector, taskCoordinator);
// push messages to second stream with same key
numbers.forEach(n -> sot.processAsync(new SecondStreamIME(n, n), messageCollector, taskCoordinator, taskCallback));
assertTrue(output.isEmpty());
}
use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestMessageStreamImpl method testMerge.
@Test
public void testMerge() {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec mockOpSpec1 = mock(OperatorSpec.class);
MessageStream<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec1);
// other streams have the same message type T as input stream message type M
OperatorSpec mockOpSpec2 = mock(OperatorSpec.class);
OperatorSpec mockOpSpec3 = mock(OperatorSpec.class);
Collection<MessageStream<TestMessageEnvelope>> otherStreams1 = ImmutableList.of(new MessageStreamImpl<>(mockGraph, mockOpSpec2), new MessageStreamImpl<>(mockGraph, mockOpSpec3));
inputStream.merge(otherStreams1);
ArgumentCaptor<OperatorSpec> registeredOpCaptor1 = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec1).registerNextOperatorSpec(registeredOpCaptor1.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec1 = registeredOpCaptor1.getValue();
assertTrue(registeredOpSpec1 instanceof StreamOperatorSpec);
FlatMapFunction transformFn = ((StreamOperatorSpec) registeredOpSpec1).getTransformFn();
ArgumentCaptor<OperatorSpec> registeredOpCaptor2 = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec2).registerNextOperatorSpec(registeredOpCaptor2.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec2 = registeredOpCaptor2.getValue();
ArgumentCaptor<OperatorSpec> registeredOpCaptor3 = ArgumentCaptor.forClass(OperatorSpec.class);
verify(mockOpSpec3).registerNextOperatorSpec(registeredOpCaptor3.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec3 = registeredOpCaptor3.getValue();
assertEquals(registeredOpSpec1, registeredOpSpec2);
assertEquals(registeredOpSpec2, registeredOpSpec3);
assertEquals(OpCode.MERGE, registeredOpSpec1.getOpCode());
assertNotNull(transformFn);
TestMessageEnvelope mockInput = mock(TestMessageEnvelope.class);
assertTrue(transformFn.apply(mockInput).contains(mockInput));
assertEquals(1, transformFn.apply(mockInput).size());
}
use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestMessageStreamImpl method testSendToTable.
@Test
public void testSendToTable() {
StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
OperatorSpec inputOpSpec = mock(OperatorSpec.class);
MessageStreamImpl<TestMessageEnvelope> source = new MessageStreamImpl<>(mockGraph, inputOpSpec);
TableImpl table = new TableImpl("t1");
source.sendTo(table);
ArgumentCaptor<OperatorSpec> registeredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
verify(inputOpSpec).registerNextOperatorSpec(registeredOpCaptor.capture());
OperatorSpec<?, TestMessageEnvelope> registeredOpSpec = registeredOpCaptor.getValue();
assertTrue(registeredOpSpec instanceof SendToTableOperatorSpec);
SendToTableOperatorSpec sendToTableOperatorSpec = (SendToTableOperatorSpec) registeredOpSpec;
assertEquals(OpCode.SEND_TO, sendToTableOperatorSpec.getOpCode());
assertEquals(table.getTableId(), sendToTableOperatorSpec.getTableId());
}
use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestOperatorImplGraph method testLinearChain.
@Test
public void testLinearChain() {
String inputStreamId = "input";
String inputSystem = "input-system";
String inputPhysicalName = "input-stream";
String outputStreamId = "output";
String outputSystem = "output-system";
String outputPhysicalName = "output-stream";
String intermediateSystem = "intermediate-system";
HashMap<String, String> configs = new HashMap<>();
configs.put(JobConfig.JOB_NAME, "jobName");
configs.put(JobConfig.JOB_ID, "jobId");
configs.put(JobConfig.JOB_DEFAULT_SYSTEM, intermediateSystem);
StreamTestUtils.addStreamConfigs(configs, inputStreamId, inputSystem, inputPhysicalName);
StreamTestUtils.addStreamConfigs(configs, outputStreamId, outputSystem, outputPhysicalName);
Config config = new MapConfig(configs);
when(this.context.getJobContext().getConfig()).thenReturn(config);
StreamApplicationDescriptorImpl graphSpec = new StreamApplicationDescriptorImpl(appDesc -> {
GenericSystemDescriptor sd = new GenericSystemDescriptor(inputSystem, "mockFactoryClass");
GenericInputDescriptor inputDescriptor = sd.getInputDescriptor(inputStreamId, mock(Serde.class));
GenericOutputDescriptor outputDescriptor = sd.getOutputDescriptor(outputStreamId, mock(Serde.class));
MessageStream<Object> inputStream = appDesc.getInputStream(inputDescriptor);
OutputStream<Object> outputStream = appDesc.getOutputStream(outputDescriptor);
inputStream.filter(mock(FilterFunction.class)).map(mock(MapFunction.class)).sendTo(outputStream);
}, config);
OperatorImplGraph opImplGraph = new OperatorImplGraph(graphSpec.getOperatorSpecGraph(), this.context, mock(Clock.class));
InputOperatorImpl inputOpImpl = opImplGraph.getInputOperator(new SystemStream(inputSystem, inputPhysicalName));
assertEquals(1, inputOpImpl.registeredOperators.size());
OperatorImpl filterOpImpl = (FlatmapOperatorImpl) inputOpImpl.registeredOperators.iterator().next();
assertEquals(1, filterOpImpl.registeredOperators.size());
assertEquals(OpCode.FILTER, filterOpImpl.getOperatorSpec().getOpCode());
OperatorImpl mapOpImpl = (FlatmapOperatorImpl) filterOpImpl.registeredOperators.iterator().next();
assertEquals(1, mapOpImpl.registeredOperators.size());
assertEquals(OpCode.MAP, mapOpImpl.getOperatorSpec().getOpCode());
OperatorImpl sendToOpImpl = (OutputOperatorImpl) mapOpImpl.registeredOperators.iterator().next();
assertEquals(0, sendToOpImpl.registeredOperators.size());
assertEquals(OpCode.SEND_TO, sendToOpImpl.getOperatorSpec().getOpCode());
}
use of org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl in project samza by apache.
the class TestOperatorImplGraph method testOperatorGraphInitAndClose.
@Test
public void testOperatorGraphInitAndClose() {
String inputStreamId1 = "input1";
String inputStreamId2 = "input2";
String inputSystem = "input-system";
TaskName mockTaskName = mock(TaskName.class);
TaskModel taskModel = mock(TaskModel.class);
when(taskModel.getTaskName()).thenReturn(mockTaskName);
when(this.context.getTaskContext().getTaskModel()).thenReturn(taskModel);
StreamApplicationDescriptorImpl graphSpec = new StreamApplicationDescriptorImpl(appDesc -> {
GenericSystemDescriptor sd = new GenericSystemDescriptor(inputSystem, "mockFactoryClass");
GenericInputDescriptor inputDescriptor1 = sd.getInputDescriptor(inputStreamId1, mock(Serde.class));
GenericInputDescriptor inputDescriptor2 = sd.getInputDescriptor(inputStreamId2, mock(Serde.class));
MessageStream<Object> inputStream1 = appDesc.getInputStream(inputDescriptor1);
MessageStream<Object> inputStream2 = appDesc.getInputStream(inputDescriptor2);
Function mapFn = (Function & Serializable) m -> m;
inputStream1.map(new TestMapFunction<Object, Object>("1", mapFn)).map(new TestMapFunction<Object, Object>("2", mapFn));
inputStream2.map(new TestMapFunction<Object, Object>("3", mapFn)).map(new TestMapFunction<Object, Object>("4", mapFn));
}, getConfig());
OperatorImplGraph opImplGraph = new OperatorImplGraph(graphSpec.getOperatorSpecGraph(), this.context, SystemClock.instance());
List<String> initializedOperators = BaseTestFunction.getInitListByTaskName(mockTaskName);
// Assert that initialization occurs in topological order.
assertEquals(initializedOperators.get(0), "1");
assertEquals(initializedOperators.get(1), "2");
assertEquals(initializedOperators.get(2), "3");
assertEquals(initializedOperators.get(3), "4");
// Assert that finalization occurs in reverse topological order.
opImplGraph.close();
List<String> closedOperators = BaseTestFunction.getCloseListByTaskName(mockTaskName);
assertEquals(closedOperators.get(0), "4");
assertEquals(closedOperators.get(1), "3");
assertEquals(closedOperators.get(2), "2");
assertEquals(closedOperators.get(3), "1");
}
Aggregations