Search in sources :

Example 91 with GridCacheContext

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

the class GridDhtTxPrepareFuture method map.

/**
 * @param entry Transaction entry.
 */
private void map(IgniteTxEntry entry) {
    if (entry.cached().isLocal())
        return;
    GridDhtCacheEntry cached = (GridDhtCacheEntry) entry.cached();
    GridCacheContext cacheCtx = entry.context();
    GridDhtCacheAdapter<?, ?> dht = cacheCtx.isNear() ? cacheCtx.near().dht() : cacheCtx.dht();
    ExpiryPolicy expiry = cacheCtx.expiryForTxEntry(entry);
    if (expiry != null && (entry.op() == READ || entry.op() == NOOP)) {
        entry.op(NOOP);
        entry.ttl(CU.toTtl(expiry.getExpiryForAccess()));
    }
    while (true) {
        try {
            List<ClusterNode> dhtNodes = dht.topology().nodes(cached.partition(), tx.topologyVersion());
            assert !dhtNodes.isEmpty() && dhtNodes.get(0).id().equals(cctx.localNodeId()) : "localNode = " + cctx.localNodeId() + ", dhtNodes = " + dhtNodes;
            if (log.isDebugEnabled())
                log.debug("Mapping entry to DHT nodes [nodes=" + U.toShortString(dhtNodes) + ", entry=" + entry + ']');
            for (int i = 1; i < dhtNodes.size(); i++) {
                ClusterNode node = dhtNodes.get(i);
                addMapping(entry, node, dhtMap);
            }
            Collection<UUID> readers = cached.readers();
            if (!F.isEmpty(readers)) {
                for (UUID readerId : readers) {
                    if (readerId.equals(tx.nearNodeId()))
                        continue;
                    ClusterNode readerNode = cctx.discovery().node(readerId);
                    if (readerNode == null || canSkipNearReader(dht, readerNode, dhtNodes))
                        continue;
                    if (log.isDebugEnabled())
                        log.debug("Mapping entry to near node [node=" + readerNode + ", entry=" + entry + ']');
                    addMapping(entry, readerNode, nearMap);
                }
            } else if (log.isDebugEnabled())
                log.debug("Entry has no near readers: " + entry);
            break;
        } catch (GridCacheEntryRemovedException ignore) {
            cached = dht.entryExx(entry.key(), tx.topologyVersion());
            entry.cached(cached);
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) UUID(java.util.UUID)

Example 92 with GridCacheContext

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

the class GridDhtTxPrepareFuture method forceRebalanceKeys.

/**
 * @param keysMap Keys to request.
 * @return Keys request future.
 */
private IgniteInternalFuture<Object> forceRebalanceKeys(Map<Integer, Collection<KeyCacheObject>> keysMap) {
    if (F.isEmpty(keysMap))
        return null;
    GridCompoundFuture<Object, Object> compFut = null;
    IgniteInternalFuture<Object> lastForceFut = null;
    for (Map.Entry<Integer, Collection<KeyCacheObject>> entry : keysMap.entrySet()) {
        if (lastForceFut != null && compFut == null) {
            compFut = new GridCompoundFuture();
            compFut.add(lastForceFut);
        }
        int cacheId = entry.getKey();
        Collection<KeyCacheObject> keys = entry.getValue();
        GridCacheContext ctx = cctx.cacheContext(cacheId);
        lastForceFut = ctx.group().preloader().request(ctx, keys, tx.topologyVersion());
        if (compFut != null && lastForceFut != null)
            compFut.add(lastForceFut);
    }
    if (compFut != null) {
        compFut.markInitialized();
        return compFut;
    } else
        return lastForceFut;
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) Collection(java.util.Collection) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) HashMap(java.util.HashMap) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 93 with GridCacheContext

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

the class GridDhtTxPrepareFuture method onEntriesLocked.

/**
 */
private void onEntriesLocked() {
    ret = new GridCacheReturn(null, tx.localResult(), true, null, true);
    for (IgniteTxEntry writeEntry : req.writes()) {
        IgniteTxEntry txEntry = tx.entry(writeEntry.txKey());
        assert txEntry != null : writeEntry;
        GridCacheContext cacheCtx = txEntry.context();
        GridCacheEntryEx cached = txEntry.cached();
        ExpiryPolicy expiry = cacheCtx.expiryForTxEntry(txEntry);
        cctx.database().checkpointReadLock();
        try {
            if ((txEntry.op() == CREATE || txEntry.op() == UPDATE) && txEntry.conflictExpireTime() == CU.EXPIRE_TIME_CALCULATE) {
                if (expiry != null) {
                    cached.unswap(true);
                    Duration duration = cached.hasValue() ? expiry.getExpiryForUpdate() : expiry.getExpiryForCreation();
                    txEntry.ttl(CU.toTtl(duration));
                }
            }
            boolean hasFilters = !F.isEmptyOrNulls(txEntry.filters()) && !F.isAlwaysTrue(txEntry.filters());
            CacheObject val;
            CacheObject oldVal = null;
            boolean readOld = hasFilters || retVal || txEntry.op() == DELETE || txEntry.op() == TRANSFORM || tx.nearOnOriginatingNode() || tx.hasInterceptor();
            if (readOld) {
                boolean readThrough = !txEntry.skipStore() && (txEntry.op() == TRANSFORM || ((retVal || hasFilters) && cacheCtx.config().isLoadPreviousValue()));
                boolean evt = retVal || txEntry.op() == TRANSFORM;
                EntryProcessor entryProc = null;
                if (evt && txEntry.op() == TRANSFORM)
                    entryProc = F.first(txEntry.entryProcessors()).get1();
                final boolean keepBinary = txEntry.keepBinary();
                val = oldVal = cached.innerGet(null, tx, readThrough, /*metrics*/
                retVal, /*event*/
                evt, tx.subjectId(), entryProc, tx.resolveTaskName(), null, keepBinary);
                if (retVal || txEntry.op() == TRANSFORM) {
                    if (!F.isEmpty(txEntry.entryProcessors())) {
                        invoke = true;
                        if (txEntry.hasValue())
                            val = txEntry.value();
                        KeyCacheObject key = txEntry.key();
                        Object procRes = null;
                        Exception err = null;
                        boolean modified = false;
                        txEntry.oldValueOnPrimary(val != null);
                        for (T2<EntryProcessor<Object, Object, Object>, Object[]> t : txEntry.entryProcessors()) {
                            CacheInvokeEntry<Object, Object> invokeEntry = new CacheInvokeEntry<>(key, val, txEntry.cached().version(), keepBinary, txEntry.cached());
                            try {
                                EntryProcessor<Object, Object, Object> processor = t.get1();
                                procRes = processor.process(invokeEntry, t.get2());
                                val = cacheCtx.toCacheObject(invokeEntry.getValue(true));
                                if (// no validation for remove case
                                val != null)
                                    cacheCtx.validateKeyAndValue(key, val);
                            } catch (Exception e) {
                                err = e;
                                break;
                            }
                            modified |= invokeEntry.modified();
                        }
                        if (modified)
                            val = cacheCtx.toCacheObject(cacheCtx.unwrapTemporary(val));
                        GridCacheOperation op = modified ? (val == null ? DELETE : UPDATE) : NOOP;
                        if (op == NOOP) {
                            if (expiry != null) {
                                long ttl = CU.toTtl(expiry.getExpiryForAccess());
                                txEntry.ttl(ttl);
                                if (ttl == CU.TTL_ZERO)
                                    op = DELETE;
                            }
                        }
                        txEntry.entryProcessorCalculatedValue(new T2<>(op, op == NOOP ? null : val));
                        if (retVal) {
                            if (err != null || procRes != null)
                                ret.addEntryProcessResult(txEntry.context(), key, null, procRes, err, keepBinary);
                            else
                                ret.invokeResult(true);
                        }
                    } else if (retVal)
                        ret.value(cacheCtx, val, keepBinary);
                }
                if (hasFilters && !cacheCtx.isAll(cached, txEntry.filters())) {
                    if (expiry != null)
                        txEntry.ttl(CU.toTtl(expiry.getExpiryForAccess()));
                    txEntry.op(GridCacheOperation.NOOP);
                    if (filterFailedKeys == null)
                        filterFailedKeys = new ArrayList<>();
                    filterFailedKeys.add(cached.txKey());
                    ret.success(false);
                } else
                    ret.success(txEntry.op() != DELETE || cached.hasValue());
            }
            // Send old value in case if rebalancing is not finished.
            final boolean sndOldVal = !cacheCtx.isLocal() && !cacheCtx.topology().rebalanceFinished(tx.topologyVersion());
            if (sndOldVal) {
                if (oldVal == null && !readOld) {
                    oldVal = cached.innerGet(null, tx, /*readThrough*/
                    false, /*metrics*/
                    false, /*event*/
                    false, /*subjectId*/
                    tx.subjectId(), /*transformClo*/
                    null, /*taskName*/
                    null, /*expiryPlc*/
                    null, /*keepBinary*/
                    true);
                }
                if (oldVal != null)
                    oldVal.prepareMarshal(cacheCtx.cacheObjectContext());
                txEntry.oldValue(oldVal);
            }
        } catch (IgniteCheckedException e) {
            U.error(log, "Failed to get result value for cache entry: " + cached, e);
        } catch (GridCacheEntryRemovedException e) {
            assert false : "Got entry removed exception while holding transactional lock on entry [e=" + e + ", cached=" + cached + ']';
        } finally {
            cctx.database().checkpointReadUnlock();
        }
    }
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) ArrayList(java.util.ArrayList) Duration(javax.cache.expiry.Duration) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) IgniteFutureCancelledException(org.apache.ignite.lang.IgniteFutureCancelledException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) EntryProcessor(javax.cache.processor.EntryProcessor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheInvokeEntry(org.apache.ignite.internal.processors.cache.CacheInvokeEntry) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 94 with GridCacheContext

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

the class GridDhtTxPrepareFuture method readyLocks.

/**
 * @param checkEntries Entries.
 */
private void readyLocks(Iterable<IgniteTxEntry> checkEntries) {
    for (IgniteTxEntry txEntry : checkEntries) {
        GridCacheContext cacheCtx = txEntry.context();
        if (cacheCtx.isLocal())
            continue;
        GridDistributedCacheEntry entry = (GridDistributedCacheEntry) txEntry.cached();
        if (entry == null) {
            entry = (GridDistributedCacheEntry) cacheCtx.cache().entryEx(txEntry.key(), tx.topologyVersion());
            txEntry.cached(entry);
        }
        if (tx.optimistic() && txEntry.explicitVersion() == null) {
            synchronized (this) {
                lockKeys.add(txEntry.txKey());
            }
        }
        while (true) {
            try {
                assert txEntry.explicitVersion() == null || entry.lockedBy(txEntry.explicitVersion());
                CacheLockCandidates owners = entry.readyLock(tx.xidVersion());
                if (log.isDebugEnabled())
                    log.debug("Current lock owners for entry [owner=" + owners + ", entry=" + entry + ']');
                // While.
                break;
            }// Possible if entry cached within transaction is obsolete.
             catch (GridCacheEntryRemovedException ignored) {
                if (log.isDebugEnabled())
                    log.debug("Got removed entry in future onAllReplies method (will retry): " + txEntry);
                entry = (GridDistributedCacheEntry) cacheCtx.cache().entryEx(txEntry.key(), tx.topologyVersion());
                txEntry.cached(entry);
            }
        }
    }
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheLockCandidates(org.apache.ignite.internal.processors.cache.CacheLockCandidates)

Example 95 with GridCacheContext

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

the class GridDhtTxPrepareRequest method prepareMarshal.

/**
 * {@inheritDoc}
 *
 * @param ctx
 */
@Override
public void prepareMarshal(GridCacheSharedContext ctx) throws IgniteCheckedException {
    super.prepareMarshal(ctx);
    if (owned != null && ownedKeys == null) {
        ownedKeys = owned.keySet();
        ownedVals = owned.values();
        for (IgniteTxKey key : ownedKeys) {
            GridCacheContext cctx = ctx.cacheContext(key.cacheId());
            key.prepareMarshal(cctx);
            if (addDepInfo)
                prepareObject(key, cctx);
        }
    }
    if (nearWrites != null)
        marshalTx(nearWrites, ctx);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)

Aggregations

GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)147 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)37 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)22 HashMap (java.util.HashMap)21 ClusterNode (org.apache.ignite.cluster.ClusterNode)20 Map (java.util.Map)19 UUID (java.util.UUID)18 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)18 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)16 ArrayList (java.util.ArrayList)15 IgniteException (org.apache.ignite.IgniteException)14 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)14 List (java.util.List)13 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)13 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)13 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)11 ConcurrentMap (java.util.concurrent.ConcurrentMap)10 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)10 GridCacheEntryInfo (org.apache.ignite.internal.processors.cache.GridCacheEntryInfo)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8