Search in sources :

Example 6 with WindowResult

use of com.hazelcast.jet.datamodel.WindowResult in project hazelcast by hazelcast.

the class StreamSourceStageTest method when_sparseItemsWithIngestionTimestamps_then_windowIsNotEmittedTooEarly.

@Test
@Category(NightlyTest.class)
public void when_sparseItemsWithIngestionTimestamps_then_windowIsNotEmittedTooEarly() {
    IList<WindowResult<Long>> sinkList = instance.getList(randomMapName());
    IMap<Integer, Integer> map = instance.getMap(randomMapName());
    Pipeline p = Pipeline.create();
    p.readFrom(Sources.mapJournal(map, START_FROM_OLDEST)).withIngestionTimestamps().window(WindowDefinition.session(15_000)).aggregate(AggregateOperations.counting()).writeTo(Sinks.list(sinkList));
    long start = System.nanoTime();
    Job job = instance.getJet().newJob(p);
    assertEquals(0, sinkList.size());
    map.put(5, 5);
    assertTrueEventually(() -> assertEquals(1, sinkList.size()), 30);
    assertTrue(System.nanoTime() - start > SECONDS.toNanos(15));
    job.cancel();
}
Also used : WindowResult(com.hazelcast.jet.datamodel.WindowResult) Job(com.hazelcast.jet.Job) Category(org.junit.experimental.categories.Category) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 7 with WindowResult

use of com.hazelcast.jet.datamodel.WindowResult in project hazelcast by hazelcast.

the class SourceBuilder_TopologyChangeTest method testTopologyChange.

