Search in sources :

Example 16 with CacheDataRow

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

the class GridCacheMapEntry method updateIndex.

/**
 * {@inheritDoc}
 */
@Override
public void updateIndex(SchemaIndexCacheVisitorClosure clo) throws IgniteCheckedException, GridCacheEntryRemovedException {
    lockEntry();
    try {
        if (isInternal())
            return;
        checkObsolete();
        CacheDataRow row = cctx.offheap().read(this);
        if (row != null)
            clo.apply(row);
    } finally {
        unlockEntry();
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)

Example 17 with CacheDataRow

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

the class GridCacheMapEntry method initialValue.

/**
 * {@inheritDoc}
 */
@Override
public boolean initialValue(CacheObject val, GridCacheVersion ver, MvccVersion mvccVer, MvccVersion newMvccVer, byte mvccTxState, byte newMvccTxState, long ttl, long expireTime, boolean preload, AffinityTopologyVersion topVer, GridDrType drType, boolean fromStore, boolean primary, CacheDataRow row) throws IgniteCheckedException, GridCacheEntryRemovedException {
    assert !primary || !(preload || fromStore);
    ensureFreeSpace();
    boolean deferred = false;
    boolean obsolete = false;
    GridCacheVersion oldVer = null;
    lockListenerReadLock();
    lockEntry();
    try {
        checkObsolete();
        boolean walEnabled = !cctx.isNear() && cctx.group().persistenceEnabled() && cctx.group().walEnabled();
        long expTime = expireTime < 0 ? CU.toExpireTime(ttl) : expireTime;
        val = cctx.kernalContext().cacheObjects().prepareForCache(val, cctx);
        final boolean unswapped = ((flags & IS_UNSWAPPED_MASK) != 0);
        boolean update;
        IgnitePredicate<CacheDataRow> p = new IgnitePredicate<CacheDataRow>() {

            @Override
            public boolean apply(@Nullable CacheDataRow row) {
                boolean update0;
                GridCacheVersion currentVer = row != null ? row.version() : GridCacheMapEntry.this.ver;
                boolean isStartVer = cctx.shared().versions().isStartVersion(currentVer);
                if (cctx.group().persistenceEnabled()) {
                    if (!isStartVer) {
                        if (cctx.atomic())
                            update0 = ATOMIC_VER_COMPARATOR.compare(currentVer, ver) < 0;
                        else
                            update0 = currentVer.compareTo(ver) < 0;
                    } else
                        update0 = true;
                } else
                    update0 = isStartVer;
                update0 |= (!preload && deletedUnlocked());
                return update0;
            }
        };
        if (unswapped) {
            update = p.apply(null);
            if (update) {
                // If entry is already unswapped and we are modifying it, we must run deletion callbacks for old value.
                long oldExpTime = expireTimeUnlocked();
                if (oldExpTime > 0 && oldExpTime < U.currentTimeMillis()) {
                    if (onExpired(this.val, null)) {
                        if (cctx.deferredDelete()) {
                            deferred = true;
                            oldVer = this.ver;
                        } else if (val == null)
                            obsolete = true;
                    }
                }
                if (cctx.mvccEnabled()) {
                    assert !preload;
                    cctx.offheap().mvccInitialValue(this, val, ver, expTime, mvccVer, newMvccVer);
                } else
                    storeValue(val, expTime, ver, null, row);
            }
        } else {
            if (cctx.mvccEnabled()) {
                // cannot identify whether the entry is exist on the fly
                unswap(false);
                if (update = p.apply(null)) {
                    // If entry is already unswapped and we are modifying it, we must run deletion callbacks for old value.
                    long oldExpTime = expireTimeUnlocked();
                    long delta = (oldExpTime == 0 ? 0 : oldExpTime - U.currentTimeMillis());
                    if (delta < 0) {
                        if (onExpired(this.val, null)) {
                            if (cctx.deferredDelete()) {
                                deferred = true;
                                oldVer = this.ver;
                            } else if (val == null)
                                obsolete = true;
                        }
                    }
                    assert !preload;
                    cctx.offheap().mvccInitialValue(this, val, ver, expTime, mvccVer, newMvccVer);
                }
            } else
                // Optimization to access storage only once.
                update = storeValue(val, expTime, ver, p, row);
        }
        if (update) {
            update(val, expTime, ttl, ver, true);
            boolean skipQryNtf = false;
            if (val == null) {
                skipQryNtf = true;
                if (cctx.deferredDelete() && !deletedUnlocked() && !isInternal())
                    deletedUnlocked(true);
            } else if (deletedUnlocked())
                deletedUnlocked(false);
            long updateCntr = 0;
            if (!preload)
                updateCntr = nextPartitionCounter(topVer, true, true, null);
            if (walEnabled) {
                if (cctx.mvccEnabled()) {
                    cctx.shared().wal().log(new MvccDataRecord(new MvccDataEntry(cctx.cacheId(), key, val, val == null ? DELETE : GridCacheOperation.CREATE, null, ver, expireTime, partition(), updateCntr, mvccVer == null ? MvccUtils.INITIAL_VERSION : mvccVer)));
                } else {
                    cctx.shared().wal().log(new DataRecord(new DataEntry(cctx.cacheId(), key, val, val == null ? DELETE : GridCacheOperation.CREATE, null, ver, expireTime, partition(), updateCntr, DataEntry.flags(primary, preload, fromStore))));
                }
            }
            drReplicate(drType, val, ver, topVer);
            if (!skipQryNtf) {
                cctx.continuousQueries().onEntryUpdated(key, val, null, this.isInternal() || !this.context().userCache(), this.partition(), true, preload, updateCntr, null, topVer);
            }
            updatePlatformCache(val, topVer);
            onUpdateFinished(updateCntr);
            if (!fromStore && cctx.store().isLocal()) {
                if (val != null)
                    cctx.store().put(null, key, val, ver);
            }
            return true;
        }
        return false;
    } finally {
        unlockEntry();
        unlockListenerReadLock();
        if (obsolete) {
            onMarkedObsolete();
            cctx.cache().removeEntry(this);
        }
        if (deferred) {
            assert oldVer != null;
            cctx.onDeferredDelete(this, oldVer);
        }
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) MvccDataEntry(org.apache.ignite.internal.pagemem.wal.record.MvccDataEntry) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) MvccDataEntry(org.apache.ignite.internal.pagemem.wal.record.MvccDataEntry) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) MvccDataRecord(org.apache.ignite.internal.pagemem.wal.record.MvccDataRecord) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) MvccDataRecord(org.apache.ignite.internal.pagemem.wal.record.MvccDataRecord) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with CacheDataRow

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

