Search in sources :

Example 21 with Sink

use of com.datatorrent.api.Sink in project apex-core by apache.

the class Node method reportStats.

protected void reportStats(ContainerStats.OperatorStats stats, long windowId) {
    stats.outputPorts = new ArrayList<>();
    for (Entry<String, Sink<Object>> e : outputs.entrySet()) {
        ContainerStats.OperatorStats.PortStats portStats = new ContainerStats.OperatorStats.PortStats(e.getKey());
        portStats.tupleCount = e.getValue().getCount(true) - controlTupleCount;
        portStats.endWindowTimestamp = endWindowEmitTime;
        stats.outputPorts.add(portStats);
    }
    controlTupleCount = 0;
    long currentCpuTime = tmb.getCurrentThreadCpuTime();
    stats.cpuTimeUsed = currentCpuTime - lastSampleCpuTime;
    lastSampleCpuTime = currentCpuTime;
    if (checkpoint != null) {
        stats.checkpoint = checkpoint;
        stats.checkpointStats = checkpointStats;
        checkpointStats = null;
        checkpoint = null;
    } else {
        Pair<FutureTask<Stats.CheckpointStats>, CheckpointWindowInfo> pair = taskQueue.peek();
        if (pair != null && pair.getFirst().isDone()) {
            taskQueue.poll();
            try {
                CheckpointWindowInfo checkpointWindowInfo = pair.getSecond();
                stats.checkpointStats = pair.getFirst().get();
                stats.checkpoint = new Checkpoint(checkpointWindowInfo.windowId, checkpointWindowInfo.applicationWindowCount, checkpointWindowInfo.checkpointWindowCount);
                if (operator instanceof Operator.CheckpointListener) {
                    ((Operator.CheckpointListener) operator).checkpointed(checkpointWindowInfo.windowId);
                }
            } catch (Exception ex) {
                throw Throwables.propagate(ex);
            }
        }
    }
    context.report(stats, windowId);
}
Also used : ContainerStats(com.datatorrent.stram.api.StreamingContainerUmbilicalProtocol.ContainerStats) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) Checkpoint(com.datatorrent.stram.api.Checkpoint) MuxSink(com.datatorrent.stram.debug.MuxSink) Sink(com.datatorrent.api.Sink) FutureTask(java.util.concurrent.FutureTask) Stats(com.datatorrent.api.Stats) ContainerStats(com.datatorrent.stram.api.StreamingContainerUmbilicalProtocol.ContainerStats)

Example 22 with Sink

use of com.datatorrent.api.Sink in project apex-core by apache.

the class InlineStreamTest method test.