private void testTopologyChange(Supplier<HazelcastInstance> secondMemberSupplier, Consumer<HazelcastInstance> changeTopologyFn, boolean assertMonotonicity) {
    stateRestored = false;
    StreamSource<Integer> source = SourceBuilder.timestampedStream("src", ctx -> new NumberGeneratorContext()).<Integer>fillBufferFn((src, buffer) -> {
        long expectedCount = NANOSECONDS.toMillis(System.nanoTime() - src.startTime);
        expectedCount = Math.min(expectedCount, src.current + 100);
        while (src.current < expectedCount) {
            buffer.add(src.current, src.current);
            src.current++;
        }
    }).createSnapshotFn(src -> {
        System.out.println("Will save " + src.current + " to snapshot");
        return src;
    }).restoreSnapshotFn((src, states) -> {
        stateRestored = true;
        assert states.size() == 1;
        src.restore(states.get(0));
        System.out.println("Restored " + src.current + " from snapshot");
    }).build();
    Config config = smallInstanceConfig();
    // restart sooner after member add
    config.getJetConfig().setScaleUpDelayMillis(1000);
    HazelcastInstance hz = createHazelcastInstance(config);
    HazelcastInstance possibleSecondNode = secondMemberSupplier.get();
    long windowSize = 100;
    IList<WindowResult<Long>> result = hz.getList("result-" + UuidUtil.newUnsecureUuidString());
    Pipeline p = Pipeline.create();
    p.readFrom(source).withNativeTimestamps(0).window(tumbling(windowSize)).aggregate(AggregateOperations.counting()).peek().writeTo(Sinks.list(result));
    Job job = hz.getJet().newJob(p, new JobConfig().setProcessingGuarantee(EXACTLY_ONCE).setSnapshotIntervalMillis(500));
    assertTrueEventually(() -> assertFalse("result list is still empty", result.isEmpty()));
    assertJobStatusEventually(job, JobStatus.RUNNING);
    JobRepository jr = new JobRepository(hz);
    waitForFirstSnapshot(jr, job.getId(), 10, false);
    assertFalse(stateRestored);
    changeTopologyFn.accept(possibleSecondNode);
    assertTrueEventually(() -> assertTrue("restoreSnapshotFn was not called", stateRestored));
    // wait until more results are added
    int oldSize = result.size();
    assertTrueEventually(() -> assertTrue("no more results added to the list", result.size() > oldSize));
    cancelAndJoin(job);
    // results should contain sequence of results, each with count=windowSize, monotonic, if job was
    // allowed to terminate gracefully
    Iterator<WindowResult<Long>> iterator = result.iterator();
    for (int i = 0; i < result.size(); i++) {
        WindowResult<Long> next = iterator.next();
        assertEquals(windowSize, (long) next.result());
        if (assertMonotonicity) {
            assertEquals(i * windowSize, next.start());
        }
    }
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) RunWith(org.junit.runner.RunWith) Supplier(java.util.function.Supplier) HazelcastSerialClassRunner(com.hazelcast.test.HazelcastSerialClassRunner) WindowDefinition.tumbling(com.hazelcast.jet.pipeline.WindowDefinition.tumbling) UuidUtil(com.hazelcast.internal.util.UuidUtil) JobStatus(com.hazelcast.jet.core.JobStatus) Job(com.hazelcast.jet.Job) IList(com.hazelcast.collection.IList) JobRepository(com.hazelcast.jet.impl.JobRepository) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Iterator(java.util.Iterator) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) JobConfig(com.hazelcast.jet.config.JobConfig) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Serializable(java.io.Serializable) Consumer(java.util.function.Consumer) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Config(com.hazelcast.config.Config) JobConfig(com.hazelcast.jet.config.JobConfig) JobRepository(com.hazelcast.jet.impl.JobRepository) JobConfig(com.hazelcast.jet.config.JobConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Job(com.hazelcast.jet.Job)

Example 8 with WindowResult

use of com.hazelcast.jet.datamodel.WindowResult in project hazelcast by hazelcast.

the class StreamSourceStageTest method when_sparseItemsWithIngestionTimestamps_then_noExtraLatency.

@Test
public void when_sparseItemsWithIngestionTimestamps_then_noExtraLatency() {
    IList<WindowResult<Long>> sinkList = instance.getList(randomMapName());
    IMap<Integer, Integer> map = instance.getMap(randomMapName());
    Pipeline p = Pipeline.create();
    p.readFrom(Sources.mapJournal(map, START_FROM_OLDEST)).withIngestionTimestamps().window(WindowDefinition.tumbling(1)).aggregate(AggregateOperations.counting()).writeTo(Sinks.list(sinkList));
    Job job = instance.getJet().newJob(p);
    assertEquals(0, sinkList.size());
    map.put(3, 3);
    assertTrueEventually(() -> assertEquals(1, sinkList.size()), 10);
    map.put(4, 4);
    assertTrueEventually(() -> assertEquals(2, sinkList.size()), 10);
    job.cancel();
}
Also used : WindowResult(com.hazelcast.jet.datamodel.WindowResult) Job(com.hazelcast.jet.Job) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 9 with WindowResult

use of com.hazelcast.jet.datamodel.WindowResult in project hazelcast by hazelcast.

the class WindowAggregateTest method aggregateBuilder_withComplexAggrOp.

@Test
public void aggregateBuilder_withComplexAggrOp() {
    // Given
    CoAggregateFixture fx = new CoAggregateFixture();
    // When
    WindowAggregateBuilder1<Integer> b = fx.stage0.aggregateBuilder();
    Tag<Integer> tag0_in = b.tag0();
    Tag<Integer> tag1_in = b.add(fx.newStage());
    CoAggregateOperationBuilder b2 = coAggregateOperationBuilder();
    Tag<Long> tag0 = b2.add(tag0_in, SUMMING);
    Tag<Long> tag1 = b2.add(tag1_in, SUMMING);
    StreamStage<WindowResult<ItemsByTag>> aggregated = b.build(b2.build());
    // Then
    aggregated.writeTo(sink);
    execute();
    assertEquals(fx.expectedString2, streamToString(this.<ItemsByTag>sinkStreamOfWinResult(), wr -> FORMAT_FN_2.apply(wr.end(), tuple2(wr.result().get(tag0), wr.result().get(tag1)))));
}
Also used : IntStream(java.util.stream.IntStream) AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) AggregateOperations.aggregateOperation2(com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation2) BiFunction(java.util.function.BiFunction) AggregateOperations.coAggregateOperationBuilder(com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder) Collections.singletonList(java.util.Collections.singletonList) BiFunctionEx(com.hazelcast.function.BiFunctionEx) WindowDefinition.tumbling(com.hazelcast.jet.pipeline.WindowDefinition.tumbling) AggregateOperations.aggregateOperation3(com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation3) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Tuple3(com.hazelcast.jet.datamodel.Tuple3) Collections.emptyList(java.util.Collections.emptyList) Tag(com.hazelcast.jet.datamodel.Tag) Test(org.junit.Test) AggregateOperation1(com.hazelcast.jet.aggregate.AggregateOperation1) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) WindowDefinition.sliding(com.hazelcast.jet.pipeline.WindowDefinition.sliding) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) AggregateOperations.summingLong(com.hazelcast.jet.aggregate.AggregateOperations.summingLong) WindowDefinition.session(com.hazelcast.jet.pipeline.WindowDefinition.session) Function.identity(java.util.function.Function.identity) Assert.assertEquals(org.junit.Assert.assertEquals) WindowResult(com.hazelcast.jet.datamodel.WindowResult) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) AggregateOperations.summingLong(com.hazelcast.jet.aggregate.AggregateOperations.summingLong) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Test(org.junit.Test)

