Search in sources :

Example 6 with Tuple2

use of com.hazelcast.jet.datamodel.Tuple2 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 7 with Tuple2

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

the class RebalanceBatchStageTest method when_hashJoinRebalanceMainStage_then_distributedEdge.

@Test
public void when_hashJoinRebalanceMainStage_then_distributedEdge() {
    // Given
    List<Integer> input = sequence(itemCount);
    String prefix = "value-";
    BatchStage<Integer> stage0Rebalanced = batchStageFromList(input).rebalance();
    BatchStage<Entry<Integer, String>> enrichingStage = batchStageFromList(input).map(i -> entry(i, prefix + i));
    // When
    BatchStage<Entry<Integer, String>> joined = stage0Rebalanced.hashJoin(enrichingStage, joinMapEntries(wholeItem()), // Method reference avoided due to JDK bug
    (k, v) -> entry(k, v));
    // Then
    joined.writeTo(sink);
    DAG dag = p.toDag();
    Edge stage0ToJoin = dag.getInboundEdges("2-way hash-join-joiner").get(0);
    assertTrue("Rebalancing should make the edge distributed", stage0ToJoin.isDistributed());
    assertNull("Didn't rebalance by key, the edge must not be partitioned", stage0ToJoin.getPartitioner());
    execute();
    Function<Entry<Integer, String>, String> formatFn = e -> String.format("(%04d, %s)", e.getKey(), e.getValue());
    assertEquals(streamToString(input.stream().map(i -> tuple2(i, prefix + i)), formatFn), streamToString(sinkStreamOfEntry(), formatFn));
}
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) Entry(java.util.Map.Entry) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with Tuple2

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

the class WriteFilePTest method stressTest.

private void stressTest(boolean graceful, boolean exactlyOnce) throws Exception {
    int numItems = 500;
    Pipeline p = Pipeline.create();
    p.readFrom(SourceBuilder.stream("src", procCtx -> tuple2(new int[1], procCtx.logger())).fillBufferFn((ctx, buf) -> {
        if (ctx.f0()[0] < numItems) {
            buf.add(ctx.f0()[0]++);
            sleepMillis(5);
        }
    }).createSnapshotFn(ctx -> {
        ctx.f1().fine("src vertex saved to snapshot: " + ctx.f0()[0]);
        return ctx.f0()[0];
    }).restoreSnapshotFn((ctx, state) -> {
        ctx.f0()[0] = state.get(0);
        ctx.f1().fine("src vertex restored from snapshot: " + ctx.f0()[0]);
    }).build()).withoutTimestamps().writeTo(Sinks.filesBuilder(directory.toString()).exactlyOnce(exactlyOnce).build()).setLocalParallelism(2);
    JobConfig config = new JobConfig().setProcessingGuarantee(EXACTLY_ONCE).setSnapshotIntervalMillis(50);
    JobProxy job = (JobProxy) instance().getJet().newJob(p, config);
    long endTime = System.nanoTime() + SECONDS.toNanos(60);
    do {
        assertJobStatusEventually(job, RUNNING);
        sleepMillis(100);
        job.restart(graceful);
        try {
            checkFileContents(0, numItems, exactlyOnce, true, false);
            // if content matches, break the loop. Otherwise restart and try again
            break;
        } catch (AssertionError ignored) {
        }
    } while (System.nanoTime() < endTime);
    waitForNextSnapshot(new JobRepository(instance()), job.getId(), 10, true);
    ditchJob(job, instances());
    // when the job is cancelled, there should be no temporary files
    checkFileContents(0, numItems, exactlyOnce, false, false);
}
Also used : AbstractProcessor(com.hazelcast.jet.core.AbstractProcessor) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) LongSupplier(java.util.function.LongSupplier) QuickTest(com.hazelcast.test.annotation.QuickTest) Processors.mapP(com.hazelcast.jet.core.processor.Processors.mapP) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) Collections.singletonList(java.util.Collections.singletonList) After(org.junit.After) Map(java.util.Map) DAG(com.hazelcast.jet.core.DAG) Path(java.nio.file.Path) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JobConfig(com.hazelcast.jet.config.JobConfig) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Serializable(java.io.Serializable) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) TestSupport(com.hazelcast.jet.core.test.TestSupport) DISABLE_ROLLING(com.hazelcast.jet.pipeline.FileSinkBuilder.DISABLE_ROLLING) Util.uncheckCall(com.hazelcast.jet.impl.util.Util.uncheckCall) TEMP_FILE_SUFFIX(com.hazelcast.jet.pipeline.FileSinkBuilder.TEMP_FILE_SUFFIX) IntStream(java.util.stream.IntStream) BeforeClass(org.junit.BeforeClass) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) JobProxy(com.hazelcast.jet.impl.JobProxy) Function(java.util.function.Function) Charset(java.nio.charset.Charset) TestPerson(com.hazelcast.jet.impl.connector.ReadFilesPTest.TestPerson) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) Job(com.hazelcast.jet.Job) Before(org.junit.Before) TestInbox(com.hazelcast.jet.core.test.TestInbox) JobRepository(com.hazelcast.jet.impl.JobRepository) IOUtil(com.hazelcast.internal.nio.IOUtil) Iterator(java.util.Iterator) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) Semaphore(java.util.concurrent.Semaphore) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) Sinks(com.hazelcast.jet.pipeline.Sinks) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) File(java.io.File) Vertex(com.hazelcast.jet.core.Vertex) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestSources(com.hazelcast.jet.pipeline.test.TestSources) SinkProcessors.writeFileP(com.hazelcast.jet.core.processor.SinkProcessors.writeFileP) JSON(com.fasterxml.jackson.jr.ob.JSON) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) HOURS(java.util.concurrent.TimeUnit.HOURS) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) SourceBuilder(com.hazelcast.jet.pipeline.SourceBuilder) Comparator(java.util.Comparator) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) Edge.between(com.hazelcast.jet.core.Edge.between) JobProxy(com.hazelcast.jet.impl.JobProxy) JobRepository(com.hazelcast.jet.impl.JobRepository) JobConfig(com.hazelcast.jet.config.JobConfig) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Example 9 with Tuple2

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

