Search in sources :

Example 11 with CacheDataRow

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

the class IndexingDefragmentation method defragmentTable.

/**
 * Defragment one given table.
 */
private boolean defragmentTable(CacheGroupContext newCtx, IntMap<LinkMap> mappingByPartition, CheckpointTimeoutLock cpLock, Runnable cancellationChecker, int pageSize, PageMemoryEx oldCachePageMem, PageMemory newCachePageMemory, long cpLockThreshold, AtomicLong lastCpLockTs, TableIndexes indexes) throws IgniteCheckedException {
    cpLock.checkpointReadLock();
    try {
        TreeIterator treeIterator = new TreeIterator(pageSize);
        GridCacheContext<?, ?> cctx = indexes.cctx;
        cancellationChecker.run();
        for (InlineIndex oldIdx : indexes.idxs) {
            InlineIndexRowHandler oldRowHnd = oldIdx.segment(0).rowHandler();
            SortedIndexDefinition idxDef = (SortedIndexDefinition) indexing.indexDefinition(oldIdx.id());
            InlineIndexImpl newIdx = new DefragIndexFactory(newCtx.offheap(), newCachePageMemory, oldIdx).createIndex(cctx, idxDef).unwrap(InlineIndexImpl.class);
            int segments = oldIdx.segmentsCount();
            for (int i = 0; i < segments; ++i) {
                treeIterator.iterate(oldIdx.segment(i), oldCachePageMem, (theTree, io, pageAddr, idx) -> {
                    cancellationChecker.run();
                    if (System.currentTimeMillis() - lastCpLockTs.get() >= cpLockThreshold) {
                        cpLock.checkpointReadUnlock();
                        cpLock.checkpointReadLock();
                        lastCpLockTs.set(System.currentTimeMillis());
                    }
                    assert 1 == io.getVersion() : "IO version " + io.getVersion() + " is not supported by current defragmentation algorithm." + " Please implement copying of tree in a new format.";
                    BPlusIO<IndexRow> h2IO = DefragIndexFactory.wrap(io, oldRowHnd);
                    IndexRow row = theTree.getRow(h2IO, pageAddr, idx);
                    if (row instanceof DefragIndexRowImpl) {
                        DefragIndexRowImpl r = (DefragIndexRowImpl) row;
                        CacheDataRow cacheDataRow = r.cacheDataRow();
                        int partition = cacheDataRow.partition();
                        long link = r.link();
                        LinkMap map = mappingByPartition.get(partition);
                        long newLink = map.get(link);
                        // Use old row handler, as MetaInfo is copied from old tree.
                        DefragIndexRowImpl newRow = DefragIndexRowImpl.create(oldRowHnd, newLink, r, ((MvccIO) io).storeMvccInfo());
                        newIdx.putIndexRow(newRow);
                    }
                    return true;
                });
            }
        }
        return true;
    } catch (Throwable t) {
        newCtx.cacheObjectContext().kernalContext().failure().process(new FailureContext(CRITICAL_ERROR, t));
        throw t;
    } finally {
        cpLock.checkpointReadUnlock();
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) LinkMap(org.apache.ignite.internal.processors.cache.persistence.defragmentation.LinkMap) SortedIndexDefinition(org.apache.ignite.internal.cache.query.index.sorted.SortedIndexDefinition) IndexRow(org.apache.ignite.internal.cache.query.index.sorted.IndexRow) InlineIndex(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex) DefragIndexRowImpl(org.apache.ignite.internal.cache.query.index.sorted.defragmentation.DefragIndexFactory.DefragIndexRowImpl) InlineIndexRowHandler(org.apache.ignite.internal.cache.query.index.sorted.InlineIndexRowHandler) InlineIndexImpl(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexImpl) FailureContext(org.apache.ignite.failure.FailureContext) TreeIterator(org.apache.ignite.internal.processors.cache.persistence.defragmentation.TreeIterator)

Example 12 with CacheDataRow

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

the class IgniteCacheGroupsTest method testRebalance2.

/**
 * @throws Exception If failed.
 */
@Test
public void testRebalance2() throws Exception {
    Ignite srv0 = startGrid(0);
    IgniteCache<Object, Object> srv0Cache1 = srv0.createCache(cacheConfiguration(GROUP1, "c1", PARTITIONED, ATOMIC, 0, false));
    IgniteCache<Object, Object> srv0Cache2 = srv0.createCache(cacheConfiguration(GROUP1, "c2", PARTITIONED, ATOMIC, 0, false));
    Affinity aff = srv0.affinity("c1");
    final int ITEMS = 2_000;
    Map<Integer, Integer> c1Data = new HashMap<>();
    Map<Integer, Integer> c2Data = new HashMap<>();
    for (int i = 0; i < ITEMS; i++) {
        srv0Cache1.put(i, i);
        c1Data.put(i, i);
        if (i % 2 == 0) {
            srv0Cache2.put(i, i);
            c2Data.put(i, i);
        }
    }
    assertEquals(ITEMS, srv0Cache1.size());
    assertEquals(ITEMS / 2, srv0Cache2.size());
    Ignite srv1 = startGrid(1);
    awaitPartitionMapExchange();
    assertEquals(ITEMS, srv0Cache1.size());
    assertEquals(ITEMS / 2, srv0Cache2.size());
    checkCacheData(c1Data, "c1");
    checkCacheData(c2Data, "c2");
    Set<Integer> srv1Parts = new HashSet<>();
    for (Integer p : aff.primaryPartitions(srv1.cluster().localNode())) srv1Parts.add(p);
    CacheGroupContext grpSrv0 = cacheGroup(srv0, GROUP1);
    CacheGroupContext grpSrv1 = cacheGroup(srv1, GROUP1);
    for (int p = 0; p < aff.partitions(); p++) {
        if (srv1Parts.contains(p)) {
            GridIterator<CacheDataRow> it = grpSrv0.offheap().partitionIterator(p);
            assertFalse(it.hasNext());
            it = grpSrv1.offheap().partitionIterator(p);
            assertTrue(it.hasNext());
        } else {
            GridIterator<CacheDataRow> it = grpSrv0.offheap().partitionIterator(p);
            assertTrue(it.hasNext());
            it = grpSrv1.offheap().partitionIterator(p);
            assertFalse(it.hasNext());
        }
    }
    c1Data = new HashMap<>();
    c2Data = new HashMap<>();
    for (int i = 0; i < ITEMS; i++) {
        srv0Cache1.put(i, i + 1);
        c1Data.put(i, i + 1);
        if (i % 2 == 0) {
            srv0Cache2.put(i, i + 1);
            c2Data.put(i, i + 1);
        }
    }
    checkCacheData(c1Data, "c1");
    checkCacheData(c2Data, "c2");
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 13 with CacheDataRow

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

the class IgniteCacheGroupsTest method testCacheIdSort.

/**
 * @throws Exception If failed.
 */
@Test
public void testCacheIdSort() throws Exception {
    Ignite node = startGrid(0);
    final List<IgniteCache> caches = new ArrayList<>(3);
    caches.add(node.createCache(cacheConfiguration(GROUP1, "c1", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
    caches.add(node.createCache(cacheConfiguration(GROUP1, "c2", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
    caches.add(node.createCache(cacheConfiguration(GROUP1, "c3", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
    Affinity aff = node.affinity("c1");
    final List<Integer> keys = new ArrayList<>();
    for (int i = 0; i < 1_000_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() >= 10_000)
                break;
        }
    }
    assertEquals(10_000, keys.size());
    final long stopTime = System.currentTimeMillis() + 10_000;
    GridTestUtils.runMultiThreaded(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            while (System.currentTimeMillis() < stopTime) {
                for (int i = 0; i < 100; i++) {
                    IgniteCache cache = caches.get(rnd.nextInt(3));
                    Integer key = keys.get(rnd.nextInt(10_000));
                    if (rnd.nextFloat() > 0.8f)
                        cache.remove(key);
                    else
                        cache.put(key, key);
                }
            }
            return null;
        }
    }, 5, "update-thread");
    CacheGroupContext grp = cacheGroup(node, GROUP1);
    Integer cacheId = null;
    GridIterator<CacheDataRow> it = grp.offheap().partitionIterator(0);
    int c = 0;
    while (it.hasNext()) {
        CacheDataRow row = it.next();
        if (cacheId == null || cacheId != row.cacheId()) {
            cacheId = row.cacheId();
            c++;
        }
    }
    assertEquals(3, c);
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheExistsException(org.apache.ignite.cache.CacheExistsException) CacheException(javax.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Affinity(org.apache.ignite.cache.affinity.Affinity) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 14 with CacheDataRow

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

the class CachePartitionDefragmentationManager method copyPartitionData.

/**
 * Defragmentate partition.
 *
 * @param partCtx
 * @param treeIter
 * @throws IgniteCheckedException If failed.
 */
private void copyPartitionData(PartitionContext partCtx, TreeIterator treeIter) throws IgniteCheckedException {
    CacheDataTree tree = partCtx.oldCacheDataStore.tree();
    CacheDataTree newTree = partCtx.newCacheDataStore.tree();
    newTree.enableSequentialWriteMode();
    PendingEntriesTree newPendingTree = partCtx.newCacheDataStore.pendingTree();
    AbstractFreeList<CacheDataRow> freeList = partCtx.newCacheDataStore.getCacheStoreFreeList();
    long cpLockThreshold = 150L;
    defragmentationCheckpoint.checkpointTimeoutLock().checkpointReadLock();
    try {
        AtomicLong lastCpLockTs = new AtomicLong(System.currentTimeMillis());
        AtomicInteger entriesProcessed = new AtomicInteger();
        treeIter.iterate(tree, partCtx.cachePageMemory, (tree0, io, pageAddr, idx) -> {
            checkCancellation();
            if (System.currentTimeMillis() - lastCpLockTs.get() >= cpLockThreshold) {
                defragmentationCheckpoint.checkpointTimeoutLock().checkpointReadUnlock();
                defragmentationCheckpoint.checkpointTimeoutLock().checkpointReadLock();
                lastCpLockTs.set(System.currentTimeMillis());
            }
            AbstractDataLeafIO leafIo = (AbstractDataLeafIO) io;
            CacheDataRow row = tree.getRow(io, pageAddr, idx);
            int cacheId = row.cacheId();
            // Reuse row that we just read.
            row.link(0);
            // "insertDataRow" will corrupt page memory if we don't do this.
            if (row instanceof DataRow && !partCtx.oldGrpCtx.storeCacheIdInDataPage())
                ((DataRow) row).cacheId(CU.UNDEFINED_CACHE_ID);
            freeList.insertDataRow(row, IoStatisticsHolderNoOp.INSTANCE);
            // Put it back.
            if (row instanceof DataRow)
                ((DataRow) row).cacheId(cacheId);
            newTree.putx(row);
            long newLink = row.link();
            partCtx.linkMap.put(leafIo.getLink(pageAddr, idx), newLink);
            if (row.expireTime() != 0)
                newPendingTree.putx(new PendingRow(cacheId, row.expireTime(), newLink));
            entriesProcessed.incrementAndGet();
            return true;
        });
        checkCancellation();
        defragmentationCheckpoint.checkpointTimeoutLock().checkpointReadUnlock();
        defragmentationCheckpoint.checkpointTimeoutLock().checkpointReadLock();
        freeList.saveMetadata(IoStatisticsHolderNoOp.INSTANCE);
        copyCacheMetadata(partCtx);
    } finally {
        defragmentationCheckpoint.checkpointTimeoutLock().checkpointReadUnlock();
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractDataLeafIO(org.apache.ignite.internal.processors.cache.tree.AbstractDataLeafIO) CacheDataTree(org.apache.ignite.internal.processors.cache.tree.CacheDataTree) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PendingEntriesTree(org.apache.ignite.internal.processors.cache.tree.PendingEntriesTree) DataRow(org.apache.ignite.internal.processors.cache.tree.DataRow) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) SimpleDataRow(org.apache.ignite.internal.processors.cache.persistence.freelist.SimpleDataRow) PendingRow(org.apache.ignite.internal.processors.cache.tree.PendingRow)

Example 15 with CacheDataRow

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

the class GridCacheMapEntry method mvccPeek.

/**
 * {@inheritDoc}
 */
@Nullable
@Override
public CacheObject mvccPeek(boolean onheapOnly) throws GridCacheEntryRemovedException, IgniteCheckedException {
    if (onheapOnly)
        return null;
    lockEntry();
    try {
        checkObsolete();
        CacheDataRow row = cctx.offheap().mvccRead(cctx, key, MVCC_MAX_SNAPSHOT);
        return row != null ? row.value() : null;
    } finally {
        unlockEntry();
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) Nullable(org.jetbrains.annotations.Nullable)

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