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());
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations