Search in sources :

Example 1 with Tuple3.tuple3

use of com.hazelcast.jet.datamodel.Tuple3.tuple3 in project hazelcast by hazelcast.

the class MasterSnapshotContext method tryBeginSnapshot.

void tryBeginSnapshot() {
    mc.coordinationService().submitToCoordinatorThread(() -> {
        boolean isTerminal;
        String snapshotMapName;
        CompletableFuture<Void> future;
        mc.lock();
        long localExecutionId;
        try {
            if (mc.jobStatus() != RUNNING) {
                logger.fine("Not beginning snapshot, " + mc.jobIdString() + " is not RUNNING, but " + mc.jobStatus());
                return;
            }
            if (snapshotInProgress) {
                logger.fine("Not beginning snapshot since one is already in progress " + mc.jobIdString());
                return;
            }
            if (terminalSnapshotFuture.isDone()) {
                logger.fine("Not beginning snapshot since terminal snapshot is already completed " + mc.jobIdString());
                return;
            }
            Tuple3<String, Boolean, CompletableFuture<Void>> requestedSnapshot = snapshotQueue.poll();
            if (requestedSnapshot == null) {
                return;
            }
            snapshotInProgress = true;
            snapshotMapName = requestedSnapshot.f0();
            assert requestedSnapshot.f1() != null;
            isTerminal = requestedSnapshot.f1();
            future = requestedSnapshot.f2();
            mc.jobExecutionRecord().startNewSnapshot(snapshotMapName);
            localExecutionId = mc.executionId();
        } finally {
            mc.unlock();
        }
        mc.writeJobExecutionRecord(false);
        long newSnapshotId = mc.jobExecutionRecord().ongoingSnapshotId();
        boolean isExport = snapshotMapName != null;
        int snapshotFlags = SnapshotFlags.create(isTerminal, isExport);
        String finalMapName = isExport ? exportedSnapshotMapName(snapshotMapName) : snapshotDataMapName(mc.jobId(), mc.jobExecutionRecord().ongoingDataMapIndex());
        mc.nodeEngine().getHazelcastInstance().getMap(finalMapName).clear();
        logFine(logger, "Starting snapshot %d for %s, flags: %s, writing to: %s", newSnapshotId, jobNameAndExecutionId(mc.jobName(), localExecutionId), SnapshotFlags.toString(snapshotFlags), snapshotMapName);
        Function<ExecutionPlan, Operation> factory = plan -> new SnapshotPhase1Operation(mc.jobId(), localExecutionId, newSnapshotId, finalMapName, snapshotFlags);
        // Need to take a copy of executionId: we don't cancel the scheduled task when the execution
        // finalizes. If a new execution is started in the meantime, we'll use the execution ID to detect it.
        mc.invokeOnParticipants(factory, responses -> onSnapshotPhase1Complete(responses, localExecutionId, newSnapshotId, finalMapName, snapshotFlags, future), null, true);
    });
}
Also used : SnapshotPhase2Operation(com.hazelcast.jet.impl.operation.SnapshotPhase2Operation) LoggingUtil(com.hazelcast.jet.impl.util.LoggingUtil) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ExecutionNotFoundException(com.hazelcast.jet.impl.exception.ExecutionNotFoundException) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) JetException(com.hazelcast.jet.JetException) Util.jobNameAndExecutionId(com.hazelcast.jet.impl.util.Util.jobNameAndExecutionId) ILogger(com.hazelcast.logging.ILogger) Operation(com.hazelcast.spi.impl.operationservice.Operation) ExceptionUtil.withTryCatch(com.hazelcast.jet.impl.util.ExceptionUtil.withTryCatch) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) Map(java.util.Map) SnapshotPhase1Operation(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation) LinkedList(java.util.LinkedList) EXPORTED_SNAPSHOTS_PREFIX(com.hazelcast.jet.impl.JobRepository.EXPORTED_SNAPSHOTS_PREFIX) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Tuple3(com.hazelcast.jet.datamodel.Tuple3) SnapshotFlags(com.hazelcast.jet.impl.execution.SnapshotFlags) Collection(java.util.Collection) JobRepository.snapshotDataMapName(com.hazelcast.jet.impl.JobRepository.snapshotDataMapName) ExecutionException(java.util.concurrent.ExecutionException) JobRepository.exportedSnapshotMapName(com.hazelcast.jet.impl.JobRepository.exportedSnapshotMapName) SnapshotPhase1Result(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation.SnapshotPhase1Result) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Util.idToString(com.hazelcast.jet.Util.idToString) LoggingUtil.logFine(com.hazelcast.jet.impl.util.LoggingUtil.logFine) ExecutionPlan(com.hazelcast.jet.impl.execution.init.ExecutionPlan) Entry(java.util.Map.Entry) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) Queue(java.util.Queue) SnapshotStats(com.hazelcast.jet.impl.JobExecutionRecord.SnapshotStats) IMap(com.hazelcast.map.IMap) SnapshotPhase1Operation(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation) Util.idToString(com.hazelcast.jet.Util.idToString) SnapshotPhase2Operation(com.hazelcast.jet.impl.operation.SnapshotPhase2Operation) Operation(com.hazelcast.spi.impl.operationservice.Operation) SnapshotPhase1Operation(com.hazelcast.jet.impl.operation.SnapshotPhase1Operation) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionPlan(com.hazelcast.jet.impl.execution.init.ExecutionPlan)

