Search in sources :

Example 1 with Server

use of com.datatorrent.bufferserver.server.Server in project apex-core by apache.

the class StramLocalCluster method run.

@Override
@SuppressWarnings({ "SleepWhileInLoop", "ResultOfObjectAllocationIgnored" })
public void run(long runMillis) {
    Thread eventLoopThread = null;
    List<Thread> containerThreads = new LinkedList<>();
    try {
        if (!perContainerBufferServer) {
            eventLoopThread = StreamingContainer.eventloop.start();
            bufferServer = new Server(StreamingContainer.eventloop, 0, 1024 * 1024, 8);
            try {
                bufferServer.setSpoolStorage(new DiskStorage());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            bufferServerAddress = InetSocketAddress.createUnresolved(LOCALHOST, bufferServer.run().getPort());
            LOG.info("Buffer server started: {}", bufferServerAddress);
        }
        long endMillis = System.currentTimeMillis() + runMillis;
        while (!appDone) {
            for (String containerIdStr : dnmgr.containerStopRequests.values()) {
                // teardown child thread
                StreamingContainer c = childContainers.get(containerIdStr);
                if (c != null) {
                    ContainerHeartbeatResponse r = new ContainerHeartbeatResponse();
                    r.shutdown = StreamingContainerUmbilicalProtocol.ShutdownType.ABORT;
                    c.processHeartbeatResponse(r);
                }
                dnmgr.containerStopRequests.remove(containerIdStr);
                LOG.info("Container {} restart.", containerIdStr);
                dnmgr.scheduleContainerRestart(containerIdStr);
            // dnmgr.removeContainerAgent(containerIdStr);
            }
            // start containers
            while (!dnmgr.containerStartRequests.isEmpty()) {
                ContainerStartRequest cdr = dnmgr.containerStartRequests.poll();
                if (cdr != null) {
                    new LocalStreamingContainerLauncher(cdr, containerThreads);
                }
            }
            if (heartbeatMonitoringEnabled) {
                // monitor child containers
                dnmgr.monitorHeartbeat(false);
            }
            if (childContainers.isEmpty() && dnmgr.containerStartRequests.isEmpty()) {
                appDone = true;
            }
            if (runMillis > 0 && System.currentTimeMillis() > endMillis) {
                appDone = true;
            }
            try {
                if (exitCondition != null && exitCondition.call()) {
                    LOG.info("Stopping on exit condition");
                    appDone = true;
                }
            } catch (Exception ex) {
                break;
            }
            if (Thread.interrupted()) {
                break;
            }
            if (!appDone) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    LOG.debug("Sleep interrupted", e);
                    break;
                }
            }
        }
    } finally {
        for (LocalStreamingContainer lsc : childContainers.values()) {
            injectShutdown.put(lsc.getContainerId(), lsc);
            lsc.triggerHeartbeat();
        }
        for (Thread thread : containerThreads) {
            try {
                thread.join(1000);
            } catch (InterruptedException e) {
                LOG.debug("Wait for {} to terminate interrupted", thread, e);
            }
            if (thread.isAlive()) {
                LOG.warn("Container thread {} is still alive", thread.getName());
            }
        }
        try {
            dnmgr.teardown();
        } catch (RuntimeException e) {
            LOG.warn("Exception during StreamingContainerManager teardown", e);
        }
        if (bufferServerAddress != null) {
            try {
                bufferServer.stop();
            } catch (RuntimeException e) {
                LOG.warn("Exception during BufferServer stop", e);
            }
        }
        if (eventLoopThread != null) {
            try {
                StreamingContainer.eventloop.stop();
                eventLoopThread.join(1000);
            } catch (InterruptedException ie) {
                LOG.debug("Wait for {} to terminate interrupted", eventLoopThread.getName(), ie);
            } catch (RuntimeException e) {
                LOG.warn("Exception during {} stop", StreamingContainer.eventloop, e);
            }
            if (StreamingContainer.eventloop.isActive()) {
                LOG.warn("Event loop {} is still active", StreamingContainer.eventloop);
            }
        }
    }
    LOG.info("Application finished.");
}
Also used : StreamingContainer(com.datatorrent.stram.engine.StreamingContainer) ContainerStartRequest(com.datatorrent.stram.StreamingContainerAgent.ContainerStartRequest) Server(com.datatorrent.bufferserver.server.Server) ContainerHeartbeatResponse(com.datatorrent.stram.api.StreamingContainerUmbilicalProtocol.ContainerHeartbeatResponse) IOException(java.io.IOException) LinkedList(java.util.LinkedList) IOException(java.io.IOException) DiskStorage(com.datatorrent.bufferserver.storage.DiskStorage)

Example 2 with Server

use of com.datatorrent.bufferserver.server.Server in project apex-core by apache.