Example 10 with WindowResult

use of com.hazelcast.jet.datamodel.WindowResult in project hazelcast by hazelcast.

the class SourceBuilderTest method testFaultTolerance.

private void testFaultTolerance(StreamSource<?> source) {
    long windowSize = 100;
    IList<WindowResult<Long>> result = hz().getList("result-" + UuidUtil.newUnsecureUuidString());
    Pipeline p = Pipeline.create();
    p.readFrom(source).withNativeTimestamps(0).window(tumbling(windowSize)).aggregate(AggregateOperations.counting()).peek().writeTo(Sinks.list(result));
    Job job = hz().getJet().newJob(p, new JobConfig().setProcessingGuarantee(EXACTLY_ONCE));
    assertTrueEventually(() -> assertFalse("result list is still empty", result.isEmpty()));
    // restart the job
    job.restart();
    assertJobStatusEventually(job, JobStatus.RUNNING);
    // wait until more results are added
    int oldSize = result.size();
    assertTrueEventually(() -> assertTrue("no more results added to the list", result.size() > oldSize));
    cancelAndJoin(job);
    // results should contain a monotonic sequence of results, each with count=windowSize
    Iterator<WindowResult<Long>> iterator = result.iterator();
    for (int i = 0; i < result.size(); i++) {
        WindowResult<Long> next = iterator.next();
        assertEquals(windowSize, (long) next.result());
        assertEquals(i * windowSize, next.start());
    }
}
Also used : WindowResult(com.hazelcast.jet.datamodel.WindowResult) Job(com.hazelcast.jet.Job) JobConfig(com.hazelcast.jet.config.JobConfig)

Aggregations

WindowResult (com.hazelcast.jet.datamodel.WindowResult)13 Test (org.junit.Test)12 Job (com.hazelcast.jet.Job)9 Assert.assertEquals (org.junit.Assert.assertEquals)7 JobConfig (com.hazelcast.jet.config.JobConfig)6 WindowDefinition.tumbling (com.hazelcast.jet.pipeline.WindowDefinition.tumbling)5 IList (com.hazelcast.collection.IList)4 UuidUtil (com.hazelcast.internal.util.UuidUtil)4 AggregateOperations (com.hazelcast.jet.aggregate.AggregateOperations)4 EXACTLY_ONCE (com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE)4 JobStatus (com.hazelcast.jet.core.JobStatus)4 ArrayList (java.util.ArrayList)4 Category (org.junit.experimental.categories.Category)4 FunctionEx (com.hazelcast.function.FunctionEx)3 ToLongFunctionEx (com.hazelcast.function.ToLongFunctionEx)3 JobRepository (com.hazelcast.jet.impl.JobRepository)3 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 FileReader (java.io.FileReader)3