SaketaChalamchala commented on code in PR #10778: URL: https://github.com/apache/ozone/pull/10778#discussion_r3725184489
########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapDiffDependencyGraph.java: ########## @@ -0,0 +1,569 @@ +/* + * 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 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.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 non-delete before ancestor DELETE on a strict source-path + * prefix.</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]; + // Hotspot/OpenJDK cap the largest array a little below Integer.MAX_VALUE. + // Node and edge counts are assumed to fit in int; the configured changed-key + // limit (one billion) is well below this bound. + private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + private static final char PATH_SEPARATOR = '/'; + + 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 = new long[INITIAL_EDGE_CAPACITY]; + 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. Released once edges are built. + 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 + */ + public SnapDiffDependencyGraph(List<SnapDiffDependencyEntry> entries) { + nodes.addAll(entries); + buildDependencyEdges(); + buildCsr(); + } + + /** + * Returns entries in dependency order using Kahn's algorithm. + * + * @return topologically sorted dependency entries + * @throws IllegalStateException if the graph contains a cycle + */ + public List<SnapDiffDependencyEntry> getOrderedEntries() { + int nodeCount = nodes.size(); + // Work on a local copy of the in-degrees so the method is idempotent and + // does not mutate the graph's shared state. + int[] remainingInDegree = Arrays.copyOf(inDegree, nodeCount); + // Split the ready set into two ring buffers so that, among nodes whose + // dependencies are already satisfied, DELETEs are emitted before other + // types. This retains the baseline "deletes first" ordering wherever the + // dependency edges leave the order free. Each node is enqueued at most + // once, so buffers sized to the node count are large enough. + int[] deleteReady = new int[nodeCount]; + int[] otherReady = new int[nodeCount]; Review Comment: Yes, the memory footprint can become large for large diffs. The 1B changed keys limit is currently being checked against the total estimated keys in the delta file set to decide whether the snapshot diff can be accepted/rejected. I propose we add another guardrail limiting the number of actual diff entries to 1M in order to be eligible for in-memory dependency ordering enforced in [HDDS-15391](https://issues.apache.org/jira/browse/HDDS-15391). (This limit may also be useful in prior stages of the diff to decide whether to spill to disk). As a follow-up task enable RocksDB backed graph for larger diffs and fallback to current order meanwhile. What do you think? More memory optimizations in the latest commit: - Free cached path strings after edge building. - Presize the four path-index HashMaps to their exact per-category counts. - Presize the edges buffer to max(INITIAL_EDGE_CAPACITY, nodeCount) so realistic graphs (edge density 3–8 per node) skip most doubling copies. - Rework buildObjectIdGroups to track only objectIds that have a RENAME (the only objectIds that can pick up intra-object edges). -- 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]
