Search in sources :

Example 11 with GridDistributedTxMapping

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

the class GridDhtTxPrepareFuture method sendPrepareRequests.

/**
 */
private void sendPrepareRequests() {
    if (tx.onePhaseCommit() && !tx.nearMap().isEmpty()) {
        for (GridDistributedTxMapping nearMapping : tx.nearMap().values()) {
            if (!tx.dhtMap().containsKey(nearMapping.primary().id())) {
                tx.onePhaseCommit(false);
                break;
            }
        }
    }
    int miniId = 0;
    assert tx.transactionNodes() != null;
    final long timeout = timeoutObj != null ? timeoutObj.timeout : 0;
    // Create mini futures.
    for (GridDistributedTxMapping dhtMapping : tx.dhtMap().values()) {
        assert !dhtMapping.empty();
        ClusterNode n = dhtMapping.primary();
        assert !n.isLocal();
        GridDistributedTxMapping nearMapping = tx.nearMap().get(n.id());
        Collection<IgniteTxEntry> nearWrites = nearMapping == null ? null : nearMapping.writes();
        Collection<IgniteTxEntry> dhtWrites = dhtMapping.writes();
        if (F.isEmpty(dhtWrites) && F.isEmpty(nearWrites))
            continue;
        if (tx.remainingTime() == -1)
            return;
        MiniFuture fut = new MiniFuture(n.id(), ++miniId, dhtMapping, nearMapping);
        // Append new future.
        add(fut);
        assert req.transactionNodes() != null;
        GridDhtTxPrepareRequest req = new GridDhtTxPrepareRequest(futId, fut.futureId(), tx.topologyVersion(), tx, timeout, dhtWrites, nearWrites, this.req.transactionNodes(), tx.nearXidVersion(), true, tx.onePhaseCommit(), tx.subjectId(), tx.taskNameHash(), tx.activeCachesDeploymentEnabled(), tx.storeWriteThrough(), retVal);
        int idx = 0;
        for (IgniteTxEntry entry : dhtWrites) {
            try {
                GridDhtCacheEntry cached = (GridDhtCacheEntry) entry.cached();
                GridCacheContext<?, ?> cacheCtx = cached.context();
                // Do not invalidate near entry on originating transaction node.
                req.invalidateNearEntry(idx, !tx.nearNodeId().equals(n.id()) && cached.readerId(n.id()) != null);
                if (cached.isNewLocked()) {
                    List<ClusterNode> owners = cacheCtx.topology().owners(cached.partition(), tx != null ? tx.topologyVersion() : cacheCtx.affinity().affinityTopologyVersion());
                    // Do not preload if local node is a partition owner.
                    if (!owners.contains(cctx.localNode()))
                        req.markKeyForPreload(idx);
                }
                break;
            } catch (GridCacheEntryRemovedException ignore) {
                assert false : "Got removed exception on entry with dht local candidate: " + entry;
            }
            idx++;
        }
        if (!F.isEmpty(nearWrites)) {
            for (IgniteTxEntry entry : nearWrites) {
                try {
                    if (entry.explicitVersion() == null) {
                        GridCacheMvccCandidate added = entry.cached().candidate(version());
                        assert added != null : "Missing candidate for cache entry:" + entry;
                        assert added.dhtLocal();
                        if (added.ownerVersion() != null)
                            req.owned(entry.txKey(), added.ownerVersion());
                    }
                    break;
                } catch (GridCacheEntryRemovedException ignore) {
                    assert false : "Got removed exception on entry with dht local candidate: " + entry;
                }
            }
        }
        assert req.transactionNodes() != null;
        try {
            cctx.io().send(n, req, tx.ioPolicy());
            if (msgLog.isDebugEnabled()) {
                msgLog.debug("DHT prepare fut, sent request dht [txId=" + tx.nearXidVersion() + ", dhtTxId=" + tx.xidVersion() + ", node=" + n.id() + ']');
            }
        } catch (ClusterTopologyCheckedException ignored) {
            fut.onNodeLeft();
        } catch (IgniteCheckedException e) {
            if (!cctx.kernalContext().isStopping()) {
                if (msgLog.isDebugEnabled()) {
                    msgLog.debug("DHT prepare fut, failed to send request dht [txId=" + tx.nearXidVersion() + ", dhtTxId=" + tx.xidVersion() + ", node=" + n.id() + ']');
                }
                fut.onResult(e);
            } else {
                if (msgLog.isDebugEnabled()) {
                    msgLog.debug("DHT prepare fut, failed to send request dht, ignore [txId=" + tx.nearXidVersion() + ", dhtTxId=" + tx.xidVersion() + ", node=" + n.id() + ", err=" + e + ']');
                }
            }
        }
    }
    for (GridDistributedTxMapping nearMapping : tx.nearMap().values()) {
        if (!tx.dhtMap().containsKey(nearMapping.primary().id())) {
            if (tx.remainingTime() == -1)
                return;
            MiniFuture fut = new MiniFuture(nearMapping.primary().id(), ++miniId, null, nearMapping);
            // Append new future.
            add(fut);
            GridDhtTxPrepareRequest req = new GridDhtTxPrepareRequest(futId, fut.futureId(), tx.topologyVersion(), tx, timeout, null, nearMapping.writes(), tx.transactionNodes(), tx.nearXidVersion(), true, tx.onePhaseCommit(), tx.subjectId(), tx.taskNameHash(), tx.activeCachesDeploymentEnabled(), tx.storeWriteThrough(), retVal);
            for (IgniteTxEntry entry : nearMapping.entries()) {
                if (CU.writes().apply(entry)) {
                    try {
                        if (entry.explicitVersion() == null) {
                            GridCacheMvccCandidate added = entry.cached().candidate(version());
                            assert added != null : "Null candidate for non-group-lock entry " + "[added=" + added + ", entry=" + entry + ']';
                            assert added.dhtLocal() : "Got non-dht-local candidate for prepare future" + "[added=" + added + ", entry=" + entry + ']';
                            if (added != null && added.ownerVersion() != null)
                                req.owned(entry.txKey(), added.ownerVersion());
                        }
                        break;
                    } catch (GridCacheEntryRemovedException ignore) {
                        assert false : "Got removed exception on entry with dht local candidate: " + entry;
                    }
                }
            }
            assert req.transactionNodes() != null;
            try {
                cctx.io().send(nearMapping.primary(), req, tx.ioPolicy());
                if (msgLog.isDebugEnabled()) {
                    msgLog.debug("DHT prepare fut, sent request near [txId=" + tx.nearXidVersion() + ", dhtTxId=" + tx.xidVersion() + ", node=" + nearMapping.primary().id() + ']');
                }
            } catch (ClusterTopologyCheckedException ignored) {
                fut.onNodeLeft();
            } catch (IgniteCheckedException e) {
                if (!cctx.kernalContext().isStopping()) {
                    if (msgLog.isDebugEnabled()) {
                        msgLog.debug("DHT prepare fut, failed to send request near [txId=" + tx.nearXidVersion() + ", dhtTxId=" + tx.xidVersion() + ", node=" + nearMapping.primary().id() + ']');
                    }
                    fut.onResult(e);
                } else {
                    if (msgLog.isDebugEnabled()) {
                        msgLog.debug("DHT prepare fut, failed to send request near, ignore [txId=" + tx.nearXidVersion() + ", dhtTxId=" + tx.xidVersion() + ", node=" + nearMapping.primary().id() + ", err=" + e + ']');
                    }
                }
            }
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 12 with GridDistributedTxMapping

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

the class GridDhtTxPrepareFuture method addMapping.

/**
 * @param entry Entry.
 * @param n Node.
 * @param globalMap Map.
 */
private void addMapping(IgniteTxEntry entry, ClusterNode n, Map<UUID, GridDistributedTxMapping> globalMap) {
    GridDistributedTxMapping global = globalMap.get(n.id());
    if (global == null)
        globalMap.put(n.id(), global = new GridDistributedTxMapping(n));
    global.add(entry);
}
Also used : GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping)

Example 13 with GridDistributedTxMapping

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

the class GridNearTxFinishFuture method finishOnePhase.

/**
 * @param commit Commit flag.
 */
private void finishOnePhase(boolean commit) {
    assert Thread.holdsLock(this);
    if (finishOnePhaseCalled)
        return;
    finishOnePhaseCalled = true;
    GridDistributedTxMapping locMapping = mappings.localMapping();
    if (locMapping != null) {
        // No need to send messages as transaction was already committed on remote node.
        // Finish local mapping only as we need send commit message to backups.
        IgniteInternalFuture<IgniteInternalTx> fut = cctx.tm().txHandler().finishColocatedLocal(commit, tx);
        // Add new future.
        if (fut != null)
            add(fut);
    }
}
Also used : IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping)

Example 14 with GridDistributedTxMapping

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

the class GridNearTxFinishFuture method finish.

/**
 * Initializes future.
 *
 * @param commit Commit flag.
 * @param clearThreadMap If {@code true} removes {@link GridNearTxLocal} from thread map.
 */
@SuppressWarnings("ForLoopReplaceableByForEach")
public void finish(boolean commit, boolean clearThreadMap) {
    if (tx.onNeedCheckBackup()) {
        assert tx.onePhaseCommit();
        checkBackup();
        // If checkBackup is set, it means that primary node has crashed and we will not need to send
        // finish request to it, so we can mark future as initialized.
        markInitialized();
        return;
    }
    try {
        if (tx.localFinish(commit, clearThreadMap) || (!commit && tx.state() == UNKNOWN)) {
            if ((tx.onePhaseCommit() && needFinishOnePhase(commit)) || (!tx.onePhaseCommit() && mappings != null)) {
                if (mappings.single()) {
                    GridDistributedTxMapping mapping = mappings.singleMapping();
                    if (mapping != null) {
                        assert !hasFutures() : futures();
                        finish(1, mapping, commit);
                    }
                } else
                    finish(mappings.mappings(), commit);
            }
            markInitialized();
        } else
            onDone(new IgniteCheckedException("Failed to commit transaction: " + CU.txString(tx)));
    } catch (Error | RuntimeException e) {
        onDone(e);
        throw e;
    } catch (IgniteCheckedException e) {
        onDone(e);
    } finally {
        if (commit && tx.onePhaseCommit() && // Readonly operations require no ack.
        !tx.writeMap().isEmpty())
            ackBackup();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping)

Example 15 with GridDistributedTxMapping

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

the class GridNearTxFinishFuture method checkBackup.

/**
 */
private void checkBackup() {
    assert !hasFutures() : futures();
    GridDistributedTxMapping mapping = mappings.singleMapping();
    if (mapping != null) {
        UUID nodeId = mapping.primary().id();
        Collection<UUID> backups = tx.transactionNodes().get(nodeId);
        if (!F.isEmpty(backups)) {
            assert backups.size() == 1;
            UUID backupId = F.first(backups);
            ClusterNode backup = cctx.discovery().node(backupId);
            // Nothing to do if backup has left the grid.
            if (backup == null) {
                readyNearMappingFromBackup(mapping);
                ClusterTopologyCheckedException cause = new ClusterTopologyCheckedException("Backup node left grid: " + backupId);
                cause.retryReadyFuture(cctx.nextAffinityReadyFuture(tx.topologyVersion()));
                onDone(new IgniteTxRollbackCheckedException("Failed to commit transaction " + "(backup has left grid): " + tx.xidVersion(), cause));
            } else {
                final CheckBackupMiniFuture mini = new CheckBackupMiniFuture(1, backup, mapping);
                add(mini);
                if (backup.isLocal()) {
                    boolean committed = !cctx.tm().addRolledbackTx(tx);
                    readyNearMappingFromBackup(mapping);
                    if (committed) {
                        try {
                            if (tx.needReturnValue() && tx.implicit()) {
                                GridCacheReturnCompletableWrapper wrapper = cctx.tm().getCommittedTxReturn(tx.xidVersion());
                                assert wrapper != null : tx.xidVersion();
                                GridCacheReturn retVal = wrapper.fut().get();
                                assert retVal != null;
                                tx.implicitSingleResult(retVal);
                            }
                            if (tx.syncMode() == FULL_SYNC) {
                                GridCacheVersion nearXidVer = tx.nearXidVersion();
                                assert nearXidVer != null : tx;
                                IgniteInternalFuture<?> fut = cctx.tm().remoteTxFinishFuture(nearXidVer);
                                fut.listen(new CI1<IgniteInternalFuture<?>>() {

                                    @Override
                                    public void apply(IgniteInternalFuture<?> fut) {
                                        mini.onDone(tx);
                                    }
                                });
                                return;
                            }
                            mini.onDone(tx);
                        } catch (IgniteCheckedException e) {
                            if (msgLog.isDebugEnabled()) {
                                msgLog.debug("Near finish fut, failed to finish [" + "txId=" + tx.nearXidVersion() + ", node=" + backup.id() + ", err=" + e + ']');
                            }
                            mini.onDone(e);
                        }
                    } else {
                        ClusterTopologyCheckedException cause = new ClusterTopologyCheckedException("Primary node left grid: " + nodeId);
                        cause.retryReadyFuture(cctx.nextAffinityReadyFuture(tx.topologyVersion()));
                        mini.onDone(new IgniteTxRollbackCheckedException("Failed to commit transaction " + "(transaction has been rolled back on backup node): " + tx.xidVersion(), cause));
                    }
                } else {
                    GridDhtTxFinishRequest finishReq = checkCommittedRequest(mini.futureId(), false);
                    try {
                        cctx.io().send(backup, finishReq, tx.ioPolicy());
                        if (msgLog.isDebugEnabled()) {
                            msgLog.debug("Near finish fut, sent check committed request [" + "txId=" + tx.nearXidVersion() + ", node=" + backup.id() + ']');
                        }
                    } catch (ClusterTopologyCheckedException ignored) {
                        mini.onNodeLeft(backupId, false);
                    } catch (IgniteCheckedException e) {
                        if (msgLog.isDebugEnabled()) {
                            msgLog.debug("Near finish fut, failed to send check committed request [" + "txId=" + tx.nearXidVersion() + ", node=" + backup.id() + ", err=" + e + ']');
                        }
                        mini.onDone(e);
                    }
                }
            }
        } else
            readyNearMappingFromBackup(mapping);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) GridDhtTxFinishRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxFinishRequest) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UUID(java.util.UUID) GridCacheReturnCompletableWrapper(org.apache.ignite.internal.processors.cache.GridCacheReturnCompletableWrapper) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

GridDistributedTxMapping (org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping)23 ClusterNode (org.apache.ignite.cluster.ClusterNode)12 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)11 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 UUID (java.util.UUID)5 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)5 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)4 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)4 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)4 GridDhtTxMapping (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxMapping)4 HashMap (java.util.HashMap)3 ClusterTopologyServerNotFoundException (org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException)3 Collection (java.util.Collection)2 Map (java.util.Map)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)2 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1