Search in sources :

Example 81 with GridCacheContext

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

the class IgniteTxAdapter method conflictResolve.

/**
 * Resolve DR conflict.
 *
 * @param op Initially proposed operation.
 * @param txEntry TX entry being updated.
 * @param newVal New value.
 * @param newVer New version.
 * @param old Old entry.
 * @return Tuple with adjusted operation type and conflict context.
 * @throws IgniteCheckedException In case of eny exception.
 * @throws GridCacheEntryRemovedException If entry got removed.
 */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
protected IgniteBiTuple<GridCacheOperation, GridCacheVersionConflictContext> conflictResolve(GridCacheOperation op, IgniteTxEntry txEntry, CacheObject newVal, GridCacheVersion newVer, GridCacheEntryEx old) throws IgniteCheckedException, GridCacheEntryRemovedException {
    assert newVer != null;
    // 1. Calculate TTL and expire time.
    long newTtl = txEntry.ttl();
    long newExpireTime = txEntry.conflictExpireTime();
    // 1.1. If TTL is not changed, then calculate it based on expiry.
    if (newTtl == CU.TTL_NOT_CHANGED) {
        ExpiryPolicy expiry = txEntry.context().expiryForTxEntry(txEntry);
        if (expiry != null) {
            if (op == CREATE)
                newTtl = CU.toTtl(expiry.getExpiryForCreation());
            else if (op == UPDATE)
                newTtl = CU.toTtl(expiry.getExpiryForUpdate());
        }
    }
    // 1.2. If TTL is set to zero, then mark operation as "DELETE".
    if (newTtl == CU.TTL_ZERO) {
        op = DELETE;
        newTtl = CU.TTL_ETERNAL;
    }
    // 1.3. If TTL is still not changed, then either use old entry TTL or set it to "ETERNAL".
    if (newTtl == CU.TTL_NOT_CHANGED) {
        if (old.isNewLocked())
            newTtl = CU.TTL_ETERNAL;
        else {
            newTtl = old.rawTtl();
            newExpireTime = old.rawExpireTime();
        }
    }
    // TTL must be resolved at this point.
    assert newTtl != CU.TTL_ZERO && newTtl != CU.TTL_NOT_CHANGED;
    // 1.4 If expire time was not set explicitly, then calculate it.
    if (newExpireTime == CU.EXPIRE_TIME_CALCULATE)
        newExpireTime = CU.toExpireTime(newTtl);
    // Expire time must be resolved at this point.
    assert newExpireTime != CU.EXPIRE_TIME_CALCULATE;
    // Construct old entry info.
    GridCacheVersionedEntryEx oldEntry = old.versionedEntry(txEntry.keepBinary());
    // Construct new entry info.
    GridCacheContext entryCtx = txEntry.context();
    GridCacheVersionedEntryEx newEntry = new GridCacheLazyPlainVersionedEntry(entryCtx, txEntry.key(), newVal, newTtl, newExpireTime, newVer, false, txEntry.keepBinary());
    GridCacheVersionConflictContext ctx = old.context().conflictResolve(oldEntry, newEntry, false);
    if (ctx.isMerge()) {
        Object resVal = ctx.mergeValue();
        if ((op == CREATE || op == UPDATE) && resVal == null)
            op = DELETE;
        else if (op == DELETE && resVal != null)
            op = old.isNewLocked() ? CREATE : UPDATE;
    }
    return F.t(op, ctx);
}
Also used : GridCacheVersionConflictContext(org.apache.ignite.internal.processors.cache.version.GridCacheVersionConflictContext) GridCacheLazyPlainVersionedEntry(org.apache.ignite.internal.processors.cache.version.GridCacheLazyPlainVersionedEntry) GridCacheVersionedEntryEx(org.apache.ignite.internal.processors.cache.version.GridCacheVersionedEntryEx) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 82 with GridCacheContext

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

the class IgniteTxAdapter method applyTransformClosures.

/**
 * @param txEntry Entry to process.
 * @param metrics {@code True} if metrics should be updated.
 * @param ret Optional return value to initialize.
 * @return Tuple containing transformation results.
 * @throws IgniteCheckedException If failed to get previous value for transform.
 * @throws GridCacheEntryRemovedException If entry was concurrently deleted.
 */
