Search in sources :

Example 1 with GridDhtTxMapping

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping in project ignite by apache.

the class GridNearOptimisticTxPrepareFuture method prepareSingle.

/**
     * @param write Write.
     * @param topLocked {@code True} if thread already acquired lock preventing topology change.
     * @param remap Remap flag.
     */
private void prepareSingle(IgniteTxEntry write, boolean topLocked, boolean remap) {
    write.clearEntryReadVersion();
    AffinityTopologyVersion topVer = tx.topologyVersion();
    assert topVer.topologyVersion() > 0;
    txMapping = new GridDhtTxMapping();
    GridDistributedTxMapping mapping = map(write, topVer, null, topLocked, remap);
    if (mapping.primary().isLocal()) {
        if (write.context().isNear())
            tx.nearLocallyMapped(true);
        else if (write.context().isColocated())
            tx.colocatedLocallyMapped(true);
    }
    if (isDone()) {
        if (log.isDebugEnabled())
            log.debug("Abandoning (re)map because future is done: " + this);
        return;
    }
    if (keyLockFut != null)
        keyLockFut.onAllKeysAdded();
    tx.addSingleEntryMapping(mapping, write);
    cctx.mvcc().recheckPendingLocks();
    mapping.last(true);
    tx.transactionNodes(txMapping.transactionNodes());
    if (!write.context().isNear())
        checkOnePhase(txMapping);
    assert !(mapping.hasColocatedCacheEntries() && mapping.hasNearCacheEntries()) : mapping;
    proceedPrepare(mapping, null);
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtTxMapping(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping)

Example 2 with GridDhtTxMapping

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping in project ignite by apache.

the class GridNearPessimisticTxPrepareFuture method preparePessimistic.

/**
     *
     */
private void preparePessimistic() {
    Map<UUID, GridDistributedTxMapping> mappings = new HashMap<>();
    AffinityTopologyVersion topVer = tx.topologyVersion();
    GridDhtTxMapping txMapping = new GridDhtTxMapping();
    boolean hasNearCache = false;
    for (IgniteTxEntry txEntry : tx.allEntries()) {
        txEntry.clearEntryReadVersion();
        GridCacheContext cacheCtx = txEntry.context();
        if (cacheCtx.isNear())
            hasNearCache = true;
        List<ClusterNode> nodes;
        if (!cacheCtx.isLocal()) {
            GridDhtPartitionTopology top = cacheCtx.topology();
            nodes = top.nodes(cacheCtx.affinity().partition(txEntry.key()), topVer);
        } else
            nodes = cacheCtx.affinity().nodesByKey(txEntry.key(), topVer);
        assert !nodes.isEmpty();
        ClusterNode primary = nodes.get(0);
        GridDistributedTxMapping nodeMapping = mappings.get(primary.id());
        if (nodeMapping == null)
            mappings.put(primary.id(), nodeMapping = new GridDistributedTxMapping(primary));
        txEntry.nodeId(primary.id());
        nodeMapping.add(txEntry);
        txMapping.addMapping(nodes);
    }
    tx.transactionNodes(txMapping.transactionNodes());
    if (!hasNearCache)
        checkOnePhase(txMapping);
    long timeout = tx.remainingTime();
    if (timeout == -1) {
        onDone(new IgniteTxTimeoutCheckedException("Transaction timed out and was rolled back: " + tx));
        return;
    }
    int miniId = 0;
    Map<UUID, Collection<UUID>> txNodes = txMapping.transactionNodes();
    for (final GridDistributedTxMapping m : mappings.values()) {
        final ClusterNode primary = m.primary();
        if (primary.isLocal()) {
            if (m.hasNearCacheEntries() && m.hasColocatedCacheEntries()) {
                GridNearTxPrepareRequest nearReq = createRequest(txMapping.transactionNodes(), m, timeout, m.nearEntriesReads(), m.nearEntriesWrites());
                prepareLocal(nearReq, m, ++miniId, true);
                GridNearTxPrepareRequest colocatedReq = createRequest(txNodes, m, timeout, m.colocatedEntriesReads(), m.colocatedEntriesWrites());
                prepareLocal(colocatedReq, m, ++miniId, false);
            } else {
                GridNearTxPrepareRequest req = createRequest(txNodes, m, timeout, m.reads(), m.writes());
                prepareLocal(req, m, ++miniId, m.hasNearCacheEntries());
            }
        } else {
            GridNearTxPrepareRequest req = createRequest(txNodes, m, timeout, m.reads(), m.writes());
            final MiniFuture fut = new MiniFuture(m, ++miniId);
            req.miniId(fut.futureId());
            add(fut);
            try {
                cctx.io().send(primary, req, tx.ioPolicy());
                if (msgLog.isDebugEnabled()) {
                    msgLog.debug("Near pessimistic prepare, sent request [txId=" + tx.nearXidVersion() + ", node=" + primary.id() + ']');
                }
            } catch (ClusterTopologyCheckedException e) {
                e.retryReadyFuture(cctx.nextAffinityReadyFuture(topVer));
                fut.onNodeLeft(e);
            } catch (IgniteCheckedException e) {
                if (msgLog.isDebugEnabled()) {
                    msgLog.debug("Near pessimistic prepare, failed send request [txId=" + tx.nearXidVersion() + ", node=" + primary.id() + ", err=" + e + ']');
                }
                fut.onError(e);
                break;
            }
        }
    }
    markInitialized();
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) HashMap(java.util.HashMap) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDhtTxMapping(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping) Collection(java.util.Collection) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) UUID(java.util.UUID) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 3 with GridDhtTxMapping

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping in project ignite by apache.

the class GridNearOptimisticSerializableTxPrepareFuture method prepare.