@Test
@SuppressWarnings("SleepWhileInLoop")
public void test() throws Exception {
    final int totalTupleCount = 5000;
    final PassThroughNode<Object> operator1 = new PassThroughNode<>();
    final GenericNode node1 = new GenericNode(operator1, new OperatorContext(1, "operator1", new DefaultAttributeMap(), null));
    node1.setId(1);
    operator1.setup(node1.context);
    final PassThroughNode<Object> operator2 = new PassThroughNode<>();
    final GenericNode node2 = new GenericNode(operator2, new OperatorContext(2, "operator2", new DefaultAttributeMap(), null));
    node2.setId(2);
    operator2.setup(node2.context);
    StreamContext streamContext = new StreamContext("node1->node2");
    final InlineStream stream = new InlineStream(1024);
    stream.setup(streamContext);
    node1.connectOutputPort("output", stream);
    node2.connectInputPort("input", stream.getReservoir());
    prev = null;
    Sink<Object> sink = new Sink<Object>() {

        @Override
        public void put(Object payload) {
            if (payload instanceof Tuple) {
                return;
            }
            if (prev == null) {
                prev = payload;
            } else {
                if (Integer.valueOf(payload.toString()) - Integer.valueOf(prev.toString()) != 1) {
                    synchronized (InlineStreamTest.this) {
                        InlineStreamTest.this.notify();
                    }
                }
                prev = payload;
            }
            if (Integer.valueOf(prev.toString()) == totalTupleCount - 1) {
                synchronized (InlineStreamTest.this) {
                    InlineStreamTest.this.notify();
                }
            }
        }

        @Override
        public int getCount(boolean reset) {
            return 0;
        }
    };
    node2.connectOutputPort("output", sink);
    AbstractReservoir reservoir1 = AbstractReservoir.newReservoir("input", 1024 * 5);
    node1.connectInputPort("input", reservoir1);
    Map<Integer, Node<?>> activeNodes = new ConcurrentHashMap<>();
    launchNodeThread(node1, activeNodes);
    launchNodeThread(node2, activeNodes);
    stream.activate(streamContext);
    reservoir1.put(StramTestSupport.generateBeginWindowTuple("irrelevant", 0));
    for (int i = 0; i < totalTupleCount; i++) {
        reservoir1.put(i);
    }
    reservoir1.put(StramTestSupport.generateEndWindowTuple("irrelevant", 0));
    synchronized (this) {
        this.wait(200);
    }
    Assert.assertNotNull(prev);
    Assert.assertEquals("processing complete", totalTupleCount, Integer.valueOf(prev.toString()) + 1);
    Assert.assertEquals("active operators", 2, activeNodes.size());
    WaitCondition c = new WaitCondition() {

        @Override
        public boolean isComplete() {
            final SweepableReservoir reservoir = stream.getReservoir();
            logger.debug("stream {} empty {}, size {}", stream, reservoir.isEmpty(), reservoir.size(false));
            return reservoir.isEmpty();
        }
    };
    Assert.assertTrue("operator should finish processing all events within 1 second", StramTestSupport.awaitCompletion(c, 1000));
    stream.deactivate();
    for (Node<?> node : activeNodes.values()) {
        node.shutdown();
    }
    for (int i = 0; i < 10; i++) {
        Thread.sleep(20);
        if (activeNodes.isEmpty()) {
            break;
        }
    }
    stream.teardown();
    operator2.teardown();
    operator1.teardown();
    Assert.assertEquals("active operators", 0, activeNodes.size());
}
Also used : WaitCondition(com.datatorrent.stram.support.StramTestSupport.WaitCondition) AbstractReservoir(com.datatorrent.stram.engine.AbstractReservoir) SweepableReservoir(com.datatorrent.stram.engine.SweepableReservoir) StreamContext(com.datatorrent.stram.engine.StreamContext) Node(com.datatorrent.stram.engine.Node) GenericNode(com.datatorrent.stram.engine.GenericNode) GenericNode(com.datatorrent.stram.engine.GenericNode) DefaultAttributeMap(com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Sink(com.datatorrent.api.Sink) OperatorContext(com.datatorrent.stram.engine.OperatorContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Tuple(com.datatorrent.stram.tuple.Tuple) Test(org.junit.Test)

Aggregations

Sink (com.datatorrent.api.Sink)22 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)8 Tuple (com.datatorrent.stram.tuple.Tuple)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 DefaultAttributeMap (com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap)4 CustomControlTuple (com.datatorrent.stram.tuple.CustomControlTuple)4 EndWindowTuple (com.datatorrent.stram.tuple.EndWindowTuple)4 ControlTupleEnabledSink (com.datatorrent.api.ControlTupleEnabledSink)3 PayloadTuple (com.datatorrent.bufferserver.packet.PayloadTuple)3 CustomControlTupleTest (com.datatorrent.stram.CustomControlTupleTest)3 Checkpoint (com.datatorrent.stram.api.Checkpoint)3 DefaultStatefulStreamCodec (com.datatorrent.stram.codec.DefaultStatefulStreamCodec)3 MuxSink (com.datatorrent.stram.debug.MuxSink)3 StreamContext (com.datatorrent.stram.engine.StreamContext)3 SweepableReservoir (com.datatorrent.stram.engine.SweepableReservoir)3 EndStreamTuple (com.datatorrent.stram.tuple.EndStreamTuple)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 OutputPort (com.datatorrent.api.Operator.OutputPort)2