Search in sources :

Example 6 with GridCacheOffheapManager

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

the class VisorFindAndDeleteGarbageInPersistenceClosure method cleanup.

/**
 * By calling this method we would delete found garbage in partitions and would try to
 * cleanup indexes.
 *
 * @param grpIdToPartIdToGarbageCount GrpId -> PartId -> Garbage count.
 */
private void cleanup(Map<Integer, Map<Integer, Long>> grpIdToPartIdToGarbageCount) throws IgniteCheckedException {
    for (Map.Entry<Integer, Map<Integer, Long>> e : grpIdToPartIdToGarbageCount.entrySet()) {
        int grpId = e.getKey();
        CacheGroupContext groupContext = ignite.context().cache().cacheGroup(grpId);
        assert groupContext != null;
        for (Integer cacheId : e.getValue().keySet()) {
            groupContext.shared().database().checkpointReadLock();
            try {
                groupContext.offheap().stopCache(cacheId, true);
            } finally {
                groupContext.shared().database().checkpointReadUnlock();
            }
            ((GridCacheOffheapManager) groupContext.offheap()).findAndCleanupLostIndexesForStoppedCache(cacheId);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheOffheapManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with GridCacheOffheapManager

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

the class CacheGroupContext method start.

/**
 * @throws IgniteCheckedException If failed.
 */
public void start() throws IgniteCheckedException {
    GridAffinityAssignmentCache affCache = ctx.affinity().groupAffinity(grpId);
    aff = affCache == null ? GridAffinityAssignmentCache.create(ctx.kernalContext(), ccfg.getAffinity(), ccfg) : affCache;
    if (ccfg.getCacheMode() != LOCAL) {
        top = ctx.kernalContext().resource().resolve(new GridDhtPartitionTopologyImpl(ctx, this));
        metrics.onTopologyInitialized();
    }
    try {
        offheapMgr = ctx.kernalContext().resource().resolve(persistenceEnabled ? new GridCacheOffheapManager() : new IgniteCacheOffheapManagerImpl());
    } catch (Exception e) {
        throw new IgniteCheckedException("Failed to initialize offheap manager", e);
    }
    offheapMgr.start(ctx, this);
    if (!isRecoveryMode()) {
        initializeIO();
        ctx.affinity().onCacheGroupCreated(this);
        ctx.evict().onCacheGroupStarted(this);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) GridCacheOffheapManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager) GridDhtPartitionTopologyImpl(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopologyImpl) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 8 with GridCacheOffheapManager

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

the class CachePartitionDefragmentationManager method executeDefragmentation.

/**
 */
public void executeDefragmentation() throws IgniteCheckedException {
    Map<Integer, List<CacheDataStore>> oldStores = new HashMap<>();
    for (CacheGroupContext oldGrpCtx : cacheGrpCtxsForDefragmentation) {
        int grpId = oldGrpCtx.groupId();
        final IgniteCacheOffheapManager offheap = oldGrpCtx.offheap();
        List<CacheDataStore> oldCacheDataStores = stream(offheap.cacheDataStores().spliterator(), false).filter(store -> {
            try {
                return filePageStoreMgr.exists(grpId, store.partId());
            } catch (IgniteCheckedException e) {
                throw new IgniteException(e);
            }
        }).collect(Collectors.toList());
        oldStores.put(grpId, oldCacheDataStores);
    }
    int partitionCount = oldStores.values().stream().mapToInt(List::size).sum();
    status.onStart(cacheGrpCtxsForDefragmentation, partitionCount);
    try {
        // Now the actual process starts.
        IgniteInternalFuture<?> idxDfrgFut = null;
        DataPageEvictionMode prevPageEvictionMode = null;
        for (CacheGroupContext oldGrpCtx : cacheGrpCtxsForDefragmentation) {
            int grpId = oldGrpCtx.groupId();
            File workDir = filePageStoreMgr.cacheWorkDir(oldGrpCtx.sharedGroup(), oldGrpCtx.cacheOrGroupName());
            List<CacheDataStore> oldCacheDataStores = oldStores.get(grpId);
            if (skipAlreadyDefragmentedCacheGroup(workDir, grpId, log)) {
                status.onCacheGroupSkipped(oldGrpCtx, oldCacheDataStores.size());
                continue;
            }
            try {
                GridCacheOffheapManager offheap = (GridCacheOffheapManager) oldGrpCtx.offheap();
                status.onCacheGroupStart(oldGrpCtx, oldCacheDataStores.size());
                if (workDir == null || oldCacheDataStores.isEmpty()) {
                    status.onCacheGroupFinish(oldGrpCtx);
                    continue;
                }
                // We can't start defragmentation of new group on the region that has wrong eviction mode.
                // So waiting of the previous cache group defragmentation is inevitable.
                DataPageEvictionMode curPageEvictionMode = oldGrpCtx.dataRegion().config().getPageEvictionMode();
                if (prevPageEvictionMode == null || prevPageEvictionMode != curPageEvictionMode) {
                    prevPageEvictionMode = curPageEvictionMode;
                    partDataRegion.config().setPageEvictionMode(curPageEvictionMode);
                    if (idxDfrgFut != null)
                        idxDfrgFut.get();
                }
                IntMap<CacheDataStore> cacheDataStores = new IntHashMap<>();
                for (CacheDataStore store : offheap.cacheDataStores()) {
                    // This would mean that these partitions are empty.
                    assert store.tree() == null || store.tree().groupId() == grpId;
                    if (store.tree() != null)
                        cacheDataStores.put(store.partId(), store);
                }
                dbMgr.checkpointedDataRegions().remove(oldGrpCtx.dataRegion());
                // Another cheat. Ttl cleanup manager knows too much shit.
                oldGrpCtx.caches().stream().filter(cacheCtx -> cacheCtx.groupId() == grpId).forEach(cacheCtx -> cacheCtx.ttl().unregister());
                // Technically wal is already disabled, but "PageHandler.isWalDeltaRecordNeeded" doesn't care
                // and WAL records will be allocated anyway just to be ignored later if we don't disable WAL for
                // cache group explicitly.
                oldGrpCtx.localWalEnabled(false, false);
                boolean encrypted = oldGrpCtx.config().isEncryptionEnabled();
                FileVersionCheckingFactory pageStoreFactory = filePageStoreMgr.getPageStoreFactory(grpId, encrypted);
                AtomicLong idxAllocationTracker = new GridAtomicLong();
                createIndexPageStore(grpId, workDir, pageStoreFactory, partDataRegion, idxAllocationTracker::addAndGet);
                checkCancellation();
                GridCompoundFuture<Object, Object> cmpFut = new GridCompoundFuture<>();
                PageMemoryEx oldPageMem = (PageMemoryEx) oldGrpCtx.dataRegion().pageMemory();
                CacheGroupContext newGrpCtx = new CacheGroupContext(sharedCtx, grpId, oldGrpCtx.receivedFrom(), CacheType.USER, oldGrpCtx.config(), oldGrpCtx.affinityNode(), partDataRegion, oldGrpCtx.cacheObjectContext(), null, null, oldGrpCtx.localStartVersion(), true, false, true);
                defragmentationCheckpoint.checkpointTimeoutLock().checkpointReadLock();
                try {
                    // This will initialize partition meta in index partition - meta tree and reuse list.
                    newGrpCtx.start();
                } finally {
                    defragmentationCheckpoint.checkpointTimeoutLock().checkpointReadUnlock();
                }
                IgniteUtils.doInParallel(defragmentationThreadPool, oldCacheDataStores, oldCacheDataStore -> defragmentOnePartition(oldGrpCtx, grpId, workDir, offheap, pageStoreFactory, cmpFut, oldPageMem, newGrpCtx, oldCacheDataStore));
                // A bit too general for now, but I like it more then saving only the last checkpoint future.
                cmpFut.markInitialized().get();
                idxDfrgFut = new GridFinishedFuture<>();
                if (filePageStoreMgr.hasIndexStore(grpId)) {
                    defragmentIndexPartition(oldGrpCtx, newGrpCtx);
                    idxDfrgFut = defragmentationCheckpoint.forceCheckpoint("index defragmented", null).futureFor(FINISHED);
                }
                PageStore oldIdxPageStore = filePageStoreMgr.getStore(grpId, INDEX_PARTITION);
                idxDfrgFut = idxDfrgFut.chain(fut -> {
                    if (log.isDebugEnabled()) {
                        log.debug(S.toString("Index partition defragmented", "grpId", grpId, false, "oldPages", oldIdxPageStore.pages(), false, "newPages", idxAllocationTracker.get() + 1, false, "pageSize", pageSize, false, "partFile", defragmentedIndexFile(workDir).getName(), false, "workDir", workDir, false));
                    }
                    oldPageMem.invalidate(grpId, INDEX_PARTITION);
                    PageMemoryEx partPageMem = (PageMemoryEx) partDataRegion.pageMemory();
                    partPageMem.invalidate(grpId, INDEX_PARTITION);
                    DefragmentationPageReadWriteManager pageMgr = (DefragmentationPageReadWriteManager) partPageMem.pageManager();
                    pageMgr.pageStoreMap().removePageStore(grpId, INDEX_PARTITION);
                    PageMemoryEx mappingPageMem = (PageMemoryEx) mappingDataRegion.pageMemory();
                    pageMgr = (DefragmentationPageReadWriteManager) mappingPageMem.pageManager();
                    pageMgr.pageStoreMap().clear(grpId);
                    renameTempIndexFile(workDir);
                    writeDefragmentationCompletionMarker(filePageStoreMgr.getPageStoreFileIoFactory(), workDir, log);
                    batchRenameDefragmentedCacheGroupPartitions(workDir, log);
                    return null;
                });
                status.onIndexDefragmented(oldGrpCtx, oldIdxPageStore.size(), // + file header.
                pageSize + idxAllocationTracker.get() * pageSize);
            } catch (DefragmentationCancelledException e) {
                DefragmentationFileUtils.deleteLeftovers(workDir);
                throw e;
            }
            status.onCacheGroupFinish(oldGrpCtx);
        }
        if (idxDfrgFut != null)
            idxDfrgFut.get();
        mntcReg.unregisterMaintenanceTask(DEFRAGMENTATION_MNTC_TASK_NAME);
        status.onFinish();
        completionFut.onDone();
    } catch (DefragmentationCancelledException e) {
        mntcReg.unregisterMaintenanceTask(DEFRAGMENTATION_MNTC_TASK_NAME);
        log.info("Defragmentation process has been cancelled.");
        status.onFinish();
        completionFut.onDone();
    } catch (Throwable t) {
        log.error("Defragmentation process failed.", t);
        status.onFinish();
        completionFut.onDone(t);
        throw t;
    } finally {
        defragmentationCheckpoint.stop(true);
    }
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Arrays(java.util.Arrays) PageStore(org.apache.ignite.internal.pagemem.store.PageStore) CacheType(org.apache.ignite.internal.processors.cache.CacheType) DataRegion(org.apache.ignite.internal.processors.cache.persistence.DataRegion) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) FLAG_DATA(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_DATA) IntMap(org.apache.ignite.internal.util.collection.IntMap) DEFRAGMENTATION_MAPPING_REGION_NAME(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager.DEFRAGMENTATION_MAPPING_REGION_NAME) DEFRAGMENTATION_PART_REGION_NAME(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager.DEFRAGMENTATION_PART_REGION_NAME) DefragmentationFileUtils.defragmentedPartMappingFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedPartMappingFile) CacheDataStore(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager.CacheDataStore) CheckpointTimeoutLock(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointTimeoutLock) DefragmentationFileUtils.defragmentedPartFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedPartFile) LightweightCheckpointManager(org.apache.ignite.internal.processors.cache.persistence.checkpoint.LightweightCheckpointManager) PagePartitionMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PagePartitionMetaIO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MaintenanceRegistry(org.apache.ignite.maintenance.MaintenanceRegistry) Map(java.util.Map) Path(java.nio.file.Path) AbstractFreeList(org.apache.ignite.internal.processors.cache.persistence.freelist.AbstractFreeList) IndexProcessor(org.apache.ignite.internal.cache.query.index.IndexProcessor) DataRow(org.apache.ignite.internal.processors.cache.tree.DataRow) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) IgniteOutClosure(org.apache.ignite.lang.IgniteOutClosure) DefragmentationFileUtils.skipAlreadyDefragmentedPartition(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.skipAlreadyDefragmentedPartition) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) Set(java.util.Set) PendingRow(org.apache.ignite.internal.processors.cache.tree.PendingRow) FilePageStoreManager(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCacheOffheapManager(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager) INDEX_PARTITION(org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION) IntRWHashMap(org.apache.ignite.internal.util.collection.IntRWHashMap) List(java.util.List) GridCacheDataStore(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager.GridCacheDataStore) StreamSupport.stream(java.util.stream.StreamSupport.stream) PageIdUtils(org.apache.ignite.internal.pagemem.PageIdUtils) CU(org.apache.ignite.internal.util.typedef.internal.CU) PagePartitionMetaIOV3(org.apache.ignite.internal.processors.cache.persistence.tree.io.PagePartitionMetaIOV3) IntHashMap(org.apache.ignite.internal.util.collection.IntHashMap) FLAG_IDX(org.apache.ignite.internal.pagemem.PageIdAllocator.FLAG_IDX) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong) SimpleDataRow(org.apache.ignite.internal.processors.cache.persistence.freelist.SimpleDataRow) DefragmentationFileUtils.defragmentedIndexTmpFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedIndexTmpFile) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) IgniteLogger(org.apache.ignite.IgniteLogger) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) DefragmentationFileUtils.renameTempIndexFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.renameTempIndexFile) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) GridCacheOffheapManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager) CheckpointManager(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointManager) DefragmentationFileUtils.writeDefragmentationCompletionMarker(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.writeDefragmentationCompletionMarker) DefragmentationFileUtils.skipAlreadyDefragmentedCacheGroup(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.skipAlreadyDefragmentedCacheGroup) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) S(org.apache.ignite.internal.util.typedef.internal.S) DefragmentationFileUtils.renameTempPartitionFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.renameTempPartitionFile) PageIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO) IgniteThreadPoolExecutor(org.apache.ignite.thread.IgniteThreadPoolExecutor) Comparator.comparing(java.util.Comparator.comparing) CacheDataTree(org.apache.ignite.internal.processors.cache.tree.CacheDataTree) AbstractDataLeafIO(org.apache.ignite.internal.processors.cache.tree.AbstractDataLeafIO) FINISHED(org.apache.ignite.internal.processors.cache.persistence.CheckpointState.FINISHED) PendingEntriesTree(org.apache.ignite.internal.processors.cache.tree.PendingEntriesTree) DefragmentationFileUtils.defragmentedPartTmpFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedPartTmpFile) DefragmentationFileUtils.defragmentedIndexFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedIndexFile) IoStatisticsHolderNoOp(org.apache.ignite.internal.metric.IoStatisticsHolderNoOp) DataPageEvictionMode(org.apache.ignite.configuration.DataPageEvictionMode) DefragmentationFileUtils.batchRenameDefragmentedCacheGroupPartitions(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.batchRenameDefragmentedCacheGroupPartitions) FileVersionCheckingFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileVersionCheckingFactory) File(java.io.File) LongConsumer(java.util.function.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) TreeMap(java.util.TreeMap) PageMemoryEx(org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryEx) GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) IntRWHashMap(org.apache.ignite.internal.util.collection.IntRWHashMap) IntHashMap(org.apache.ignite.internal.util.collection.IntHashMap) HashMap(java.util.HashMap) GridCacheOffheapManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager) PageStore(org.apache.ignite.internal.pagemem.store.PageStore) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) FileVersionCheckingFactory(org.apache.ignite.internal.processors.cache.persistence.file.FileVersionCheckingFactory) IgniteException(org.apache.ignite.IgniteException) AbstractFreeList(org.apache.ignite.internal.processors.cache.persistence.freelist.AbstractFreeList) List(java.util.List) DataPageEvictionMode(org.apache.ignite.configuration.DataPageEvictionMode) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntHashMap(org.apache.ignite.internal.util.collection.IntHashMap) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) CacheDataStore(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager.CacheDataStore) GridCacheDataStore(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager.GridCacheDataStore) IgniteCacheOffheapManager(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager) PageMemoryEx(org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryEx) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) DefragmentationFileUtils.defragmentedPartMappingFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedPartMappingFile) DefragmentationFileUtils.defragmentedPartFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedPartFile) DefragmentationFileUtils.defragmentedIndexTmpFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedIndexTmpFile) DefragmentationFileUtils.renameTempIndexFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.renameTempIndexFile) DefragmentationFileUtils.renameTempPartitionFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.renameTempPartitionFile) DefragmentationFileUtils.defragmentedPartTmpFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedPartTmpFile) DefragmentationFileUtils.defragmentedIndexFile(org.apache.ignite.internal.processors.cache.persistence.defragmentation.DefragmentationFileUtils.defragmentedIndexFile) File(java.io.File)

