Search in sources :

Example 16 with ProcessorSupplier

use of com.hazelcast.jet.core.ProcessorSupplier in project hazelcast-jet by hazelcast.

the class ExecutionPlanBuilder method createExecutionPlans.

public static Map<MemberInfo, ExecutionPlan> createExecutionPlans(NodeEngine nodeEngine, MembersView membersView, DAG dag, JobConfig jobConfig, long lastSnapshotId) {
    final JetInstance instance = getJetInstance(nodeEngine);
    final int defaultParallelism = instance.getConfig().getInstanceConfig().getCooperativeThreadCount();
    final Collection<MemberInfo> members = new HashSet<>(membersView.size());
    final Address[] partitionOwners = new Address[nodeEngine.getPartitionService().getPartitionCount()];
    initPartitionOwnersAndMembers(nodeEngine, membersView, members, partitionOwners);
    final List<Address> addresses = members.stream().map(MemberInfo::getAddress).collect(toList());
    final int clusterSize = members.size();
    final boolean isJobDistributed = clusterSize > 1;
    final EdgeConfig defaultEdgeConfig = instance.getConfig().getDefaultEdgeConfig();
    final Map<MemberInfo, ExecutionPlan> plans = members.stream().collect(toMap(m -> m, m -> new ExecutionPlan(partitionOwners, jobConfig, lastSnapshotId)));
    final Map<String, Integer> vertexIdMap = assignVertexIds(dag);
    for (Entry<String, Integer> entry : vertexIdMap.entrySet()) {
        final Vertex vertex = dag.getVertex(entry.getKey());
        final ProcessorMetaSupplier metaSupplier = vertex.getMetaSupplier();
        final int vertexId = entry.getValue();
        final int localParallelism = determineParallelism(vertex, metaSupplier.preferredLocalParallelism(), defaultParallelism);
        final int totalParallelism = localParallelism * clusterSize;
        final List<EdgeDef> inbound = toEdgeDefs(dag.getInboundEdges(vertex.getName()), defaultEdgeConfig, e -> vertexIdMap.get(e.getSourceName()), isJobDistributed);
        final List<EdgeDef> outbound = toEdgeDefs(dag.getOutboundEdges(vertex.getName()), defaultEdgeConfig, e -> vertexIdMap.get(e.getDestName()), isJobDistributed);
        final ILogger logger = nodeEngine.getLogger(String.format("%s.%s#ProcessorMetaSupplier", metaSupplier.getClass().getName(), vertex.getName()));
        metaSupplier.init(new MetaSupplierCtx(instance, logger, vertex.getName(), localParallelism, totalParallelism));
        Function<Address, ProcessorSupplier> procSupplierFn = metaSupplier.get(addresses);
        int procIdxOffset = 0;
        for (Entry<MemberInfo, ExecutionPlan> e : plans.entrySet()) {
            final ProcessorSupplier processorSupplier = procSupplierFn.apply(e.getKey().getAddress());
            checkSerializable(processorSupplier, "ProcessorSupplier in vertex '" + vertex.getName() + '\'');
            final VertexDef vertexDef = new VertexDef(vertexId, vertex.getName(), processorSupplier, procIdxOffset, localParallelism, totalParallelism);
            vertexDef.addInboundEdges(inbound);
            vertexDef.addOutboundEdges(outbound);
            e.getValue().addVertex(vertexDef);
            procIdxOffset += localParallelism;
        }
    }
    return plans;
}
Also used : EdgeConfig(com.hazelcast.jet.config.EdgeConfig) JetInstance(com.hazelcast.jet.JetInstance) Util.checkSerializable(com.hazelcast.jet.impl.util.Util.checkSerializable) Address(com.hazelcast.nio.Address) LOCAL_PARALLELISM_USE_DEFAULT(com.hazelcast.jet.core.Vertex.LOCAL_PARALLELISM_USE_DEFAULT) Function(java.util.function.Function) HashSet(java.util.HashSet) 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) MembersView(com.hazelcast.internal.cluster.impl.MembersView) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) Integer.min(java.lang.Integer.min) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Collection(java.util.Collection) Util.getJetInstance(com.hazelcast.jet.impl.util.Util.getJetInstance) JobConfig(com.hazelcast.jet.config.JobConfig) IPartitionService(com.hazelcast.spi.partition.IPartitionService) MetaSupplierCtx(com.hazelcast.jet.impl.execution.init.Contexts.MetaSupplierCtx) NodeEngine(com.hazelcast.spi.NodeEngine) Vertex(com.hazelcast.jet.core.Vertex) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Entry(java.util.Map.Entry) TopologyChangedException(com.hazelcast.jet.core.TopologyChangedException) Vertex(com.hazelcast.jet.core.Vertex) Address(com.hazelcast.nio.Address) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) ILogger(com.hazelcast.logging.ILogger) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) HashSet(java.util.HashSet) JetInstance(com.hazelcast.jet.JetInstance) Util.getJetInstance(com.hazelcast.jet.impl.util.Util.getJetInstance) EdgeConfig(com.hazelcast.jet.config.EdgeConfig) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) MetaSupplierCtx(com.hazelcast.jet.impl.execution.init.Contexts.MetaSupplierCtx)

Example 17 with ProcessorSupplier

use of com.hazelcast.jet.core.ProcessorSupplier in project hazelcast-jet by hazelcast.

the class StreamFilesPTest method when_metaSupplier_then_returnsCorrectProcessors.

