Search in sources :

Example 1 with ComputeTaskSession

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

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

the class EventsExample method remoteListen.

/**
     * Listen to events coming from all cluster nodes.
     *
     * @throws IgniteException If failed.
     */
private static void remoteListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Remote event listener example.");
    // This optional local callback is called for each event notification
    // that passed remote predicate listener.
    IgniteBiPredicate<UUID, TaskEvent> locLsnr = (nodeId, evt) -> {
        // Remote filter only accepts tasks whose name being with "good-task" prefix.
        assert evt.taskName().startsWith("good-task");
        System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());
        // Return true to continue listening.
        return true;
    };
    // Remote filter which only accepts tasks whose name begins with "good-task" prefix.
    IgnitePredicate<TaskEvent> rmtLsnr = evt -> evt.taskName().startsWith("good-task");
    Ignite ignite = Ignition.ignite();
    // Register event listeners on all nodes to listen for task events.
    ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    for (int i = 0; i < 10; i++) {
        ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() {

            // Auto-inject task session.
            @TaskSessionResource
            private ComputeTaskSession ses;

            @Override
            public void run() {
                System.out.println("Executing sample job for task: " + ses.getTaskName());
            }
        });
    }
}
Also used : EVTS_TASK_EXECUTION(org.apache.ignite.events.EventType.EVTS_TASK_EXECUTION) Ignition(org.apache.ignite.Ignition) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) IgniteException(org.apache.ignite.IgniteException) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) TaskEvent(org.apache.ignite.events.TaskEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) UUID(java.util.UUID) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 3 with ComputeTaskSession

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

the class EventsExample method remoteListen.

/**
     * Listen to events coming from all cluster nodes.
     *
     * @throws IgniteException If failed.
     */
private static void remoteListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Remote event listener example.");
    // This optional local callback is called for each event notification
    // that passed remote predicate listener.
    IgniteBiPredicate<UUID, TaskEvent> locLsnr = new IgniteBiPredicate<UUID, TaskEvent>() {

        @Override
        public boolean apply(UUID nodeId, TaskEvent evt) {
            // Remote filter only accepts tasks whose name being with "good-task" prefix.
            assert evt.taskName().startsWith("good-task");
            System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());
            // Return true to continue listening.
            return true;
        }
    };
    // Remote filter which only accepts tasks whose name begins with "good-task" prefix.
    IgnitePredicate<TaskEvent> rmtLsnr = new IgnitePredicate<TaskEvent>() {

        @Override
        public boolean apply(TaskEvent evt) {
            return evt.taskName().startsWith("good-task");
        }
    };
    Ignite ignite = Ignition.ignite();
    // Register event listeners on all nodes to listen for task events.
    ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    for (int i = 0; i < 10; i++) {
        ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() {

            // Auto-inject task session.
            @TaskSessionResource
            private ComputeTaskSession ses;

            @Override
            public void run() {
                System.out.println("Executing sample job for task: " + ses.getTaskName());
            }
        });
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) UUID(java.util.UUID) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 4 with ComputeTaskSession

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

the class ComputeTaskInternalFuture method finishedFuture.

/**
     * @param ctx Context.
     * @param taskCls Task class.
     * @param e Error.
     * @return Finished task future.
     */
