Search in sources :

Example 6 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable 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 7 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridIoManager method processOrderedMessage.

/**
     * @param nodeId Node ID.
     * @param msg Ordered message.
     * @param plc Execution policy.
     * @param msgC Closure to call when message processing finished ({@code null} for sync processing).
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
private void processOrderedMessage(final UUID nodeId, final GridIoMessage msg, final byte plc, @Nullable final IgniteRunnable msgC) throws IgniteCheckedException {
    assert msg != null;
    long timeout = msg.timeout();
    boolean skipOnTimeout = msg.skipOnTimeout();
    boolean isNew = false;
    ConcurrentMap<UUID, GridCommunicationMessageSet> map;
    GridCommunicationMessageSet set = null;
    while (true) {
        map = msgSetMap.get(msg.topic());
        if (map == null) {
            set = new GridCommunicationMessageSet(plc, msg.topic(), nodeId, timeout, skipOnTimeout, msg, msgC);
            map = new ConcurrentHashMap0<>();
            map.put(nodeId, set);
            ConcurrentMap<UUID, GridCommunicationMessageSet> old = msgSetMap.putIfAbsent(msg.topic(), map);
            if (old != null)
                map = old;
            else {
                isNew = true;
                // Put succeeded.
                break;
            }
        }
        boolean rmv = false;
        synchronized (map) {
            if (map.isEmpty())
                rmv = true;
            else {
                set = map.get(nodeId);
                if (set == null) {
                    GridCommunicationMessageSet old = map.putIfAbsent(nodeId, set = new GridCommunicationMessageSet(plc, msg.topic(), nodeId, timeout, skipOnTimeout, msg, msgC));
                    assert old == null;
                    isNew = true;
                    // Put succeeded.
                    break;
                }
            }
        }
        if (rmv)
            msgSetMap.remove(msg.topic(), map);
        else {
            assert set != null;
            assert !isNew;
            set.add(msg, msgC);
            break;
        }
    }
    if (isNew && ctx.discovery().node(nodeId) == null) {
        if (log.isDebugEnabled())
            log.debug("Message is ignored as sender has left the grid: " + msg);
        assert map != null;
        boolean rmv;
        synchronized (map) {
            map.remove(nodeId);
            rmv = map.isEmpty();
        }
        if (rmv)
            msgSetMap.remove(msg.topic(), map);
        return;
    }
    if (isNew && set.endTime() != Long.MAX_VALUE)
        ctx.timeout().addTimeoutObject(set);
    final GridMessageListener lsnr = listenerGet0(msg.topic());
    if (lsnr == null) {
        if (closedTopics.contains(msg.topic())) {
            if (log.isDebugEnabled())
                log.debug("Message is ignored as it came for the closed topic: " + msg);
            assert map != null;
            msgSetMap.remove(msg.topic(), map);
        } else if (log.isDebugEnabled()) {
            // Note that we simply keep messages if listener is not
            // registered yet, until one will be registered.
            log.debug("Received message for unknown listener (messages will be kept until a " + "listener is registered): " + msg);
        }
        // may stop.
        if (msgC != null)
            msgC.run();
        return;
    }
    if (msgC == null) {
        // Message from local node can be processed in sync manner.
        assert locNodeId.equals(nodeId);
        unwindMessageSet(set, lsnr);
        return;
    }
    final GridCommunicationMessageSet msgSet0 = set;
    Runnable c = new Runnable() {

        @Override
        public void run() {
            try {
                threadProcessingMessage(true, msgC);
                unwindMessageSet(msgSet0, lsnr);
            } finally {
                threadProcessingMessage(false, null);
            }
        }
    };
    try {
        pools.poolForPolicy(plc).execute(c);
    } catch (RejectedExecutionException e) {
        U.error(log, "Failed to process ordered message due to execution rejection. " + "Increase the upper bound on executor service provided by corresponding " + "configuration property. Will attempt to process message in the listener " + "thread instead [msgPlc=" + plc + ']', e);
        c.run();
    }
}
Also used : IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 8 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridIoManager method processRegularMessage.

/**
     * @param nodeId Node ID.
     * @param msg Message.
     * @param plc Execution policy.
     * @param msgC Closure to call when message processing finished.
     * @throws IgniteCheckedException If failed.
     */
