use of org.apache.samza.operators.MessageStreamImpl in project samza by apache.
the class ExecutionPlanner method findReachableJoins.
/**
* This function traverses the StreamGraph to find and update mappings for all Joins reachable from this input StreamEdge
* @param inputMessageStream next input MessageStream to traverse {@link MessageStream}
* @param sourceStreamEdge source {@link StreamEdge}
* @param joinSpecToStreamEdges mapping from join spec to its source {@link StreamEdge}s
* @param streamEdgeToJoinSpecs mapping from source {@link StreamEdge} to the join specs that consumes it
* @param outputStreamToJoinSpec mapping from the output stream to the join spec
* @param joinQ queue that contains joinSpecs where at least one of the input stream edge partitions is known.
*/
private static void findReachableJoins(MessageStream inputMessageStream, StreamEdge sourceStreamEdge, Multimap<OperatorSpec, StreamEdge> joinSpecToStreamEdges, Multimap<StreamEdge, OperatorSpec> streamEdgeToJoinSpecs, Map<MessageStream, OperatorSpec> outputStreamToJoinSpec, Queue<OperatorSpec> joinQ, Set<OperatorSpec> visited) {
Collection<OperatorSpec> specs = ((MessageStreamImpl) inputMessageStream).getRegisteredOperatorSpecs();
for (OperatorSpec spec : specs) {
if (spec instanceof PartialJoinOperatorSpec) {
// every join will have two partial join operators
// we will choose one of them in order to consolidate the inputs
// the first one who registered with the outputStreamToJoinSpec will win
MessageStream output = spec.getNextStream();
OperatorSpec joinSpec = outputStreamToJoinSpec.get(output);
if (joinSpec == null) {
joinSpec = spec;
outputStreamToJoinSpec.put(output, joinSpec);
}
joinSpecToStreamEdges.put(joinSpec, sourceStreamEdge);
streamEdgeToJoinSpecs.put(sourceStreamEdge, joinSpec);
if (!visited.contains(joinSpec) && sourceStreamEdge.getPartitionCount() > 0) {
// put the joins with known input partitions into the queue
joinQ.add(joinSpec);
visited.add(joinSpec);
}
}
if (spec.getNextStream() != null) {
findReachableJoins(spec.getNextStream(), sourceStreamEdge, joinSpecToStreamEdges, streamEdgeToJoinSpecs, outputStreamToJoinSpec, joinQ, visited);
}
}
}
use of org.apache.samza.operators.MessageStreamImpl in project samza by apache.
the class TestOperatorSpecs method testCreateMergeOperator.
@Test
public void testCreateMergeOperator() {
StreamGraphImpl mockGraph = mock(StreamGraphImpl.class);
MessageStreamImpl<TestMessageEnvelope> output = TestMessageStreamImplUtil.<TestMessageEnvelope>getMessageStreamImpl(mockGraph);
StreamOperatorSpec<TestMessageEnvelope, TestMessageEnvelope> mergeOp = OperatorSpecs.createMergeOperatorSpec(output, 1);
Function<TestMessageEnvelope, Collection<TestMessageEnvelope>> mergeFn = t -> new ArrayList<TestMessageEnvelope>() {
{
this.add(t);
}
};
TestMessageEnvelope t = mock(TestMessageEnvelope.class);
assertEquals(mergeOp.getTransformFn().apply(t), mergeFn.apply(t));
assertEquals(mergeOp.getNextStream(), output);
}
use of org.apache.samza.operators.MessageStreamImpl 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));
}
use of org.apache.samza.operators.MessageStreamImpl 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);
}
use of org.apache.samza.operators.MessageStreamImpl 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);
}
Aggregations