Skip to content

Commit

Permalink
IGNITE-24454 Cache partition files calculation in NodeFileTree
Browse files Browse the repository at this point in the history
  • Loading branch information
nizhikov committed Feb 11, 2025
1 parent 2d8891e commit f240623
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.client.util.GridConcurrentHashSet;
import org.apache.ignite.internal.managers.encryption.EncryptionCacheKeyProvider;
import org.apache.ignite.internal.pagemem.PageIdAllocator;
import org.apache.ignite.internal.pagemem.PageIdUtils;
import org.apache.ignite.internal.pagemem.store.IgnitePageStoreManager;
import org.apache.ignite.internal.pagemem.store.PageStore;
Expand Down Expand Up @@ -82,7 +81,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.lang.String.format;
import static java.nio.file.Files.delete;
import static java.nio.file.Files.newDirectoryStream;
import static java.util.Objects.requireNonNull;
Expand All @@ -92,9 +90,9 @@
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.CACHE_DIR_WITH_META_FILTER;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.FILE_SUFFIX;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.INDEX_FILE_NAME;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.PART_FILE_PREFIX;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.PART_FILE_TEMPLATE;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.TMP_SUFFIX;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.partitionFileName;
import static org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage.METASTORAGE_DIR_NAME;

/**
* File page store manager.
Expand Down Expand Up @@ -420,7 +418,7 @@ private List<CacheConfiguration> findCacheGroupsWithDisabledWal() {
PageMetrics pageMetrics = dataRegion.metrics().cacheGrpPageMetrics(grpId);

CacheStoreHolder holder = initDir(
new File(ft.nodeStorage(), MetaStorage.METASTORAGE_DIR_NAME),
METASTORAGE_DIR_NAME,
grpId,
MetaStorage.METASTORAGE_CACHE_NAME,
MetaStorage.METASTORAGE_PARTITIONS.size(),
Expand Down Expand Up @@ -510,14 +508,12 @@ private List<CacheConfiguration> findCacheGroupsWithDisabledWal() {
private CacheStoreHolder initForCache(CacheGroupDescriptor grpDesc, CacheConfiguration ccfg) throws IgniteCheckedException {
assert !grpDesc.sharedGroup() || ccfg.getGroupName() != null : ccfg.getName();

File cacheWorkDir = ft.cacheStorage(ccfg);

String dataRegionName = grpDesc.config().getDataRegionName();
DataRegion dataRegion = cctx.database().dataRegion(dataRegionName);
PageMetrics pageMetrics = dataRegion.metrics().cacheGrpPageMetrics(grpDesc.groupId());

return initDir(
cacheWorkDir,
ft.cacheDirName(ccfg),
grpDesc.groupId(),
ccfg.getName(),
grpDesc.config().getAffinity().partitions(),
Expand Down Expand Up @@ -590,7 +586,7 @@ public EncryptedFileIOFactory encryptedFileIoFactory(FileIOFactory plainFileIOFa
}

/**
* @param cacheWorkDir Work directory.
* @param cacheDir Cache dir name.
* @param grpId Group ID.
* @param cacheName Cache name.
* @param partitions Number of partitions.
Expand All @@ -599,14 +595,17 @@ public EncryptedFileIOFactory encryptedFileIoFactory(FileIOFactory plainFileIOFa
* @return Cache store holder.
* @throws IgniteCheckedException If failed.
*/
private CacheStoreHolder initDir(File cacheWorkDir,
private CacheStoreHolder initDir(
String cacheDir,
int grpId,
String cacheName,
int partitions,
PageMetrics pageMetrics,
boolean encrypted,
Collection<String> grpCaches) throws IgniteCheckedException {
try {
File cacheWorkDir = ft.cacheStorage(cacheDir);

boolean dirExisted = checkAndInitCacheWorkDir(cacheWorkDir, log);

if (dirExisted) {
Expand Down Expand Up @@ -656,7 +655,7 @@ private CacheStoreHolder initDir(File cacheWorkDir,
PageStore partStore =
pageStoreFactory.createPageStore(
PageStore.TYPE_DATA,
() -> getPartitionFilePath(cacheWorkDir, p),
() -> NodeFileTree.partitionFile(ft.nodeStorage(), cacheDir, p).toPath(),
pageMetrics.totalPages()::add);

partStores[partId] = partStore;
Expand All @@ -677,27 +676,7 @@ private CacheStoreHolder initDir(File cacheWorkDir,
* @param partId Partition id.
*/
@NotNull private Path getPartitionFilePath(File cacheWorkDir, int partId) {
return new File(cacheWorkDir, getPartitionFileName(partId)).toPath();
}

/**
* @param workDir Cache work directory.
* @param cacheDirName Cache directory name.
* @param partId Partition id.
* @return Partition file.
*/
@NotNull public static File getPartitionFile(File workDir, String cacheDirName, int partId) {
return new File(NodeFileTree.cacheStorage(workDir, cacheDirName), getPartitionFileName(partId));
}

/**
* @param partId Partition id.
* @return File name.
*/
public static String getPartitionFileName(int partId) {
assert partId <= MAX_PARTITION_ID || partId == INDEX_PARTITION;

return partId == INDEX_PARTITION ? INDEX_FILE_NAME : format(PART_FILE_TEMPLATE, partId);
return new File(cacheWorkDir, partitionFileName(partId)).toPath();
}

/**
Expand Down Expand Up @@ -822,20 +801,6 @@ public static File cacheDirectory(File dir, int grpId) {
.orElse(null);
}

/**
* @param partFileName Partition file name.
* @return Partition id.
*/
public static int partId(String partFileName) {
if (partFileName.equals(INDEX_FILE_NAME))
return PageIdAllocator.INDEX_PARTITION;

if (partFileName.startsWith(PART_FILE_PREFIX))
return Integer.parseInt(partFileName.substring(PART_FILE_PREFIX.length(), partFileName.indexOf('.')));

throw new IllegalStateException("Illegal partition file name: " + partFileName);
}

/**
* @param cacheDir Cache directory to check.
* @return List of cache partitions in given directory.
Expand Down Expand Up @@ -896,7 +861,7 @@ public static File[] cacheDataFiles(File root) {
*/
public String cacheDirName(int grpId) throws IgniteCheckedException {
if (grpId == MetaStorage.METASTORAGE_CACHE_ID)
return MetaStorage.METASTORAGE_DIR_NAME;
return METASTORAGE_DIR_NAME;

CacheGroupContext gctx = cctx.cache().cacheGroup(grpId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@
import org.apache.ignite.internal.util.typedef.internal.S;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.spi.checkpoint.sharedfs.SharedFsCheckpointSpi;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.lang.String.format;
import static org.apache.ignite.configuration.DataStorageConfiguration.DFLT_WAL_ARCHIVE_PATH;
import static org.apache.ignite.configuration.DataStorageConfiguration.DFLT_WAL_CDC_PATH;
import static org.apache.ignite.configuration.DataStorageConfiguration.DFLT_WAL_PATH;
import static org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION;
import static org.apache.ignite.internal.pagemem.PageIdAllocator.MAX_PARTITION_ID;
import static org.apache.ignite.internal.processors.cache.persistence.filename.PdsFolderResolver.DB_DEFAULT_FOLDER;
import static org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage.METASTORAGE_CACHE_NAME;
import static org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage.METASTORAGE_DIR_NAME;

/**
* Provides access to Ignite node file tree.
Expand Down Expand Up @@ -489,6 +495,26 @@ public static String cacheDataFilename(CacheConfiguration<?, ?> ccfg) {
return ccfg.getGroupName() == null ? CACHE_DATA_FILENAME : (ccfg.getName() + CACHE_DATA_FILENAME);
}

/**
* @param workDir Cache work directory.
* @param cacheDirName Cache directory name.
* @param partId Partition id.
* @return Partition file.
*/
@NotNull public static File partitionFile(File workDir, String cacheDirName, int partId) {
return new File(cacheStorage(workDir, cacheDirName), partitionFileName(partId));
}

/**
* @param partId Partition id.
* @return File name.
*/
public static String partitionFileName(int partId) {
assert partId <= MAX_PARTITION_ID || partId == INDEX_PARTITION;

return partId == INDEX_PARTITION ? INDEX_FILE_NAME : format(PART_FILE_TEMPLATE, partId);
}

/**
* @param cacheDirName Cache directory name.
* @return Store directory for given cache.
Expand Down Expand Up @@ -552,6 +578,9 @@ public File cacheStorage(String cacheDirName) {
* @return The full cache directory name.
*/
public static String cacheDirName(boolean isSharedGroup, String cacheOrGroupName) {
if (cacheOrGroupName.equals(METASTORAGE_CACHE_NAME))
return METASTORAGE_DIR_NAME;

return isSharedGroup
? CACHE_GRP_DIR_PREFIX + cacheOrGroupName
: CACHE_DIR_PREFIX + cacheOrGroupName;
Expand All @@ -569,7 +598,7 @@ public static String cacheName(File dir) {
else if (name.startsWith(CACHE_DIR_PREFIX))
return name.substring(CACHE_DIR_PREFIX.length());
else if (name.equals(MetaStorage.METASTORAGE_DIR_NAME))
return MetaStorage.METASTORAGE_CACHE_NAME;
return METASTORAGE_CACHE_NAME;
else
throw new IgniteException("Directory doesn't match the cache or cache group prefix: " + dir);
}
Expand All @@ -584,6 +613,21 @@ public long walSegmentIndex(Path segment) {
return Long.parseLong(fn.substring(0, fn.indexOf('.')));
}

/**
* @param part Partition file name.
* @return Partition id.
*/
public static int partId(File part) {
String name = part.getName();
if (name.equals(INDEX_FILE_NAME))
return INDEX_PARTITION;

if (name.startsWith(PART_FILE_PREFIX))
return Integer.parseInt(name.substring(PART_FILE_PREFIX.length(), name.indexOf('.')));

throw new IllegalStateException("Illegal partition file name: " + name);
}

/**
* Resolves directory specified by the given arguments.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@
import static org.apache.ignite.internal.processors.cache.GridCacheUtils.baselineNode;
import static org.apache.ignite.internal.processors.cache.GridCacheUtils.isPersistenceEnabled;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cacheDirectories;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.getPartitionFile;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.getPartitionFileName;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.INDEX_FILE_NAME;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.PART_FILE_TEMPLATE;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.cacheName;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.partitionFile;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.partitionFileName;
import static org.apache.ignite.internal.processors.cache.persistence.filename.PdsFolderResolver.DB_DEFAULT_FOLDER;
import static org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage.METASTORAGE_CACHE_ID;
import static org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage.METASTORAGE_CACHE_NAME;
Expand Down Expand Up @@ -2587,7 +2587,7 @@ public GridCloseableIterator<CacheDataRow> partitionRowIterator(String snpName,
);
}

File snpPart = getPartitionFile(new File(snapshotLocalDir(snpName, null), databaseRelativePath(folderName)),
File snpPart = partitionFile(new File(snapshotLocalDir(snpName, null), databaseRelativePath(folderName)),
grps.get(0).getName(), partId);

int grpId = CU.cacheId(grpName);
Expand Down Expand Up @@ -3898,7 +3898,7 @@ else if (msg instanceof SnapshotFilesFailureMessage) {
File tmpCacheDir = U.resolveWorkDirectory(ft.nodeStorage().getAbsolutePath(),
formatTmpDirName(cacheDir).getName(), false);

return Paths.get(tmpCacheDir.getAbsolutePath(), getPartitionFileName(partId)).toString();
return Paths.get(tmpCacheDir.getAbsolutePath(), partitionFileName(partId)).toString();
}
catch (IgniteCheckedException e) {
throw new IgniteException(e);
Expand Down Expand Up @@ -4178,7 +4178,7 @@ public LocalSnapshotSender(String snpName, @Nullable String snpPath) {

/** {@inheritDoc} */
@Override public void sendDelta0(File delta, String cacheDirName, GroupPartitionId pair) {
File snpPart = getPartitionFile(dbDir, cacheDirName, pair.getPartitionId());
File snpPart = partitionFile(dbDir, cacheDirName, pair.getPartitionId());

if (log.isDebugEnabled()) {
log.debug("Start partition snapshot recovery with the given delta page file [part=" + snpPart +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import org.apache.ignite.internal.util.typedef.internal.U;
import org.jetbrains.annotations.Nullable;

import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.getPartitionFile;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.cacheStorage;
import static org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.copy;
import static org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.databaseRelativePath;
Expand Down Expand Up @@ -412,7 +411,7 @@ public IgniteInternalFuture<?> started() {

return runAsync(() -> {
snpSndr.sendPart(
getPartitionFile(ft.nodeStorage(), cacheDirName, partId),
NodeFileTree.partitionFile(ft.nodeStorage(), cacheDirName, partId),
cacheDirName,
pair,
partLen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@
import static org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.fromOrdinal;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cacheDirectories;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cachePartitionFiles;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.partId;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.FILE_SUFFIX;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.ZIP_SUFFIX;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.cacheName;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.partId;
import static org.apache.ignite.internal.processors.cache.persistence.partstate.GroupPartitionId.getTypeByPartId;
import static org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.databaseRelativePath;
import static org.apache.ignite.internal.processors.cache.persistence.snapshot.dump.CreateDumpFutureTask.DUMP_FILE_EXT;
Expand Down Expand Up @@ -148,7 +148,7 @@ public SnapshotPartitionsVerifyHandler(GridCacheSharedContext<?, ?> cctx) {
for (File part : cachePartitionFiles(dir,
(meta.dump() ? DUMP_FILE_EXT : FILE_SUFFIX) + (meta.compressPartitions() ? ZIP_SUFFIX : "")
)) {
int partId = partId(part.getName());
int partId = partId(part);

if (!parts.remove(partId))
continue;
Expand Down Expand Up @@ -212,7 +212,7 @@ private Map<PartitionKey, PartitionHashRecord> checkSnapshotFiles(
part -> {
String grpName = cacheName(part.getParentFile());
int grpId = CU.cacheId(grpName);
int partId = partId(part.getName());
int partId = partId(part);

try (FilePageStore pageStore =
(FilePageStore)storeMgr.getPageStoreFactory(grpId, snpEncrKeyProvider.getActiveKey(grpId) != null ?
Expand Down Expand Up @@ -372,7 +372,7 @@ private Map<PartitionKey, PartitionHashRecord> checkDumpFiles(
Collection<PartitionHashRecord> partitionHashRecords = U.doInParallel(
cctx.snapshotMgr().snapshotExecutorService(),
partFiles,
part -> calculateDumpedPartitionHash(dump, cacheName(part.getParentFile()), partId(part.getName()))
part -> calculateDumpedPartitionHash(dump, cacheName(part.getParentFile()), partId(part))
);

return partitionHashRecords.stream().collect(Collectors.toMap(PartitionHashRecord::partitionKey, r -> r));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.jetbrains.annotations.Nullable;

import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.cacheDirectory;
import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.getPartitionFile;
import static org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.partitionFile;
import static org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.databaseRelativePath;

/** */
Expand Down Expand Up @@ -117,7 +117,7 @@ public SnapshotResponseRemoteFutureTask(
", pair=" + gp + ']');
}

File snpPart = getPartitionFile(cacheDir.getParentFile(), cacheDir.getName(), gp.getPartitionId());
File snpPart = partitionFile(cacheDir.getParentFile(), cacheDir.getName(), gp.getPartitionId());

if (!snpPart.exists()) {
throw new IgniteException("Snapshot partition file not found [cacheDir=" + cacheDir +
Expand Down
Loading

0 comments on commit f240623

Please sign in to comment.