Search in sources :

Example 86 with GridCacheVersion

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

the class GridCacheMvcc method readyNearLocal.

/**
 * Marks near-local candidate as ready and makes locks reassignment. Following reorderings are performed when
 * candidate is marked ready:
 * <ul>
 *     <li/> All candidates preceding ready one are moved right after it.
 *     <li/> Near local candidate is assigned a mapped dht version. All remote non-pending candidates with
 *          version less then mapped dht version are marked as owned.
 * </ul>
 *
 * @param ver Version to mark as ready.
 * @param mappedVer Mapped dht version.
 * @param committedVers Committed versions.
 * @param rolledBackVers Rolled back versions.
 * @param pending Pending dht versions that are not owned and which version is less then mapped.
 * @return Lock owner after reassignment.
 */
@Nullable
public CacheLockCandidates readyNearLocal(GridCacheVersion ver, GridCacheVersion mappedVer, Collection<GridCacheVersion> committedVers, Collection<GridCacheVersion> rolledBackVers, Collection<GridCacheVersion> pending) {
    GridCacheMvccCandidate cand = candidate(locs, ver);
    if (cand != null) {
        assert cand.nearLocal() : "Near local candidate is not marked as near local: " + cand;
        cand.setReady();
        boolean setMapped = cand.otherVersion(mappedVer);
        assert setMapped : "Failed to set mapped dht version for near local candidate [mappedVer=" + mappedVer + ", cand=" + cand + ']';
        // For near locals we move all not owned candidates after this one.
        List<GridCacheMvccCandidate> mvAfter = null;
        for (ListIterator<GridCacheMvccCandidate> it = locs.listIterator(); it.hasNext(); ) {
            GridCacheMvccCandidate c = it.next();
            assert c.nearLocal() : "Near local candidate is not marked as near local: " + c;
            if (c == cand) {
                if (mvAfter != null)
                    for (GridCacheMvccCandidate mv : mvAfter) it.add(mv);
                break;
            } else {
                if (c.owner())
                    continue;
                assert !c.ready() || (c.read() && cand.read()) : "Cannot have more then one ready near-local candidate [c=" + c + ", cand=" + cand + ", mvcc=" + this + ']';
                it.remove();
                if (mvAfter == null)
                    mvAfter = new LinkedList<>();
                mvAfter.add(c);
            }
        }
        // Mark all remote candidates with less version as owner unless it is pending.
        if (rmts != null) {
            for (GridCacheMvccCandidate rmt : rmts) {
                GridCacheVersion rmtVer = rmt.version();
                if (rmtVer.isLess(mappedVer)) {
                    if (!pending.contains(rmtVer) && !mappedVer.equals(rmt.ownerVersion()))
                        rmt.setOwner();
                } else {
                    // Remote version is greater, so need to check if it was committed or rolled back.
                    if (committedVers.contains(rmtVer) || rolledBackVers.contains(rmtVer))
                        rmt.setOwner();
                }
            }
        }
        reassign();
    }
    return allOwners();
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) LinkedList(java.util.LinkedList) Nullable(org.jetbrains.annotations.Nullable)

Example 87 with GridCacheVersion

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

the class GridCacheMvcc method compareSerializableVersion.

/**
 * @param cand Existing candidate.
 * @param newCand New candidate.
 * @return {@code False} if new candidate can not be added.
 */
private boolean compareSerializableVersion(GridCacheMvccCandidate cand, GridCacheMvccCandidate newCand) {
    assert cand.serializable() && newCand.serializable();
    GridCacheVersion candOrder = cand.serializableOrder();
    assert candOrder != null : cand;
    GridCacheVersion newCandOrder = newCand.serializableOrder();
    assert newCandOrder != null : newCand;
    int cmp = SER_VER_COMPARATOR.compare(candOrder, newCandOrder);
    assert cmp != 0;
    return cmp < 0;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion)

Example 88 with GridCacheVersion

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

the class GridCacheUtils method createBackupPostProcessingClosure.

/**
 * Creates closure that saves initial value to backup partition.
 * <p>
 * Useful only when store with readThrough is used. In situation when
 * get() on backup node returns successful result, it's expected that
 * localPeek() will be successful as well. But it isn't true when
 * primary node loaded value from local store, in this case backups
 * will remain non-initialized.
 * <br>
 * To meet that requirement the value requested from primary should
 * be saved on backup during get().
 * </p>
 *
 * @param topVer Topology version.
 * @param log Logger.
 * @param cctx Cache context.
 * @param key Key.
 * @param expiryPlc Expiry policy.
 * @param readThrough Read through.
 * @param skipVals Skip values.
 */
