Tejaskriya commented on code in PR #8016:
URL: https://github.com/apache/ozone/pull/8016#discussion_r2057708586


##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdiff/RocksDBCheckpointDiffer.java:
##########
@@ -758,22 +681,27 @@ public void loadAllCompactionLogs() {
     synchronized (this) {
       preconditionChecksForLoadAllCompactionLogs();
       addEntriesFromLogFilesToDagAndCompactionLogTable();

Review Comment:
   Added to the java doc for `loadAllCompactionLogs()`



##########
hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdiff/CompactionDag.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.ozone.rocksdiff;
+
+import com.google.common.graph.GraphBuilder;
+import com.google.common.graph.MutableGraph;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import org.apache.ozone.compaction.log.CompactionFileInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Helper methods for creating compaction DAGs.
+ */
+public class CompactionDag {
+  private static final Logger LOG = 
LoggerFactory.getLogger(CompactionDag.class);
+
+  private final ConcurrentMap<String, CompactionNode> compactionNodeMap = new 
ConcurrentHashMap<>();
+  private final MutableGraph<CompactionNode> forwardCompactionDAG = 
GraphBuilder.directed().build();
+  private final MutableGraph<CompactionNode> backwardCompactionDAG = 
GraphBuilder.directed().build();
+
+  private CompactionNode addNodeToDAG(String file, long seqNum, String 
startKey, String endKey, String columnFamily) {
+    CompactionNode fileNode = new CompactionNode(file, seqNum, startKey, 
endKey, columnFamily);
+    backwardCompactionDAG.addNode(fileNode);
+    forwardCompactionDAG.addNode(fileNode);
+    return fileNode;
+  }
+
+  /**
+   * Populate the compaction DAG with input and output SST files lists.
+   *
+   * @param inputFiles  List of compaction input files.
+   * @param outputFiles List of compaction output files.
+   * @param seqNum      DB transaction sequence number.
+   */
+  public void populateCompactionDAG(List<CompactionFileInfo> inputFiles,
+                                    List<CompactionFileInfo> outputFiles,
+                                    long seqNum) {
+
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Input files: {} -> Output files: {}", inputFiles, 
outputFiles);
+    }
+
+    for (CompactionFileInfo outfile : outputFiles) {
+      final CompactionNode outfileNode = 
compactionNodeMap.computeIfAbsent(outfile.getFileName(),
+          file -> addNodeToDAG(file, seqNum, outfile.getStartKey(), 
outfile.getEndKey(), outfile.getColumnFamily()));
+
+      for (CompactionFileInfo infile : inputFiles) {
+        final CompactionNode infileNode = 
compactionNodeMap.computeIfAbsent(infile.getFileName(),
+            file -> addNodeToDAG(file, seqNum, infile.getStartKey(), 
infile.getEndKey(), infile.getColumnFamily()));
+
+        // Draw the edges
+        if (!Objects.equals(outfileNode.getFileName(), 
infileNode.getFileName())) {
+          forwardCompactionDAG.putEdge(outfileNode, infileNode);
+          backwardCompactionDAG.putEdge(infileNode, outfileNode);
+        }
+      }
+    }
+  }
+
+  public Set<String> pruneNodesFromDag(Set<CompactionNode> nodesToRemove) {
+    pruneBackwardDag(backwardCompactionDAG, nodesToRemove);
+    Set<String> sstFilesPruned = pruneForwardDag(forwardCompactionDAG, 
nodesToRemove);
+    // Remove SST file nodes from compactionNodeMap too,
+    // since those nodes won't be needed after clean up.
+    nodesToRemove.forEach(compactionNodeMap::remove);
+    return sstFilesPruned;
+  }
+
+  /**
+   * Prunes backward DAG's upstream from the level, that needs to be removed.
+   */
+  Set<String> pruneBackwardDag(MutableGraph<CompactionNode> backwardDag,
+                               Set<CompactionNode> startNodes) {
+    Set<String> removedFiles = new HashSet<>();
+    Set<CompactionNode> currentLevel = startNodes;
+
+    synchronized (this) {

Review Comment:
   We have the following synchronized block in 
RocksDBCheckpointDiffer#pruneSstFileNodesFromDag
   ```
   synchronized (this) {
         return compactionDag.pruneNodesFromDag(startNodes);
       }
   ```
   I have removed the synchronized blocks present in CompactionDag now



-- 
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