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 99182f55b04 HDDS-17079. Reduce OM startup GC by streaming snapshot
local data YAMLs for the version graph (#11576)
99182f55b04 is described below
commit 99182f55b04307ac444445a2e1e8d2bd4ba88a7f
Author: Siyao Meng <[email protected]>
AuthorDate: Thu Oct 29 00:44:51 2026 -0700
HDDS-17079. Reduce OM startup GC by streaming snapshot local data YAMLs for
the version graph (#11576)
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Wei-Chiu Chuang <[email protected]>
---
.../hadoop/ozone/om/OmSnapshotLocalDataYaml.java | 124 +++++++++++++++++++++
.../om/snapshot/OmSnapshotLocalDataManager.java | 7 +-
.../ozone/om/TestOmSnapshotLocalDataYaml.java | 26 +++++
.../snapshot/TestOmSnapshotLocalDataManager.java | 28 ++---
4 files changed, 167 insertions(+), 18 deletions(-)
diff --git
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalDataYaml.java
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalDataYaml.java
index 24c7fb3e3e7..ad559ee1f1c 100644
---
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalDataYaml.java
+++
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalDataYaml.java
@@ -17,7 +17,12 @@
package org.apache.hadoop.ozone.om;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
import java.util.Collections;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -34,6 +39,15 @@
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.AbstractConstruct;
import org.yaml.snakeyaml.constructor.SafeConstructor;
+import org.yaml.snakeyaml.events.CollectionEndEvent;
+import org.yaml.snakeyaml.events.CollectionStartEvent;
+import org.yaml.snakeyaml.events.DocumentEndEvent;
+import org.yaml.snakeyaml.events.DocumentStartEvent;
+import org.yaml.snakeyaml.events.Event;
+import org.yaml.snakeyaml.events.MappingStartEvent;
+import org.yaml.snakeyaml.events.ScalarEvent;
+import org.yaml.snakeyaml.events.StreamEndEvent;
+import org.yaml.snakeyaml.events.StreamStartEvent;
import org.yaml.snakeyaml.introspector.BeanAccess;
import org.yaml.snakeyaml.introspector.Property;
import org.yaml.snakeyaml.introspector.PropertyUtils;
@@ -42,6 +56,10 @@
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.Tag;
+import org.yaml.snakeyaml.parser.Parser;
+import org.yaml.snakeyaml.parser.ParserImpl;
+import org.yaml.snakeyaml.reader.StreamReader;
+import org.yaml.snakeyaml.reader.UnicodeReader;
import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;
@@ -63,6 +81,112 @@ public final class OmSnapshotLocalDataYaml {
private OmSnapshotLocalDataYaml() {
}
+ /**
+ * Loads only the snapshot ids and versions of a snapshot local data YAML,
streaming parser events instead of
+ * building the document, so the SST file lists are skipped without being
held in memory.
+ * The returned object has empty SST file lists, no transaction info and no
checksum, so it must not be saved.
+ */
+ public static OmSnapshotLocalData loadWithoutSstFiles(File yamlFile) throws
IOException {
+ try (InputStream in = Files.newInputStream(yamlFile.toPath())) {
+ Parser parser = new ParserImpl(new StreamReader(new UnicodeReader(in)),
+ SnapshotLocalDataConstructor.createLoaderOptions());
+ nextEvent(parser, StreamStartEvent.class);
+ nextEvent(parser, DocumentStartEvent.class);
+ MappingStartEvent root = nextEvent(parser, MappingStartEvent.class);
+ if (!SNAPSHOT_YAML_TAG.getValue().equals(root.getTag())) {
+ throw new IOException("Expected tag " + SNAPSHOT_YAML_TAG + " but
found " + root);
+ }
+ String snapId = null;
+ String prevSnapId = null;
+ String version = null;
+ Map<Integer, VersionMeta> versionMetas = null;
+ while (!parser.checkEvent(Event.ID.MappingEnd)) {
+ switch (nextScalar(parser)) {
+ case OzoneConsts.OM_SLD_SNAP_ID:
+ snapId = nextScalar(parser);
+ break;
+ case OzoneConsts.OM_SLD_PREV_SNAP_ID:
+ prevSnapId = nextScalar(parser);
+ break;
+ case OzoneConsts.OM_SLD_VERSION:
+ version = nextScalar(parser);
+ break;
+ case OzoneConsts.OM_SLD_VERSION_SST_FILE_INFO:
+ versionMetas = nextVersionMetas(parser);
+ break;
+ default:
+ skipValue(parser);
+ }
+ }
+ parser.getEvent();
+ nextEvent(parser, DocumentEndEvent.class);
+ nextEvent(parser, StreamEndEvent.class);
+ OmSnapshotLocalData data = new
OmSnapshotLocalData(UUID.fromString(require(snapId,
OzoneConsts.OM_SLD_SNAP_ID)),
+ Collections.emptyList(), prevSnapId == null ? null :
UUID.fromString(prevSnapId), null, 0);
+ data.setVersion(Integer.parseInt(require(version,
OzoneConsts.OM_SLD_VERSION)));
+ if (versionMetas != null) {
+ data.setVersionSstFileInfos(versionMetas);
+ }
+ return data;
+ } catch (RuntimeException e) {
+ throw new IOException("Failed to load " + yamlFile, e);
+ }
+ }
+
+ private static Map<Integer, VersionMeta> nextVersionMetas(Parser parser)
throws IOException {
+ Map<Integer, VersionMeta> versionMetas = new LinkedHashMap<>();
+ nextEvent(parser, MappingStartEvent.class);
+ while (!parser.checkEvent(Event.ID.MappingEnd)) {
+ int version = Integer.parseInt(nextScalar(parser));
+ String prevVersion = null;
+ nextEvent(parser, MappingStartEvent.class);
+ while (!parser.checkEvent(Event.ID.MappingEnd)) {
+ if
(OzoneConsts.OM_SLD_VERSION_META_PREV_SNAP_VERSION.equals(nextScalar(parser))) {
+ prevVersion = nextScalar(parser);
+ } else {
+ skipValue(parser);
+ }
+ }
+ parser.getEvent();
+ versionMetas.put(version, new VersionMeta(
+ Integer.parseInt(require(prevVersion,
OzoneConsts.OM_SLD_VERSION_META_PREV_SNAP_VERSION)),
+ Collections.emptyList()));
+ }
+ parser.getEvent();
+ return versionMetas;
+ }
+
+ private static <T extends Event> T nextEvent(Parser parser, Class<T> type)
throws IOException {
+ Event event = parser.getEvent();
+ if (!type.isInstance(event)) {
+ throw new IOException("Expected " + type.getSimpleName() + " but found "
+ event);
+ }
+ return type.cast(event);
+ }
+
+ private static String nextScalar(Parser parser) throws IOException {
+ return nextEvent(parser, ScalarEvent.class).getValue();
+ }
+
+ private static void skipValue(Parser parser) {
+ int depth = 0;
+ do {
+ Event event = parser.getEvent();
+ if (event instanceof CollectionStartEvent) {
+ depth++;
+ } else if (event instanceof CollectionEndEvent) {
+ depth--;
+ }
+ } while (depth > 0);
+ }
+
+ private static String require(String value, String key) throws IOException {
+ if (value == null) {
+ throw new IOException("Missing " + key);
+ }
+ return value;
+ }
+
/**
* Representer class to define which fields need to be stored in yaml file.
*/
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 606fdbd32f5..dbfc15106e5 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
@@ -348,7 +348,7 @@ private boolean addVersionNodeWithDependents(UUID
snapshotId, SnapshotVersionsMe
}
prevSnapshotLocalData = loadedLocalData.get();
} else {
- prevSnapshotLocalData =
snapshotLocalDataSerializer.load(previousSnapshotLocalDataFile);
+ prevSnapshotLocalData =
OmSnapshotLocalDataYaml.loadWithoutSstFiles(previousSnapshotLocalDataFile);
}
if (!prevSnapId.equals(prevSnapshotLocalData.getSnapshotId())) {
String mismatch = "Expected SnapshotId " + prevSnapId +
@@ -370,13 +370,16 @@ private boolean addVersionNodeWithDependents(UUID
snapshotId, SnapshotVersionsMe
return true;
}
+ /**
+ * Loads the snapshot ids and versions of a local data YAML, without its SST
file lists.
+ */
private Optional<OmSnapshotLocalData> tryLoadSnapshotLocalData(File
localDataFile, Set<String> failedFilePaths) {
String path = localDataFile.getAbsolutePath();
if (failedFilePaths.contains(path)) {
return Optional.empty();
}
try {
- return Optional.of(snapshotLocalDataSerializer.load(localDataFile));
+ return
Optional.of(OmSnapshotLocalDataYaml.loadWithoutSstFiles(localDataFile));
} catch (IOException e) {
failedFilePaths.add(path);
LOG.error("Skipping snapshot local data file {} because it could not be
loaded. " +
diff --git
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshotLocalDataYaml.java
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshotLocalDataYaml.java
index bfcb4b0b990..2b43ed47067 100644
---
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshotLocalDataYaml.java
+++
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshotLocalDataYaml.java
@@ -231,6 +231,31 @@ public void testUpdateSnapshotDataFile() throws
IOException {
defraggedFiles.get(45));
}
+ @Test
+ public void testLoadWithoutSstFiles() throws IOException {
+ File yamlFile = writeToYaml(UUID.randomUUID(), "snapshot7",
TransactionInfo.valueOf(1, 2)).getLeft();
+ OmSnapshotLocalData fullData =
omSnapshotLocalDataSerializer.load(yamlFile);
+ OmSnapshotLocalData metaData =
OmSnapshotLocalDataYaml.loadWithoutSstFiles(yamlFile);
+
+ assertEquals(fullData.getSnapshotId(), metaData.getSnapshotId());
+ assertEquals(fullData.getPreviousSnapshotId(),
metaData.getPreviousSnapshotId());
+ assertEquals(fullData.getVersion(), metaData.getVersion());
+
assertThat(metaData.getVersionSstFileInfos()).hasSameSizeAs(fullData.getVersionSstFileInfos());
+ fullData.getVersionSstFileInfos().forEach((version, versionMeta) -> {
+ assertThat(versionMeta.getSstFiles()).isNotEmpty();
+ assertThat(metaData.getVersionSstFileInfos()).containsKey(version);
+ VersionMeta metaOnly = metaData.getVersionSstFileInfos().get(version);
+ assertEquals(versionMeta.getPreviousSnapshotVersion(),
metaOnly.getPreviousSnapshotVersion());
+ assertThat(metaOnly.getSstFiles()).isEmpty();
+ });
+
+ File noSnapshotId = new File(testRoot, "noSnapshotId.yaml");
+ FileUtils.writeStringToFile(noSnapshotId,
FileUtils.readFileToString(yamlFile, StandardCharsets.UTF_8)
+ .replaceFirst("(?m)^" + OzoneConsts.OM_SLD_SNAP_ID + ": .*\\n", ""),
StandardCharsets.UTF_8);
+ IOException ex = assertThrows(IOException.class, () ->
OmSnapshotLocalDataYaml.loadWithoutSstFiles(noSnapshotId));
+ assertThat(ex).hasMessageContaining("Missing " +
OzoneConsts.OM_SLD_SNAP_ID);
+ }
+
@Test
public void testEmptyFile() throws IOException {
File emptyFile = new File(testRoot, "empty.yaml");
@@ -239,6 +264,7 @@ public void testEmptyFile() throws IOException {
IOException ex = assertThrows(IOException.class, () ->
omSnapshotLocalDataSerializer.load(emptyFile));
assertThat(ex).hasMessageContaining("Failed to load file. File is empty.");
+ assertThrows(IOException.class, () ->
OmSnapshotLocalDataYaml.loadWithoutSstFiles(emptyFile));
}
@Test
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 f666f4fdb97..532dd964f4e 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
@@ -34,24 +34,21 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
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 com.google.common.util.concurrent.MoreExecutors;
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;
@@ -68,6 +65,7 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
+import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@@ -114,7 +112,6 @@
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;
@@ -987,16 +984,15 @@ public void testInitWithExistingYamlFiles() throws
IOException {
// Initialize - should load existing files
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())) {
+ // Static mocks are thread-local, so run the loader tasks on this thread
while counting actual parses.
+ try (MockedStatic<Executors> executors = mockStatic(Executors.class);
+ MockedStatic<OmSnapshotLocalDataYaml> yaml =
mockStatic(OmSnapshotLocalDataYaml.class, CALLS_REAL_METHODS)) {
+ executors.when(() -> Executors.newFixedThreadPool(anyInt(), any()))
+ .thenReturn(MoreExecutors.newDirectExecutorService());
+ yaml.when(() ->
OmSnapshotLocalDataYaml.loadWithoutSstFiles(any(File.class))).thenAnswer(load
-> {
+ yamlLoads.incrementAndGet();
+ return load.callRealMethod();
+ });
localDataManager = getNewOmSnapshotLocalDataManager();
}
// snapshotId sorts first and depends on previousSnapshotId, but each YAML
must still be parsed only once.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]