the class SubscriberTest method setupServerAndClients.

@BeforeClass
public static void setupServerAndClients() throws Exception {
    try {
        eventloopServer = DefaultEventLoop.createEventLoop("server");
        eventloopClient = DefaultEventLoop.createEventLoop("client");
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    eventloopServer.start();
    eventloopClient.start();
    instance = new Server(eventloopServer, 0, 64, 2);
    address = instance.run();
    assertTrue(address instanceof InetSocketAddress);
    assertFalse(address.isUnresolved());
}
Also used : Server(com.datatorrent.bufferserver.server.Server) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) BeforeClass(org.testng.annotations.BeforeClass)

Example 3 with Server

use of com.datatorrent.bufferserver.server.Server in project apex-core by apache.

the class GenericNodeTest method testBufferServerSubscriberActivationBeforeOperator.

@Test
public void testBufferServerSubscriberActivationBeforeOperator() throws InterruptedException, IOException {
    final String streamName = "streamName";
    final String upstreamNodeId = "upstreamNodeId";
    final String downstreamNodeId = "downStreamNodeId";
    EventLoop eventloop = DefaultEventLoop.createEventLoop("StreamTestEventLoop");
    ((DefaultEventLoop) eventloop).start();
    // find random port
    final Server bufferServer = new Server(eventloop, 0);
    final int bufferServerPort = bufferServer.run().getPort();
    final StreamCodec<Object> serde = new DefaultStatefulStreamCodec<>();
    final BlockingQueue<Object> tuples = new ArrayBlockingQueue<>(10);
    GenericTestOperator go = new GenericTestOperator();
    final GenericNode gn = new GenericNode(go, new com.datatorrent.stram.engine.OperatorContext(0, "operator", new DefaultAttributeMap(), null));
    gn.setId(1);
    Sink<Object> output = new Sink<Object>() {

        @Override
        public void put(Object tuple) {
            tuples.add(tuple);
        }

        @Override
        public int getCount(boolean reset) {
            return 0;
        }
    };
    InetSocketAddress socketAddress = new InetSocketAddress("localhost", bufferServerPort);
    StreamContext issContext = new StreamContext(streamName);
    issContext.setSourceId(upstreamNodeId);
    issContext.setSinkId(downstreamNodeId);
    issContext.setFinishedWindowId(-1);
    issContext.setBufferServerAddress(socketAddress);
    issContext.put(StreamContext.CODEC, serde);
    issContext.put(StreamContext.EVENT_LOOP, eventloop);
    StreamContext ossContext = new StreamContext(streamName);
    ossContext.setSourceId(upstreamNodeId);
    ossContext.setSinkId(downstreamNodeId);
    ossContext.setBufferServerAddress(socketAddress);
    ossContext.put(StreamContext.CODEC, serde);
    ossContext.put(StreamContext.EVENT_LOOP, eventloop);
    BufferServerPublisher oss = new BufferServerPublisher(upstreamNodeId, 1024);
    oss.setup(ossContext);
    oss.activate(ossContext);
    oss.put(new Tuple(MessageType.BEGIN_WINDOW, 0x1L));
    byte[] buff = PayloadTuple.getSerializedTuple(0, 1);
    buff[buff.length - 1] = (byte) 1;
    oss.put(buff);
    oss.put(new EndWindowTuple(0x1L));
    oss.put(new Tuple(MessageType.BEGIN_WINDOW, 0x2L));
    buff = PayloadTuple.getSerializedTuple(0, 1);
    buff[buff.length - 1] = (byte) 2;
    oss.put(buff);
    oss.put(new EndWindowTuple(0x2L));
    oss.put(new Tuple(MessageType.BEGIN_WINDOW, 0x3L));
    buff = PayloadTuple.getSerializedTuple(0, 1);
    buff[buff.length - 1] = (byte) 3;
    oss.put(buff);
    oss.put(new EndWindowTuple(0x3L));
    oss.put(new EndStreamTuple(0L));
    BufferServerSubscriber iss = new BufferServerSubscriber(downstreamNodeId, 1024);
    iss.setup(issContext);
    gn.connectInputPort(GenericTestOperator.IPORT1, iss.acquireReservoir("testReservoir", 10));
    gn.connectOutputPort(GenericTestOperator.OPORT1, output);
    SweepableReservoir tupleWait = iss.acquireReservoir("testReservoir2", 10);
    iss.activate(issContext);
    while (tupleWait.sweep() == null) {
        Thread.sleep(100);
    }
    gn.firstWindowMillis = 0;
    gn.windowWidthMillis = 100;
    Thread t = new Thread() {

        @Override
        public void run() {
            gn.activate();
            gn.run();
            gn.deactivate();
        }
    };
    t.start();
    t.join();
    Assert.assertEquals(10, tuples.size());
    List<Object> list = new ArrayList<>(tuples);
    Assert.assertEquals("Payload Tuple 1", 1, ((byte[]) list.get(1))[5]);
    Assert.assertEquals("Payload Tuple 2", 2, ((byte[]) list.get(4))[5]);
    Assert.assertEquals("Payload Tuple 3", 3, ((byte[]) list.get(7))[5]);
    if (bufferServer != null) {
        bufferServer.stop();
    }
    ((DefaultEventLoop) eventloop).stop();
}
Also used : Server(com.datatorrent.bufferserver.server.Server) InetSocketAddress(java.net.InetSocketAddress) BufferServerSubscriber(com.datatorrent.stram.stream.BufferServerSubscriber) ArrayList(java.util.ArrayList) DefaultAttributeMap(com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap) BufferServerPublisher(com.datatorrent.stram.stream.BufferServerPublisher) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Sink(com.datatorrent.api.Sink) EndWindowTuple(com.datatorrent.stram.tuple.EndWindowTuple) EndStreamTuple(com.datatorrent.stram.tuple.EndStreamTuple) DefaultEventLoop(com.datatorrent.netlet.DefaultEventLoop) Checkpoint(com.datatorrent.stram.api.Checkpoint) EventLoop(com.datatorrent.netlet.EventLoop) DefaultEventLoop(com.datatorrent.netlet.DefaultEventLoop) DefaultStatefulStreamCodec(com.datatorrent.stram.codec.DefaultStatefulStreamCodec) EndStreamTuple(com.datatorrent.stram.tuple.EndStreamTuple) EndWindowTuple(com.datatorrent.stram.tuple.EndWindowTuple) Tuple(com.datatorrent.stram.tuple.Tuple) CustomControlTuple(com.datatorrent.stram.tuple.CustomControlTuple) PayloadTuple(com.datatorrent.bufferserver.packet.PayloadTuple) CustomControlTupleTest(com.datatorrent.stram.CustomControlTupleTest) Test(org.junit.Test)

