Search in sources :

Example 1 with NettySharedResource

use of edu.snu.mist.core.shared.NettySharedResource in project mist by snuspl.

the class NettySourceTest method testPunctuatedNettyTextSource.

/**
 * Test whether the created source using DataGenerator by NettyTextDataGeneratorFactory receive event-time data
 * correctly from netty server, and generate proper punctuated watermark and outputs.
 * It creates 4 sources each having data generator using Netty server.
 * @throws Exception
 */
@Test(timeout = 4000L)
public void testPunctuatedNettyTextSource() throws Exception {
    final int numSources = 4;
    final int numData = 3;
    final int numWatermark = 2;
    final List<String> inputStreamWithTimestamp = Arrays.asList("Lorem ipsum dolor sit amet, consectetur adipiscing elit.:100", "In in leo nec erat fringilla mattis eu non massa.:800", "Watermark:1000", "Cras quis diam suscipit, commodo enim id, pulvinar nunc.:1200", "Watermark:1500");
    final List<String> expectedDataWithoutTimestamp = 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 List<Long> expectedPunctuatedWatermark = Arrays.asList(1000L, 1500L);
    final CountDownLatch dataCountDownLatch = new CountDownLatch(numSources * numData);
    final CountDownLatch watermarkCountDownLatch = new CountDownLatch(numSources * numWatermark);
    final CountDownLatch channelCountDown = new CountDownLatch(numSources);
    LOG.log(Level.FINE, "Count down data: {0}", dataCountDownLatch);
    LOG.log(Level.FINE, "Count down watermark: {0}", watermarkCountDownLatch);
    // create netty server
    try (final NettyTextMessageStreamGenerator textMessageStreamGenerator = new NettyTextMessageStreamGenerator(SERVER_ADDR, SERVER_PORT, new TestChannelHandler(channelCountDown))) {
        final Injector injector = Tang.Factory.getTang().newInjector();
        // source list
        final List<Tuple<DataGenerator, EventGenerator>> sources = new LinkedList<>();
        // result data list
        final List<List<String>> punctuatedDataResults = new LinkedList<>();
        // result watermark list
        final List<List<Long>> punctuatedWatermarkResults = new LinkedList<>();
        // Create sources having punctuated watermark
        for (int i = 0; i < numSources; i++) {
            final DataGenerator<String> dataGenerator = new NettyTextDataGenerator(SERVER_ADDR, SERVER_PORT, nettySharedResource);
            final MISTFunction<String, Tuple<String, Long>> extractFunc = (input) -> new Tuple<>(input.toString().split(":")[0], Long.parseLong(input.toString().split(":")[1]));
            final MISTPredicate<String> isWatermark = (input) -> input.toString().split(":")[0].equals("Watermark");
            final WatermarkTimestampFunction<String> parseTsFunc = (input) -> Long.parseLong(input.toString().split(":")[1]);
            final EventGenerator<String> eventGenerator = new PunctuatedEventGenerator<>(extractFunc, isWatermark, parseTsFunc, 0, null, null);
            sources.add(new Tuple<>(dataGenerator, eventGenerator));
            dataGenerator.setEventGenerator(eventGenerator);
            final List<String> receivedData = new LinkedList<>();
            final List<Long> receivedWatermark = new LinkedList<>();
            punctuatedDataResults.add(receivedData);
            punctuatedWatermarkResults.add(receivedWatermark);
            eventGenerator.setOutputEmitter(new SourceTestOutputEmitter<>(receivedData, receivedWatermark, dataCountDownLatch, watermarkCountDownLatch));
        }
        // Start to receive data stream from stream generator
        for (final Tuple<DataGenerator, EventGenerator> source : sources) {
            // start event generator
            source.getValue().start();
            // start data generator
            source.getKey().start();
        }
        // Wait until all sources connect to stream generator
        channelCountDown.await();
        inputStreamWithTimestamp.forEach(textMessageStreamGenerator::write);
        // Wait until all data are sent to source
        dataCountDownLatch.await();
        watermarkCountDownLatch.await();
        for (final List<String> received : punctuatedDataResults) {
            Assert.assertEquals(expectedDataWithoutTimestamp, received);
        }
        for (final List<Long> received : punctuatedWatermarkResults) {
            Assert.assertEquals(expectedPunctuatedWatermark, received);
        }
        // Closes
        for (final Tuple<DataGenerator, EventGenerator> source : sources) {
            // stop data generator
            source.getKey().close();
            // stop event generator
            source.getValue().close();
        }
    }
}
Also used : Injector(org.apache.reef.tang.Injector) Arrays(java.util.Arrays) MistCheckpointEvent(edu.snu.mist.core.MistCheckpointEvent) NettyChannelHandler(edu.snu.mist.common.stream.NettyChannelHandler) Level(java.util.logging.Level) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) WatermarkTimestampFunction(edu.snu.mist.common.functions.WatermarkTimestampFunction) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StringIdentifierFactory(org.apache.reef.io.network.util.StringIdentifierFactory) LinkedList(java.util.LinkedList) Before(org.junit.Before) Tang(org.apache.reef.tang.Tang) MISTFunction(edu.snu.mist.common.functions.MISTFunction) MISTPredicate(edu.snu.mist.common.functions.MISTPredicate) NettySharedResource(edu.snu.mist.core.shared.NettySharedResource) Tuple(org.apache.reef.io.Tuple) Test(org.junit.Test) Logger(java.util.logging.Logger) Executors(java.util.concurrent.Executors) OutputEmitter(edu.snu.mist.core.OutputEmitter) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MistDataEvent(edu.snu.mist.core.MistDataEvent) List(java.util.List) Assert(junit.framework.Assert) InjectionException(org.apache.reef.tang.exceptions.InjectionException) NettyTextMessageStreamGenerator(edu.snu.mist.common.stream.textmessage.NettyTextMessageStreamGenerator) Injector(org.apache.reef.tang.Injector) LinkedList(java.util.LinkedList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) NettyTextMessageStreamGenerator(edu.snu.mist.common.stream.textmessage.NettyTextMessageStreamGenerator) Tuple(org.apache.reef.io.Tuple) Test(org.junit.Test)

