smengcl commented on code in PR #8555:
URL: https://github.com/apache/ozone/pull/8555#discussion_r2151110209


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshotLocalDataYaml.java:
##########
@@ -0,0 +1,249 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.om;
+
+import com.google.common.base.Preconditions;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.util.List;
+import java.util.Map;
+import org.apache.hadoop.hdds.server.YamlUtils;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.yaml.snakeyaml.DumperOptions;
+import org.yaml.snakeyaml.LoaderOptions;
+import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.constructor.AbstractConstruct;
+import org.yaml.snakeyaml.constructor.SafeConstructor;
+import org.yaml.snakeyaml.error.YAMLException;
+import org.yaml.snakeyaml.introspector.BeanAccess;
+import org.yaml.snakeyaml.introspector.PropertyUtils;
+import org.yaml.snakeyaml.nodes.MappingNode;
+import org.yaml.snakeyaml.nodes.Node;
+import org.yaml.snakeyaml.nodes.Tag;
+import org.yaml.snakeyaml.representer.Representer;
+
+/**
+ * Class for creating and reading snapshot local properties / data YAML files.
+ * Checksum of the YAML fields are computed and stored in the YAML file 
transparently to callers.
+ * Inspired by org.apache.hadoop.ozone.container.common.impl.ContainerDataYaml
+ */
+public final class OmSnapshotLocalDataYaml extends OmSnapshotLocalData {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(OmSnapshotLocalDataYaml.class);
+
+  public static final Tag SNAPSHOT_YAML_TAG = new Tag("OmSnapshotLocalData");
+
+  /**
+   * Creates a new OmSnapshotLocalDataYaml with default values.
+   */
+  public OmSnapshotLocalDataYaml() {
+    super();
+  }
+
+  /**
+   * Copy constructor to create a deep copy.
+   * @param source The source OmSnapshotLocalData to copy from
+   */
+  public OmSnapshotLocalDataYaml(OmSnapshotLocalData source) {
+    super(source);
+  }
+
+  /**
+   * Verifies the checksum of the snapshot data.
+   * @param snapshotData The snapshot data to verify
+   * @return true if the checksum is valid, false otherwise
+   * @throws IOException if there's an error computing the checksum
+   */
+  public static boolean verifyChecksum(OmSnapshotLocalData snapshotData)
+      throws IOException {
+    Preconditions.checkNotNull(snapshotData, "snapshotData cannot be null");
+
+    // Get the stored checksum
+    String storedChecksum = snapshotData.getChecksum();
+    if (storedChecksum == null) {
+      LOG.warn("No checksum found in snapshot data for verification");
+      return false;
+    }
+
+    // Create a copy of the snapshot data for computing checksum
+    OmSnapshotLocalDataYaml snapshotDataCopy = new 
OmSnapshotLocalDataYaml(snapshotData);
+
+    // Clear the existing checksum in the copy
+    snapshotDataCopy.setChecksum(null);
+
+    // Get the YAML representation
+    final Yaml yaml = getYamlForSnapshotLocalData();
+
+    // Compute new checksum
+    snapshotDataCopy.computeAndSetChecksum(yaml);
+
+    // Compare the stored and computed checksums
+    String computedChecksum = snapshotDataCopy.getChecksum();
+    boolean isValid = storedChecksum.equals(computedChecksum);
+
+    if (!isValid) {
+      LOG.warn("Checksum verification failed for snapshot local data. " +
+          "Stored: {}, Computed: {}", storedChecksum, computedChecksum);
+    }
+
+    return isValid;
+  }
+
+  /**
+   * Constructor class for OmSnapshotLocalData.
+   * This is used when parsing YAML files into OmSnapshotLocalDataYaml objects.
+   */
+  private static class SnapshotLocalDataConstructor extends SafeConstructor {
+    SnapshotLocalDataConstructor() {
+      super(new LoaderOptions());
+      //Adding our own specific constructors for tags.
+      this.yamlConstructors.put(SNAPSHOT_YAML_TAG, new 
ConstructSnapshotLocalData());
+    }
+
+    private final class ConstructSnapshotLocalData extends AbstractConstruct {
+      @SuppressWarnings("unchecked")
+      @Override
+      public Object construct(Node node) {
+        MappingNode mnode = (MappingNode) node;
+        Map<Object, Object> nodes = constructMapping(mnode);
+
+        OmSnapshotLocalDataYaml snapshotData = new OmSnapshotLocalDataYaml();
+
+        // Set fields from parsed YAML
+        snapshotData.setSstFiltered((Boolean) 
nodes.getOrDefault(OzoneConsts.IS_SST_FILTERED, false));
+
+        Map<String, List<String>> uncompactedSSTFileList =
+            (Map<String, List<String>>) 
nodes.get(OzoneConsts.UNCOMPACTED_SST_FILE_LIST);
+        if (uncompactedSSTFileList != null) {
+          snapshotData.setUncompactedSSTFileList(uncompactedSSTFileList);
+        }
+
+        snapshotData.setLastCompactionTime((Long) 
nodes.getOrDefault(OzoneConsts.LAST_COMPACTION_TIME, -1L));
+        snapshotData.setNeedsCompaction((Boolean) 
nodes.getOrDefault(OzoneConsts.NEEDS_COMPACTION, false));
+
+        Map<Integer, Map<String, List<String>>> compactedSSTFileList =
+            (Map<Integer, Map<String, List<String>>>) 
nodes.get(OzoneConsts.COMPACTED_SST_FILE_LIST);
+        if (compactedSSTFileList != null) {
+          snapshotData.setCompactedSSTFileList(compactedSSTFileList);
+        }
+
+        String checksum = (String) nodes.get(OzoneConsts.CHECKSUM);
+        if (checksum != null) {
+          snapshotData.setChecksum(checksum);
+        }
+
+        return snapshotData;
+      }
+    }
+  }
+
+  /**
+   * Returns the YAML representation of this object as a String
+   * (without triggering checksum computation or persistence).
+   * @return YAML string representation
+   */
+  public String getYaml() {
+    final Yaml yaml = getYamlForSnapshotLocalData();
+    return yaml.dump(this);
+  }
+
+  /**
+   * Computes checksum (stored in this object), and writes this object to a 
YAML file.
+   * @param yamlFile The file to write to
+   * @throws IOException If there's an error writing to the file
+   */
+  public void writeToYaml(File yamlFile) throws IOException {

Review Comment:
   Yes. We might use existing `SNAPSHOT_DB_LOCK` or a new 
`SNAPSHOT_LOCAL_DATA_LOCK` lock to lock it during read/write but that would be 
up to the caller to implement that. It is out of the scope of this PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to