Search in sources :

Example 26 with ComputeJob

use of org.apache.ignite.compute.ComputeJob in project ignite by apache.

the class SystemViewComputeJobTest method testComputeTask.

/**
 */
@Test
public void testComputeTask() throws Exception {
    barrier = new CyclicBarrier(2);
    SystemView<ComputeJobView> jobs = server.context().systemView().view(JOBS_VIEW);
    client.compute().executeAsync(new ComputeTask<Object, Object>() {

        @Override
        @NotNull
        public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable Object arg) throws IgniteException {
            return Collections.singletonMap(new ComputeJob() {

                @Override
                public void cancel() {
                // No-op.
                }

                @Override
                public Object execute() throws IgniteException {
                    try {
                        barrier.await(TIMEOUT, MILLISECONDS);
                        barrier.await(TIMEOUT, MILLISECONDS);
                    } catch (InterruptedException | BrokenBarrierException | TimeoutException e) {
                        throw new RuntimeException(e);
                    }
                    return 1;
                }
            }, subgrid.get(0));
        }

        @Override
        public ComputeJobResultPolicy result(ComputeJobResult res, List<ComputeJobResult> rcvd) throws IgniteException {
            return null;
        }

        @Nullable
        @Override
        public Object reduce(List<ComputeJobResult> results) throws IgniteException {
            return 1;
        }
    }, 1);
    barrier.await(TIMEOUT, MILLISECONDS);
    assertEquals(1, jobs.size());
    ComputeJobView t = jobs.iterator().next();
    checkJobView(t);
    barrier.await(TIMEOUT, MILLISECONDS);
    boolean res = waitForCondition(() -> jobs.size() == 0, TIMEOUT);
    assertTrue(res);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) NotNull(org.jetbrains.annotations.NotNull) CyclicBarrier(java.util.concurrent.CyclicBarrier) ComputeJobView(org.apache.ignite.spi.systemview.view.ComputeJobView) ComputeJob(org.apache.ignite.compute.ComputeJob) ComputeJobResultPolicy(org.apache.ignite.compute.ComputeJobResultPolicy) IgniteException(org.apache.ignite.IgniteException) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable) TimeoutException(java.util.concurrent.TimeoutException) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 27 with ComputeJob

use of org.apache.ignite.compute.ComputeJob in project ignite by apache.

the class GridP2PTestTask method map.

/**
 * {@inheritDoc}
 */
@NotNull
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, Object arg) {
    assert subgrid != null;
    assert !subgrid.isEmpty();
    Integer arg1 = null;
    if (arg instanceof GridifyArgument)
        arg1 = (Integer) ((GridifyArgument) arg).getMethodParameters()[0];
    else if (arg instanceof Integer)
        arg1 = (Integer) arg;
    else
        assert false : "Failed to map task (unknown argument type) [type=" + arg.getClass() + ", val=" + arg + ']';
    Map<ComputeJob, ClusterNode> map = new HashMap<>(subgrid.size());
    UUID nodeId = ignite != null ? ignite.configuration().getNodeId() : null;
    for (ClusterNode node : subgrid) if (!node.id().equals(nodeId))
        map.put(new GridP2PTestJob(arg1), node);
    return map;
}
Also used : GridifyArgument(org.apache.ignite.compute.gridify.GridifyArgument) ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) HashMap(java.util.HashMap) UUID(java.util.UUID) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with ComputeJob

use of org.apache.ignite.compute.ComputeJob in project ignite by apache.

the class GarHelloWorldTask method split.

/**
 * {@inheritDoc}
 */
@Override
public Collection<? extends ComputeJob> split(int gridSize, String arg) throws IgniteException {
    // Create Spring context.
    AbstractBeanFactory fac = new XmlBeanFactory(new ClassPathResource("org/apache/ignite/spi/deployment/uri/tasks/gar-spring-bean.xml", getClass().getClassLoader()));
    fac.setBeanClassLoader(getClass().getClassLoader());
    // Load imported bean from GAR/lib folder.
    GarHelloWorldBean bean = (GarHelloWorldBean) fac.getBean("example.bean");
    String msg = bean.getMessage(arg);
    assert msg != null;
    // Split the passed in phrase into multiple words separated by spaces.
    List<String> words = Arrays.asList(msg.split(" "));
    Collection<ComputeJob> jobs = new ArrayList<>(words.size());
    // Use imperative OOP APIs.
    for (String word : words) {
        // Every job gets its own word as an argument.
        jobs.add(new ComputeJobAdapter(word) {

            /*
                 * Simply prints the job's argument.
                 */
            @Nullable
            @Override
            public Serializable execute() {
                System.out.println(">>>");
                System.out.println(">>> Printing '" + argument(0) + "' on this node from grid job.");
                System.out.println(">>>");
                // This job does not return any result.
                return null;
            }
        });
    }
    return jobs;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ComputeJobAdapter(org.apache.ignite.compute.ComputeJobAdapter) ClassPathResource(org.springframework.core.io.ClassPathResource) ComputeJob(org.apache.ignite.compute.ComputeJob) AbstractBeanFactory(org.springframework.beans.factory.support.AbstractBeanFactory) XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ComputeJob (org.apache.ignite.compute.ComputeJob)28 ClusterNode (org.apache.ignite.cluster.ClusterNode)26 HashMap (java.util.HashMap)14 NotNull (org.jetbrains.annotations.NotNull)11 Map (java.util.Map)10 IgniteException (org.apache.ignite.IgniteException)9 Nullable (org.jetbrains.annotations.Nullable)8 UUID (java.util.UUID)7 List (java.util.List)5 ComputeJobResult (org.apache.ignite.compute.ComputeJobResult)5 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)5 Test (org.junit.Test)5 Affinity (org.apache.ignite.cache.affinity.Affinity)4 ArrayList (java.util.ArrayList)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 ComputeJobAdapter (org.apache.ignite.compute.ComputeJobAdapter)3 ComputeJobResultPolicy (org.apache.ignite.compute.ComputeJobResultPolicy)3 Collection (java.util.Collection)2