SaketaChalamchala commented on code in PR #10778: URL: https://github.com/apache/ozone/pull/10778#discussion_r3762301932
########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapDiffDependencyGraph.java: ########## @@ -0,0 +1,656 @@ +/* + * 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.snapshot; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.apache.hadoop.hdfs.protocol.SnapshotDiffReport.DiffReportEntry; +import org.apache.hadoop.hdfs.protocol.SnapshotDiffReport.DiffType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Directed graph of snapshot diff entries and Kahn topological sort for + * dependency-ordered report emission. + * + * <p>Dependency rules encoded by edges (edge {@code u -> v} means {@code u} + * must appear before {@code v}): + * <ul> + * <li>Descendant DELETE before ancestor DELETE or RENAME(source), using + * strict source-path prefixes when intermediate directories are omitted + * from the report.</li> + * <li>Descendant RENAME or MODIFY before ancestor DELETE or RENAME(source) + * on a strict source-path prefix. CREATE is omitted because its source + * path is a to-snapshot path, not a from-snapshot path.</li> + * <li>Ancestor CREATE/RENAME(target) before descendant CREATE/RENAME/MODIFY + * on a strict to-snapshot path prefix.</li> + * <li>DELETE before CREATE/RENAME(target) that targets the same path.</li> + * <li>RENAME(source) before CREATE that reuses the rename source path.</li> + * <li>RENAME(source) before RENAME(target) that reuses the same path, so a + * path is freed before another rename occupies it.</li> + * <li>For the same object, an entry at the RENAME source path before the + * RENAME, and the RENAME before an entry at its target path.</li> + * <li>A RENAME target path cannot match a CREATE path in the same diff + * report; such input is rejected with {@link IllegalStateException}.</li> + * </ul> + */ +public final class SnapDiffDependencyGraph { + + private static final Logger LOG = + LoggerFactory.getLogger(SnapDiffDependencyGraph.class); + + private static final int INITIAL_EDGE_CAPACITY = 16; + private static final int[] EMPTY_INT_ARRAY = new int[0]; + private static final int MAX_CYCLE_ENTRIES_IN_MESSAGE = 16; + // Defensive JVM-side ceiling on the growable long[] edge buffer used during + // construction (see grownCapacity). Hotspot/OpenJDK cap the largest array a + // little below Integer.MAX_VALUE; going any higher throws OutOfMemoryError + // with an unhelpful message. + private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + private static final String PATH_SEPARATOR = OM_KEY_PREFIX; + + private final List<SnapDiffDependencyEntry> nodes = new ArrayList<>(); + + // Edges collected during construction, each encoded as (from << 32 | to). + // Deduplicated and compacted into the CSR arrays below by buildCsr(), then + // released so the graph keeps only the primitive adjacency. + private long[] edges; + private int edgeCount; + + // Compressed sparse row adjacency. The out-edges of node i are the targets + // adjTargets[adjOffsets[i] .. adjOffsets[i + 1]). + private int[] adjOffsets; + private int[] adjTargets; + private int[] inDegree; + + // Construction-only grouping of node ids by objectId for intra-object + // RENAME ordering. Only objectIds that have a RENAME entry are tracked; + private long[] groupObjectIds; + private int[] groupOffsets; + private int[] groupNodes; + + /** + * @throws IllegalStateException if entries contain a RENAME target path that + * matches a CREATE path, or if dependency edges form a cycle + */ 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: [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]