protected IgniteBiTuple<GridCacheOperation, CacheObject> applyTransformClosures(IgniteTxEntry txEntry, boolean metrics, @Nullable GridCacheReturn ret) throws GridCacheEntryRemovedException, IgniteCheckedException {
    assert txEntry.op() != TRANSFORM || !F.isEmpty(txEntry.entryProcessors()) : txEntry;
    GridCacheContext cacheCtx = txEntry.context();
    assert cacheCtx != null;
    if (isSystemInvalidate())
        return F.t(cacheCtx.writeThrough() ? RELOAD : DELETE, null);
    if (F.isEmpty(txEntry.entryProcessors())) {
        if (ret != null)
            ret.value(cacheCtx, txEntry.value(), txEntry.keepBinary());
        return F.t(txEntry.op(), txEntry.value());
    } else {
        T2<GridCacheOperation, CacheObject> calcVal = txEntry.entryProcessorCalculatedValue();
        if (calcVal != null)
            return calcVal;
        boolean recordEvt = cctx.gridEvents().isRecordable(EVT_CACHE_OBJECT_READ);
        final boolean keepBinary = txEntry.keepBinary();
        CacheObject cacheVal;
        if (txEntry.hasValue())
            cacheVal = txEntry.value();
        else if (txEntry.hasOldValue())
            cacheVal = txEntry.oldValue();
        else {
            cacheVal = txEntry.cached().innerGet(null, this, /*read through*/
            false, /*metrics*/
            metrics, /*event*/
            recordEvt, /*subjId*/
            subjId, /*closure name */
            recordEvt ? F.first(txEntry.entryProcessors()).get1() : null, resolveTaskName(), null, keepBinary);
        }
        boolean modified = false;
        Object val = null;
        Object key = null;
        GridCacheVersion ver;
        try {
            ver = txEntry.cached().version();
        } catch (GridCacheEntryRemovedException e) {
            assert optimistic() : txEntry;
            if (log.isDebugEnabled())
                log.debug("Failed to get entry version: [msg=" + e.getMessage() + ']');
            ver = null;
        }
        for (T2<EntryProcessor<Object, Object, Object>, Object[]> t : txEntry.entryProcessors()) {
            CacheInvokeEntry<Object, Object> invokeEntry = new CacheInvokeEntry<>(txEntry.key(), key, cacheVal, val, ver, keepBinary, txEntry.cached());
            Object procRes = null;
            Exception err = null;
            try {
                EntryProcessor<Object, Object, Object> processor = t.get1();
                procRes = processor.process(invokeEntry, t.get2());
                val = invokeEntry.getValue();
                key = invokeEntry.key();
            } catch (Exception e) {
                err = e;
            }
            if (ret != null) {
                if (err != null || procRes != null)
                    ret.addEntryProcessResult(txEntry.context(), txEntry.key(), null, procRes, err, keepBinary);
                else
                    ret.invokeResult(true);
            }
            modified |= invokeEntry.modified();
        }
        if (modified)
            cacheVal = cacheCtx.toCacheObject(cacheCtx.unwrapTemporary(val));
        GridCacheOperation op = modified ? (cacheVal == null ? DELETE : UPDATE) : NOOP;
        if (op == NOOP) {
            ExpiryPolicy expiry = cacheCtx.expiryForTxEntry(txEntry);
            if (expiry != null) {
                long ttl = CU.toTtl(expiry.getExpiryForAccess());
                txEntry.ttl(ttl);
                if (ttl == CU.TTL_ZERO)
                    op = DELETE;
            }
        }
        return F.t(op, cacheVal);
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IOException(java.io.IOException) ObjectStreamException(java.io.ObjectStreamException) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) EntryProcessor(javax.cache.processor.EntryProcessor) 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)

Example 83 with GridCacheContext

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

the class AffinityHistoryCleanupTest method checkHistory.

/**
 * @param ignite Node.
 * @param expHist Expected history.
 * @param expSize Expected 'non client events' history size.
 * @throws Exception If failed.
 */