Example 2 with Tuple3.tuple3

use of com.hazelcast.jet.datamodel.Tuple3.tuple3 in project hazelcast by hazelcast.

the class RebalanceBatchStageTest method when_rebalanceAndGroupAggregateBuilderWithComplexAggrOp_then_singleStageAggregation.

@Test
public void when_rebalanceAndGroupAggregateBuilderWithComplexAggrOp_then_singleStageAggregation() {
    // Given
    FunctionEx<Integer, Integer> keyFn = i -> i % 10;
    Iterator<Integer> sequence = IntStream.iterate(0, i -> i + 1).boxed().iterator();
    List<Integer> input0 = streamFromIterator(sequence).limit(itemCount).collect(toList());
    List<Integer> input1 = streamFromIterator(sequence).limit(itemCount).collect(toList());
    List<Integer> input2 = streamFromIterator(sequence).limit(itemCount).collect(toList());
    BatchStageWithKey<Integer, Integer> stage0 = batchStageFromList(input0).groupingKey(keyFn);
    BatchStageWithKey<Integer, Integer> stage1 = batchStageFromList(input1).groupingKey(keyFn);
    BatchStageWithKey<Integer, Integer> stage2Rebalanced = batchStageFromList(input2).rebalance().groupingKey(keyFn);
    // When
    GroupAggregateBuilder1<Integer, Integer> b = stage0.aggregateBuilder();
    Tag<Integer> tag0_in = b.tag0();
    Tag<Integer> tag1_in = b.add(stage1);
    Tag<Integer> tag2_in = b.add(stage2Rebalanced);
    CoAggregateOperationBuilder agb = coAggregateOperationBuilder();
    Tag<Long> tag0 = agb.add(tag0_in, SUMMING);
    Tag<Long> tag1 = agb.add(tag1_in, SUMMING);
    Tag<Long> tag2 = agb.add(tag2_in, SUMMING);
    AggregateOperation<Object[], ItemsByTag> aggrOp = agb.build();
    BatchStage<Entry<Integer, ItemsByTag>> aggregated = b.build(aggrOp);
    // Then
    aggregated.writeTo(sink);
    assertSingleStageAggregation();
    execute();
    Collector<Integer, ?, Map<Integer, Long>> groupAndSum = groupingBy(keyFn, summingLong(i -> i));
    Map<Integer, Long> expectedMap0 = input0.stream().collect(groupAndSum);
    Map<Integer, Long> expectedMap1 = input1.stream().collect(groupAndSum);
    Map<Integer, Long> expectedMap2 = input2.stream().collect(groupAndSum);
    assertEquals(streamToString(expectedMap0.entrySet().stream(), e -> FORMAT_FN_3.apply(e.getKey(), tuple3(e.getValue(), expectedMap1.get(e.getKey()), expectedMap2.get(e.getKey())))), streamToString(this.<Integer, ItemsByTag>sinkStreamOfEntry(), e -> FORMAT_FN_3.apply(e.getKey(), tuple3(e.getValue().get(tag0), e.getValue().get(tag1), e.getValue().get(tag2)))));
}
Also used : Spliterators.spliteratorUnknownSize(java.util.Spliterators.spliteratorUnknownSize) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AggregateOperations.coAggregateOperationBuilder(com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder) Collections.singletonList(java.util.Collections.singletonList) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) Map(java.util.Map) DAG(com.hazelcast.jet.core.DAG) Collector(java.util.stream.Collector) FunctionEx(com.hazelcast.function.FunctionEx) Tag(com.hazelcast.jet.datamodel.Tag) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Category(org.junit.experimental.categories.Category) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) Function.identity(java.util.function.Function.identity) IntStream(java.util.stream.IntStream) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Function(java.util.function.Function) FORMAT_FN_3(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN_3) JoinClause.joinMapEntries(com.hazelcast.jet.pipeline.JoinClause.joinMapEntries) ArrayList(java.util.ArrayList) JetException(com.hazelcast.jet.JetException) JetAssert.assertFalse(com.hazelcast.jet.core.test.JetAssert.assertFalse) FORMAT_FN(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN) AssertionSinks.assertAnyOrder(com.hazelcast.jet.pipeline.test.AssertionSinks.assertAnyOrder) FORMAT_FN_2(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN_2) Collectors.summingLong(java.util.stream.Collectors.summingLong) Util.entry(com.hazelcast.jet.Util.entry) Edge(com.hazelcast.jet.core.Edge) Nonnull(javax.annotation.Nonnull) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Tuple3(com.hazelcast.jet.datamodel.Tuple3) IList(com.hazelcast.collection.IList) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AggregateOperation1(com.hazelcast.jet.aggregate.AggregateOperation1) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) TestSources(com.hazelcast.jet.pipeline.test.TestSources) Collectors.toList(java.util.stream.Collectors.toList) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) Assert.assertNull(org.junit.Assert.assertNull) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Functions.wholeItem(com.hazelcast.function.Functions.wholeItem) Util(com.hazelcast.jet.Util) Assert.assertEquals(org.junit.Assert.assertEquals) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Collectors.summingLong(java.util.stream.Collectors.summingLong) Map(java.util.Map) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with Tuple3.tuple3