/**
     * @param reads Read entries.
     * @param writes Write entries.
     * @param remap Remap flag.
     * @param topLocked Topology locked flag.
     */
@SuppressWarnings("unchecked")
private void prepare(Iterable<IgniteTxEntry> reads, Iterable<IgniteTxEntry> writes, boolean remap, boolean topLocked) {
    AffinityTopologyVersion topVer = tx.topologyVersion();
    assert topVer.topologyVersion() > 0;
    GridDhtTxMapping txMapping = new GridDhtTxMapping();
    Map<UUID, GridDistributedTxMapping> mappings = new HashMap<>();
    boolean hasNearCache = false;
    for (IgniteTxEntry write : writes) {
        map(write, topVer, mappings, txMapping, remap, topLocked);
        if (write.context().isNear())
            hasNearCache = true;
    }
    for (IgniteTxEntry read : reads) map(read, topVer, mappings, txMapping, remap, topLocked);
    if (keyLockFut != null)
        keyLockFut.onAllKeysAdded();
    if (isDone()) {
        if (log.isDebugEnabled())
            log.debug("Abandoning (re)map because future is done: " + this);
        return;
    }
    tx.addEntryMapping(mappings.values());
    cctx.mvcc().recheckPendingLocks();
    tx.transactionNodes(txMapping.transactionNodes());
    if (!hasNearCache)
        checkOnePhase(txMapping);
    MiniFuture locNearEntriesFut = null;
    // Create futures in advance to have all futures when process {@link GridNearTxPrepareResponse#clientRemapVersion}.
    for (GridDistributedTxMapping m : mappings.values()) {
        assert !m.empty();
        MiniFuture fut = new MiniFuture(this, m, ++miniId);
        add(fut);
        if (m.primary().isLocal() && m.hasNearCacheEntries() && m.hasColocatedCacheEntries()) {
            assert locNearEntriesFut == null;
            locNearEntriesFut = fut;
            add(new MiniFuture(this, m, ++miniId));
        }
    }
    Collection<IgniteInternalFuture<?>> futs = (Collection) futures();
    Iterator<IgniteInternalFuture<?>> it = futs.iterator();
    while (it.hasNext()) {
        IgniteInternalFuture<?> fut0 = it.next();
        if (skipFuture(remap, fut0))
            continue;
        MiniFuture fut = (MiniFuture) fut0;
        IgniteCheckedException err = prepare(fut, txMapping.transactionNodes(), locNearEntriesFut);
        if (err != null) {
            while (it.hasNext()) {
                fut0 = it.next();
                if (skipFuture(remap, fut0))
                    continue;
                fut = (MiniFuture) fut0;
                tx.removeMapping(fut.mapping().primary().id());
                fut.onResult(new IgniteCheckedException("Failed to prepare transaction.", err));
            }
            break;
        }
    }
    markInitialized();
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) HashMap(java.util.HashMap) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDhtTxMapping(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping) Collection(java.util.Collection) UUID(java.util.UUID)

Example 4 with GridDhtTxMapping

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping in project ignite by apache.

the class GridNearOptimisticTxPrepareFuture method prepare.

/**
     * @param writes Write entries.
     * @param topLocked {@code True} if thread already acquired lock preventing topology change.
     * @param remap Remap flag.
     */
private void prepare(Iterable<IgniteTxEntry> writes, boolean topLocked, boolean remap) {
    AffinityTopologyVersion topVer = tx.topologyVersion();
    assert topVer.topologyVersion() > 0;
    txMapping = new GridDhtTxMapping();
    Map<Object, GridDistributedTxMapping> map = new HashMap<>();
    // Assign keys to primary nodes.
    GridDistributedTxMapping cur = null;
    Queue<GridDistributedTxMapping> mappings = new ArrayDeque<>();
    boolean hasNearCache = false;
    for (IgniteTxEntry write : writes) {
        write.clearEntryReadVersion();
        GridDistributedTxMapping updated = map(write, topVer, cur, topLocked, remap);
        if (write.context().isNear())
            hasNearCache = true;
        if (cur != updated) {
            mappings.offer(updated);
            updated.last(true);
            ClusterNode primary = updated.primary();
            assert !primary.isLocal() || !cctx.kernalContext().clientNode();
            // Minor optimization to not create MappingKey: on client node can not have mapping for local node.
            Object key = cctx.kernalContext().clientNode() ? primary.id() : new MappingKey(primary.id(), primary.isLocal() && updated.hasNearCacheEntries());
            GridDistributedTxMapping prev = map.put(key, updated);
            if (prev != null)
                prev.last(false);
            if (updated.primary().isLocal()) {
                if (write.context().isNear())
                    tx.nearLocallyMapped(true);
                else if (write.context().isColocated())
                    tx.colocatedLocallyMapped(true);
            }
            cur = updated;
        }
    }
    if (isDone()) {
        if (log.isDebugEnabled())
            log.debug("Abandoning (re)map because future is done: " + this);
        return;
    }
    if (keyLockFut != null)
        keyLockFut.onAllKeysAdded();
    tx.addEntryMapping(mappings);
    cctx.mvcc().recheckPendingLocks();
    tx.transactionNodes(txMapping.transactionNodes());
    if (!hasNearCache)
        checkOnePhase(txMapping);
    proceedPrepare(mappings);
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) HashMap(java.util.HashMap) GridDhtTxMapping(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) ArrayDeque(java.util.ArrayDeque)

Aggregations

AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)4 GridDistributedTxMapping (org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping)4 GridDhtTxMapping (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping)4 HashMap (java.util.HashMap)3 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)3 Collection (java.util.Collection)2 UUID (java.util.UUID)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 ArrayDeque (java.util.ArrayDeque)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 GridDhtPartitionTopology (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology)1 IgniteTxTimeoutCheckedException (org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException)1