Search in sources :

Example 6 with GridPlainRunnable

use of org.apache.ignite.internal.util.lang.GridPlainRunnable in project ignite by apache.

the class GridDhtCacheEntry method addReader.

/**
 * @param nodeId Reader to add.
 * @param msgId Message ID.
 * @param topVer Topology version.
 * @return Future for all relevant transactions that were active at the time of adding reader,
 *      or {@code null} if reader was added
 * @throws GridCacheEntryRemovedException If entry was removed.
 */
@SuppressWarnings("unchecked")
@Nullable
public IgniteInternalFuture<Boolean> addReader(UUID nodeId, long msgId, AffinityTopologyVersion topVer) throws GridCacheEntryRemovedException {
    // Don't add local node as reader.
    if (cctx.nodeId().equals(nodeId))
        return null;
    ClusterNode node = cctx.discovery().node(nodeId);
    if (node == null) {
        if (log.isDebugEnabled())
            log.debug("Ignoring near reader because node left the grid: " + nodeId);
        return null;
    }
    // If remote node is (primary?) or back up, don't add it as a reader.
    if (cctx.affinity().partitionBelongs(node, partition(), topVer)) {
        if (log.isDebugEnabled())
            log.debug("Ignoring near reader because remote node is affinity node [locNodeId=" + cctx.localNodeId() + ", rmtNodeId=" + nodeId + ", key=" + key + ']');
        return null;
    }
    boolean ret = false;
    GridCacheMultiTxFuture txFut = null;
    Collection<GridCacheMvccCandidate> cands = null;
    ReaderId reader;
    lockEntry();
    try {
        checkObsolete();
        reader = readerId(nodeId);
        if (reader == null) {
            reader = new ReaderId(nodeId, msgId);
            ReaderId[] rdrs = Arrays.copyOf(this.rdrs, this.rdrs.length + 1);
            rdrs[rdrs.length - 1] = reader;
            // Seal.
            this.rdrs = rdrs;
            // No transactions in ATOMIC cache.
            if (!cctx.atomic()) {
                txFut = reader.getOrCreateTxFuture(cctx);
                cands = localCandidates();
                ret = true;
            }
        } else {
            txFut = reader.txFuture();
            long id = reader.messageId();
            if (id < msgId)
                reader.messageId(msgId);
        }
    } finally {
        unlockEntry();
    }
    if (ret) {
        assert txFut != null;
        if (!F.isEmpty(cands)) {
            for (GridCacheMvccCandidate c : cands) {
                IgniteInternalTx tx = cctx.tm().tx(c.version());
                if (tx != null && tx.local())
                    txFut.addTx(tx);
            }
        }
        txFut.init();
        if (!txFut.isDone()) {
            final ReaderId reader0 = reader;
            txFut.listen(new CI1<IgniteInternalFuture<?>>() {

                @Override
                public void apply(IgniteInternalFuture<?> f) {
                    cctx.kernalContext().closure().runLocalSafe(new GridPlainRunnable() {

                        @Override
                        public void run() {
                            lockEntry();
                            try {
                                // Release memory.
                                reader0.resetTxFuture();
                            } finally {
                                unlockEntry();
                            }
                        }
                    });
                }
            });
        } else {
            lockEntry();
            try {
                // Release memory.
                reader.resetTxFuture();
            } finally {
                unlockEntry();
            }
            txFut = null;
        }
    }
    return txFut;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) GridCacheMultiTxFuture(org.apache.ignite.internal.processors.cache.GridCacheMultiTxFuture) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GridPlainRunnable

use of org.apache.ignite.internal.util.lang.GridPlainRunnable 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

GridPlainRunnable (org.apache.ignite.internal.util.lang.GridPlainRunnable)7 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)2 GridDhtFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtFuture)2 GridCompoundFuture (org.apache.ignite.internal.util.future.GridCompoundFuture)2 CI1 (org.apache.ignite.internal.util.typedef.CI1)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)1 GridCacheMultiTxFuture (org.apache.ignite.internal.processors.cache.GridCacheMultiTxFuture)1 GridCacheMvccCandidate (org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)1 GridDhtTopologyFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTopologyFuture)1 GridCacheQueryMarshallable (org.apache.ignite.internal.processors.cache.query.GridCacheQueryMarshallable)1 IgniteInternalTx (org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx)1 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)1 GridWorker (org.apache.ignite.internal.util.worker.GridWorker)1 GridWorkerFuture (org.apache.ignite.internal.util.worker.GridWorkerFuture)1