Search in sources :

Example 6 with MISTFunction

use of edu.snu.mist.common.functions.MISTFunction 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 7 with MISTFunction

use of edu.snu.mist.common.functions.MISTFunction in project mist by snuspl.

the class AggregateWindowOperatorTest method testAggregateWindowOperator.

/**
 * Test AggregateWindowOperator.
 * It calculates the maximum value from the collection.
 */
@Test
public void testAggregateWindowOperator() {
    // input stream events
    final WindowImpl<Integer> window = new WindowImpl<>(0L, 100L);
    window.putData(new MistDataEvent(10, 10));
    window.putData(new MistDataEvent(20, 20));
    window.putData(new MistDataEvent(15, 30));
    window.putData(new MistDataEvent(30, 90));
    final MistDataEvent dataEvent = new MistDataEvent(window, 90L);
    final MistWatermarkEvent watermarkEvent = new MistWatermarkEvent(101L);
    // functions that dealing with input WindowData
    final MISTFunction<WindowData<Integer>, String> aggregateFunc = (windowData) -> {
        return windowData.getDataCollection().toString() + ", " + windowData.getStart() + ", " + windowData.getEnd();
    };
    final AggregateWindowOperator<Integer, String> aggregateWindowOperator = new AggregateWindowOperator<>(aggregateFunc);
    final List<MistEvent> result = new LinkedList<>();
    aggregateWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
    aggregateWindowOperator.processLeftData(dataEvent);
    Assert.assertEquals(1, result.size());
    Assert.assertTrue(result.get(0).isData());
    Assert.assertEquals("[10, 20, 15, 30], 0, 99", ((MistDataEvent) result.get(0)).getValue());
    Assert.assertEquals(90L, result.get(0).getTimestamp());
    aggregateWindowOperator.processLeftWatermark(watermarkEvent);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(watermarkEvent, result.get(1));
}
Also used : WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistDataEvent(edu.snu.mist.core.MistDataEvent) List(java.util.List) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) MISTFunction(edu.snu.mist.common.functions.MISTFunction) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MistEvent(edu.snu.mist.core.MistEvent) AggregateWindowOperator(edu.snu.mist.core.operators.window.AggregateWindowOperator) Test(org.junit.Test) WindowData(edu.snu.mist.common.windows.WindowData) Assert(org.junit.Assert) LinkedList(java.util.LinkedList) WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistEvent(edu.snu.mist.core.MistEvent) LinkedList(java.util.LinkedList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) WindowData(edu.snu.mist.common.windows.WindowData) AggregateWindowOperator(edu.snu.mist.core.operators.window.AggregateWindowOperator) MistDataEvent(edu.snu.mist.core.MistDataEvent) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) Test(org.junit.Test)

Example 8 with MISTFunction

use of edu.snu.mist.common.functions.MISTFunction in project mist by snuspl.

the class WindowAndAggregate method submitQuery.

/**
 * Submit a windowing and aggregating query.
 * The query reads strings from a source server, puts them into window,
 * concatenates all inputs in the window into a single string using toString function, and
 * print out the start and end time of the window.
 * @return result of the submission
 * @throws IOException
 * @throws InjectionException
 */
public static APIQueryControlResult submitQuery(final Configuration configuration) throws IOException, InjectionException, URISyntaxException {
    // configurations for source and sink
    final String sourceSocket = Tang.Factory.getTang().newInjector(configuration).getNamedInstance(NettySourceAddress.class);
    final SourceConfiguration localTextSocketSourceConf = MISTExampleUtils.getLocalTextSocketSourceConf(sourceSocket);
    // configurations for windowing and aggregation
    final int windowSize = 5000;
    final int windowEmissionInterval = 2500;
    final MISTFunction<WindowData<String>, String> aggregateFunc = (windowData) -> {
        return windowData.getDataCollection().toString() + ", window is started at " + windowData.getStart() + ", window is ended at " + windowData.getEnd() + ".";
    };
    final MISTQueryBuilder queryBuilder = new MISTQueryBuilder();
    queryBuilder.socketTextStream(localTextSocketSourceConf).window(new TimeWindowInformation(windowSize, windowEmissionInterval)).aggregateWindow(aggregateFunc).textSocketOutput(MISTExampleUtils.SINK_HOSTNAME, MISTExampleUtils.SINK_PORT);
    return MISTExampleUtils.submit(queryBuilder, configuration);
}
Also used : TimeWindowInformation(edu.snu.mist.common.windows.TimeWindowInformation) Tang(org.apache.reef.tang.Tang) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) MISTFunction(edu.snu.mist.common.functions.MISTFunction) NettySourceAddress(edu.snu.mist.examples.parameters.NettySourceAddress) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CommandLine(org.apache.reef.tang.formats.CommandLine) APIQueryControlResult(edu.snu.mist.client.APIQueryControlResult) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) Configuration(org.apache.reef.tang.Configuration) JavaConfigurationBuilder(org.apache.reef.tang.JavaConfigurationBuilder) WindowData(edu.snu.mist.common.windows.WindowData) InjectionException(org.apache.reef.tang.exceptions.InjectionException) MISTQueryBuilder(edu.snu.mist.client.MISTQueryBuilder) WindowData(edu.snu.mist.common.windows.WindowData) SourceConfiguration(edu.snu.mist.client.datastreams.configurations.SourceConfiguration) TimeWindowInformation(edu.snu.mist.common.windows.TimeWindowInformation)

Aggregations

MISTFunction (edu.snu.mist.common.functions.MISTFunction)8 InjectionException (org.apache.reef.tang.exceptions.InjectionException)6 LinkedList (java.util.LinkedList)5 List (java.util.List)5 MISTPredicate (edu.snu.mist.common.functions.MISTPredicate)4 MistDataEvent (edu.snu.mist.core.MistDataEvent)4 Tang (org.apache.reef.tang.Tang)4 Test (org.junit.Test)4 APIQueryControlResult (edu.snu.mist.client.APIQueryControlResult)3 MISTQueryBuilder (edu.snu.mist.client.MISTQueryBuilder)3 SourceConfiguration (edu.snu.mist.client.datastreams.configurations.SourceConfiguration)3 WindowData (edu.snu.mist.common.windows.WindowData)3 MistEvent (edu.snu.mist.core.MistEvent)3 OutputBufferEmitter (edu.snu.mist.core.utils.OutputBufferEmitter)3 NettySourceAddress (edu.snu.mist.examples.parameters.NettySourceAddress)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 Arrays (java.util.Arrays)3 Logger (java.util.logging.Logger)3 Tuple (org.apache.reef.io.Tuple)3