Search in sources :

Example 1 with GridCacheVersion

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

the class IgniteUtils method readVersion.

/**
     * @param arr Array.
     * @param off Offset.
     * @param verEx If {@code true} reads {@link GridCacheVersionEx} instance.
     * @return Version.
     */
public static GridCacheVersion readVersion(byte[] arr, long off, boolean verEx) {
    int topVer = GridUnsafe.getInt(arr, off);
    off += 4;
    int nodeOrderDrId = GridUnsafe.getInt(arr, off);
    off += 4;
    long order = GridUnsafe.getLong(arr, off);
    off += 8;
    GridCacheVersion ver = new GridCacheVersion(topVer, nodeOrderDrId, order);
    if (verEx) {
        topVer = GridUnsafe.getInt(arr, off);
        off += 4;
        nodeOrderDrId = GridUnsafe.getInt(arr, off);
        off += 4;
        order = GridUnsafe.getLong(arr, off);
        ver = new GridCacheVersionEx(topVer, nodeOrderDrId, order, ver);
    }
    return ver;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheVersionEx(org.apache.ignite.internal.processors.cache.version.GridCacheVersionEx)

Example 2 with GridCacheVersion

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

the class IgniteUtils method readVersion.

/**
     * @param ptr Offheap address.
     * @param verEx If {@code true} reads {@link GridCacheVersionEx} instance.
     * @return Version.
     */
public static GridCacheVersion readVersion(long ptr, boolean verEx) {
    GridCacheVersion ver = new GridCacheVersion(GridUnsafe.getInt(ptr), GridUnsafe.getInt(ptr + 4), GridUnsafe.getLong(ptr + 8));
    if (verEx) {
        ptr += 16;
        ver = new GridCacheVersionEx(GridUnsafe.getInt(ptr), GridUnsafe.getInt(ptr + 4), GridUnsafe.getLong(ptr + 8), ver);
    }
    return ver;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheVersionEx(org.apache.ignite.internal.processors.cache.version.GridCacheVersionEx)

Example 3 with GridCacheVersion

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

the class GridPartitionedGetFuture method localGet.

/**
     * @param key Key.
     * @param part Partition.
     * @param locVals Local values.
     * @return {@code True} if there is no need to further search value.
     */
private boolean localGet(KeyCacheObject key, int part, Map<K, V> locVals) {
    assert cctx.affinityNode() : this;
    GridDhtCacheAdapter<K, V> cache = cache();
    boolean readNoEntry = cctx.readNoEntry(expiryPlc, false);
    boolean evt = !skipVals;
    while (true) {
        try {
            boolean skipEntry = readNoEntry;
            EntryGetResult getRes = null;
            CacheObject v = null;
            GridCacheVersion ver = null;
            if (readNoEntry) {
                CacheDataRow row = cctx.offheap().read(key);
                if (row != null) {
                    long expireTime = row.expireTime();
                    if (expireTime == 0 || expireTime > U.currentTimeMillis()) {
                        v = row.value();
                        if (needVer)
                            ver = row.version();
                        if (evt) {
                            cctx.events().readEvent(key, null, row.value(), subjId, taskName, !deserializeBinary);
                        }
                    } else
                        skipEntry = false;
                }
            }
            if (!skipEntry) {
                GridCacheEntryEx entry = cache.entryEx(key);
                // If our DHT cache do has value, then we peek it.
                if (entry != null) {
                    boolean isNew = entry.isNewLocked();
                    if (needVer) {
                        getRes = entry.innerGetVersioned(null, null, /*update-metrics*/
                        false, /*event*/
                        evt, subjId, null, taskName, expiryPlc, !deserializeBinary, null);
                        if (getRes != null) {
                            v = getRes.value();
                            ver = getRes.version();
                        }
                    } else {
                        v = entry.innerGet(null, null, /*read-through*/
                        false, /*update-metrics*/
                        false, /*event*/
                        evt, subjId, null, taskName, expiryPlc, !deserializeBinary);
                    }
                    cache.context().evicts().touch(entry, topVer);
                    // Entry was not in memory or in swap, so we remove it from cache.
                    if (v == null) {
                        if (isNew && entry.markObsoleteIfEmpty(ver))
                            cache.removeEntry(entry);
                    }
                }
            }
            if (v != null) {
                cctx.addResult(locVals, key, v, skipVals, keepCacheObjects, deserializeBinary, true, getRes, ver, 0, 0, needVer);
                return true;
            }
            boolean topStable = cctx.isReplicated() || topVer.equals(cctx.topology().topologyVersion());
            // Entry not found, do not continue search if topology did not change and there is no store.
            if (!cctx.readThroughConfigured() && (topStable || partitionOwned(part))) {
                if (!skipVals && cctx.config().isStatisticsEnabled())
                    cache.metrics0().onRead(false);
                return true;
            }
            return false;
        } catch (GridCacheEntryRemovedException ignored) {
        // No-op, will retry.
        } catch (GridDhtInvalidPartitionException ignored) {
            return false;
        } catch (IgniteCheckedException e) {
            onDone(e);
            return true;
        }
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 4 with GridCacheVersion

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

the class IgniteCacheOffheapManagerImpl method expire.

/**
 * {@inheritDoc}
 */
@Override
public boolean expire(GridCacheContext cctx, IgniteInClosure2X<GridCacheEntryEx, GridCacheVersion> c, int amount) throws IgniteCheckedException {
    assert !cctx.isNear() : cctx.name();
    if (hasPendingEntries && pendingEntries != null) {
        GridCacheVersion obsoleteVer = null;
        long now = U.currentTimeMillis();
        GridCursor<PendingRow> cur;
        if (grp.sharedGroup())
            cur = pendingEntries.find(new PendingRow(cctx.cacheId()), new PendingRow(cctx.cacheId(), now, 0));
        else
            cur = pendingEntries.find(null, new PendingRow(CU.UNDEFINED_CACHE_ID, now, 0));
        if (!cur.next())
            return false;
        int cleared = 0;
        cctx.shared().database().checkpointReadLock();
        try {
            do {
                PendingRow row = cur.get();
                if (amount != -1 && cleared > amount)
                    return true;
                if (row.key.partition() == -1)
                    row.key.partition(cctx.affinity().partition(row.key));
                assert row.key != null && row.link != 0 && row.expireTime != 0 : row;
                if (pendingEntries.removex(row)) {
                    if (obsoleteVer == null)
                        obsoleteVer = ctx.versions().next();
                    c.apply(cctx.cache().entryEx(row.key), obsoleteVer);
                }
                cleared++;
            } while (cur.next());
        } finally {
            cctx.shared().database().checkpointReadUnlock();
        }
    }
    return false;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) PendingRow(org.apache.ignite.internal.processors.cache.tree.PendingRow)

Example 5 with GridCacheVersion

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

the class GridCacheMapEntry method innerGet0.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked", "RedundantTypeArguments", "TooBroadScope" })
private Object innerGet0(GridCacheVersion nextVer, IgniteInternalTx tx, boolean readThrough, boolean evt, boolean updateMetrics, UUID subjId, Object transformClo, String taskName, @Nullable IgniteCacheExpiryPolicy expiryPlc, boolean retVer, boolean keepBinary, boolean reserveForLoad, @Nullable ReaderArguments readerArgs) throws IgniteCheckedException, GridCacheEntryRemovedException {
    assert !(retVer && readThrough);
    assert !(reserveForLoad && readThrough);
    // Disable read-through if there is no store.
    if (readThrough && !cctx.readThrough())
        readThrough = false;
    GridCacheVersion startVer;
    GridCacheVersion resVer = null;
    boolean obsolete = false;
    boolean deferred = false;
    GridCacheVersion ver0 = null;
    Object res = null;
    lockEntry();
    try {
        checkObsolete();
        boolean valid = valid(tx != null ? tx.topologyVersion() : cctx.affinity().affinityTopologyVersion());
        CacheObject val;
        if (valid) {
            val = this.val;
            if (val == null) {
                if (isStartVersion()) {
                    unswap(null, false);
                    val = this.val;
                }
            }
            if (val != null) {
                long expireTime = expireTimeExtras();
                if (expireTime > 0 && (expireTime - U.currentTimeMillis() <= 0)) {
                    if (onExpired((CacheObject) cctx.unwrapTemporary(val), null)) {
                        val = null;
                        evt = false;
                        if (cctx.deferredDelete()) {
                            deferred = true;
                            ver0 = ver;
                        } else
                            obsolete = true;
                    }
                }
            }
        } else
            val = null;
        CacheObject ret = val;
        if (ret == null) {
            if (updateMetrics && cctx.statisticsEnabled())
                cctx.cache().metrics0().onRead(false);
        } else {
            if (updateMetrics && cctx.statisticsEnabled())
                cctx.cache().metrics0().onRead(true);
        }
        if (evt && cctx.events().isRecordable(EVT_CACHE_OBJECT_READ)) {
            transformClo = EntryProcessorResourceInjectorProxy.unwrap(transformClo);
            GridCacheMvcc mvcc = mvccExtras();
            cctx.events().addEvent(partition(), key, tx, mvcc != null ? mvcc.anyOwner() : null, EVT_CACHE_OBJECT_READ, ret, ret != null, ret, ret != null, subjId, transformClo != null ? transformClo.getClass().getName() : null, taskName, keepBinary);
            // No more notifications.
            evt = false;
        }
        if (ret != null && expiryPlc != null)
            updateTtl(expiryPlc);
        if (retVer) {
            resVer = (isNear() && cctx.transactional()) ? ((GridNearCacheEntry) this).dhtVersion() : this.ver;
            if (resVer == null)
                ret = null;
        }
        // Cache version for optimistic check.
        startVer = ver;
        addReaderIfNeed(readerArgs);
        if (ret != null) {
            assert !obsolete;
            assert !deferred;
            // If return value is consistent, then done.
            res = retVer ? entryGetResult(ret, resVer, false) : ret;
        } else if (reserveForLoad && !obsolete) {
            assert !readThrough;
            assert retVer;
            boolean reserve = !evictionDisabled();
            if (reserve)
                flags |= IS_EVICT_DISABLED;
            res = entryGetResult(null, resVer, reserve);
        }
    } finally {
        unlockEntry();
    }
    if (obsolete) {
        onMarkedObsolete();
        throw new GridCacheEntryRemovedException();
    }
    if (deferred)
        cctx.onDeferredDelete(this, ver0);
    if (res != null)
        return res;
    CacheObject ret = null;
    if (readThrough) {
        IgniteInternalTx tx0 = null;
        if (tx != null && tx.local()) {
            if (cctx.isReplicated() || cctx.isColocated() || tx.near())
                tx0 = tx;
            else if (tx.dht()) {
                GridCacheVersion ver = tx.nearXidVersion();
                tx0 = cctx.dht().near().context().tm().tx(ver);
            }
        }
        Object storeVal = readThrough(tx0, key, false, subjId, taskName);
        ret = cctx.toCacheObject(storeVal);
    }
    if (ret == null && !evt)
        return null;
    lockEntry();
    try {
        long ttl = ttlExtras();
        // If version matched, set value.
        if (startVer.equals(ver)) {
            if (ret != null) {
                // Detach value before index update.
                ret = cctx.kernalContext().cacheObjects().prepareForCache(ret, cctx);
                nextVer = nextVer != null ? nextVer : nextVersion();
                long expTime = CU.toExpireTime(ttl);
                // Update indexes before actual write to entry.
                storeValue(ret, expTime, nextVer, null);
                update(ret, expTime, ttl, nextVer, true);
                if (cctx.deferredDelete() && deletedUnlocked() && !isInternal() && !detached())
                    deletedUnlocked(false);
                assert readerArgs == null;
            }
            if (evt && cctx.events().isRecordable(EVT_CACHE_OBJECT_READ)) {
                transformClo = EntryProcessorResourceInjectorProxy.unwrap(transformClo);
                GridCacheMvcc mvcc = mvccExtras();
                cctx.events().addEvent(partition(), key, tx, mvcc != null ? mvcc.anyOwner() : null, EVT_CACHE_OBJECT_READ, ret, ret != null, null, false, subjId, transformClo != null ? transformClo.getClass().getName() : null, taskName, keepBinary);
            }
        }
    } finally {
        unlockEntry();
    }
    assert ret == null || !retVer;
    return ret;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridNearCacheEntry(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheEntry) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx)

Aggregations

GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)247 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)81 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)70 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)60 UUID (java.util.UUID)58 Test (org.junit.Test)58 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)55 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)51 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)34 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)32 Map (java.util.Map)30 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)30 ArrayList (java.util.ArrayList)29 ClusterNode (org.apache.ignite.cluster.ClusterNode)26 Collection (java.util.Collection)24 HashMap (java.util.HashMap)24 IgniteException (org.apache.ignite.IgniteException)22 Nullable (org.jetbrains.annotations.Nullable)22 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)21 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)20