Search in sources :

Example 1 with NettyTextMessageOutputReceiver

use of edu.snu.mist.common.stream.textmessage.NettyTextMessageOutputReceiver in project mist by snuspl.

the class NettyTextSinkTest method testNettyTextSink.

/**
 * Test whether the created sinks by NettyTextSinkFactory send data stream correctly to output receiver.
 * It creates 4 sinks and send outputs to output receiver.
 * @throws Exception
 */
@Test(timeout = 4000L)
public void testNettyTextSink() throws Exception {
    final int numSinks = 4;
    final List<String> outputStream = Arrays.asList("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "In in leo nec erat fringilla mattis eu non massa.", "Cras quis diam suscipit, commodo enim id, pulvinar nunc.");
    final CountDownLatch countDownLatch = new CountDownLatch(numSinks * outputStream.size());
    final CountDownLatch channelCountDown = new CountDownLatch(numSinks);
    final Map<Channel, List<String>> channelListMap = new ConcurrentHashMap<>();
    final NettyChannelHandler channelHandler = new TestReceiverChannelHandler(channelCountDown, countDownLatch, channelListMap);
    // Create output receiver
    try (final NettyTextMessageOutputReceiver outputReceiver = new NettyTextMessageOutputReceiver(SERVER_ADDR, SERVER_PORT, channelHandler)) {
        // create netty sinks
        final List<Sink<String>> sinks = new LinkedList<>();
        // Create sinks
        for (int i = 0; i < numSinks; i++) {
            final Sink<String> sink = new NettyTextSink(SERVER_ADDR, SERVER_PORT, nettySharedResource, stringIdentifierFactory);
            sinks.add(sink);
        }
        // Wait until all sinks connect to output receiver
        channelCountDown.await();
        outputStream.forEach((output) -> {
            for (final Sink<String> sink : sinks) {
                sink.handle(output);
            }
        });
        // Wait until all data are sent to source
        countDownLatch.await();
        for (List<String> received : channelListMap.values()) {
            Assert.assertEquals(outputStream, received);
        }
        // Closes
        for (final Sink<String> sink : sinks) {
            sink.close();
        }
    }
}
Also used : NettyChannelHandler(edu.snu.mist.common.stream.NettyChannelHandler) Channel(io.netty.channel.Channel) NettyTextMessageOutputReceiver(edu.snu.mist.common.stream.textmessage.NettyTextMessageOutputReceiver) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 2 with NettyTextMessageOutputReceiver

use of edu.snu.mist.common.stream.textmessage.NettyTextMessageOutputReceiver in project mist by snuspl.

the class GroupRecoveryTest method testSingleQueryRecovery.

@Test(timeout = 500000)
public void testSingleQueryRecovery() throws Exception {
    // Start source servers.
    final CountDownLatch sourceCountDownLatch1 = new CountDownLatch(1);
    final NettyTextMessageStreamGenerator textMessageStreamGenerator1 = new NettyTextMessageStreamGenerator(SERVER_ADDR, SOURCE_PORT1, new TestChannelHandler(sourceCountDownLatch1));
    final TestSinkHandler handler1 = new TestSinkHandler();
    final NettyTextMessageOutputReceiver receiver1 = new NettyTextMessageOutputReceiver("localhost", SINK_PORT, handler1);
    // Submit query.
    final MISTQuery query = buildQuery();
    // Generate avro chained dag : needed parts from MISTExamplesUtils.submitQuery()
    final Tuple<List<AvroVertex>, List<Edge>> initialAvroOpChainDag = query.getAvroOperatorDag();
    final String appId = "testApp";
    final String queryId = "testQuery";
    final AvroDag.Builder avroDagBuilder = AvroDag.newBuilder();
    final AvroDag avroDag = avroDagBuilder.setAppId(appId).setQueryId(queryId).setJarPaths(new ArrayList<>()).setAvroVertices(initialAvroOpChainDag.getKey()).setEdges(initialAvroOpChainDag.getValue()).build();
    // Build QueryManager.
    final JavaConfigurationBuilder jcb = Tang.Factory.getTang().newConfigurationBuilder();
    jcb.bindNamedParameter(PeriodicCheckpointPeriod.class, "1000");
    jcb.bindImplementation(QueryManager.class, GroupAwareQueryManagerImpl.class);
    jcb.bindImplementation(QueryStarter.class, ImmediateQueryMergingStarter.class);
    jcb.bindNamedParameter(TaskHostname.class, "127.0.0.1");
    final Injector injector = Tang.Factory.getTang().newInjector(jcb.build());
    injector.bindVolatileInstance(GroupIdRequestor.class, new TestGroupIdRequestor());
    injector.bindVolatileInstance(TaskStatsUpdater.class, mock(TaskStatsUpdater.class));
    final CheckpointManager checkpointManager = injector.getInstance(CheckpointManager.class);
    final QueryManager queryManager = injector.getInstance(QueryManager.class);
    queryManager.createApplication(appId, Arrays.asList(""));
    queryManager.create(avroDag);
    // Wait until all sources connect to stream generator
    sourceCountDownLatch1.await();
    final ExecutionDags executionDags = checkpointManager.getApplication(appId).getGroups().get(0).getExecutionDags();
    Assert.assertEquals(executionDags.values().size(), 1);
    final ExecutionDag executionDag = executionDags.values().iterator().next();
    // 1st stage. Push inputs to all sources and see if the results are proper.
    SRC0INPUT1.forEach(textMessageStreamGenerator1::write);
    LATCH1.await();
    Assert.assertEquals("{aa=2, bb=1, cc=1}", handler1.getResults().get(handler1.getResults().size() - 1));
    // Sleep 2 seconds for the checkpoint events to be sent out.
    sleep(2000);
    // Checkpoint the entire MISTTask, delete it, and restore it to see if it works.
    final String groupId = checkpointManager.getApplication(appId).getGroups().get(0).getGroupId();
    checkpointManager.checkpointGroup(groupId);
    checkpointManager.deleteGroup(groupId);
    // Close the generator.
    textMessageStreamGenerator1.close();
    receiver1.close();
    // Restart source servers.
    final CountDownLatch sourceCountDownLatch2 = new CountDownLatch(1);
    final NettyTextMessageStreamGenerator textMessageStreamGenerator2 = new NettyTextMessageStreamGenerator(SERVER_ADDR, SOURCE_PORT1, new TestChannelHandler(sourceCountDownLatch2));
    final TestSinkHandler handler2 = new TestSinkHandler();
    final NettyTextMessageOutputReceiver receiver2 = new NettyTextMessageOutputReceiver("localhost", SINK_PORT, handler2);
    // Recover the group.
    checkpointManager.recoverGroup(groupId);
    // Wait until all sources connect to stream generator
    sourceCountDownLatch2.await();
    final ExecutionDags executionDags2 = checkpointManager.getApplication(appId).getGroups().get(0).getExecutionDags();
    Assert.assertEquals(executionDags2.values().size(), 1);
    final ExecutionDag executionDag2 = executionDags2.values().iterator().next();
    // 2nd stage. Push inputs to the recovered the query to see if it works.
    SRC0INPUT2.forEach(textMessageStreamGenerator2::write);
    LATCH2.await();
    Assert.assertEquals("{aa=3, bb=3, cc=3}", handler2.getResults().get(handler2.getResults().size() - 1));
    // Close the generators.
    textMessageStreamGenerator2.close();
    receiver2.close();
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NettyTextMessageOutputReceiver(edu.snu.mist.common.stream.textmessage.NettyTextMessageOutputReceiver) CountDownLatch(java.util.concurrent.CountDownLatch) AvroDag(edu.snu.mist.formats.avro.AvroDag) TaskStatsUpdater(edu.snu.mist.core.task.groupaware.TaskStatsUpdater) Injector(org.apache.reef.tang.Injector) MISTQuery(edu.snu.mist.client.MISTQuery) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NettyTextMessageStreamGenerator(edu.snu.mist.common.stream.textmessage.NettyTextMessageStreamGenerator) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) Test(org.junit.Test)

Aggregations

NettyTextMessageOutputReceiver (edu.snu.mist.common.stream.textmessage.NettyTextMessageOutputReceiver)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 MISTQuery (edu.snu.mist.client.MISTQuery)1 NettyChannelHandler (edu.snu.mist.common.stream.NettyChannelHandler)1 NettyTextMessageStreamGenerator (edu.snu.mist.common.stream.textmessage.NettyTextMessageStreamGenerator)1 TaskStatsUpdater (edu.snu.mist.core.task.groupaware.TaskStatsUpdater)1 AvroDag (edu.snu.mist.formats.avro.AvroDag)1 Channel (io.netty.channel.Channel)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Injector (org.apache.reef.tang.Injector)1 JavaConfigurationBuilder (org.apache.reef.tang.JavaConfigurationBuilder)1