use of com.datatorrent.stram.stream.BufferServerPublisher 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();
}
use of com.datatorrent.stram.stream.BufferServerPublisher in project apex-core by apache.
the class StreamingContainer method deployBufferServerPublisher.
private HashMap.SimpleEntry<String, ComponentContextPair<Stream, StreamContext>> deployBufferServerPublisher(String connIdentifier, StreamCodec<?> streamCodec, long finishedWindowId, int queueCapacity, OperatorDeployInfo.OutputDeployInfo nodi) throws UnknownHostException {
String sinkIdentifier = "tcp://".concat(nodi.bufferServerHost).concat(":").concat(String.valueOf(nodi.bufferServerPort)).concat("/").concat(connIdentifier);
StreamContext bssc = new StreamContext(nodi.declaredStreamId);
bssc.setPortId(nodi.portName);
bssc.setSourceId(connIdentifier);
bssc.setSinkId(sinkIdentifier);
bssc.setFinishedWindowId(finishedWindowId);
bssc.put(StreamContext.CODEC, streamCodec);
bssc.put(StreamContext.EVENT_LOOP, eventloop);
bssc.setBufferServerAddress(InetSocketAddress.createUnresolved(nodi.bufferServerHost, nodi.bufferServerPort));
bssc.put(StreamContext.BUFFER_SERVER_TOKEN, nodi.bufferServerToken);
InetAddress inetAddress = bssc.getBufferServerAddress().getAddress();
if (inetAddress != null && NetUtils.isLocalAddress(inetAddress)) {
bssc.setBufferServerAddress(new InetSocketAddress(InetAddress.getByName(null), nodi.bufferServerPort));
}
Stream publisher = fastPublisherSubscriber ? new FastPublisher(connIdentifier, queueCapacity * 256) : new BufferServerPublisher(connIdentifier, queueCapacity);
return new HashMap.SimpleEntry<>(sinkIdentifier, new ComponentContextPair<>(publisher, bssc));
}
Aggregations