Search in sources :

Example 31 with CacheGroupContext

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

the class GridEncryptionManager method changeCacheGroupKeyLocal.

/**
 * @param grpIds Cache group IDs.
 * @param keyIds Encryption key IDs.
 * @param keys Encryption keys.
 * @throws IgniteCheckedException If failed.
 */
protected void changeCacheGroupKeyLocal(int[] grpIds, byte[] keyIds, byte[][] keys) throws IgniteCheckedException {
    Map<Integer, Byte> encryptionStatus = U.newHashMap(grpIds.length);
    for (int i = 0; i < grpIds.length; i++) encryptionStatus.put(grpIds[i], keyIds[i]);
    WALPointer ptr = ctx.cache().context().wal().log(new ReencryptionStartRecord(encryptionStatus));
    if (ptr != null)
        ctx.cache().context().wal().flush(ptr, false);
    for (int i = 0; i < grpIds.length; i++) {
        int grpId = grpIds[i];
        int newKeyId = keyIds[i] & 0xff;
        withMasterKeyChangeReadLock(() -> {
            synchronized (metaStorageMux) {
                // Set new key as key for writing. Note that we cannot pass the encrypted key here because the master
                // key may have changed in which case we will not be able to decrypt the cache encryption key.
                GroupKey prevGrpKey = grpKeys.changeActiveKey(grpId, newKeyId);
                writeGroupKeysToMetaStore(grpId, grpKeys.getAll(grpId));
                if (ptr == null)
                    return null;
                grpKeys.reserveWalKey(grpId, prevGrpKey.unsignedId(), ctx.cache().context().wal().currentSegment());
                writeTrackedWalIdxsToMetaStore();
            }
            return null;
        });
        CacheGroupContext grp = ctx.cache().cacheGroup(grpId);
        if (grp != null && grp.affinityNode())
            reencryptGroups.put(grpId, pageScanner.pagesCount(grp));
        if (log.isInfoEnabled())
            log.info("New encryption key for group was added [grpId=" + grpId + ", keyId=" + newKeyId + ']');
    }
    startReencryption(encryptionStatus.keySet());
}
Also used : ReencryptionStartRecord(org.apache.ignite.internal.pagemem.wal.record.ReencryptionStartRecord) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)

Example 32 with CacheGroupContext

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

the class CacheGroupPageScanner method schedule.

/**
 * Schedule scanning partitions.
 *
 * @param grpId Cache group ID.
 */
public IgniteInternalFuture<Void> schedule(int grpId) throws IgniteCheckedException {
    CacheGroupContext grp = ctx.cache().cacheGroup(grpId);
    if (grp == null || !grp.affinityNode()) {
        if (log.isInfoEnabled())
            log.info("Skip reencryption, cache group doesn't exist on the local node [grp=" + grpId + "]");
        return new GridFinishedFuture<>();
    }
    GroupScanTask grpScanTask = new GroupScanTask(grp);
    lock.lock();
    try {
        if (stopped)
            throw new NodeStoppingException("Operation has been cancelled (node is stopping).");
        if (grps.isEmpty())
            ((GridCacheDatabaseSharedManager) ctx.cache().context().database()).addCheckpointListener(this);
        GroupScanTask prevState = grps.get(grpId);
        if (prevState != null && !prevState.isDone()) {
            if (log.isDebugEnabled())
                log.debug("Reencryption already scheduled [grpId=" + grpId + "]");
            return prevState;
        }
        grps.put(grpId, grpScanTask);
    } finally {
        lock.unlock();
    }
    ctx.pools().getReencryptionExecutorService().submit(() -> schedule0(grpScanTask));
    return grpScanTask;
}
Also used : NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 33 with CacheGroupContext

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

the class Checkpointer method destroyEvictedPartitions.

/**
 * Processes all evicted partitions scheduled for destroy.
 *
 * @return The number of destroyed partition files.
 * @throws IgniteCheckedException If failed.
 */