@Nullable
public static BackupPostProcessingClosure createBackupPostProcessingClosure(final AffinityTopologyVersion topVer, final IgniteLogger log, final GridCacheContext cctx, @Nullable final KeyCacheObject key, @Nullable final IgniteCacheExpiryPolicy expiryPlc, boolean readThrough, boolean skipVals) {
    if (cctx.mvccEnabled() || !readThrough || skipVals || (key != null && !cctx.affinity().backupsByKey(key, topVer).contains(cctx.localNode())))
        return null;
    return new BackupPostProcessingClosure() {

        private void process(KeyCacheObject key, CacheObject val, GridCacheVersion ver, GridDhtCacheAdapter colocated) {
            while (true) {
                GridCacheEntryEx entry = null;
                cctx.shared().database().checkpointReadLock();
                try {
                    entry = colocated.entryEx(key, topVer);
                    entry.initialValue(val, ver, expiryPlc == null ? 0 : expiryPlc.forCreate(), expiryPlc == null ? 0 : toExpireTime(expiryPlc.forCreate()), true, topVer, GridDrType.DR_BACKUP, true, false);
                    break;
                } catch (GridCacheEntryRemovedException ignore) {
                    if (log.isDebugEnabled())
                        log.debug("Got removed entry during postprocessing (will retry): " + entry);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Error saving backup value: " + entry, e);
                    throw new GridClosureException(e);
                } catch (GridDhtInvalidPartitionException ignored) {
                    break;
                } finally {
                    if (entry != null)
                        entry.touch();
                    cctx.shared().database().checkpointReadUnlock();
                }
            }
        }

        @Override
        public void apply(CacheObject val, GridCacheVersion ver) {
            process(key, val, ver, cctx.dht());
        }

        @Override
        public void apply(Collection<GridCacheEntryInfo> infos) {
            if (!F.isEmpty(infos)) {
                GridCacheAffinityManager aff = cctx.affinity();
                ClusterNode locNode = cctx.localNode();
                GridDhtCacheAdapter colocated = cctx.cache().isNear() ? ((GridNearCacheAdapter) cctx.cache()).dht() : cctx.dht();
                for (GridCacheEntryInfo info : infos) {
                    // Save backup value.
                    if (aff.backupsByKey(info.key(), topVer).contains(locNode))
                        process(info.key(), info.value(), info.version(), colocated);
                }
            }
        }
    };
}
Also used : GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException) GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Collection(java.util.Collection) Nullable(org.jetbrains.annotations.Nullable)

Example 89 with GridCacheVersion

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

the class RecordDataV1Serializer method readPlainDataEntry.

/**
 * @param in Input to read from.
 * @return Read entry.
 */
DataEntry readPlainDataEntry(ByteBufferBackedDataInput in, RecordType type) throws IOException, IgniteCheckedException {
    int cacheId = in.readInt();
    int keySize = in.readInt();
    byte keyType = in.readByte();
    byte[] keyBytes = new byte[keySize];
    in.readFully(keyBytes);
    int valSize = in.readInt();
    byte valType = 0;
    byte[] valBytes = null;
    if (valSize >= 0) {
        valType = in.readByte();
        valBytes = new byte[valSize];
        in.readFully(valBytes);
    }
    byte ord = in.readByte();
    GridCacheOperation op = GridCacheOperation.fromOrdinal(ord & 0xFF);
    GridCacheVersion nearXidVer = readVersion(in, true);
    GridCacheVersion writeVer = readVersion(in, false);
    int partId = in.readInt();
    long partCntr = in.readLong();
    long expireTime = in.readLong();
    byte flags = type == DATA_RECORD_V2 ? in.readByte() : (byte) 0;
    GridCacheContext cacheCtx = cctx.cacheContext(cacheId);
    if (cacheCtx != null) {
        CacheObjectContext coCtx = cacheCtx.cacheObjectContext();
        KeyCacheObject key = co.toKeyCacheObject(coCtx, keyType, keyBytes);
        if (key.partition() == -1)
            key.partition(partId);
        CacheObject val = valBytes != null ? co.toCacheObject(coCtx, valType, valBytes) : null;
        return new DataEntry(cacheId, key, val, op, nearXidVer, writeVer, expireTime, partId, partCntr, flags);
    } else
        return new LazyDataEntry(cctx, cacheId, keyType, keyBytes, valType, valBytes, op, nearXidVer, writeVer, expireTime, partId, partCntr, flags);
}
Also used : DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) MvccDataEntry(org.apache.ignite.internal.pagemem.wal.record.MvccDataEntry) LazyDataEntry(org.apache.ignite.internal.pagemem.wal.record.LazyDataEntry) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) LazyDataEntry(org.apache.ignite.internal.pagemem.wal.record.LazyDataEntry) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 90 with GridCacheVersion

use of org.apache.ignite.internal.processors.cache.version.GridCacheVersion 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

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