Search in sources :

Example 1 with GridCacheMappedVersion

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

the class GridCacheMvccManager method addFuture.

/**
 *    /**
 * Adds future.
 *
 * @param fut Future.
 * @return {@code True} if added.
 */
@SuppressWarnings({ "SynchronizationOnLocalVariableOrMethodParameter" })
public boolean addFuture(final GridCacheVersionedFuture<?> fut) {
    if (fut.isDone()) {
        fut.markNotTrackable();
        return true;
    }
    if (!fut.trackable())
        return true;
    while (true) {
        Collection<GridCacheVersionedFuture<?>> old = verFuts.get(fut.version());
        if (old == null) {
            Collection<GridCacheVersionedFuture<?>> col = new HashSet<GridCacheVersionedFuture<?>>(U.capacity(1), 0.75f) {

                {
                    // Make sure that we add future to queue before
                    // adding queue to the map of futures.
                    add(fut);
                }

                @Override
                public int hashCode() {
                    return System.identityHashCode(this);
                }

                @Override
                public boolean equals(Object obj) {
                    return obj == this;
                }
            };
            old = verFuts.putIfAbsent(fut.version(), col);
        }
        if (old != null) {
            boolean empty, dup = false;
            synchronized (old) {
                empty = old.isEmpty();
                if (!empty)
                    dup = !old.add(fut);
            }
            // Future is being removed, so we force-remove here and try again.
            if (empty) {
                if (verFuts.remove(fut.version(), old)) {
                    if (log.isDebugEnabled())
                        log.debug("Removed future list from futures map for lock version: " + fut.version());
                }
                continue;
            }
            if (dup) {
                if (log.isDebugEnabled())
                    log.debug("Found duplicate future in futures map (will not add): " + fut);
                return false;
            }
        }
        // Handle version mappings.
        if (fut instanceof GridCacheMappedVersion) {
            GridCacheVersion from = ((GridCacheMappedVersion) fut).mappedVersion();
            if (from != null)
                mapVersion(from, fut.version());
        }
        if (log.isDebugEnabled())
            log.debug("Added future to future map: " + fut);
        break;
    }
    // Just in case if future was completed before it was added.
    if (fut.isDone())
        removeVersionedFuture(fut);
    else
        onFutureAdded(fut);
    return true;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) GridBoundedConcurrentLinkedHashSet(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet) GridCacheMappedVersion(org.apache.ignite.internal.processors.cache.distributed.GridCacheMappedVersion)

Example 2 with GridCacheMappedVersion

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

the class GridCacheMvccManager method addFuture.

/**

    /**
     * Adds future.
     *
     * @param fut Future.
     * @return {@code True} if added.
     */
@SuppressWarnings({ "SynchronizationOnLocalVariableOrMethodParameter" })
public boolean addFuture(final GridCacheMvccFuture<?> fut) {
    if (fut.isDone()) {
        fut.markNotTrackable();
        return true;
    }
    if (!fut.trackable())
        return true;
    while (true) {
        Collection<GridCacheMvccFuture<?>> old = mvccFuts.get(fut.version());
        if (old == null) {
            Collection<GridCacheMvccFuture<?>> col = new HashSet<GridCacheMvccFuture<?>>(U.capacity(1), 0.75f) {

                {
                    // Make sure that we add future to queue before
                    // adding queue to the map of futures.
                    add(fut);
                }

                @Override
                public int hashCode() {
                    return System.identityHashCode(this);
                }

                @Override
                public boolean equals(Object obj) {
                    return obj == this;
                }
            };
            old = mvccFuts.putIfAbsent(fut.version(), col);
        }
        if (old != null) {
            boolean empty, dup = false;
            synchronized (old) {
                empty = old.isEmpty();
                if (!empty)
                    dup = !old.add(fut);
            }
            // Future is being removed, so we force-remove here and try again.
            if (empty) {
                if (mvccFuts.remove(fut.version(), old)) {
                    if (log.isDebugEnabled())
                        log.debug("Removed future list from futures map for lock version: " + fut.version());
                }
                continue;
            }
            if (dup) {
                if (log.isDebugEnabled())
                    log.debug("Found duplicate future in futures map (will not add): " + fut);
                return false;
            }
        }
        // Handle version mappings.
        if (fut instanceof GridCacheMappedVersion) {
            GridCacheVersion from = ((GridCacheMappedVersion) fut).mappedVersion();
            if (from != null)
                mapVersion(from, fut.version());
        }
        if (log.isDebugEnabled())
            log.debug("Added future to future map: " + fut);
        break;
    }
    // Just in case if future was completed before it was added.
    if (fut.isDone())
        removeMvccFuture(fut);
    else
        onFutureAdded(fut);
    return true;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) GridBoundedConcurrentLinkedHashSet(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet) GridCacheMappedVersion(org.apache.ignite.internal.processors.cache.distributed.GridCacheMappedVersion)