the class AsyncTransformUsingServiceOrderedP method tryFlushQueue.

/**
 * Drains items from the queue until either:
 * <ul><li>
 *     encountering a non-completed item
 * </li><li>
 *     the outbox gets full
 * </li></ul>
 *
 * @return true if there are no more in-flight items and everything was emitted
 *         to the outbox
 */
boolean tryFlushQueue() {
    // queue. It also doesn't shuffle the stream items.
    for (; ; ) {
        if (!emitFromTraverser(currentTraverser)) {
            return false;
        }
        Object o = queue.peek();
        if (o == null) {
            return true;
        }
        if (o instanceof Watermark) {
            watermarkTraverser.accept((Watermark) o);
            currentTraverser = watermarkTraverser;
            queuedWmCount--;
        } else {
            @SuppressWarnings("unchecked") Tuple2<T, CompletableFuture<IR>> cast = (Tuple2<T, CompletableFuture<IR>>) o;
            T item = cast.f0();
            CompletableFuture<IR> future = cast.f1();
            assert future != null;
            if (!future.isDone()) {
                return false;
            }
            try {
                currentTraverser = mapResultFn.apply(item, future.get());
                if (currentTraverser == null) {
                    currentTraverser = Traversers.empty();
                }
            } catch (Throwable e) {
                throw new JetException("Async operation completed exceptionally: " + e, e);
            }
        }
        queue.remove();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Tuple2(com.hazelcast.jet.datamodel.Tuple2) JetException(com.hazelcast.jet.JetException) Watermark(com.hazelcast.jet.core.Watermark)

Example 10 with Tuple2

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

the class ExecutionPlanBuilder method getPartitionAssignment.

/**
 * Assign the partitions to their owners. Partitions whose owner isn't in
 * the {@code memberList}, are assigned to one of the members in a
 * round-robin way.
 */
public static Map<MemberInfo, int[]> getPartitionAssignment(NodeEngine nodeEngine, List<MemberInfo> memberList) {
    IPartitionService partitionService = nodeEngine.getPartitionService();
    Map<Address, MemberInfo> membersByAddress = memberList.stream().collect(toMap(MemberInfo::getAddress, identity()));
    final MemberInfo[] partitionOwners = new MemberInfo[partitionService.getPartitionCount()];
    int memberIndex = 0;
    for (int partitionId = 0; partitionId < partitionOwners.length; partitionId++) {
        Address address = partitionService.getPartitionOwnerOrWait(partitionId);
        MemberInfo member = membersByAddress.get(address);
        if (member == null) {
            // if the partition owner isn't in the current memberList, assign to one of the other members in
            // round-robin fashion
            member = memberList.get(memberIndex++ % memberList.size());
        }
        partitionOwners[partitionId] = member;
    }
    return IntStream.range(0, partitionOwners.length).mapToObj(i -> tuple2(partitionOwners[i], i)).collect(Collectors.groupingBy(Tuple2::f0, Collectors.mapping(Tuple2::f1, Collectors.collectingAndThen(Collectors.<Integer>toList(), intList -> intList.stream().mapToInt(Integer::intValue).toArray()))));
}
Also used : IntStream(java.util.stream.IntStream) Address(com.hazelcast.cluster.Address) EdgeConfig(com.hazelcast.jet.config.EdgeConfig) Util.checkSerializable(com.hazelcast.jet.impl.util.Util.checkSerializable) Util.doWithClassLoader(com.hazelcast.jet.impl.util.Util.doWithClassLoader) HashMap(java.util.HashMap) ExceptionUtil.sneakyThrow(com.hazelcast.jet.impl.util.ExceptionUtil.sneakyThrow) Function(java.util.function.Function) ArrayList(java.util.ArrayList) PrefixedLogger.prefixedLogger(com.hazelcast.jet.impl.util.PrefixedLogger.prefixedLogger) LinkedHashMap(java.util.LinkedHashMap) Collectors.toMap(java.util.stream.Collectors.toMap) ILogger(com.hazelcast.logging.ILogger) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) Map(java.util.Map) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) Tuple2(com.hazelcast.jet.datamodel.Tuple2) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) PrefixedLogger.prefix(com.hazelcast.jet.impl.util.PrefixedLogger.prefix) NodeEngine(com.hazelcast.spi.impl.NodeEngine) Util.toList(com.hazelcast.jet.impl.util.Util.toList) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) IPartitionService(com.hazelcast.internal.partition.IPartitionService) JobConfig(com.hazelcast.jet.config.JobConfig) MetaSupplierCtx(com.hazelcast.jet.impl.execution.init.Contexts.MetaSupplierCtx) Collectors(java.util.stream.Collectors) Subject(javax.security.auth.Subject) Vertex(com.hazelcast.jet.core.Vertex) List(java.util.List) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) FunctionEx.identity(com.hazelcast.function.FunctionEx.identity) Entry(java.util.Map.Entry) JobClassLoaderService(com.hazelcast.jet.impl.JobClassLoaderService) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend) Address(com.hazelcast.cluster.Address) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) IPartitionService(com.hazelcast.internal.partition.IPartitionService) Tuple2(com.hazelcast.jet.datamodel.Tuple2)

Aggregations

Tuple2 (com.hazelcast.jet.datamodel.Tuple2)10 Tuple2.tuple2 (com.hazelcast.jet.datamodel.Tuple2.tuple2)7 List (java.util.List)7 Entry (java.util.Map.Entry)7 DAG (com.hazelcast.jet.core.DAG)6 Map (java.util.Map)6 Function (java.util.function.Function)6 JetException (com.hazelcast.jet.JetException)5 Edge (com.hazelcast.jet.core.Edge)5 ItemsByTag (com.hazelcast.jet.datamodel.ItemsByTag)5 ArrayList (java.util.ArrayList)5 Collections.singletonList (java.util.Collections.singletonList)5 Nonnull (javax.annotation.Nonnull)5 LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)4 AggregateOperation1 (com.hazelcast.jet.aggregate.AggregateOperation1)4 AggregateOperations.coAggregateOperationBuilder (com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder)4 CoAggregateOperationBuilder (com.hazelcast.jet.aggregate.CoAggregateOperationBuilder)4 Tag (com.hazelcast.jet.datamodel.Tag)4 Tuple3 (com.hazelcast.jet.datamodel.Tuple3)4 Tuple3.tuple3 (com.hazelcast.jet.datamodel.Tuple3.tuple3)4