Example 9 with GridCacheOffheapManager

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

the class FreeListCachingTest method testFreeListCaching.

/**
 */
@Test
public void testFreeListCaching() throws Exception {
    IgniteEx ignite = startGrid(0);
    ignite.cluster().active(true);
    int partCnt = 10;
    GridCacheProcessor cacheProc = ignite.context().cache();
    GridCacheDatabaseSharedManager dbMgr = (GridCacheDatabaseSharedManager) cacheProc.context().database();
    dbMgr.enableCheckpoints(false).get();
    IgniteCache<Object, Object> cache = ignite.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME).setAffinity(new RendezvousAffinityFunction().setPartitions(partCnt)).setAtomicityMode(CacheAtomicityMode.ATOMIC));
    GridCacheOffheapManager offheap = (GridCacheOffheapManager) cacheProc.cache(DEFAULT_CACHE_NAME).context().group().offheap();
    for (int i = 0; i < 5_000; i++) {
        for (int p = 0; p < partCnt; p++) {
            Integer key = i * partCnt + p;
            cache.put(key, new byte[i + 1]);
            cache.remove(key);
        }
    }
    offheap.cacheDataStores().forEach(cacheData -> {
        PagesList list = (PagesList) cacheData.rowStore().freeList();
        AtomicLongArray bucketsSize = list.bucketsSize;
        // All buckets except reuse bucket must be empty after puts and removes of the same key.
        for (int i = 0; i < bucketsSize.length(); i++) {
            if (list.isReuseBucket(i))
                assertTrue(bucketsSize.get(i) > 0);
            else
                assertEquals(0, bucketsSize.get(i));
        }
    });
    for (int i = 0; i < 100; i++) {
        for (int p = 0; p < partCnt; p++) cache.put(i * partCnt + p, new byte[(i + p) * 10]);
    }
    for (int i = 0; i < 50; i += 2) {
        for (int p = 0; p < partCnt; p++) cache.remove(i * partCnt + p);
    }
    Map<Integer, List<Long>> partsBucketsSize = new HashMap<>();
    offheap.cacheDataStores().forEach(cacheData -> {
        PagesList list = (PagesList) cacheData.rowStore().freeList();
        AtomicLongArray bucketsSize = list.bucketsSize;
        List<Long> bucketsSizeList = new ArrayList<>(bucketsSize.length());
        partsBucketsSize.put(cacheData.partId(), bucketsSizeList);
        long notReuseSize = 0;
        for (int i = 0; i < bucketsSize.length(); i++) {
            bucketsSizeList.add(bucketsSize.get(i));
            PagesList.Stripe[] bucket = list.getBucket(i);
            // by indexes bypassing caching.
            if (!list.isReuseBucket(i)) {
                notReuseSize += bucketsSize.get(i);
                assertNull("Expected null bucket [partId=" + cacheData.partId() + ", i=" + i + ", bucket=" + bucket + ']', bucket);
                PagesList.PagesCache pagesCache = list.getBucketCache(i, false);
                assertEquals("Wrong pages cache size [partId=" + cacheData.partId() + ", i=" + i + ']', bucketsSize.get(i), pagesCache == null ? 0 : pagesCache.size());
            }
        }
        assertTrue(notReuseSize > 0);
    });
    dbMgr.enableCheckpoints(true).get();
    forceCheckpoint(ignite);
    offheap.cacheDataStores().forEach(cacheData -> {
        PagesList list = (PagesList) cacheData.rowStore().freeList();
        AtomicLongArray bucketsSize = list.bucketsSize;
        for (int i = 0; i < bucketsSize.length(); i++) {
            long bucketSize = bucketsSize.get(i);
            PagesList.Stripe[] bucket = list.getBucket(i);
            // After checkpoint all buckets must flush onheap cache to page memory.
            if (bucketSize > 0) {
                assertNotNull("Expected not null bucket [partId=" + cacheData.partId() + ", i=" + i + ']', bucket);
            }
            PagesList.PagesCache pagesCache = list.getBucketCache(i, false);
            assertEquals("Wrong pages cache size [partId=" + cacheData.partId() + ", i=" + i + ']', 0, pagesCache == null ? 0 : pagesCache.size());
            assertEquals("Bucket size changed after checkpoint [partId=" + cacheData.partId() + ", i=" + i + ']', (long) partsBucketsSize.get(cacheData.partId()).get(i), bucketSize);
        }
    });
    dbMgr.enableCheckpoints(false).get();
    for (int i = 0; i < 50; i++) {
        for (int p = 0; p < partCnt; p++) cache.put(i * partCnt + p, new byte[(i + p) * 10]);
    }
    offheap.cacheDataStores().forEach(cacheData -> {
        PagesList list = (PagesList) cacheData.rowStore().freeList();
        int totalCacheSize = 0;
        for (int i = 0; i < list.bucketsSize.length(); i++) {
            PagesList.PagesCache pagesCache = list.getBucketCache(i, false);
            totalCacheSize += pagesCache == null ? 0 : pagesCache.size();
        }
        assertTrue("Some buckets should be cached [partId=" + cacheData.partId() + ']', totalCacheSize > 0);
    });
}
Also used : GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) HashMap(java.util.HashMap) GridCacheOffheapManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager) ArrayList(java.util.ArrayList) GridCacheProcessor(org.apache.ignite.internal.processors.cache.GridCacheProcessor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) ArrayList(java.util.ArrayList) List(java.util.List) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 10 with GridCacheOffheapManager

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

