Search in sources :

Example 56 with Message

use of org.apache.ignite.plugin.extensions.communication.Message in project ignite by apache.

the class IgniteH2Indexing method send.

/**
 * @param topic Topic.
 * @param topicOrd Topic ordinal for {@link GridTopic}.
 * @param nodes Nodes.
 * @param msg Message.
 * @param specialize Optional closure to specialize message for each node.
 * @param locNodeHnd Handler for local node.
 * @param plc Policy identifying the executor service which will process message.
 * @param runLocParallel Run local handler in parallel thread.
 * @return {@code true} If all messages sent successfully.
 */
public boolean send(Object topic, int topicOrd, Collection<ClusterNode> nodes, Message msg, @Nullable IgniteBiClosure<ClusterNode, Message, Message> specialize, @Nullable final IgniteInClosure2X<ClusterNode, Message> locNodeHnd, byte plc, boolean runLocParallel) {
    boolean ok = true;
    if (specialize == null && msg instanceof GridCacheQueryMarshallable)
        ((GridCacheQueryMarshallable) msg).marshall(marshaller);
    ClusterNode locNode = null;
    for (ClusterNode node : nodes) {
        if (node.isLocal()) {
            if (locNode != null)
                throw new IllegalStateException();
            locNode = node;
            continue;
        }
        try {
            if (specialize != null) {
                msg = specialize.apply(node, msg);
                if (msg instanceof GridCacheQueryMarshallable)
                    ((GridCacheQueryMarshallable) msg).marshall(marshaller);
            }
            ctx.io().sendGeneric(node, topic, topicOrd, msg, plc);
        } catch (IgniteCheckedException e) {
            ok = false;
            U.warn(log, "Failed to send message [node=" + node + ", msg=" + msg + ", errMsg=" + e.getMessage() + "]");
        }
    }
    // Local node goes the last to allow parallel execution.
    if (locNode != null) {
        assert locNodeHnd != null;
        if (specialize != null)
            msg = specialize.apply(locNode, msg);
        if (runLocParallel) {
            final ClusterNode finalLocNode = locNode;
            final Message finalMsg = msg;
            try {
                // We prefer runLocal to runLocalSafe, because the latter can produce deadlock here.
                ctx.closure().runLocal(new GridPlainRunnable() {

                    @Override
                    public void run() {
                        if (!busyLock.enterBusy())
                            return;
                        try {
                            locNodeHnd.apply(finalLocNode, finalMsg);
                        } finally {
                            busyLock.leaveBusy();
                        }
                    }
                }, plc).listen(logger);
            } catch (IgniteCheckedException e) {
                ok = false;
                U.error(log, "Failed to execute query locally.", e);
            }
        } else
            locNodeHnd.apply(locNode, msg);
    }
    return ok;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Message(org.apache.ignite.plugin.extensions.communication.Message) GridCacheQueryMarshallable(org.apache.ignite.internal.processors.cache.query.GridCacheQueryMarshallable) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable)

Aggregations

Message (org.apache.ignite.plugin.extensions.communication.Message)56 ClusterNode (org.apache.ignite.cluster.ClusterNode)30 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 Ignite (org.apache.ignite.Ignite)14 IgniteException (org.apache.ignite.IgniteException)11 UUID (java.util.UUID)9 TestRecordingCommunicationSpi (org.apache.ignite.internal.TestRecordingCommunicationSpi)9 GridTestMessage (org.apache.ignite.spi.communication.GridTestMessage)9 GridDhtPartitionsFullMessage (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage)8 IgniteTestResources (org.apache.ignite.testframework.junits.IgniteTestResources)8 ArrayList (java.util.ArrayList)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 GridIoMessage (org.apache.ignite.internal.managers.communication.GridIoMessage)7 GridDhtPartitionSupplyMessage (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage)7 HashMap (java.util.HashMap)6 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)6 Collection (java.util.Collection)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)5