the class IgniteCacheOffheapManagerImpl method iterator.

/**
 * @param cacheId Cache ID.
 * @param dataIt Data store iterator.
 * @param mvccSnapshot Mvcc snapshot.
 * @param dataPageScanEnabled Flag to enable data page scan.
 * @return Rows iterator
 */
private GridCloseableIterator<CacheDataRow> iterator(int cacheId, Iterator<CacheDataStore> dataIt, MvccSnapshot mvccSnapshot, Boolean dataPageScanEnabled) {
    return new GridCloseableIteratorAdapter<CacheDataRow>() {

        /**
         */
        private GridCursor<? extends CacheDataRow> cur;

        /**
         */
        private int curPart;

        /**
         */
        private CacheDataRow next;

        @Override
        protected CacheDataRow onNext() {
            CacheDataRow res = next;
            next = null;
            return res;
        }

        @Override
        protected boolean onHasNext() throws IgniteCheckedException {
            if (next != null)
                return true;
            while (true) {
                try {
                    if (cur == null) {
                        if (dataIt.hasNext()) {
                            CacheDataStore ds = dataIt.next();
                            curPart = ds.partId();
                            // Data page scan is disabled by default for scan queries.
                            // TODO https://issues.apache.org/jira/browse/IGNITE-11998
                            CacheDataTree.setDataPageScanEnabled(false);
                            try {
                                if (mvccSnapshot == null)
                                    cur = cacheId == CU.UNDEFINED_CACHE_ID ? ds.cursor() : ds.cursor(cacheId);
                                else {
                                    cur = cacheId == CU.UNDEFINED_CACHE_ID ? ds.cursor(mvccSnapshot) : ds.cursor(cacheId, mvccSnapshot);
                                }
                            } finally {
                                CacheDataTree.setDataPageScanEnabled(false);
                            }
                        } else
                            break;
                    }
                    if (cur.next()) {
                        next = cur.get();
                        next.key().partition(curPart);
                        break;
                    } else
                        cur = null;
                } catch (IgniteCheckedException ex) {
                    throw new IgniteCheckedException("Failed to get next data row due to underlying cursor " + "invalidation", ex);
                }
            }
            return next != null;
        }
    };
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCursor(org.apache.ignite.internal.util.lang.GridCursor) GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 19 with CacheDataRow

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

the class IgniteCacheOffheapManagerImpl method read.

/**
 * {@inheritDoc}
 */
@Nullable
@Override
public CacheDataRow read(GridCacheContext cctx, KeyCacheObject key) throws IgniteCheckedException {
    CacheDataStore dataStore = dataStore(cctx, key);
    CacheDataRow row = dataStore != null ? dataStore.find(cctx, key) : null;
    assert row == null || row.value() != null : row;
    return row;
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with CacheDataRow

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

the class IgniteCacheOffheapManagerImpl method clearCache.

/**
 * Clears offheap entries.
 *
 * @param readers {@code True} to clear readers.
 */
@Override
public void clearCache(GridCacheContext cctx, boolean readers) {
    GridCacheVersion obsoleteVer = null;
    try (GridCloseableIterator<CacheDataRow> it = grp.isLocal() ? iterator(cctx.cacheId(), cacheDataStores().iterator(), null, null) : evictionSafeIterator(cctx.cacheId(), cacheDataStores().iterator())) {
        while (it.hasNext()) {
            cctx.shared().database().checkpointReadLock();
            try {
                KeyCacheObject key = it.next().key();
                try {
                    if (obsoleteVer == null)
                        obsoleteVer = cctx.cache().nextVersion();
                    GridCacheEntryEx entry = cctx.cache().entryEx(key);
                    entry.clear(obsoleteVer, readers);
                } catch (GridDhtInvalidPartitionException ignore) {
                // Ignore.
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to clear cache entry: " + key, e);
                }
            } finally {
                cctx.shared().database().checkpointReadUnlock();
            }
        }
    } catch (IgniteCheckedException e) {
        U.error(log, "Failed to close iterator", e);
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Aggregations

CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)78 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)35 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)20 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)16 ArrayList (java.util.ArrayList)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 IgniteException (org.apache.ignite.IgniteException)14 Nullable (org.jetbrains.annotations.Nullable)12 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)11 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)11 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)11 HashMap (java.util.HashMap)10 IgniteEx (org.apache.ignite.internal.IgniteEx)10 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)10 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)10 HashSet (java.util.HashSet)9 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)9 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)8 GridCursor (org.apache.ignite.internal.util.lang.GridCursor)8 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)7