private int destroyEvictedPartitions() throws IgniteCheckedException {
    PartitionDestroyQueue destroyQueue = curCpProgress.getDestroyQueue();
    if (destroyQueue.pendingReqs().isEmpty())
        return 0;
    List<PartitionDestroyRequest> reqs = null;
    for (final PartitionDestroyRequest req : destroyQueue.pendingReqs().values()) {
        if (!req.beginDestroy())
            continue;
        final int grpId = req.groupId();
        final int partId = req.partitionId();
        CacheGroupContext grp = cacheProcessor.cacheGroup(grpId);
        assert grp != null : "Cache group is not initialized [grpId=" + grpId + "]";
        assert grp.offheap() instanceof GridCacheOffheapManager : "Destroying partition files when persistence is off " + grp.offheap();
        final GridCacheOffheapManager offheap = (GridCacheOffheapManager) grp.offheap();
        Runnable destroyPartTask = () -> {
            try {
                offheap.destroyPartitionStore(partId);
                req.onDone(null);
                grp.metrics().decrementInitializedLocalPartitions();
                if (log.isDebugEnabled())
                    log.debug("Partition file has destroyed [grpId=" + grpId + ", partId=" + partId + "]");
            } catch (Exception e) {
                req.onDone(new IgniteCheckedException("Partition file destroy has failed [grpId=" + grpId + ", partId=" + partId + "]", e));
            }
        };
        IgniteThreadPoolExecutor pool = checkpointWritePagesPool;
        if (pool != null) {
            try {
                pool.execute(destroyPartTask);
            } catch (RejectedExecutionException ignore) {
                // Run the task synchronously.
                destroyPartTask.run();
            }
        } else
            destroyPartTask.run();
        if (reqs == null)
            reqs = new ArrayList<>();
        reqs.add(req);
    }
    if (reqs != null)
        for (PartitionDestroyRequest req : reqs) req.waitCompleted();
    destroyQueue.pendingReqs().clear();
    return reqs != null ? reqs.size() : 0;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheOffheapManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheOffheapManager) ArrayList(java.util.ArrayList) IgniteThreadPoolExecutor(org.apache.ignite.thread.IgniteThreadPoolExecutor) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 34 with CacheGroupContext

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

the class CachePartitionDefragmentationManager method defragmentOnePartition.

/**
 * Defragment one given partition.
 */
private boolean defragmentOnePartition(CacheGroupContext oldGrpCtx, int grpId, File workDir, GridCacheOffheapManager offheap, FileVersionCheckingFactory pageStoreFactory, GridCompoundFuture<Object, Object> cmpFut, PageMemoryEx oldPageMem, CacheGroupContext newGrpCtx, CacheDataStore oldCacheDataStore) throws IgniteCheckedException {
    TreeIterator treeIter = new TreeIterator(pageSize);
    checkCancellation();
    int partId = oldCacheDataStore.partId();
    PartitionContext partCtx = new PartitionContext(workDir, grpId, partId, partDataRegion, mappingDataRegion, oldGrpCtx, newGrpCtx, oldCacheDataStore, pageStoreFactory);
    if (skipAlreadyDefragmentedPartition(workDir, grpId, partId, log)) {
        partCtx.createPageStore(() -> defragmentedPartMappingFile(workDir, partId).toPath(), partCtx.mappingPagesAllocated, partCtx.mappingPageMemory);
        linkMapByPart.put(partId, partCtx.createLinkMapTree(false));
        return false;
    }
    partCtx.createPageStore(() -> defragmentedPartMappingFile(workDir, partId).toPath(), partCtx.mappingPagesAllocated, partCtx.mappingPageMemory);
    linkMapByPart.put(partId, partCtx.createLinkMapTree(true));
    checkCancellation();
    partCtx.createPageStore(() -> defragmentedPartTmpFile(workDir, partId).toPath(), partCtx.partPagesAllocated, partCtx.partPageMemory);
    partCtx.createNewCacheDataStore(offheap);
    copyPartitionData(partCtx, treeIter);
    DefragmentationPageReadWriteManager pageMgr = (DefragmentationPageReadWriteManager) partCtx.partPageMemory.pageManager();
    PageStore oldPageStore = filePageStoreMgr.getStore(grpId, partId);
    status.onPartitionDefragmented(oldGrpCtx, oldPageStore.size(), // + file header.
    pageSize + partCtx.partPagesAllocated.get() * pageSize);
    // TODO Move inside of defragmentSinglePartition.
    IgniteInClosure<IgniteInternalFuture<?>> cpLsnr = fut -> {
        if (fut.error() == null) {
            if (log.isDebugEnabled()) {
                log.debug(S.toString("Partition defragmented", "grpId", grpId, false, "partId", partId, false, "oldPages", oldPageStore.pages(), false, "newPages", partCtx.partPagesAllocated.get() + 1, false, "mappingPages", partCtx.mappingPagesAllocated.get() + 1, false, "pageSize", pageSize, false, "partFile", defragmentedPartFile(workDir, partId).getName(), false, "workDir", workDir, false));
            }
            oldPageMem.invalidate(grpId, partId);
            partCtx.partPageMemory.invalidate(grpId, partId);
            // Yes, it'll be invalid in a second.
            pageMgr.pageStoreMap().removePageStore(grpId, partId);
            renameTempPartitionFile(workDir, partId);
        }
    };
    GridFutureAdapter<?> cpFut = defragmentationCheckpoint.forceCheckpoint("partition defragmented", null).futureFor(FINISHED);
    cpFut.listen(cpLsnr);
    cmpFut.add((IgniteInternalFuture<Object>) cpFut);
    return 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) PageStore(org.apache.ignite.internal.pagemem.store.PageStore) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 35 with CacheGroupContext

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

