Search in sources :

Example 96 with GridCacheContext

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

the class DmlStatementsProcessor method doFastUpdate.

/**
     * Perform single cache operation based on given args.
     * @param args Query parameters.
     * @return 1 if an item was affected, 0 otherwise.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private static UpdateResult doFastUpdate(UpdatePlan plan, Object[] args) throws IgniteCheckedException {
    GridCacheContext cctx = plan.tbl.rowDescriptor().context();
    FastUpdateArguments singleUpdate = plan.fastUpdateArgs;
    assert singleUpdate != null;
    boolean valBounded = (singleUpdate.val != FastUpdateArguments.NULL_ARGUMENT);
    if (singleUpdate.newVal != FastUpdateArguments.NULL_ARGUMENT) {
        // Single item UPDATE
        Object key = singleUpdate.key.apply(args);
        Object newVal = singleUpdate.newVal.apply(args);
        if (valBounded) {
            Object val = singleUpdate.val.apply(args);
            return (cctx.cache().replace(key, val, newVal) ? UpdateResult.ONE : UpdateResult.ZERO);
        } else
            return (cctx.cache().replace(key, newVal) ? UpdateResult.ONE : UpdateResult.ZERO);
    } else {
        // Single item DELETE
        Object key = singleUpdate.key.apply(args);
        Object val = singleUpdate.val.apply(args);
        if (// No _val bound in source query
        singleUpdate.val == FastUpdateArguments.NULL_ARGUMENT)
            return cctx.cache().remove(key) ? UpdateResult.ONE : UpdateResult.ZERO;
        else
            return cctx.cache().remove(key, val) ? UpdateResult.ONE : UpdateResult.ZERO;
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) BinaryObject(org.apache.ignite.binary.BinaryObject) FastUpdateArguments(org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)

Example 97 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 98 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 99 with GridCacheContext

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

the class IgniteTxHandler method needRemap.

/**
     * @param expVer Expected topology version.
     * @param curVer Current topology version.
     * @param req Request.
     * @return {@code True} if cache affinity changed and request should be remapped.
     */
private boolean needRemap(AffinityTopologyVersion expVer, AffinityTopologyVersion curVer, GridNearTxPrepareRequest req) {
    if (expVer.equals(curVer))
        return false;
    for (IgniteTxEntry e : F.concat(false, req.reads(), req.writes())) {
        GridCacheContext ctx = e.context();
        Collection<ClusterNode> cacheNodes0 = ctx.discovery().cacheAffinityNodes(ctx.cacheId(), expVer);
        Collection<ClusterNode> cacheNodes1 = ctx.discovery().cacheAffinityNodes(ctx.cacheId(), curVer);
        if (!cacheNodes0.equals(cacheNodes1) || ctx.affinity().affinityTopologyVersion().compareTo(curVer) < 0)
            return true;
        try {
            List<List<ClusterNode>> aff1 = ctx.affinity().assignments(expVer);
            List<List<ClusterNode>> aff2 = ctx.affinity().assignments(curVer);
            if (!aff1.equals(aff2))
                return true;
        } catch (IllegalStateException ignored) {
            return true;
        }
    }
    return false;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) List(java.util.List)

Example 100 with GridCacheContext

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

the class GridCacheJdbcBlobStoreMultithreadedSelfTest method checkOpenedClosedCount.

/**
     *
     */
private void checkOpenedClosedCount() {
    assertEquals(GRID_CNT, Ignition.allGrids().size());
    for (Ignite ignite : Ignition.allGrids()) {
        GridCacheContext cctx = ((IgniteKernal) ignite).internalCache(DEFAULT_CACHE_NAME).context();
        CacheStore store = cctx.store().configuredStore();
        long opened = ((LongAdder8) U.field(store, "opened")).sum();
        long closed = ((LongAdder8) U.field(store, "closed")).sum();
        assert opened > 0;
        assert closed > 0;
        assertEquals(opened, closed);
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) Ignite(org.apache.ignite.Ignite) CacheStore(org.apache.ignite.cache.store.CacheStore) LongAdder8(org.jsr166.LongAdder8)

Aggregations

GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)121 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)27 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)20 ClusterNode (org.apache.ignite.cluster.ClusterNode)18 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)17 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)15 UUID (java.util.UUID)14 HashMap (java.util.HashMap)13 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)13 Map (java.util.Map)12 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)12 ArrayList (java.util.ArrayList)11 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)11 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)10 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)10 List (java.util.List)9 IgniteException (org.apache.ignite.IgniteException)8 GridCacheEntryInfo (org.apache.ignite.internal.processors.cache.GridCacheEntryInfo)8 Ignite (org.apache.ignite.Ignite)7 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)7