private void processRegularMessage(final UUID nodeId, final GridIoMessage msg, final byte plc, final IgniteRunnable msgC) throws IgniteCheckedException {
    Runnable c = new Runnable() {

        @Override
        public void run() {
            try {
                threadProcessingMessage(true, msgC);
                processRegularMessage0(msg, nodeId);
            } finally {
                threadProcessingMessage(false, null);
                msgC.run();
            }
        }

        @Override
        public String toString() {
            return "Message closure [msg=" + msg + ']';
        }
    };
    if (msg.topicOrdinal() == TOPIC_IO_TEST.ordinal()) {
        IgniteIoTestMessage msg0 = (IgniteIoTestMessage) msg.message();
        if (msg0.processFromNioThread())
            c.run();
        else
            ctx.getStripedExecutorService().execute(-1, c);
        return;
    }
    if (plc == GridIoPolicy.SYSTEM_POOL && msg.partition() != GridIoMessage.STRIPE_DISABLED_PART) {
        ctx.getStripedExecutorService().execute(msg.partition(), c);
        return;
    }
    if (msg.topicOrdinal() == TOPIC_IO_TEST.ordinal()) {
        IgniteIoTestMessage msg0 = (IgniteIoTestMessage) msg.message();
        if (msg0.processFromNioThread()) {
            c.run();
            return;
        }
    }
    try {
        String execName = msg.executorName();
        if (execName != null) {
            Executor exec = pools.customExecutor(execName);
            if (exec != null) {
                exec.execute(c);
                return;
            } else {
                LT.warn(log, "Custom executor doesn't exist (message will be processed in default " + "thread pool): " + execName);
            }
        }
        pools.poolForPolicy(plc).execute(c);
    } catch (RejectedExecutionException e) {
        U.error(log, "Failed to process regular message due to execution rejection. Will attempt to process " + "message in the listener thread instead.", e);
        c.run();
    }
}
Also used : Executor(java.util.concurrent.Executor) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 9 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridIoManager method processP2PMessage.

/**
     * @param nodeId Node ID.
     * @param msg Message.
     * @param msgC Closure to call when message processing finished.
     */
private void processP2PMessage(final UUID nodeId, final GridIoMessage msg, final IgniteRunnable msgC) {
    Runnable c = new Runnable() {

        @Override
        public void run() {
            try {
                threadProcessingMessage(true, msgC);
                GridMessageListener lsnr = listenerGet0(msg.topic());
                if (lsnr == null)
                    return;
                Object obj = msg.message();
                assert obj != null;
                invokeListener(msg.policy(), lsnr, nodeId, obj);
            } finally {
                threadProcessingMessage(false, null);
                msgC.run();
            }
        }
    };
    try {
        pools.p2pPool().execute(c);
    } catch (RejectedExecutionException e) {
        U.error(log, "Failed to process P2P message due to execution rejection. Increase the upper bound " + "on 'ExecutorService' provided by 'IgniteConfiguration.getPeerClassLoadingThreadPoolSize()'. " + "Will attempt to process message in the listener thread instead.", e);
        c.run();
    }
}
Also used : IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 10 with IgniteRunnable

use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.

the class GridBinaryAffinityKeySelfTest method testAffinityRun.

/**
     * @throws Exception If failed.
     */
public void testAffinityRun() throws Exception {
    Affinity<Object> aff = grid(0).affinity(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 1000; i++) {
        nodeId.set(null);
        grid(0).compute().affinityRun(DEFAULT_CACHE_NAME, new TestObject(i), new IgniteRunnable() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public void run() {
                nodeId.set(ignite.configuration().getNodeId());
            }
        });
        assertEquals(aff.mapKeyToNode(i).id(), nodeId.get());
        grid(0).compute().affinityRun(DEFAULT_CACHE_NAME, new AffinityKey(0, i), new IgniteRunnable() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public void run() {
                nodeId.set(ignite.configuration().getNodeId());
            }
        });
        assertEquals(aff.mapKeyToNode(i).id(), nodeId.get());
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Aggregations

IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)45 Ignite (org.apache.ignite.Ignite)21 IgniteException (org.apache.ignite.IgniteException)11 IgniteEx (org.apache.ignite.internal.IgniteEx)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 UUID (java.util.UUID)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 IgniteCompute (org.apache.ignite.IgniteCompute)4 TaskEvent (org.apache.ignite.events.TaskEvent)4 IgniteFuture (org.apache.ignite.lang.IgniteFuture)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Event (org.apache.ignite.events.Event)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Collection (java.util.Collection)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2