the class FreeListCachingTest method testPageListCacheLimit.

/**
 * @throws Exception If test failed.
 */
@Test
public void testPageListCacheLimit() throws Exception {
    IgniteEx ignite = startGrid(0);
    ignite.cluster().active(true);
    ignite.getOrCreateCache("cache1");
    ignite.getOrCreateCache("cache2");
    GridCacheContext<?, ?> cctx1 = ignite.context().cache().cache("cache1").context();
    GridCacheContext<?, ?> cctx2 = ignite.context().cache().cache("cache2").context();
    GridCacheOffheapManager offheap1 = (GridCacheOffheapManager) cctx1.offheap();
    GridCacheOffheapManager offheap2 = (GridCacheOffheapManager) cctx2.offheap();
    GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager) ignite.context().cache().context().database();
    assertEquals(db.pageListCacheLimitHolder(cctx1.dataRegion()), db.pageListCacheLimitHolder(cctx2.dataRegion()));
    long limit = db.pageListCacheLimitHolder(cctx1.dataRegion()).get();
    try (IgniteDataStreamer<Object, Object> streamer1 = ignite.dataStreamer("cache1");
        IgniteDataStreamer<Object, Object> streamer2 = ignite.dataStreamer("cache2")) {
        // Fill caches to trigger "too many dirty pages" checkpoint.
        for (int i = 0; i < 50_000; i++) {
            streamer1.addData(i, new byte[i % 2048]);
            streamer2.addData(i, new byte[i % 2048]);
            // Calculates page list caches count and validate this value periodically.
            if (i % 5_000 == 0) {
                streamer1.flush();
                streamer2.flush();
                AtomicInteger pageCachesCnt = new AtomicInteger();
                for (GridCacheOffheapManager offheap : F.asList(offheap1, offheap2)) {
                    offheap.cacheDataStores().forEach(cacheData -> {
                        if (cacheData.rowStore() == null)
                            return;
                        PagesList list = (PagesList) cacheData.rowStore().freeList();
                        for (int b = 0; b < list.bucketsSize.length(); b++) {
                            PagesList.PagesCache pagesCache = list.getBucketCache(b, false);
                            if (pagesCache != null && pagesCache.size() > 0)
                                pageCachesCnt.incrementAndGet();
                        }
                    });
                }
                // There can be a race and actual page list caches count can exceed the limit in very rare cases.
                assertTrue("Page list caches count is more than expected [count: " + pageCachesCnt.get() + ", limit=" + limit + ']', pageCachesCnt.get() <= limit + ignite.configuration().getDataStreamerThreadPoolSize() - 1);
            }
        }
    }
}
Also used : GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheOffheapManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager) IgniteEx(org.apache.ignite.internal.IgniteEx) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

GridCacheOffheapManager (org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager)13 HashMap (java.util.HashMap)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)5 Test (org.junit.Test)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 GridCacheDatabaseSharedManager (org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager)4 List (java.util.List)3 Map (java.util.Map)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 File (java.io.File)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Comparator.comparing (java.util.Comparator.comparing)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2