This is an automated email from the ASF dual-hosted git repository.
smengcl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/master by this push:
new 669912118cf HDDS-17078. Parse each snapshot local data YAML once
during OM startup (#11575)
669912118cf is described below
commit 669912118cfbc9cc346bf77e89b178a4fab78702
Author: Siyao Meng <[email protected]>
AuthorDate: Wed Oct 21 17:32:49 2026 -0700
HDDS-17078. Parse each snapshot local data YAML once during OM startup
(#11575)
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../om/snapshot/OmSnapshotLocalDataManager.java | 105 ++++++++++++++++-----
.../snapshot/TestOmSnapshotLocalDataManager.java | 23 ++++-
2 files changed, 102 insertions(+), 26 deletions(-)
diff --git
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/OmSnapshotLocalDataManager.java
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/OmSnapshotLocalDataManager.java
index c3a29d6fb42..86f87430c22 100644
---
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/OmSnapshotLocalDataManager.java
+++
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/OmSnapshotLocalDataManager.java
@@ -29,6 +29,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@@ -49,11 +50,16 @@
import java.util.Stack;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.utils.Scheduler;
import org.apache.hadoop.hdds.utils.TransactionInfo;
@@ -288,7 +294,8 @@ private void addMissingSnapshotYamlFiles(
}
void addVersionNodeWithDependents(OmSnapshotLocalData snapshotLocalData)
throws IOException {
- addVersionNodeWithDependents(snapshotLocalData, null);
+ addVersionNodeWithDependents(snapshotLocalData.getSnapshotId(), new
SnapshotVersionsMeta(snapshotLocalData),
+ Collections.emptyMap(), null);
}
/**
@@ -297,7 +304,9 @@ void addVersionNodeWithDependents(OmSnapshotLocalData
snapshotLocalData) throws
* added. This method walks the previous-snapshot chain using persisted YAML
metadata and processes the stack in
* dependency order.
*
- * @param snapshotLocalData snapshot local data to add to the version graph
+ * @param snapshotId id of the snapshot to add to the version graph
+ * @param versionsMeta version metadata of the snapshot to add to the
version graph
+ * @param loadedVersions version metadata already parsed from YAML, used
instead of reloading a previous snapshot
* @param failedFilePaths when non-null, a previous snapshot YAML that
cannot be loaded or whose snapshotId does not
* match its path is skipped instead of thrown, and its path is recorded
here so it is not reloaded during
* startup (a path recorded here is not logged again on subsequent
lookups)
@@ -306,14 +315,14 @@ void addVersionNodeWithDependents(OmSnapshotLocalData
snapshotLocalData) throws
* @throws IOException if a required YAML load fails, or the loaded
snapshotId does not match, when failedFilePaths is
* null
*/
- private boolean addVersionNodeWithDependents(OmSnapshotLocalData
snapshotLocalData, Set<String> failedFilePaths)
- throws IOException {
- if (versionNodeMap.containsKey(snapshotLocalData.getSnapshotId())) {
+ private boolean addVersionNodeWithDependents(UUID snapshotId,
SnapshotVersionsMeta versionsMeta,
+ Map<UUID, SnapshotVersionsMeta> loadedVersions, Set<String>
failedFilePaths) throws IOException {
+ if (versionNodeMap.containsKey(snapshotId)) {
return true;
}
Set<UUID> visitedSnapshotIds = new HashSet<>();
Stack<Pair<UUID, SnapshotVersionsMeta>> stack = new Stack<>();
- stack.push(Pair.of(snapshotLocalData.getSnapshotId(), new
SnapshotVersionsMeta(snapshotLocalData)));
+ stack.push(Pair.of(snapshotId, versionsMeta));
while (!stack.isEmpty()) {
Pair<UUID, SnapshotVersionsMeta> versionNodeToProcess = stack.peek();
UUID snapId = versionNodeToProcess.getLeft();
@@ -323,7 +332,10 @@ private boolean
addVersionNodeWithDependents(OmSnapshotLocalData snapshotLocalDa
stack.pop();
} else {
UUID prevSnapId = snapshotVersionsMeta.getPreviousSnapshotId();
- if (prevSnapId != null && !versionNodeMap.containsKey(prevSnapId)) {
+ SnapshotVersionsMeta loadedPrevVersionsMeta = prevSnapId == null ?
null : loadedVersions.get(prevSnapId);
+ if (loadedPrevVersionsMeta != null &&
!versionNodeMap.containsKey(prevSnapId)) {
+ stack.push(Pair.of(prevSnapId, loadedPrevVersionsMeta));
+ } else if (prevSnapId != null &&
!versionNodeMap.containsKey(prevSnapId)) {
File previousSnapshotLocalDataFile = new
File(getSnapshotLocalPropertyYamlPath(prevSnapId));
OmSnapshotLocalData prevSnapshotLocalData;
if (failedFilePaths != null) {
@@ -415,27 +427,17 @@ private void init(OzoneConfiguration configuration,
SnapshotChainManager chainMa
throw new IOException("Error while listing yaml files inside directory:
" + snapshotDir.getAbsolutePath());
}
Arrays.sort(localDataFiles, Comparator.comparing(File::getName));
- Set<String> failedFilePaths = new HashSet<>();
- for (File localDataFile : localDataFiles) {
- Optional<OmSnapshotLocalData> loadedLocalData =
tryLoadSnapshotLocalData(localDataFile, failedFilePaths);
- if (!loadedLocalData.isPresent()) {
- continue;
- }
- OmSnapshotLocalData snapshotLocalData = loadedLocalData.get();
- File file = new
File(getSnapshotLocalPropertyYamlPath(snapshotLocalData.getSnapshotId()));
- String expectedPath = file.getAbsolutePath();
- String actualPath = localDataFile.getAbsolutePath();
- if (!expectedPath.equals(actualPath)) {
- failedFilePaths.add(actualPath);
- LOG.error("Skipping snapshot local data file {} because its stored
snapshotId {} does not match its path. " +
- "Expected path: {}.", actualPath,
snapshotLocalData.getSnapshotId(), expectedPath);
- continue;
- }
- if (!addVersionNodeWithDependents(snapshotLocalData, failedFilePaths)) {
+ Set<String> failedFilePaths = ConcurrentHashMap.newKeySet();
+ List<Pair<UUID, SnapshotVersionsMeta>> loadedFiles =
loadVersionsMeta(localDataFiles, failedFilePaths);
+ Map<UUID, SnapshotVersionsMeta> loadedVersions = new HashMap<>();
+ loadedFiles.forEach(loaded -> loadedVersions.put(loaded.getLeft(),
loaded.getRight()));
+ for (Pair<UUID, SnapshotVersionsMeta> loaded : loadedFiles) {
+ if (!addVersionNodeWithDependents(loaded.getLeft(), loaded.getRight(),
loadedVersions, failedFilePaths)) {
// A previous snapshot in the dependency chain could not be loaded, so
this snapshot was not added to the
// version graph. Record its path as failed so later snapshots that
depend on it short-circuit in
// tryLoadSnapshotLocalData instead of reparsing this YAML.
- failedFilePaths.add(actualPath);
+ loadedVersions.remove(loaded.getLeft());
+ failedFilePaths.add(new
File(getSnapshotLocalPropertyYamlPath(loaded.getLeft())).getAbsolutePath());
}
}
for (UUID snapshotId : versionNodeMap.keySet()) {
@@ -459,6 +461,59 @@ private void init(OzoneConfiguration configuration,
SnapshotChainManager chainMa
}
+ /**
+ * Parses each YAML exactly once, in parallel, keeping only its version
metadata. A YAML lists every SST file of its
+ * snapshot, so with many snapshots this parse dominates OM startup.
+ *
+ * @return snapshot id and version metadata of each loaded file, in the
order of localDataFiles
+ */
+ private List<Pair<UUID, SnapshotVersionsMeta>> loadVersionsMeta(File[]
localDataFiles, Set<String> failedFilePaths)
+ throws IOException {
+ // The serializer pools GenericObjectPoolConfig.DEFAULT_MAX_TOTAL Yaml
instances, so more threads would only wait.
+ int threads = Math.min(Runtime.getRuntime().availableProcessors(),
GenericObjectPoolConfig.DEFAULT_MAX_TOTAL);
+ ExecutorService executor = Executors.newFixedThreadPool(threads,
+ new
ThreadFactoryBuilder().setNameFormat("SnapshotLocalDataLoader-%d").setDaemon(true).build());
+ try {
+ List<Future<Pair<UUID, SnapshotVersionsMeta>>> futures = new
ArrayList<>();
+ for (File localDataFile : localDataFiles) {
+ futures.add(executor.submit(() -> loadVersionsMeta(localDataFile,
failedFilePaths)));
+ }
+ List<Pair<UUID, SnapshotVersionsMeta>> loadedFiles = new ArrayList<>();
+ for (Future<Pair<UUID, SnapshotVersionsMeta>> future : futures) {
+ Pair<UUID, SnapshotVersionsMeta> loaded = future.get();
+ if (loaded != null) {
+ loadedFiles.add(loaded);
+ }
+ }
+ return loadedFiles;
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new IOException("Interrupted while loading snapshot local data
files", e);
+ } catch (ExecutionException e) {
+ throw new IOException("Failed to load snapshot local data files",
e.getCause());
+ } finally {
+ executor.shutdownNow();
+ }
+ }
+
+ private Pair<UUID, SnapshotVersionsMeta> loadVersionsMeta(File
localDataFile, Set<String> failedFilePaths) {
+ Optional<OmSnapshotLocalData> loadedLocalData =
tryLoadSnapshotLocalData(localDataFile, failedFilePaths);
+ if (!loadedLocalData.isPresent()) {
+ return null;
+ }
+ OmSnapshotLocalData snapshotLocalData = loadedLocalData.get();
+ File file = new
File(getSnapshotLocalPropertyYamlPath(snapshotLocalData.getSnapshotId()));
+ String expectedPath = file.getAbsolutePath();
+ String actualPath = localDataFile.getAbsolutePath();
+ if (!expectedPath.equals(actualPath)) {
+ failedFilePaths.add(actualPath);
+ LOG.error("Skipping snapshot local data file {} because its stored
snapshotId {} does not match its path. " +
+ "Expected path: {}.", actualPath, snapshotLocalData.getSnapshotId(),
expectedPath);
+ return null;
+ }
+ return Pair.of(snapshotLocalData.getSnapshotId(), new
SnapshotVersionsMeta(snapshotLocalData));
+ }
+
private void checkOrphanSnapshotVersions(OMMetadataManager metadataManager,
SnapshotChainManager chainManager)
throws IOException {
for (Map.Entry<UUID, Integer> entry :
snapshotToBeCheckedForOrphans.entrySet()) {
diff --git
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshotLocalDataManager.java
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshotLocalDataManager.java
index fc6b7ff721a..cab1d373fde 100644
---
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshotLocalDataManager.java
+++
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshotLocalDataManager.java
@@ -37,16 +37,21 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
@@ -65,6 +70,7 @@
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@@ -108,6 +114,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
+import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.rocksdb.LiveFileMetaData;
@@ -979,7 +986,21 @@ public void testInitWithExistingYamlFiles() throws
IOException {
createSnapshotLocalDataFile(snapshotId, previousSnapshotId);
// Initialize - should load existing files
- localDataManager = getNewOmSnapshotLocalDataManager();
+ AtomicInteger yamlLoads = new AtomicInteger();
+ try (MockedConstruction<OmSnapshotLocalDataYaml.YamlFactory> ignored =
mockConstruction(
+ OmSnapshotLocalDataYaml.YamlFactory.class,
withSettings().defaultAnswer(CALLS_REAL_METHODS),
+ (factory, context) -> doAnswer(create -> {
+ Yaml yaml = spy((Yaml) create.callRealMethod());
+ doAnswer(load -> {
+ yamlLoads.incrementAndGet();
+ return load.callRealMethod();
+ }).when(yaml).load(any(InputStream.class));
+ return yaml;
+ }).when(factory).create())) {
+ localDataManager = getNewOmSnapshotLocalDataManager();
+ }
+ // snapshotId sorts first and depends on previousSnapshotId, but each YAML
must still be parsed only once.
+ assertEquals(2, yamlLoads.get());
assertNotNull(localDataManager);
Map<UUID, OmSnapshotLocalDataManager.SnapshotVersionsMeta> versionMap =
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]