use of org.apache.ignite.internal.processors.cache.verify.PartitionKeyV2 in project ignite by apache.
the class SnapshotPartitionsVerifyHandler method invoke.
/**
* {@inheritDoc}
*/
@Override
public Map<PartitionKeyV2, PartitionHashRecordV2> invoke(SnapshotHandlerContext opCtx) throws IgniteCheckedException {
SnapshotMetadata meta = opCtx.metadata();
Set<Integer> grps = F.isEmpty(opCtx.groups()) ? new HashSet<>(meta.partitions().keySet()) : opCtx.groups().stream().map(CU::cacheId).collect(Collectors.toSet());
Set<File> partFiles = new HashSet<>();
IgniteSnapshotManager snpMgr = cctx.snapshotMgr();
for (File dir : snpMgr.snapshotCacheDirectories(meta.snapshotName(), meta.folderName())) {
int grpId = CU.cacheId(cacheGroupName(dir));
if (!grps.remove(grpId))
continue;
Set<Integer> parts = meta.partitions().get(grpId) == null ? Collections.emptySet() : new HashSet<>(meta.partitions().get(grpId));
for (File part : cachePartitionFiles(dir)) {
int partId = partId(part.getName());
if (!parts.remove(partId))
continue;
partFiles.add(part);
}
if (!parts.isEmpty()) {
throw new IgniteException("Snapshot data doesn't contain required cache group partition " + "[grpId=" + grpId + ", snpName=" + meta.snapshotName() + ", consId=" + meta.consistentId() + ", missed=" + parts + ", meta=" + meta + ']');
}
}
if (!grps.isEmpty()) {
throw new IgniteException("Snapshot data doesn't contain required cache groups " + "[grps=" + grps + ", snpName=" + meta.snapshotName() + ", consId=" + meta.consistentId() + ", meta=" + meta + ']');
}
Map<PartitionKeyV2, PartitionHashRecordV2> res = new ConcurrentHashMap<>();
ThreadLocal<ByteBuffer> buff = ThreadLocal.withInitial(() -> ByteBuffer.allocateDirect(meta.pageSize()).order(ByteOrder.nativeOrder()));
GridKernalContext snpCtx = snpMgr.createStandaloneKernalContext(meta.snapshotName(), meta.folderName());
for (GridComponent comp : snpCtx) comp.start();
try {
U.doInParallel(snpMgr.snapshotExecutorService(), partFiles, part -> {
String grpName = cacheGroupName(part.getParentFile());
int grpId = CU.cacheId(grpName);
int partId = partId(part.getName());
FilePageStoreManager storeMgr = (FilePageStoreManager) cctx.pageStore();
try (FilePageStore pageStore = (FilePageStore) storeMgr.getPageStoreFactory(grpId, false).createPageStore(getTypeByPartId(partId), part::toPath, val -> {
})) {
if (partId == INDEX_PARTITION) {
checkPartitionsPageCrcSum(() -> pageStore, INDEX_PARTITION, FLAG_IDX);
return null;
}
if (grpId == MetaStorage.METASTORAGE_CACHE_ID) {
checkPartitionsPageCrcSum(() -> pageStore, partId, FLAG_DATA);
return null;
}
ByteBuffer pageBuff = buff.get();
pageBuff.clear();
pageStore.read(0, pageBuff, true);
long pageAddr = GridUnsafe.bufferAddress(pageBuff);
PagePartitionMetaIO io = PageIO.getPageIO(pageBuff);
GridDhtPartitionState partState = fromOrdinal(io.getPartitionState(pageAddr));
if (partState != OWNING) {
throw new IgniteCheckedException("Snapshot partitions must be in the OWNING " + "state only: " + partState);
}
long updateCntr = io.getUpdateCounter(pageAddr);
long size = io.getSize(pageAddr);
if (log.isDebugEnabled()) {
log.debug("Partition [grpId=" + grpId + ", id=" + partId + ", counter=" + updateCntr + ", size=" + size + "]");
}
// Snapshot partitions must always be in OWNING state.
// There is no `primary` partitions for snapshot.
PartitionKeyV2 key = new PartitionKeyV2(grpId, partId, grpName);
PartitionHashRecordV2 hash = calculatePartitionHash(key, updateCntr, meta.consistentId(), GridDhtPartitionState.OWNING, false, size, snpMgr.partitionRowIterator(snpCtx, grpName, partId, pageStore));
assert hash != null : "OWNING must have hash: " + key;
res.put(key, hash);
} catch (IOException e) {
throw new IgniteCheckedException(e);
}
return null;
});
} catch (Throwable t) {
log.error("Error executing handler: ", t);
throw t;
} finally {
for (GridComponent comp : snpCtx) comp.stop(true);
}
return res;
}
use of org.apache.ignite.internal.processors.cache.verify.PartitionKeyV2 in project ignite by apache.
the class SnapshotPartitionsVerifyHandler method complete.
/**
* {@inheritDoc}
*/
@Override
public void complete(String name, Collection<SnapshotHandlerResult<Map<PartitionKeyV2, PartitionHashRecordV2>>> results) throws IgniteCheckedException {
Map<PartitionKeyV2, List<PartitionHashRecordV2>> clusterHashes = new HashMap<>();
Map<ClusterNode, Exception> errs = new HashMap<>();
for (SnapshotHandlerResult<Map<PartitionKeyV2, PartitionHashRecordV2>> res : results) {
if (res.error() != null) {
errs.put(res.node(), res.error());
continue;
}
for (Map.Entry<PartitionKeyV2, PartitionHashRecordV2> entry : res.data().entrySet()) clusterHashes.computeIfAbsent(entry.getKey(), v -> new ArrayList<>()).add(entry.getValue());
}
IdleVerifyResultV2 verifyResult = new IdleVerifyResultV2(clusterHashes, errs);
if (errs.isEmpty() && !verifyResult.hasConflicts())
return;
GridStringBuilder buf = new GridStringBuilder();
verifyResult.print(buf::a, true);
throw new IgniteCheckedException(buf.toString());
}
use of org.apache.ignite.internal.processors.cache.verify.PartitionKeyV2 in project ignite by apache.
the class IgniteClusterSnapshotCheckTest method saveHashes.
/**
* @param cls Class of running task.
* @param results Results of compute.
*/
private void saveHashes(Class<?> cls, List<ComputeJobResult> results) {
Map<PartitionKeyV2, List<PartitionHashRecordV2>> hashes = new HashMap<>();
for (ComputeJobResult job : results) {
if (job.getException() != null)
continue;
job.<Map<PartitionKeyV2, PartitionHashRecordV2>>getData().forEach((k, v) -> hashes.computeIfAbsent(k, k0 -> new ArrayList<>()).add(v));
}
Object mustBeNull = jobResults.putIfAbsent(cls, hashes);
assertNull(mustBeNull);
}
use of org.apache.ignite.internal.processors.cache.verify.PartitionKeyV2 in project ignite by apache.
the class IgniteClusterSnapshotCheckTest method testClusterSnapshotCheckHashesSameAsIdleVerifyHashes.
/**
* @throws Exception If fails.
*/
@Test
public void testClusterSnapshotCheckHashesSameAsIdleVerifyHashes() throws Exception {
Random rnd = new Random();
CacheConfiguration<Integer, Value> ccfg = txCacheConfig(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
IgniteEx ignite = startGridsWithCache(1, CACHE_KEYS_RANGE, k -> new Value(new byte[rnd.nextInt(32768)]), ccfg);
ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get();
IdleVerifyResultV2 idleVerifyRes = ignite.compute().execute(new TestVisorBackupPartitionsTask(), new VisorIdleVerifyTaskArg(new HashSet<>(singletonList(ccfg.getName())), new HashSet<>(), false, CacheFilterEnum.USER, true));
IdleVerifyResultV2 snpVerifyRes = ignite.compute().execute(new TestSnapshotPartitionsVerifyTask(), new SnapshotPartitionsVerifyTaskArg(new HashSet<>(), Collections.singletonMap(ignite.cluster().localNode(), Collections.singletonList(snp(ignite).readSnapshotMetadata(SNAPSHOT_NAME, (String) ignite.configuration().getConsistentId()))))).idleVerifyResult();
Map<PartitionKeyV2, List<PartitionHashRecordV2>> idleVerifyHashes = jobResults.get(TestVisorBackupPartitionsTask.class);
Map<PartitionKeyV2, List<PartitionHashRecordV2>> snpCheckHashes = jobResults.get(TestVisorBackupPartitionsTask.class);
assertFalse(F.isEmpty(idleVerifyHashes));
assertFalse(F.isEmpty(snpCheckHashes));
assertEquals(idleVerifyHashes, snpCheckHashes);
assertEquals(idleVerifyRes, snpVerifyRes);
}
Aggregations