smengcl commented on code in PR #8555: URL: https://github.com/apache/ozone/pull/8555#discussion_r2164450484
########## 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 Review Comment: done -- 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: issues-unsubscr...@ozone.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For additional commands, e-mail: issues-h...@ozone.apache.org