use of com.hazelcast.jet.datamodel.Tuple3.tuple3 in project hazelcast by hazelcast.

the class BatchAggregateTest method groupAggregateBuilder_withComplexAggrOp.

@Test
public void groupAggregateBuilder_withComplexAggrOp() {
    // Given
    GroupAggregateFixture fx = new GroupAggregateFixture();
    BatchStageWithKey<Integer, Integer> stage0 = fx.srcStage0.groupingKey(fx.keyFn);
    BatchStageWithKey<Integer, Integer> stage1 = fx.srcStage1().groupingKey(fx.keyFn);
    BatchStageWithKey<Integer, Integer> stage2 = fx.srcStage2().groupingKey(fx.keyFn);
    // When
    GroupAggregateBuilder1<Integer, Integer> b = stage0.aggregateBuilder();
    Tag<Integer> tag0_in = b.tag0();
    Tag<Integer> tag1_in = b.add(stage1);
    Tag<Integer> tag2_in = b.add(stage2);
    CoAggregateOperationBuilder agb = coAggregateOperationBuilder();
    Tag<Long> tag0 = agb.add(tag0_in, SUMMING);
    Tag<Long> tag1 = agb.add(tag1_in, SUMMING);
    Tag<Long> tag2 = agb.add(tag2_in, SUMMING);
    AggregateOperation<Object[], ItemsByTag> aggrOp = agb.build();
    BatchStage<Entry<Integer, ItemsByTag>> aggregated = b.build(aggrOp);
    // Then
    aggregated.writeTo(sink);
    execute();
    Map<Integer, Long> expectedMap0 = input.stream().collect(groupingBy(fx.keyFn, fx.collectOp));
    Map<Integer, Long> expectedMap1 = input.stream().map(fx.mapFn1).collect(groupingBy(fx.keyFn, fx.collectOp));
    Map<Integer, Long> expectedMap2 = input.stream().map(fx.mapFn2).collect(groupingBy(fx.keyFn, fx.collectOp));
    assertEquals(streamToString(expectedMap0.entrySet().stream(), e -> FORMAT_FN_3.apply(e.getKey(), tuple3(e.getValue(), expectedMap1.get(e.getKey()), expectedMap2.get(e.getKey())))), streamToString(this.<Integer, ItemsByTag>sinkStreamOfEntry(), e -> FORMAT_FN_3.apply(e.getKey(), tuple3(e.getValue().get(tag0), e.getValue().get(tag1), e.getValue().get(tag2)))));
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) AggregateOperations.maxBy(com.hazelcast.jet.aggregate.AggregateOperations.maxBy) AggregateOperations.aggregateOperation2(com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation2) QuickTest(com.hazelcast.test.annotation.QuickTest) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) AggregateOperations.coAggregateOperationBuilder(com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) BiFunctionEx(com.hazelcast.function.BiFunctionEx) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) Collectors.summingLong(java.util.stream.Collectors.summingLong) Util.entry(com.hazelcast.jet.Util.entry) Map(java.util.Map) ComparatorEx.comparingInt(com.hazelcast.function.ComparatorEx.comparingInt) Collector(java.util.stream.Collector) AggregateOperations.aggregateOperation3(com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation3) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Tuple3(com.hazelcast.jet.datamodel.Tuple3) Before(org.junit.Before) FunctionEx(com.hazelcast.function.FunctionEx) Collections.emptyList(java.util.Collections.emptyList) Tag(com.hazelcast.jet.datamodel.Tag) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Test(org.junit.Test) AggregateOperation1(com.hazelcast.jet.aggregate.AggregateOperation1) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Category(org.junit.experimental.categories.Category) ItemsByTag.itemsByTag(com.hazelcast.jet.datamodel.ItemsByTag.itemsByTag) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) Assert.assertEquals(org.junit.Assert.assertEquals) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Collectors.summingLong(java.util.stream.Collectors.summingLong) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with Tuple3.tuple3