private void checkHistory(Ignite ignite, List<AffinityTopologyVersion> expHist, int expSize) throws Exception {
    awaitPartitionMapExchange();
    GridCacheProcessor proc = ((IgniteKernal) ignite).context().cache();
    int cnt = 0;
    for (GridCacheContext cctx : proc.context().cacheContexts()) {
        GridAffinityAssignmentCache aff = GridTestUtils.getFieldValue(cctx.affinity(), "aff");
        AtomicInteger histSize = GridTestUtils.getFieldValue(aff, "histSize");
        assertEquals(expSize, histSize.get());
        Map<AffinityTopologyVersion, Object> cache = GridTestUtils.getFieldValue(aff, "affCache");
        assertEquals("Unexpected history: " + cache.keySet(), expHist.size(), cache.size());
        for (AffinityTopologyVersion topVer : expHist) assertTrue("No history [ver=" + topVer + ", hist=" + cache.keySet() + ']', cache.containsKey(topVer));
        cnt++;
    }
    assert cnt > 4;
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) GridCacheProcessor(org.apache.ignite.internal.processors.cache.GridCacheProcessor)

Example 84 with GridCacheContext

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

the class IgniteTxAdapter method isNearLocallyMapped.

/**
 * @param e Transaction entry.
 * @param primaryOnly Flag to include backups into check or not.
 * @return {@code True} if entry is locally mapped as a primary or back up node.
 */
protected boolean isNearLocallyMapped(IgniteTxEntry e, boolean primaryOnly) {
    GridCacheContext cacheCtx = e.context();
    if (!cacheCtx.isNear())
        return false;
    // Try to take either entry-recorded primary node ID,
    // or transaction node ID from near-local transactions.
    UUID nodeId = e.nodeId() == null ? local() ? this.nodeId : null : e.nodeId();
    if (nodeId != null && nodeId.equals(cctx.localNodeId()))
        return true;
    GridCacheEntryEx cached = e.cached();
    int part = cached != null ? cached.partition() : cacheCtx.affinity().partition(e.key());
    List<ClusterNode> affNodes = cacheCtx.affinity().nodesByPartition(part, topologyVersion());
    e.locallyMapped(F.contains(affNodes, cctx.localNode()));
    if (primaryOnly) {
        ClusterNode primary = F.first(affNodes);
        if (primary == null && !cacheCtx.affinityNode())
            return false;
        assert primary != null : "Primary node is null for affinity nodes: " + affNodes;
        return primary.isLocal();
    } else
        return e.locallyMapped();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) UUID(java.util.UUID)

Example 85 with GridCacheContext

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

the class GridContinuousProcessor method processStartAckRequest.

/**
 * @param topVer Topology version.
 * @param msg Message.
 */
private void processStartAckRequest(AffinityTopologyVersion topVer, StartRoutineAckDiscoveryMessage msg) {
    StartFuture fut = startFuts.remove(msg.routineId());
    if (fut != null) {
        if (msg.errs().isEmpty()) {
            LocalRoutineInfo routine = locInfos.get(msg.routineId());
            // Update partition counters.
            if (routine != null && routine.handler().isQuery()) {
                Map<UUID, Map<Integer, T2<Long, Long>>> cntrsPerNode = msg.updateCountersPerNode();
                Map<Integer, T2<Long, Long>> cntrs = msg.updateCounters();
                GridCacheAdapter<Object, Object> interCache = ctx.cache().internalCache(routine.handler().cacheName());
                GridCacheContext cctx = interCache != null ? interCache.context() : null;
                if (cctx != null && cntrsPerNode != null && !cctx.isLocal() && cctx.affinityNode())
                    cntrsPerNode.put(ctx.localNodeId(), toCountersMap(cctx.topology().localUpdateCounters(false)));
                routine.handler().updateCounters(topVer, cntrsPerNode, cntrs);
            }
            fut.onRemoteRegistered();
        } else {
            IgniteCheckedException firstEx = F.first(msg.errs().values());
            fut.onDone(firstEx);
            stopRoutine(msg.routineId());
        }
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicLong(java.util.concurrent.atomic.AtomicLong) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) UUID(java.util.UUID) CachePartitionPartialCountersMap.toCountersMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.CachePartitionPartialCountersMap.toCountersMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) T2(org.apache.ignite.internal.util.typedef.T2)

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