@Test
public void when_metaSupplier_then_returnsCorrectProcessors() {
    ProcessorMetaSupplier metaSupplier = streamFilesP(workDir.getAbsolutePath(), UTF_8, "*", Util::entry);
    Address a = new Address();
    ProcessorSupplier supplier = metaSupplier.get(singletonList(a)).apply(a);
    supplier.init(new TestProcessorContext());
    assertEquals(1, supplier.get(1).size());
    supplier.close(null);
}
Also used : Address(com.hazelcast.nio.Address) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) IOUtil(com.hazelcast.nio.IOUtil) Util(com.hazelcast.jet.Util) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Test(org.junit.Test)

Example 18 with ProcessorSupplier

use of com.hazelcast.jet.core.ProcessorSupplier in project hazelcast-jet by hazelcast.

the class TestSupportTest method when_processorSupplierTested_then_completeCalled.

@Test
public void when_processorSupplierTested_then_completeCalled() {
    boolean[] completeCalled = { false };
    ProcessorSupplier supplier = new ProcessorSupplier() {

        @Nonnull
        @Override
        public Collection<? extends Processor> get(int count) {
            assertEquals(1, count);
            return singletonList(noopP().get());
        }

        @Override
        public void close(Throwable error) {
            completeCalled[0] = true;
        }
    };
    TestSupport.verifyProcessor(supplier).expectOutput(emptyList());
    assertTrue("PS.complete not called", completeCalled[0]);
    // test once more with PMS
    completeCalled[0] = false;
    TestSupport.verifyProcessor(ProcessorMetaSupplier.of(supplier)).expectOutput(emptyList());
    assertTrue("PS.complete not called when using PMS", completeCalled[0]);
}
Also used : ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) Test(org.junit.Test)

Example 19 with ProcessorSupplier

use of com.hazelcast.jet.core.ProcessorSupplier in project hazelcast-jet-reference-manual by hazelcast.

the class S8 method get.

@Override
@Nonnull
public Function<Address, ProcessorSupplier> get(@Nonnull List<Address> addresses) {
    Map<Address, ProcessorSupplier> map = new HashMap<>();
    for (int i = 0; i < addresses.size(); i++) {
        // We'll calculate the global index of each processor in the cluster:
        int globalIndexBase = localParallelism * i;
        // Capture the value of the transient field for the lambdas below:
        int divisor = totalParallelism;
        // processorCount will be equal to localParallelism:
        ProcessorSupplier supplier = processorCount -> range(globalIndexBase, globalIndexBase + processorCount).mapToObj(globalIndex -> new GenerateNumbersP(upperBound, divisor, globalIndex)).collect(toList());
        map.put(addresses.get(i), supplier);
    }
    return map::get;
}
Also used : IntStream(java.util.stream.IntStream) AbstractProcessor(com.hazelcast.jet.core.AbstractProcessor) Traverser(com.hazelcast.jet.Traverser) DiagnosticProcessors(com.hazelcast.jet.core.processor.DiagnosticProcessors) JetInstance(com.hazelcast.jet.JetInstance) IntStream.range(java.util.stream.IntStream.range) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Processor(com.hazelcast.jet.core.Processor) Address(com.hazelcast.nio.Address) HashMap(java.util.HashMap) Traversers(com.hazelcast.jet.Traversers) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Vertex(com.hazelcast.jet.core.Vertex) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Map(java.util.Map) Context(com.hazelcast.jet.core.Processor.Context) Jet(com.hazelcast.jet.Jet) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) Nonnull(javax.annotation.Nonnull) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) Address(com.hazelcast.nio.Address) HashMap(java.util.HashMap) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) Nonnull(javax.annotation.Nonnull)

Example 20 with ProcessorSupplier

use of com.hazelcast.jet.core.ProcessorSupplier in project hazelcast by hazelcast.

the class TestSupportTest method test_processorSupplierHasJetInstance.

@Test
public void test_processorSupplierHasJetInstance() {
    HazelcastInstance hazelcastInstance = mockHazelcastInstance();
    boolean[] called = { false };
    verifyProcessor(new ProcessorSupplier() {

        @Override
        public void init(@Nonnull Context context) {
            assertSame(context.hazelcastInstance(), hazelcastInstance);
            called[0] = true;
        }

        @Nonnull
        @Override
        public Collection<? extends Processor> get(int count) {
            assertEquals(1, count);
            return singletonList(new MockP());
        }
    }).hazelcastInstance(hazelcastInstance).expectOutput(emptyList());
    assertTrue(called[0]);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Processor(com.hazelcast.jet.core.Processor) TestSupport.verifyProcessor(com.hazelcast.jet.core.test.TestSupport.verifyProcessor) Nonnull(javax.annotation.Nonnull) MockP(com.hazelcast.jet.core.TestProcessors.MockP) Collection(java.util.Collection) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ProcessorSupplier (com.hazelcast.jet.core.ProcessorSupplier)29 ProcessorMetaSupplier (com.hazelcast.jet.core.ProcessorMetaSupplier)14 Nonnull (javax.annotation.Nonnull)10 Test (org.junit.Test)9 List (java.util.List)8 Function (java.util.function.Function)8 JobConfig (com.hazelcast.jet.config.JobConfig)6 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)6 QuickTest (com.hazelcast.test.annotation.QuickTest)6 Traverser (com.hazelcast.jet.Traverser)5 DAG (com.hazelcast.jet.core.DAG)5 Processor (com.hazelcast.jet.core.Processor)5 Vertex (com.hazelcast.jet.core.Vertex)5 EdgeConfig (com.hazelcast.jet.config.EdgeConfig)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 FunctionEx (com.hazelcast.function.FunctionEx)3 Edge (com.hazelcast.jet.core.Edge)3 Address (com.hazelcast.nio.Address)3 Map (java.util.Map)3 Assert.assertEquals (org.junit.Assert.assertEquals)3