the class CheckpointWorkflow method fillCacheGroupState.

/**
 * Fill cache group state in checkpoint record.
 *
 * @param cpRec Checkpoint record for filling.
 * @throws IgniteCheckedException if fail.
 */
private void fillCacheGroupState(CheckpointRecord cpRec) throws IgniteCheckedException {
    GridCompoundFuture grpHandleFut = checkpointCollectPagesInfoPool == null ? null : new GridCompoundFuture();
    for (CacheGroupContext grp : cacheGroupsContexts.get()) {
        if (grp.isLocal() || !grp.walEnabled())
            continue;
        Runnable r = () -> {
            ArrayList<GridDhtLocalPartition> parts = new ArrayList<>(grp.topology().localPartitions().size());
            for (GridDhtLocalPartition part : grp.topology().currentLocalPartitions()) parts.add(part);
            CacheState state = new CacheState(parts.size());
            for (GridDhtLocalPartition part : parts) {
                GridDhtPartitionState partState = part.state();
                if (partState == LOST)
                    partState = OWNING;
                state.addPartitionState(part.id(), part.dataStore().fullSize(), part.updateCounter(), (byte) partState.ordinal());
            }
            synchronized (cpRec) {
                cpRec.addCacheGroupState(grp.groupId(), state);
            }
        };
        if (checkpointCollectPagesInfoPool == null)
            r.run();
        else
            try {
                GridFutureAdapter<?> res = new GridFutureAdapter<>();
                checkpointCollectPagesInfoPool.execute(U.wrapIgniteFuture(r, res));
                grpHandleFut.add(res);
            } catch (RejectedExecutionException e) {
                assert false : "Task should never be rejected by async runner";
                // to protect from disabled asserts and call to failure handler
                throw new IgniteException(e);
            }
    }
    if (grpHandleFut != null) {
        grpHandleFut.markInitialized();
        grpHandleFut.get();
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) ArrayList(java.util.ArrayList) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheState(org.apache.ignite.internal.pagemem.wal.record.CacheState) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)103 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)31 IgniteEx (org.apache.ignite.internal.IgniteEx)29 Map (java.util.Map)27 HashMap (java.util.HashMap)24 IgniteException (org.apache.ignite.IgniteException)22 ArrayList (java.util.ArrayList)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Test (org.junit.Test)20 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)19 List (java.util.List)17 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)17 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)16 GridDhtPartitionTopology (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology)16 HashSet (java.util.HashSet)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)12 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)12 GridCacheSharedContext (org.apache.ignite.internal.processors.cache.GridCacheSharedContext)12 Set (java.util.Set)11 Collection (java.util.Collection)10