use of com.hazelcast.jet.datamodel.Tuple3.tuple3 in project hazelcast-jet-reference-manual by hazelcast.

the class BuildComputation method s10.

static void s10() {
    // tag::s10[]
    Pipeline p = Pipeline.create();
    // The stream to be enriched: trades
    StreamStage<Trade> trades = p.drawFrom(Sources.mapJournal("trades", mapPutEvents(), mapEventNewValue(), START_FROM_CURRENT));
    // The enriching streams: products and brokers
    BatchStage<Entry<Integer, Product>> prodEntries = p.drawFrom(Sources.map("products"));
    BatchStage<Entry<Integer, Broker>> brokEntries = p.drawFrom(Sources.map("brokers"));
    // Join the trade stream with the product and broker streams
    StreamStage<Tuple3<Trade, Product, Broker>> joined = trades.hashJoin2(prodEntries, joinMapEntries(Trade::productId), brokEntries, joinMapEntries(Trade::brokerId), Tuple3::tuple3);
// end::s10[]
}
Also used : Trade(datamodel.Trade) Entry(java.util.Map.Entry) Tuple3(com.hazelcast.jet.datamodel.Tuple3) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Aggregations

Tuple3 (com.hazelcast.jet.datamodel.Tuple3)4 Entry (java.util.Map.Entry)4 Tuple3.tuple3 (com.hazelcast.jet.datamodel.Tuple3.tuple3)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 FunctionEx (com.hazelcast.function.FunctionEx)2 JetException (com.hazelcast.jet.JetException)2 Util.entry (com.hazelcast.jet.Util.entry)2 LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)2 AggregateOperation (com.hazelcast.jet.aggregate.AggregateOperation)2 AggregateOperation1 (com.hazelcast.jet.aggregate.AggregateOperation1)2 AggregateOperations (com.hazelcast.jet.aggregate.AggregateOperations)2 AggregateOperations.coAggregateOperationBuilder (com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder)2 CoAggregateOperationBuilder (com.hazelcast.jet.aggregate.CoAggregateOperationBuilder)2 ItemsByTag (com.hazelcast.jet.datamodel.ItemsByTag)2 Tag (com.hazelcast.jet.datamodel.Tag)2 Tuple2 (com.hazelcast.jet.datamodel.Tuple2)2 Tuple2.tuple2 (com.hazelcast.jet.datamodel.Tuple2.tuple2)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2