Search in sources :

Example 1 with ComputeJobAdapter

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

the class SingleSplitTestTask method map.

/** {@inheritDoc} */
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, Integer arg) {
    assert !subgrid.isEmpty() : "Subgrid cannot be empty.";
    Map<ComputeJobAdapter, ClusterNode> jobs = new HashMap<>(subgrid.size());
    taskSes.setAttribute("1st", "1");
    taskSes.setAttribute("2nd", "2");
    Collection<UUID> assigned = new ArrayList<>(subgrid.size());
    for (int i = 0; i < arg; i++) {
        ComputeJobAdapter job = new ComputeJobAdapter(1) {

            /** */
            @TaskSessionResource
            private ComputeTaskSession jobSes;

            /** {@inheritDoc} */
            @Override
            public Object execute() {
                assert jobSes != null;
                Integer arg = this.<Integer>argument(0);
                assert arg != null;
                return new SingleSplitTestJobTarget().executeLoadTestJob(arg, jobSes);
            }
        };
        ClusterNode node = balancer.getBalancedNode(job, null);
        assert node != null;
        assigned.add(node.id());
        jobs.put(job, node);
    }
    taskSes.setAttribute("nodes", assigned);
    return jobs;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ComputeJobAdapter(org.apache.ignite.compute.ComputeJobAdapter) UUID(java.util.UUID) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 2 with ComputeJobAdapter

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

the class GridContinuousMapperTask1 method sendJob.

/**
     * Sends job to node.
     *
     * @param n Node.
     * @throws IgniteException If failed.
     */
private void sendJob(ClusterNode n) {
    try {
        int jobId = queue.take();
        sentJobs.incrementAndGet();
        mapper.send(new ComputeJobAdapter(jobId) {

            @IgniteInstanceResource
            private Ignite g;

            @Override
            public Object execute() {
                Integer jobId = argument(0);
                X.println(">>> Received job for ID: " + jobId);
                return g.cache("replicated").localPeek(jobId, CachePeekMode.ONHEAP);
            }
        }, n);
    } catch (InterruptedException e) {
        throw new IgniteException(e);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteException(org.apache.ignite.IgniteException) ComputeJobAdapter(org.apache.ignite.compute.ComputeJobAdapter) Ignite(org.apache.ignite.Ignite)

Example 3 with ComputeJobAdapter

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

the class GridSingleSplitNewNodesTestTask method map.

/** {@inheritDoc} */
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, Integer arg) {
    assert !subgrid.isEmpty() : "Subgrid cannot be empty.";
    Map<ComputeJobAdapter, ClusterNode> jobs = new HashMap<>(subgrid.size());
    taskSes.setAttribute("1st", "1");
    taskSes.setAttribute("2nd", "2");
    Collection<UUID> assigned = new ArrayList<>(subgrid.size());
    for (int i = 0; i < arg; i++) {
        ComputeJobAdapter job = new ComputeJobAdapter(1) {

            /** */
            @TaskSessionResource
            private ComputeTaskSession jobSes;

            /** {@inheritDoc} */
            @Override
            public Serializable execute() {
                assert jobSes != null;
                Integer arg = this.<Integer>argument(0);
                assert arg != null;
                return new GridSingleSplitNewNodesTestJobTarget().executeLoadTestJob(arg, jobSes);
            }
        };
        ClusterNode node = balancer.getBalancedNode(job, null);
        assert node != null;
        assigned.add(node.id());
        jobs.put(job, node);
    }
    taskSes.setAttribute("nodes", assigned);
    return jobs;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ComputeJobAdapter(org.apache.ignite.compute.ComputeJobAdapter) UUID(java.util.UUID) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 4 with ComputeJobAdapter

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

the class GridContinuousMapperTask2 method map.

/** {@inheritDoc} */
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable int[] jobIds) {
    Map<ComputeJob, ClusterNode> mappings = new HashMap<>(jobIds.length);
    Iterator<ClusterNode> nodeIter = g.cluster().forRemotes().nodes().iterator();
    for (int jobId : jobIds) {
        ComputeJob job = new ComputeJobAdapter(jobId) {

            @IgniteInstanceResource
            private Ignite g;

            @Override
            public Object execute() {
                Integer jobId = argument(0);
                X.println(">>> Received job for ID: " + jobId);
                return g.cache("replicated").localPeek(jobId, CachePeekMode.ONHEAP);
            }
        };
        // If only local node in the grid.
        if (g.cluster().nodes().size() == 1)
            mappings.put(job, g.cluster().localNode());
        else {
            ClusterNode n = nodeIter.hasNext() ? nodeIter.next() : (nodeIter = g.cluster().forRemotes().nodes().iterator()).next();
            mappings.put(job, n);
        }
    }
    return mappings;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) HashMap(java.util.HashMap) ComputeJobAdapter(org.apache.ignite.compute.ComputeJobAdapter) Ignite(org.apache.ignite.Ignite)

Example 5 with ComputeJobAdapter

use of org.apache.ignite.compute.ComputeJobAdapter 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

ComputeJobAdapter (org.apache.ignite.compute.ComputeJobAdapter)6 HashMap (java.util.HashMap)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 ArrayList (java.util.ArrayList)3 Ignite (org.apache.ignite.Ignite)3 UUID (java.util.UUID)2 IgniteException (org.apache.ignite.IgniteException)2 ComputeJob (org.apache.ignite.compute.ComputeJob)2 ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)2 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)2 Serializable (java.io.Serializable)1 Iterator (java.util.Iterator)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CacheException (javax.cache.CacheException)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 LoggerResource (org.apache.ignite.resources.LoggerResource)1 Nullable (org.jetbrains.annotations.Nullable)1 AbstractBeanFactory (org.springframework.beans.factory.support.AbstractBeanFactory)1 XmlBeanFactory (org.springframework.beans.factory.xml.XmlBeanFactory)1