This is an automated email from the ASF dual-hosted git repository.
adoroszlai 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 4531bb94439 HDDS-17098. Stop reading snapshot local data YAML before
SST file lists at OM startup (#11597)
4531bb94439 is described below
commit 4531bb94439f32574f153a8ed930a7a248205fdb
Author: Siyao Meng <[email protected]>
AuthorDate: Thu Nov 12 22:14:33 2026 -0800
HDDS-17098. Stop reading snapshot local data YAML before SST file lists at
OM startup (#11597)
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../java/org/apache/hadoop/ozone/OzoneConsts.java | 1 +
.../hadoop/ozone/om/OmSnapshotLocalData.java | 4 +
.../hadoop/ozone/om/OmSnapshotLocalDataYaml.java | 101 +++++++++++++++++++--
.../ozone/om/TestOmSnapshotLocalDataYaml.java | 87 ++++++++++++++++--
4 files changed, 179 insertions(+), 14 deletions(-)
diff --git
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConsts.java
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConsts.java
index 9d1d4a3ef4f..317a9b50e96 100644
--- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConsts.java
+++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConsts.java
@@ -222,6 +222,7 @@ public final class OzoneConsts {
public static final String OM_SLD_VERSION_SST_FILE_INFO =
"versionSstFileInfos";
public static final String OM_SLD_SNAP_ID = "snapshotId";
public static final String OM_SLD_PREV_SNAP_ID = "previousSnapshotId";
+ public static final String OM_SLD_PREV_SNAP_VERSIONS =
"previousSnapshotVersions";
public static final String OM_SLD_VERSION_META_SST_FILES = "sstFiles";
public static final String OM_SLD_VERSION_META_PREV_SNAP_VERSION =
"previousSnapshotVersion";
public static final String OM_SST_FILE_INFO_FILE_NAME = "fileName";
diff --git
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalData.java
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalData.java
index a74416d8690..165c630996e 100644
---
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalData.java
+++
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalData.java
@@ -242,6 +242,10 @@ public void setChecksumTo0ByteArray() {
this.checksum = DUMMY_CHECKSUM;
}
+ boolean hasDummyChecksum() {
+ return DUMMY_CHECKSUM.equals(checksum);
+ }
+
/**
* Compute and set checksum for the snapshot data.
* @param yaml Yaml instance for serialization
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 ad559ee1f1c..19dd11e81d0 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
@@ -20,11 +20,14 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.annotation.Annotation;
import java.nio.file.Files;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
import java.util.UUID;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
@@ -84,6 +87,8 @@ 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.
+ * If the file has the previousSnapshotVersions summary, parsing stops
before the SST file lists, so the rest of
+ * the file is not validated.
* 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 {
@@ -111,7 +116,14 @@ public static OmSnapshotLocalData loadWithoutSstFiles(File
yamlFile) throws IOEx
case OzoneConsts.OM_SLD_VERSION:
version = nextScalar(parser);
break;
+ case OzoneConsts.OM_SLD_PREV_SNAP_VERSIONS:
+ versionMetas = nextPreviousSnapshotVersions(parser);
+ break;
case OzoneConsts.OM_SLD_VERSION_SST_FILE_INFO:
+ if (versionMetas != null) {
+ // Keys are written in sorted order, so every field needed here
precedes the SST file lists.
+ return newLocalData(snapId, prevSnapId, version, versionMetas);
+ }
versionMetas = nextVersionMetas(parser);
break;
default:
@@ -121,18 +133,34 @@ public static OmSnapshotLocalData
loadWithoutSstFiles(File yamlFile) throws IOEx
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;
+ return newLocalData(snapId, prevSnapId, version, versionMetas);
} catch (RuntimeException e) {
throw new IOException("Failed to load " + yamlFile, e);
}
}
+ private static OmSnapshotLocalData newLocalData(String snapId, String
prevSnapId, String version,
+ Map<Integer, VersionMeta> versionMetas) throws IOException {
+ 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;
+ }
+
+ private static Map<Integer, VersionMeta> nextPreviousSnapshotVersions(Parser
parser) throws IOException {
+ Map<Integer, VersionMeta> versionMetas = new LinkedHashMap<>();
+ nextEvent(parser, MappingStartEvent.class);
+ while (!parser.checkEvent(Event.ID.MappingEnd)) {
+ versionMetas.put(Integer.parseInt(nextScalar(parser)),
+ new VersionMeta(Integer.parseInt(nextScalar(parser)),
Collections.emptyList()));
+ }
+ parser.getEvent();
+ return versionMetas;
+ }
+
private static Map<Integer, VersionMeta> nextVersionMetas(Parser parser)
throws IOException {
Map<Integer, VersionMeta> versionMetas = new LinkedHashMap<>();
nextEvent(parser, MappingStartEvent.class);
@@ -192,6 +220,50 @@ private static String require(String value, String key)
throws IOException {
*/
private static class OmSnapshotLocalDataRepresenter extends Representer {
+ /**
+ * Map of version to previousSnapshotVersion, derived from
versionSstFileInfos. It is left out while the checksum
+ * is computed (dummy checksum), so the checksum is the same with or
without it.
+ */
+ private static final Property PREVIOUS_SNAPSHOT_VERSIONS =
+ new Property(OzoneConsts.OM_SLD_PREV_SNAP_VERSIONS, Map.class) {
+ @Override
+ public Object get(Object bean) {
+ OmSnapshotLocalData data = (OmSnapshotLocalData) bean;
+ if (data.hasDummyChecksum()) {
+ return null;
+ }
+ Map<Integer, Integer> previousSnapshotVersions = new
LinkedHashMap<>();
+ data.getVersionSstFileInfos().forEach((version, meta) ->
+ previousSnapshotVersions.put(version,
meta.getPreviousSnapshotVersion()));
+ return previousSnapshotVersions;
+ }
+
+ @Override
+ public void set(Object bean, Object value) {
+ throw new UnsupportedOperationException(getName() + " is derived
from versionSstFileInfos");
+ }
+
+ @Override
+ public boolean isWritable() {
+ return false;
+ }
+
+ @Override
+ public Class<?>[] getActualTypeArguments() {
+ return null;
+ }
+
+ @Override
+ public List<Annotation> getAnnotations() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public <A extends Annotation> A getAnnotation(Class<A>
annotationType) {
+ return null;
+ }
+ };
+
OmSnapshotLocalDataRepresenter(DumperOptions options) {
super(options);
this.addClassTag(OmSnapshotLocalData.class, SNAPSHOT_YAML_TAG);
@@ -234,6 +306,21 @@ public Node representData(Object data) {
}
+ /**
+ * Adds previousSnapshotVersions, a summary of versionSstFileInfos that
sorts before it, so that
+ * {@link OmSnapshotLocalDataYaml#loadWithoutSstFiles} can stop parsing
before the SST file lists.
+ */
+ @Override
+ protected Set<Property> getProperties(Class<?> type) {
+ Set<Property> properties = super.getProperties(type);
+ if (type != OmSnapshotLocalData.class) {
+ return properties;
+ }
+ Set<Property> withSummary = new TreeSet<>(properties);
+ withSummary.add(PREVIOUS_SNAPSHOT_VERSIONS);
+ return withSummary;
+ }
+
/**
* Omit properties with null value.
*/
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 2b43ed47067..9c3b44bf20d 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,9 +231,88 @@ public void testUpdateSnapshotDataFile() throws
IOException {
defraggedFiles.get(45));
}
+ /**
+ * Writes a copy of the YAML without previousSnapshotVersions, as written
before it was added.
+ */
+ private File writeWithoutPreviousSnapshotVersions(File yamlFile) throws
IOException {
+ File oldLayout = new File(testRoot, "old-" + yamlFile.getName());
+ String content = FileUtils.readFileToString(yamlFile,
StandardCharsets.UTF_8);
+ String oldContent = content.replaceFirst("(?m)^" +
OzoneConsts.OM_SLD_PREV_SNAP_VERSIONS + ":.*\\n( .*\\n)*", "");
+
assertThat(oldContent).isNotEqualTo(content).doesNotContain(OzoneConsts.OM_SLD_PREV_SNAP_VERSIONS);
+ FileUtils.writeStringToFile(oldLayout, oldContent, StandardCharsets.UTF_8);
+ return oldLayout;
+ }
+
+ /**
+ * Writes a copy of the YAML cut at versionSstFileInfos, followed by an
unterminated mapping.
+ */
+ private File writeTruncatedAtSstFiles(File yamlFile) throws IOException {
+ File truncated = new File(testRoot, "truncated-" + yamlFile.getName());
+ String content = FileUtils.readFileToString(yamlFile,
StandardCharsets.UTF_8);
+ String key = OzoneConsts.OM_SLD_VERSION_SST_FILE_INFO + ":";
+ FileUtils.writeStringToFile(truncated, content.substring(0,
content.indexOf(key)) + key + " {\n",
+ StandardCharsets.UTF_8);
+ return truncated;
+ }
+
+ @Test
+ public void testPreviousSnapshotVersionsWrittenBeforeSstFiles() throws
IOException {
+ File yamlFile = writeToYaml(UUID.randomUUID(), "snapshot8",
TransactionInfo.valueOf(1, 2)).getLeft();
+ String content = FileUtils.readFileToString(yamlFile,
StandardCharsets.UTF_8);
+ int sstFilesIndex = content.indexOf("\n" +
OzoneConsts.OM_SLD_VERSION_SST_FILE_INFO + ":");
+ assertThat(sstFilesIndex).isPositive();
+ for (String key : asList(OzoneConsts.OM_SLD_SNAP_ID,
OzoneConsts.OM_SLD_PREV_SNAP_ID, OzoneConsts.OM_SLD_VERSION,
+ OzoneConsts.OM_SLD_PREV_SNAP_VERSIONS)) {
+ assertThat(content.indexOf("\n" + key +
":")).as(key).isPositive().isLessThan(sstFilesIndex);
+ }
+
+ // Loading only the part before the SST file lists gives the same result
as loading the whole file.
+ File oldLayout = writeWithoutPreviousSnapshotVersions(yamlFile);
+ OmSnapshotLocalData expected =
OmSnapshotLocalDataYaml.loadWithoutSstFiles(oldLayout);
+ OmSnapshotLocalData loaded =
OmSnapshotLocalDataYaml.loadWithoutSstFiles(writeTruncatedAtSstFiles(yamlFile));
+ assertEquals(expected.getSnapshotId(), loaded.getSnapshotId());
+ assertEquals(expected.getPreviousSnapshotId(),
loaded.getPreviousSnapshotId());
+ assertEquals(expected.getVersion(), loaded.getVersion());
+ assertEquals(expected.getVersionSstFileInfos(),
loaded.getVersionSstFileInfos());
+
+ // Without previousSnapshotVersions the SST file lists are parsed.
+ File oldTruncated = writeTruncatedAtSstFiles(oldLayout);
+ assertThrows(IOException.class, () ->
OmSnapshotLocalDataYaml.loadWithoutSstFiles(oldTruncated));
+ }
+
+ @Test
+ public void testChecksumWithAndWithoutPreviousSnapshotVersions() throws
IOException {
+ File yamlFile = writeToYaml(UUID.randomUUID(), "snapshot9",
TransactionInfo.valueOf(1, 2)).getLeft();
+ File oldLayout = writeWithoutPreviousSnapshotVersions(yamlFile);
+ OmSnapshotLocalData newData = omSnapshotLocalDataSerializer.load(yamlFile);
+ OmSnapshotLocalData oldData =
omSnapshotLocalDataSerializer.load(oldLayout);
+
+ // previousSnapshotVersions is not part of the checksum, so a file
verifies the same with or without it.
+ assertEquals(newData.getChecksum(), oldData.getChecksum());
+ assertTrue(omSnapshotLocalDataSerializer.verifyChecksum(newData));
+ assertTrue(omSnapshotLocalDataSerializer.verifyChecksum(oldData));
+
+ // Saving a file loaded without previousSnapshotVersions adds it back.
+ omSnapshotLocalDataSerializer.save(oldLayout, oldData);
+ assertEquals(FileUtils.readFileToString(yamlFile, StandardCharsets.UTF_8),
+ FileUtils.readFileToString(oldLayout, StandardCharsets.UTF_8));
+ }
+
@Test
public void testLoadWithoutSstFiles() throws IOException {
- File yamlFile = writeToYaml(UUID.randomUUID(), "snapshot7",
TransactionInfo.valueOf(1, 2)).getLeft();
+ File newLayout = writeToYaml(UUID.randomUUID(), "snapshot7",
TransactionInfo.valueOf(1, 2)).getLeft();
+ for (File yamlFile : asList(newLayout,
writeWithoutPreviousSnapshotVersions(newLayout))) {
+ assertLoadWithoutSstFiles(yamlFile);
+ }
+
+ File noSnapshotId = new File(testRoot, "noSnapshotId.yaml");
+ FileUtils.writeStringToFile(noSnapshotId,
FileUtils.readFileToString(newLayout, 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);
+ }
+
+ private void assertLoadWithoutSstFiles(File yamlFile) throws IOException {
OmSnapshotLocalData fullData =
omSnapshotLocalDataSerializer.load(yamlFile);
OmSnapshotLocalData metaData =
OmSnapshotLocalDataYaml.loadWithoutSstFiles(yamlFile);
@@ -248,12 +327,6 @@ public void testLoadWithoutSstFiles() throws IOException {
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
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]