Search in sources :

Example 1 with GridCacheMultiTxFuture

use of org.apache.ignite.internal.processors.cache.GridCacheMultiTxFuture 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 has no near cache, don't add it.
    if (!cctx.discovery().cacheNearNode(node, cacheName())) {
        if (log.isDebugEnabled())
            log.debug("Ignoring near reader because near cache is disabled: " + 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;
    synchronized (this) {
        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);
        }
    }
    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() {
                            synchronized (this) {
                                // Release memory.
                                reader0.resetTxFuture();
                            }
                        }
                    });
                }
            });
        } else {
            synchronized (this) {
                // Release memory.
                reader.resetTxFuture();
            }
            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)

Aggregations

ClusterNode (org.apache.ignite.cluster.ClusterNode)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 GridCacheMultiTxFuture (org.apache.ignite.internal.processors.cache.GridCacheMultiTxFuture)1 GridCacheMvccCandidate (org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)1 IgniteInternalTx (org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx)1 GridPlainRunnable (org.apache.ignite.internal.util.lang.GridPlainRunnable)1 Nullable (org.jetbrains.annotations.Nullable)1