Example 2 with NettySharedResource

use of edu.snu.mist.core.shared.NettySharedResource in project mist by snuspl.

the class NettySourceTest method setUp.

@Before
public void setUp() throws InjectionException {
    final Injector injector = Tang.Factory.getTang().newInjector();
    nettySharedResource = injector.getInstance(NettySharedResource.class);
    identifierFactory = injector.getInstance(StringIdentifierFactory.class);
    scheduler = Executors.newScheduledThreadPool(1);
}
Also used : Injector(org.apache.reef.tang.Injector) NettySharedResource(edu.snu.mist.core.shared.NettySharedResource) StringIdentifierFactory(org.apache.reef.io.network.util.StringIdentifierFactory) Before(org.junit.Before)

Example 3 with NettySharedResource

use of edu.snu.mist.core.shared.NettySharedResource in project mist by snuspl.

the class NettyTextSinkTest method setUp.

@Before
public void setUp() throws InjectionException {
    final Injector injector = Tang.Factory.getTang().newInjector();
    nettySharedResource = injector.getInstance(NettySharedResource.class);
    stringIdentifierFactory = injector.getInstance(StringIdentifierFactory.class);
}
Also used : Injector(org.apache.reef.tang.Injector) NettySharedResource(edu.snu.mist.core.shared.NettySharedResource) StringIdentifierFactory(org.apache.reef.io.network.util.StringIdentifierFactory) Before(org.junit.Before)

Aggregations

NettySharedResource (edu.snu.mist.core.shared.NettySharedResource)3 StringIdentifierFactory (org.apache.reef.io.network.util.StringIdentifierFactory)3 Injector (org.apache.reef.tang.Injector)3 Before (org.junit.Before)3 MISTFunction (edu.snu.mist.common.functions.MISTFunction)1 MISTPredicate (edu.snu.mist.common.functions.MISTPredicate)1 WatermarkTimestampFunction (edu.snu.mist.common.functions.WatermarkTimestampFunction)1 NettyChannelHandler (edu.snu.mist.common.stream.NettyChannelHandler)1 NettyTextMessageStreamGenerator (edu.snu.mist.common.stream.textmessage.NettyTextMessageStreamGenerator)1 MistCheckpointEvent (edu.snu.mist.core.MistCheckpointEvent)1 MistDataEvent (edu.snu.mist.core.MistDataEvent)1 MistWatermarkEvent (edu.snu.mist.core.MistWatermarkEvent)1 OutputEmitter (edu.snu.mist.core.OutputEmitter)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1