Example 4 with Server

use of com.datatorrent.bufferserver.server.Server in project apex-core by apache.

the class FastStreamTest method setup.

@BeforeClass
public static void setup() throws InterruptedException, IOException, Exception {
    ((DefaultEventLoop) eventloop).start();
    // find random port
    bufferServer = new Server(eventloop, 0);
    InetSocketAddress bindAddr = bufferServer.run();
    bufferServerPort = bindAddr.getPort();
}
Also used : Server(com.datatorrent.bufferserver.server.Server) InetSocketAddress(java.net.InetSocketAddress) DefaultEventLoop(com.datatorrent.netlet.DefaultEventLoop) BeforeClass(org.junit.BeforeClass)

Example 5 with Server

use of com.datatorrent.bufferserver.server.Server in project apex-core by apache.

the class SocketStreamTest method setup.

@BeforeClass
public static void setup() throws InterruptedException, IOException, Exception {
    ((DefaultEventLoop) eventloop).start();
    // find random port
    bufferServer = new Server(eventloop, 0);
    InetSocketAddress bindAddr = bufferServer.run();
    bufferServerPort = bindAddr.getPort();
}
Also used : Server(com.datatorrent.bufferserver.server.Server) InetSocketAddress(java.net.InetSocketAddress) DefaultEventLoop(com.datatorrent.netlet.DefaultEventLoop) BeforeClass(org.junit.BeforeClass)

Aggregations

Server (com.datatorrent.bufferserver.server.Server)7 InetSocketAddress (java.net.InetSocketAddress)4 DefaultEventLoop (com.datatorrent.netlet.DefaultEventLoop)3 IOException (java.io.IOException)3 DiskStorage (com.datatorrent.bufferserver.storage.DiskStorage)2 Checkpoint (com.datatorrent.stram.api.Checkpoint)2 BeforeClass (org.junit.BeforeClass)2 BeforeClass (org.testng.annotations.BeforeClass)2 DefaultAttributeMap (com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap)1 Component (com.datatorrent.api.Component)1 Sink (com.datatorrent.api.Sink)1 StringCodec (com.datatorrent.api.StringCodec)1 PayloadTuple (com.datatorrent.bufferserver.packet.PayloadTuple)1 Controller (com.datatorrent.bufferserver.support.Controller)1 Publisher (com.datatorrent.bufferserver.support.Publisher)1 Subscriber (com.datatorrent.bufferserver.support.Subscriber)1 EventLoop (com.datatorrent.netlet.EventLoop)1 CustomControlTupleTest (com.datatorrent.stram.CustomControlTupleTest)1 ContainerStartRequest (com.datatorrent.stram.StreamingContainerAgent.ContainerStartRequest)1 ContainerContext (com.datatorrent.stram.api.ContainerContext)1