Example 3 with GridCacheMappedVersion

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

the class IgniteTxManager method uncommitTx.

/**
 * Tries to minimize damage from partially-committed transaction.
 *
 * @param tx Tx to uncommit.
 */
void uncommitTx(IgniteInternalTx tx) {
    assert tx != null;
    if (log.isDebugEnabled())
        log.debug("Uncommiting from TM: " + tx);
    ConcurrentMap<GridCacheVersion, IgniteInternalTx> txIdMap = transactionMap(tx);
    if (txIdMap.remove(tx.xidVersion(), tx)) {
        // 1. Unlock write resources.
        unlockMultiple(tx, tx.writeEntries());
        // 2. Unlock read resources if required.
        if (unlockReadEntries(tx))
            unlockMultiple(tx, tx.readEntries());
        // 3. Notify evictions.
        notifyEvictions(tx);
        // 4. Remove from per-thread storage.
        clearThreadMap(tx);
        // 5. Unregister explicit locks.
        if (!tx.alternateVersions().isEmpty()) {
            for (GridCacheVersion ver : tx.alternateVersions()) idMap.remove(ver);
        }
        // 6. Remove Near-2-DHT mappings.
        if (tx instanceof GridCacheMappedVersion)
            mappedVers.remove(((GridCacheMappedVersion) tx).mappedVersion());
        // 7. Clear context.
        resetContext();
        if (log.isDebugEnabled())
            log.debug("Uncommitted from TM: " + tx);
    } else if (log.isDebugEnabled())
        log.debug("Did not uncommit from TM (was already committed or rolled back): " + tx);
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheMappedVersion(org.apache.ignite.internal.processors.cache.distributed.GridCacheMappedVersion)

Example 4 with GridCacheMappedVersion

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

the class IgniteTxManager method commitTx.

/**
 * Commits a transaction.
 *
 * @param tx Transaction to commit.
 * @throws IgniteCheckedException If failed.
 */
public void commitTx(IgniteInternalTx tx) throws IgniteCheckedException {
    assert tx != null;
    assert tx.state() == COMMITTING : "Invalid transaction state for commit from tm [state=" + tx.state() + ", expected=COMMITTING, tx=" + tx + ']';
    if (log.isDebugEnabled())
        log.debug("Committing from TM [locNodeId=" + cctx.localNodeId() + ", tx=" + tx + ']');
    /*
         * Note that write phase is handled by transaction adapter itself,
         * so we don't do it here.
         */
    Object committed0 = completedVersHashMap.get(tx.xidVersion());
    Boolean committed = committed0 != null && !committed0.equals(Boolean.FALSE);
    // 1. Make sure that committed version has been recorded.
    if (!(committed || tx.writeSet().isEmpty() || tx.isSystemInvalidate())) {
        uncommitTx(tx);
        tx.errorWhenCommitting();
        throw new IgniteCheckedException("Missing commit version (consider increasing " + IGNITE_MAX_COMPLETED_TX_COUNT + " system property) [ver=" + tx.xidVersion() + ", committed0=" + committed0 + ", tx=" + tx.getClass().getSimpleName() + ']');
    }
    ConcurrentMap<GridCacheVersion, IgniteInternalTx> txIdMap = transactionMap(tx);
    if (txIdMap.remove(tx.xidVersion(), tx)) {
        // 2. Must process completed entries before unlocking!
        processCompletedEntries(tx);
        if (tx instanceof GridDhtTxLocal) {
            GridDhtTxLocal dhtTxLoc = (GridDhtTxLocal) tx;
            collectPendingVersions(dhtTxLoc);
        }
        // 3. Unlock write resources.
        unlockMultiple(tx, tx.writeEntries());
        // 4. Unlock read resources if required.
        if (unlockReadEntries(tx))
            unlockMultiple(tx, tx.readEntries());
        // 5. Notify evictions.
        notifyEvictions(tx);
        // 6. Remove obsolete entries from cache.
        removeObsolete(tx);
        // 7. Remove from per-thread storage.
        clearThreadMap(tx);
        // 8. Unregister explicit locks.
        if (!tx.alternateVersions().isEmpty()) {
            for (GridCacheVersion ver : tx.alternateVersions()) idMap.remove(ver);
        }
        // 9. Remove Near-2-DHT mappings.
        if (tx instanceof GridCacheMappedVersion) {
            GridCacheVersion mapped = ((GridCacheMappedVersion) tx).mappedVersion();
            if (mapped != null)
                mappedVers.remove(mapped);
        }
        // 10. Clear context.
        resetContext();
        // 11. Update metrics.
        if (!tx.dht() && tx.local()) {
            if (!tx.system())
                cctx.txMetrics().onTxCommit();
            writeStatistics(tx, true);
            tx.txState().onTxEnd(cctx, tx, true);
        }
        if (slowTxWarnTimeout > 0 && tx.local() && U.currentTimeMillis() - tx.startTime() > slowTxWarnTimeout)
            U.warn(log, "Slow transaction detected [tx=" + tx + ", slowTxWarnTimeout=" + slowTxWarnTimeout + ']');
        if (log.isDebugEnabled())
            log.debug("Committed from TM [locNodeId=" + cctx.localNodeId() + ", tx=" + tx + ']');
    } else if (log.isDebugEnabled())
        log.debug("Did not commit from TM (was already committed): " + tx);
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDhtTxLocal(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxLocal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCacheMappedVersion(org.apache.ignite.internal.processors.cache.distributed.GridCacheMappedVersion)

Example 5 with GridCacheMappedVersion

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

the class IgniteTxManager method onCreated.

/**
 * @param cacheCtx Cache context.
 * @param tx Created transaction.
 * @return Started transaction.
 */
@Nullable
public <T extends IgniteInternalTx> T onCreated(@Nullable GridCacheContext cacheCtx, T tx) {
    ConcurrentMap<GridCacheVersion, IgniteInternalTx> txIdMap = transactionMap(tx);
    // Start clean.
    resetContext();
    if (isCompleted(tx)) {
        if (log.isDebugEnabled())
            log.debug("Attempt to create a completed transaction (will ignore): " + tx);
        return null;
    }
    IgniteInternalTx t;
    if ((t = txIdMap.putIfAbsent(tx.xidVersion(), tx)) == null) {
        if (tx.local() && !tx.dht()) {
            assert tx instanceof GridNearTxLocal : tx;
            if (!tx.implicit()) {
                if (cacheCtx == null || !cacheCtx.systemTx())
                    threadMap.put(tx.threadId(), tx);
                else
                    sysThreadMap.put(new TxThreadKey(tx.threadId(), cacheCtx.cacheId()), tx);
            }
            ((GridNearTxLocal) tx).recordStateChangedEvent(EVT_TX_STARTED);
        }
        // Handle mapped versions.
        if (tx instanceof GridCacheMappedVersion) {
            GridCacheMappedVersion mapped = (GridCacheMappedVersion) tx;
            GridCacheVersion from = mapped.mappedVersion();
            if (from != null)
                mappedVers.put(from, tx.xidVersion());
            if (log.isDebugEnabled())
                log.debug("Added transaction version mapping [from=" + from + ", to=" + tx.xidVersion() + ", tx=" + tx + ']');
        }
    } else {
        if (log.isDebugEnabled())
            log.debug("Attempt to create an existing transaction (will ignore) [newTx=" + tx + ", existingTx=" + t + ']');
        return null;
    }
    if (log.isDebugEnabled())
        log.debug("Transaction created: " + tx);
    return tx;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) GridCacheMappedVersion(org.apache.ignite.internal.processors.cache.distributed.GridCacheMappedVersion) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GridCacheMappedVersion (org.apache.ignite.internal.processors.cache.distributed.GridCacheMappedVersion)5 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)5 HashSet (java.util.HashSet)2 GridBoundedConcurrentLinkedHashSet (org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet)2 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 GridDhtTxLocal (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxLocal)1 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)1 Nullable (org.jetbrains.annotations.Nullable)1