Search in sources :

Example 81 with KeyCacheObject

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

the class CacheMvccBackupsAbstractTest method assertVersionsEquals.

/**
 * Checks stored versions equality.
 *
 * @param left Keys versions to compare.
 * @param right Keys versions to compare.
 * @throws IgniteCheckedException If failed.
 */
private void assertVersionsEquals(Map<KeyCacheObject, List<CacheDataRow>> left, Map<KeyCacheObject, List<CacheDataRow>> right) throws IgniteCheckedException {
    assertNotNull(left);
    assertNotNull(right);
    assertTrue(!left.isEmpty());
    assertTrue(!right.isEmpty());
    assertEqualsCollections(left.keySet(), right.keySet());
    for (KeyCacheObject key : right.keySet()) {
        List<CacheDataRow> leftRows = left.get(key);
        List<CacheDataRow> rightRows = right.get(key);
        assertKeyVersionsEquals(leftRows, rightRows);
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 82 with KeyCacheObject

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

the class CacheMvccBackupsAbstractTest method allKeyVersions.

/**
 * @param cache Cache.
 * @param key Key.
 * @return Collection of versioned rows.
 * @throws IgniteCheckedException if failed.
 */
private List<CacheDataRow> allKeyVersions(IgniteCache cache, Object key) throws IgniteCheckedException {
    IgniteCacheProxy cache0 = (IgniteCacheProxy) cache;
    GridCacheContext cctx = cache0.context();
    KeyCacheObject key0 = cctx.toCacheKeyObject(key);
    GridCursor<CacheDataRow> cur = cctx.offheap().mvccAllVersionsCursor(cctx, key0, null);
    List<CacheDataRow> rows = new ArrayList<>();
    while (cur.next()) {
        CacheDataRow row = cur.get();
        rows.add(row);
    }
    return rows;
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) ArrayList(java.util.ArrayList) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 83 with KeyCacheObject

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

the class GridCacheNearReadersSelfTest method testTwoNodesTwoKeysOneBackup.

/**
 * @throws Exception If failed.
 */
@Test
public void testTwoNodesTwoKeysOneBackup() throws Exception {
    aff.backups(1);
    grids = 2;
    aff.partitions(grids);
    startGrids();
    ClusterNode n1 = F.first(aff.nodes(aff.partition(1), grid(0).cluster().nodes()));
    ClusterNode n2 = F.first(aff.nodes(aff.partition(2), grid(0).cluster().nodes()));
    assertNotNull(n1);
    assertNotNull(n2);
    assertNotSame(n1, n2);
    assertFalse("Nodes cannot be equal: " + n1, n1.equals(n2));
    Ignite g1 = grid(n1.id());
    Ignite g2 = grid(n2.id());
    awaitPartitionMapExchange();
    GridCacheContext ctx = ((IgniteKernal) g1).internalCache(DEFAULT_CACHE_NAME).context();
    List<KeyCacheObject> cacheKeys = F.asList(ctx.toCacheKeyObject(1), ctx.toCacheKeyObject(2));
    IgniteInternalFuture<Object> f1 = ((IgniteKernal) g1).internalCache(DEFAULT_CACHE_NAME).preloader().request(ctx, cacheKeys, new AffinityTopologyVersion(2));
    if (f1 != null)
        f1.get();
    IgniteInternalFuture<Object> f2 = ((IgniteKernal) g2).internalCache(DEFAULT_CACHE_NAME).preloader().request(((IgniteKernal) g2).internalCache(DEFAULT_CACHE_NAME).context(), cacheKeys, new AffinityTopologyVersion(2));
    if (f2 != null)
        f2.get();
    IgniteCache<Integer, String> cache1 = g1.cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, String> cache2 = g2.cache(DEFAULT_CACHE_NAME);
    assertEquals(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(1), g1.cluster().localNode());
    assertFalse(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(2).equals(g1.cluster().localNode()));
    assertEquals(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(2), g2.cluster().localNode());
    assertFalse(g2.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(1).equals(g2.cluster().localNode()));
    // Store first value in cache.
    assertNull(cache1.getAndPut(1, "v1"));
    assertTrue(cache1.containsKey(1));
    assertTrue(cache2.containsKey(1));
    assertEquals("v1", nearPeek(cache1, 1));
    assertEquals("v1", nearPeek(cache2, 1));
    assertEquals("v1", dhtPeek(cache1, 1));
    assertEquals("v1", dhtPeek(cache2, 1));
    assertNull(near(cache1).peekEx(1));
    assertNull(near(cache2).peekEx(1));
    GridDhtCacheEntry e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
    // Store second value in cache.
    assertNull(cache1.getAndPut(2, "v2"));
    assertTrue(cache1.containsKey(2));
    assertTrue(cache2.containsKey(2));
    assertEquals("v2", nearPeek(cache1, 2));
    assertEquals("v2", nearPeek(cache2, 2));
    assertEquals("v2", dhtPeek(cache1, 2));
    assertEquals("v2", dhtPeek(cache2, 2));
    assertNull(near(cache1).peekEx(2));
    assertNull(near(cache2).peekEx(2));
    GridDhtCacheEntry c2e2 = (GridDhtCacheEntry) dht(cache2).entryEx(2);
    // Nodes are backups of each other, so no readers should be added.
    assertFalse(c2e2.readers().contains(n1.id()));
    assertFalse(e1.readers().contains(n2.id()));
    // Get key1 on node2 (value should come from local DHT cache, as it has a backup).
    assertEquals("v1", cache2.get(1));
    // Since DHT cache2 has the value, Near cache2 should not have it.
    assertNull(near(cache2).peekEx(1));
    e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
    // Since v1 was retrieved locally from cache2, cache1 should not know about it.
    assertFalse(e1.readers().contains(n2.id()));
    // Evict locally from cache2.
    // It should not be successful since it's not allowed to evict entry on backup node.
    cache2.localEvict(Collections.singleton(1));
    assertNull(near(cache2).peekEx(1));
    assertEquals("v1", dhtPeek(cache2, 1));
    assertEquals("v1", cache1.getAndPut(1, "z1"));
    e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
    // Node 1 should not have node2 in readers map.
    assertFalse(e1.readers().contains(n2.id()));
    assertNull(near(cache2).peekEx(1));
    assertEquals("z1", dhtPeek(cache2, 1));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) Ignite(org.apache.ignite.Ignite) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 84 with KeyCacheObject

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

the class TestStorageUtils method corruptDataEntry.

/**
 * Corrupts data entry.
 *
 * @param ctx Context.
 * @param key Key.
 * @param breakCntr Break counter.
 * @param breakData Break data.
 */
public static void corruptDataEntry(GridCacheContext<?, ?> ctx, Object key, boolean breakCntr, boolean breakData) throws IgniteCheckedException {
    assert !ctx.isLocal();
    int partId = ctx.affinity().partition(key);
    GridDhtLocalPartition locPart = ctx.topology().localPartition(partId);
    CacheEntry<Object, Object> e = ctx.cache().keepBinary().getEntry(key);
    KeyCacheObject keyCacheObj = e.getKey() instanceof BinaryObject ? (KeyCacheObject) e.getKey() : new KeyCacheObjectImpl(e.getKey(), null, partId);
    DataEntry dataEntry = new DataEntry(ctx.cacheId(), keyCacheObj, new CacheObjectImpl(breakData ? e.getValue().toString() + "brokenValPostfix" : e.getValue(), null), GridCacheOperation.UPDATE, new GridCacheVersion(), new GridCacheVersion(), 0L, partId, breakCntr ? locPart.updateCounter() + 1 : locPart.updateCounter(), DataEntry.EMPTY_FLAGS);
    IgniteCacheDatabaseSharedManager db = ctx.shared().database();
    db.checkpointReadLock();
    try {
        assert dataEntry.op() == GridCacheOperation.UPDATE;
        ctx.offheap().update(ctx, dataEntry.key(), dataEntry.value(), dataEntry.writeVersion(), dataEntry.expireTime(), locPart, null);
        ctx.offheap().dataStore(locPart).updateInitialCounter(dataEntry.partitionCounter() - 1, 1);
    } finally {
        db.checkpointReadUnlock();
    }
}
Also used : DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager)

Example 85 with KeyCacheObject

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

the class ValidateIndexesClosure method processIndex.

/**
 * @param cacheCtxWithIdx Cache context and appropriate index.
 * @param idleChecker Idle check closure.
 */
private Map<String, ValidateIndexesPartitionResult> processIndex(T2<GridCacheContext, Index> cacheCtxWithIdx, IgniteInClosure<Integer> idleChecker) {
    if (validateCtx.isCancelled())
        return emptyMap();
    GridCacheContext ctx = cacheCtxWithIdx.get1();
    Index idx = cacheCtxWithIdx.get2();
    ValidateIndexesPartitionResult idxValidationRes = new ValidateIndexesPartitionResult();
    boolean enoughIssues = false;
    Cursor cursor = null;
    try (Session session = mvccSession(cacheCtxWithIdx.get1())) {
        cursor = idx.find(session, null, null);
        if (cursor == null)
            throw new IgniteCheckedException("Can't iterate through index: " + idx);
    } catch (Throwable t) {
        IndexValidationIssue is = new IndexValidationIssue(null, ctx.name(), idx.getName(), t);
        log.error("Find in index failed: " + is.toString());
        idxValidationRes.reportIssue(is);
        enoughIssues = true;
    }
    final boolean skipConditions = checkFirst > 0 || checkThrough > 0;
    final boolean bothSkipConditions = checkFirst > 0 && checkThrough > 0;
    long current = 0;
    long processedNumber = 0;
    KeyCacheObject previousKey = null;
    while (!enoughIssues && !validateCtx.isCancelled()) {
        KeyCacheObject h2key = null;
        try {
            try {
                if (!cursor.next())
                    break;
            } catch (DbException e) {
                if (X.hasCause(e, CorruptedTreeException.class))
                    throw new IgniteCheckedException("Key is present in SQL index, but is missing in corresponding " + "data page. Previous successfully read key: " + CacheObjectUtils.unwrapBinaryIfNeeded(ctx.cacheObjectContext(), previousKey, true, true), X.cause(e, CorruptedTreeException.class));
                throw e;
            }
            H2CacheRow h2Row = (H2CacheRow) cursor.get();
            if (skipConditions) {
                if (bothSkipConditions) {
                    if (processedNumber > checkFirst)
                        break;
                    else if (current++ % checkThrough > 0)
                        continue;
                    else
                        processedNumber++;
                } else {
                    if (checkFirst > 0) {
                        if (current++ > checkFirst)
                            break;
                    } else {
                        if (current++ % checkThrough > 0)
                            continue;
                    }
                }
            }
            h2key = h2Row.key();
            if (h2Row.link() != 0L) {
                CacheDataRow cacheDataStoreRow = ctx.group().offheap().read(ctx, h2key);
                if (cacheDataStoreRow == null)
                    throw new IgniteCheckedException("Key is present in SQL index, but can't be found in CacheDataTree.");
            } else
                throw new IgniteCheckedException("Invalid index row, possibly deleted " + h2Row);
        } catch (Throwable t) {
            Object o = CacheObjectUtils.unwrapBinaryIfNeeded(ctx.cacheObjectContext(), h2key, true, true);
            IndexValidationIssue is = new IndexValidationIssue(String.valueOf(o), ctx.name(), idx.getName(), t);
            log.error("Failed to lookup key: " + is.toString());
            enoughIssues |= idxValidationRes.reportIssue(is);
        } finally {
            previousKey = h2key;
        }
    }
    CacheGroupContext group = ctx.group();
    String uniqueIdxName = String.format("[cacheGroup=%s, cacheGroupId=%s, cache=%s, cacheId=%s, idx=%s]", group.name(), group.groupId(), ctx.name(), ctx.cacheId(), idx.getName());
    idleChecker.apply(group.groupId());
    processedIndexes.incrementAndGet();
    printProgressOfIndexValidationIfNeeded();
    return Collections.singletonMap(uniqueIdxName, idxValidationRes);
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) Index(org.h2.index.Index) H2CacheRow(org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow) Cursor(org.h2.index.Cursor) DbException(org.h2.message.DbException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) Session(org.h2.engine.Session) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Aggregations

KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)171 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)99 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)92 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)73 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)58 ArrayList (java.util.ArrayList)50 Map (java.util.Map)39 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)37 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)34 ClusterNode (org.apache.ignite.cluster.ClusterNode)33 HashMap (java.util.HashMap)32 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)31 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)29 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)27 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)27 IgniteException (org.apache.ignite.IgniteException)25 LinkedHashMap (java.util.LinkedHashMap)24 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)24 GridDhtInvalidPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException)21 GridCacheOperation (org.apache.ignite.internal.processors.cache.GridCacheOperation)20