Search in sources :

Example 1 with StreamContext

use of com.datatorrent.stram.engine.StreamContext in project apex-core by apache.

the class SocketStreamTest method init.

@Before
public void init() {
    final StreamCodec<Object> serde = new DefaultStatefulStreamCodec<>();
    messageCount = new AtomicInteger(0);
    Sink<Object> sink = new Sink<Object>() {

        @Override
        public void put(Object tuple) {
            logger.debug("received: " + tuple);
            messageCount.incrementAndGet();
        }

        @Override
        public int getCount(boolean reset) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
    issContext = new StreamContext(streamName);
    issContext.setSourceId(upstreamNodeId);
    issContext.setSinkId(downstreamNodeId);
    issContext.setFinishedWindowId(-1);
    issContext.setBufferServerAddress(InetSocketAddress.createUnresolved("localhost", bufferServerPort));
    issContext.put(StreamContext.CODEC, serde);
    issContext.put(StreamContext.EVENT_LOOP, eventloop);
    iss = new BufferServerSubscriber(downstreamNodeId, 1024);
    iss.setup(issContext);
    reservoir = iss.acquireReservoir("testReservoir", 1);
    reservoir.setSink(sink);
    ossContext = new StreamContext(streamName);
    ossContext.setSourceId(upstreamNodeId);
    ossContext.setSinkId(downstreamNodeId);
    ossContext.setBufferServerAddress(InetSocketAddress.createUnresolved("localhost", bufferServerPort));
    ossContext.put(StreamContext.CODEC, serde);
    ossContext.put(StreamContext.EVENT_LOOP, eventloop);
    oss = new BufferServerPublisher(upstreamNodeId, 1024);
    oss.setup(ossContext);
}
Also used : Sink(com.datatorrent.api.Sink) DefaultStatefulStreamCodec(com.datatorrent.stram.codec.DefaultStatefulStreamCodec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamContext(com.datatorrent.stram.engine.StreamContext) Before(org.junit.Before)

Example 2 with StreamContext

use of com.datatorrent.stram.engine.StreamContext in project apex-core by apache.

the class FastStreamTest method testBufferServerStream.

/**
   * Test buffer server stream by sending
   * tuple on outputstream and receive same tuple from inputstream
   *
   * @throws Exception
   */
@Test
@SuppressWarnings({ "SleepWhileInLoop" })
public void testBufferServerStream() throws Exception {
    final StreamCodec<Object> serde = new DefaultStatefulStreamCodec<>();
    final AtomicInteger messageCount = new AtomicInteger();
    Sink<Object> sink = new Sink<Object>() {

        @Override
        public void put(Object tuple) {
            logger.debug("received: " + tuple);
            messageCount.incrementAndGet();
        }

        @Override
        public int getCount(boolean reset) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
    String streamName = "streamName";
    String upstreamNodeId = "upstreamNodeId";
    String downstreamNodeId = "downStreamNodeId";
    StreamContext issContext = new StreamContext(streamName);
    issContext.setSourceId(upstreamNodeId);
    issContext.setSinkId(downstreamNodeId);
    issContext.setFinishedWindowId(-1);
    issContext.setBufferServerAddress(InetSocketAddress.createUnresolved("localhost", bufferServerPort));
    issContext.put(StreamContext.CODEC, serde);
    issContext.put(StreamContext.EVENT_LOOP, eventloop);
    FastSubscriber subscriber = new FastSubscriber(downstreamNodeId, 1024);
    subscriber.setup(issContext);
    SweepableReservoir reservoir = subscriber.acquireReservoir("testReservoir", 1);
    reservoir.setSink(sink);
    StreamContext ossContext = new StreamContext(streamName);
    ossContext.setSourceId(upstreamNodeId);
    ossContext.setSinkId(downstreamNodeId);
    ossContext.setFinishedWindowId(-1);
    ossContext.setBufferServerAddress(InetSocketAddress.createUnresolved("localhost", bufferServerPort));
    ossContext.put(StreamContext.CODEC, serde);
    ossContext.put(StreamContext.EVENT_LOOP, eventloop);
    FastPublisher publisher = new FastPublisher(upstreamNodeId, 8);
    StreamContext publisherContext = new StreamContext(streamName);
    publisherContext.setSourceId(upstreamNodeId);
    publisherContext.setSinkId(downstreamNodeId);
    publisherContext.setBufferServerAddress(InetSocketAddress.createUnresolved("localhost", bufferServerPort));
    publisherContext.put(StreamContext.CODEC, serde);
    publisherContext.put(StreamContext.EVENT_LOOP, eventloop);
    publisher.setup(publisherContext);
    subscriber.activate(issContext);
    LOG.debug("input stream activated");
    publisher.activate(publisherContext);
    LOG.debug("output stream activated");
    LOG.debug("Sending hello message");
    publisher.put(StramTestSupport.generateBeginWindowTuple(upstreamNodeId, 0));
    publisher.put(StramTestSupport.generateTuple("hello", 0));
    publisher.put(StramTestSupport.generateEndWindowTuple(upstreamNodeId, 0));
    // it's a spurious tuple, presence of it should not affect the outcome of the test.
    publisher.put(StramTestSupport.generateBeginWindowTuple(upstreamNodeId, 1));
    for (int i = 0; i < 100; i++) {
        Tuple t = reservoir.sweep();
        if (t == null) {
            sleep(5);
            continue;
        }
        reservoir.remove();
        if (t instanceof EndWindowTuple) {
            break;
        }
    }
    eventloop.disconnect(publisher);
    eventloop.disconnect(subscriber);
    Assert.assertEquals("Received messages", 1, messageCount.get());
}
Also used : SweepableReservoir(com.datatorrent.stram.engine.SweepableReservoir) StreamContext(com.datatorrent.stram.engine.StreamContext) Sink(com.datatorrent.api.Sink) EndWindowTuple(com.datatorrent.stram.tuple.EndWindowTuple) DefaultStatefulStreamCodec(com.datatorrent.stram.codec.DefaultStatefulStreamCodec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EndWindowTuple(com.datatorrent.stram.tuple.EndWindowTuple) Tuple(com.datatorrent.stram.tuple.Tuple) Test(org.junit.Test)

Example 3 with StreamContext

use of com.datatorrent.stram.engine.StreamContext 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)3 StreamContext (com.datatorrent.stram.engine.StreamContext)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 DefaultStatefulStreamCodec (com.datatorrent.stram.codec.DefaultStatefulStreamCodec)2 SweepableReservoir (com.datatorrent.stram.engine.SweepableReservoir)2 Tuple (com.datatorrent.stram.tuple.Tuple)2 Test (org.junit.Test)2 DefaultAttributeMap (com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap)1 AbstractReservoir (com.datatorrent.stram.engine.AbstractReservoir)1 GenericNode (com.datatorrent.stram.engine.GenericNode)1 Node (com.datatorrent.stram.engine.Node)1 OperatorContext (com.datatorrent.stram.engine.OperatorContext)1 WaitCondition (com.datatorrent.stram.support.StramTestSupport.WaitCondition)1 EndWindowTuple (com.datatorrent.stram.tuple.EndWindowTuple)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Before (org.junit.Before)1