public static <R> ComputeTaskInternalFuture<R> finishedFuture(final GridKernalContext ctx, final Class<?> taskCls, IgniteCheckedException e) {
    assert ctx != null;
    assert taskCls != null;
    assert e != null;
    final long time = U.currentTimeMillis();
    final IgniteUuid id = IgniteUuid.fromUuid(ctx.localNodeId());
    ComputeTaskSession ses = new ComputeTaskSession() {

        @Override
        public String getTaskName() {
            return taskCls.getName();
        }

        @Override
        public UUID getTaskNodeId() {
            return ctx.localNodeId();
        }

        @Override
        public long getStartTime() {
            return time;
        }

        @Override
        public long getEndTime() {
            return time;
        }

        @Override
        public IgniteUuid getId() {
            return id;
        }

        @Override
        public ClassLoader getClassLoader() {
            return null;
        }

        @Override
        public Collection<ComputeJobSibling> getJobSiblings() throws IgniteException {
            return Collections.emptyList();
        }

        @Override
        public Collection<ComputeJobSibling> refreshJobSiblings() throws IgniteException {
            return Collections.emptyList();
        }

        @Nullable
        @Override
        public ComputeJobSibling getJobSibling(IgniteUuid jobId) throws IgniteException {
            return null;
        }

        @Override
        public void setAttribute(Object key, @Nullable Object val) throws IgniteException {
        }

        @Nullable
        @Override
        public <K, V> V getAttribute(K key) {
            return null;
        }

        @Override
        public void setAttributes(Map<?, ?> attrs) throws IgniteException {
        // No-op.
        }

        @Override
        public Map<?, ?> getAttributes() {
            return Collections.emptyMap();
        }

        @Override
        public void addAttributeListener(ComputeTaskSessionAttributeListener lsnr, boolean rewind) {
        // No-op.
        }

        @Override
        public boolean removeAttributeListener(ComputeTaskSessionAttributeListener lsnr) {
            return false;
        }

        @Override
        public <K, V> V waitForAttribute(K key, long timeout) throws InterruptedException {
            throw new InterruptedException("Session was closed.");
        }

        @Override
        public <K, V> boolean waitForAttribute(K key, @Nullable V val, long timeout) throws InterruptedException {
            throw new InterruptedException("Session was closed.");
        }

        @Override
        public Map<?, ?> waitForAttributes(Collection<?> keys, long timeout) throws InterruptedException {
            throw new InterruptedException("Session was closed.");
        }

        @Override
        public boolean waitForAttributes(Map<?, ?> attrs, long timeout) throws InterruptedException {
            throw new InterruptedException("Session was closed.");
        }

        @Override
        public void saveCheckpoint(String key, Object state) {
            throw new IgniteException("Session was closed.");
        }

        @Override
        public void saveCheckpoint(String key, Object state, ComputeTaskSessionScope scope, long timeout) {
            throw new IgniteException("Session was closed.");
        }

        @Override
        public void saveCheckpoint(String key, Object state, ComputeTaskSessionScope scope, long timeout, boolean overwrite) {
            throw new IgniteException("Session was closed.");
        }

        @Nullable
        @Override
        public <T> T loadCheckpoint(String key) throws IgniteException {
            throw new IgniteException("Session was closed.");
        }

        @Override
        public boolean removeCheckpoint(String key) throws IgniteException {
            throw new IgniteException("Session was closed.");
        }

        @Override
        public Collection<UUID> getTopology() {
            return Collections.emptyList();
        }

        @Override
        public IgniteFuture<?> mapFuture() {
            return new IgniteFinishedFutureImpl<Object>();
        }
    };
    ComputeTaskInternalFuture<R> fut = new ComputeTaskInternalFuture<>(ses, ctx);
    fut.onDone(e);
    return fut;
}
Also used : ComputeTaskSessionScope(org.apache.ignite.compute.ComputeTaskSessionScope) ComputeTaskSessionAttributeListener(org.apache.ignite.compute.ComputeTaskSessionAttributeListener) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteException(org.apache.ignite.IgniteException) Collection(java.util.Collection) ComputeJobSibling(org.apache.ignite.compute.ComputeJobSibling) UUID(java.util.UUID) Map(java.util.Map) IgniteFinishedFutureImpl(org.apache.ignite.internal.util.future.IgniteFinishedFutureImpl) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ComputeTaskSession

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

the class PriorityQueueCollisionSpi method getJobPriority.

/**
     * Gets job priority. At first tries to get from job context. If job context has no priority,
     * then tries to get from task session. If task session has no priority default one will be used.
     *
     * @param ctx Collision job context.
     * @return Job priority.
     */
private int getJobPriority(CollisionJobContext ctx) {
    assert ctx != null;
    Integer p = null;
    ComputeJobContext jctx = ctx.getJobContext();
    try {
        p = (Integer) jctx.getAttribute(jobPriAttrKey);
    } catch (ClassCastException e) {
        LT.error(log, e, "Type of job context priority attribute '" + jobPriAttrKey + "' is not java.lang.Integer [type=" + jctx.getAttribute(jobPriAttrKey).getClass() + ']');
    }
    if (p == null) {
        ComputeTaskSession ses = ctx.getTaskSession();
        try {
            p = (Integer) ses.getAttribute(taskPriAttrKey);
        } catch (ClassCastException e) {
            LT.error(log, e, "Type of task session priority attribute '" + taskPriAttrKey + "' is not java.lang.Integer [type=" + ses.getAttribute(taskPriAttrKey).getClass() + ']');
        }
        if (p == null) {
            if (log.isDebugEnabled()) {
                log.debug("Failed get priority from job context attribute '" + jobPriAttrKey + "' and task session attribute '" + taskPriAttrKey + "' (will use default priority): " + dfltPri);
            }
            p = dfltPri;
        }
    }
    assert p != null;
    return p;
}
Also used : ComputeJobContext(org.apache.ignite.compute.ComputeJobContext) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Aggregations

ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)19 ClusterNode (org.apache.ignite.cluster.ClusterNode)12 UUID (java.util.UUID)10 GridTestTaskSession (org.apache.ignite.GridTestTaskSession)10 GridTestJob (org.apache.ignite.GridTestJob)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Ignite (org.apache.ignite.Ignite)5 IgniteException (org.apache.ignite.IgniteException)4 TaskEvent (org.apache.ignite.events.TaskEvent)4 GridTestNode (org.apache.ignite.testframework.GridTestNode)4 HashMap (java.util.HashMap)3 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ComputeJobAdapter (org.apache.ignite.compute.ComputeJobAdapter)2 ComputeJobSibling (org.apache.ignite.compute.ComputeJobSibling)2 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)2 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)2 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)2 TaskSessionResource (org.apache.ignite.resources.TaskSessionResource)2