hemantk-12 commented on code in PR #8016: URL: https://github.com/apache/ozone/pull/8016#discussion_r2018938837
########## hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/compaction/log/RocksDBConsts.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.compaction.log; + +import com.google.common.collect.ImmutableSet; +import java.util.Set; + +/** + * Constants related to rocksDB compaction. + */ +public final class RocksDBConsts { Review Comment: 1. This separate file is not needed if we don't move compaction-log file code to CompactionDagHelper. 2. If this class is needed, rename it to `RocksDBDifferConsts`. ########## hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/compaction/log/CompactionDagHelper.java: ########## @@ -0,0 +1,254 @@ +/* + * 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.compaction.log; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_COMMENT_LINE_PREFIX; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_ENTRY_FILE_DELIMITER; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_ENTRY_INPUT_OUTPUT_FILES_DELIMITER; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_ENTRY_LINE_PREFIX; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_FILE_NAME_SUFFIX; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_SEQ_NUM_LINE_PREFIX; +import static org.apache.ozone.compaction.log.RocksDBConsts.LONG_MAX_STR_LEN; +import static org.apache.ozone.compaction.log.RocksDBConsts.SPACE_DELIMITER; +import static org.apache.ozone.compaction.log.RocksDBConsts.SST_FILE_EXTENSION; + +import com.google.common.base.Preconditions; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.hadoop.hdds.utils.db.managed.ManagedOptions; +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB; +import org.apache.hadoop.hdds.utils.db.managed.ManagedSstFileReader; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.RocksDBException; +import org.rocksdb.TableProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility methods to populate compactionLogTable from a compaction-log file. + */ +public final class CompactionDagHelper { + private static final Logger LOG = + LoggerFactory.getLogger(CompactionDagHelper.class); + + /** + * Used during DAG construction. + */ + private long reconstructionSnapshotCreationTime; + private String reconstructionCompactionReason; + private final String compactionLogDir; + private ManagedRocksDB activeRocksDB; + private ColumnFamilyHandle compactionLogTableCFHandle; + + public CompactionDagHelper(String compactLogDir, ManagedRocksDB db, ColumnFamilyHandle cf) { + compactionLogDir = compactLogDir; + activeRocksDB = db; + compactionLogTableCFHandle = cf; + reconstructionSnapshotCreationTime = 0L; + reconstructionCompactionReason = null; + } + + public void setActiveRocksDB(ManagedRocksDB activeRocksDB) { + this.activeRocksDB = activeRocksDB; + } + + public void setCompactionLogTableCFHandle(ColumnFamilyHandle compactionLogTableCFHandle) { + this.compactionLogTableCFHandle = compactionLogTableCFHandle; + } + + public void addEntriesFromLogFilesToCompactionLogTable() { Review Comment: Let's not move this or anything related to compaction-log file. During the start-up, RockDBCheckpointDiffer populates the compaction-log table from the compaction-log file and clean-up these files. So it is no-op to do it for `CompactionLogDagPrinter`. ########## hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/compaction/log/CompactionDagHelper.java: ########## @@ -0,0 +1,254 @@ +/* + * 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.compaction.log; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_COMMENT_LINE_PREFIX; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_ENTRY_FILE_DELIMITER; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_ENTRY_INPUT_OUTPUT_FILES_DELIMITER; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_ENTRY_LINE_PREFIX; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_FILE_NAME_SUFFIX; +import static org.apache.ozone.compaction.log.RocksDBConsts.COMPACTION_LOG_SEQ_NUM_LINE_PREFIX; +import static org.apache.ozone.compaction.log.RocksDBConsts.LONG_MAX_STR_LEN; +import static org.apache.ozone.compaction.log.RocksDBConsts.SPACE_DELIMITER; +import static org.apache.ozone.compaction.log.RocksDBConsts.SST_FILE_EXTENSION; + +import com.google.common.base.Preconditions; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.hadoop.hdds.utils.db.managed.ManagedOptions; +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB; +import org.apache.hadoop.hdds.utils.db.managed.ManagedSstFileReader; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.RocksDBException; +import org.rocksdb.TableProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility methods to populate compactionLogTable from a compaction-log file. + */ +public final class CompactionDagHelper { Review Comment: I initially thought this was just helper code in the draft version of the patch, but now it seems to be more than that. Can we consider it merely a wrapper over the `compaction-log` table? If so, I will rename it to `CompactionLogTable` and keep only minimal functions, such as adding and removing an entry from the table. ########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/om/CompactionLogDagPrinter.java: ########## @@ -0,0 +1,202 @@ +/* + * 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.debug.om; + +import static org.apache.hadoop.ozone.OzoneConsts.COMPACTION_LOG_TABLE; + +import com.google.common.graph.GraphBuilder; +import com.google.common.graph.MutableGraph; +import com.google.protobuf.InvalidProtocolBufferException; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.hadoop.hdds.cli.AbstractSubcommand; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos; +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB; +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksIterator; +import org.apache.hadoop.ozone.debug.RocksDBUtils; +import org.apache.ozone.compaction.log.CompactionDagHelper; +import org.apache.ozone.compaction.log.CompactionFileInfo; +import org.apache.ozone.compaction.log.CompactionLogEntry; +import org.apache.ozone.graph.PrintableGraph; +import org.apache.ozone.rocksdiff.CompactionNode; +import org.rocksdb.ColumnFamilyDescriptor; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.RocksDBException; +import picocli.CommandLine; + +/** + * Handler to generate image for current compaction DAG in the OM leader node. + * ozone debug om print-compaction-dag. + */ [email protected]( + name = "print-compaction-dag", + aliases = "pcd", + description = "Create an image of the current compaction log DAG. " + + "This command is an offline command. i.e., it can run on any instance of om.db " + + "and does not require OM to be up.") +public class CompactionLogDagPrinter extends AbstractSubcommand implements Callable<Void> { + + @CommandLine.Option(names = {"-o", "--output-file"}, + required = true, + description = "Path to location at which image will be downloaded. " + + "Should include the image file name with \".png\" extension.") + private String imageLocation; + + @CommandLine.Option(names = {"--db"}, + required = true, + scope = CommandLine.ScopeType.INHERIT, + description = "Path to OM RocksDB") + private String dbPath; + + @CommandLine.Option(names = {"--compaction-log"}, + scope = CommandLine.ScopeType.INHERIT, + description = "Path to compaction-log directory.") + private String compactionLogDir; + + // TODO: Change graphType to enum. + @CommandLine.Option(names = {"-t", "--graph-type"}, + description = "Type of node name to use in the graph image. (optional)\n Accepted values are: \n" + + " FILE_NAME (default) : to use file name as node name in DAG,\n" + + " KEY_SIZE: to show the no. of keys in the file along with file name in the DAG node name,\n" + + " CUMULATIVE_SIZE: to show the cumulative size along with file name in the DAG node name.", + defaultValue = "FILE_NAME") + private String graphType; + + @Override + public Void call() throws Exception { + try { + CreateCompactionDag createCompactionDag = new CreateCompactionDag(dbPath, compactionLogDir); + createCompactionDag.pngPrintMutableGraph(imageLocation, PrintableGraph.GraphType.valueOf(graphType)); + out().println("Graph was generated at '" + imageLocation + "'."); + } catch (RocksDBException ex) { + err().println("Failed to open RocksDB: " + ex); + throw new IOException(ex); + } + return null; + } + + class CreateCompactionDag { + // Hash table to track CompactionNode for a given SST File. + private final ConcurrentHashMap<String, CompactionNode> compactionNodeMap = + new ConcurrentHashMap<>(); + private final MutableGraph<CompactionNode> backwardCompactionDAG = + GraphBuilder.directed().build(); + + private ColumnFamilyHandle compactionLogTableCFHandle; + private ManagedRocksDB activeRocksDB; + private CompactionDagHelper compactionDagHelper; + + CreateCompactionDag(String dbPath, String compactDir) throws RocksDBException { + final List<ColumnFamilyHandle> cfHandleList = new ArrayList<>(); + List<ColumnFamilyDescriptor> cfDescList = RocksDBUtils.getColumnFamilyDescriptors(dbPath); + activeRocksDB = ManagedRocksDB.openReadOnly(dbPath, cfDescList, cfHandleList); + compactionLogTableCFHandle = RocksDBUtils.getColumnFamilyHandle(COMPACTION_LOG_TABLE, cfHandleList); + compactionDagHelper = new CompactionDagHelper(compactDir, activeRocksDB, compactionLogTableCFHandle); + } + + public void pngPrintMutableGraph(String filePath, PrintableGraph.GraphType gType) + throws IOException, RocksDBException { + Objects.requireNonNull(filePath, "Image file path is required."); + Objects.requireNonNull(gType, "Graph type is required."); + + loadAllCompactionLogs(); + + PrintableGraph graph; Review Comment: Can you move [graph](https://github.com/apache/ozone/tree/9d2c7a8ac5de788da83c7b691d3f82a2af02a6b9/hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/graph) to tools package and deprecate the `PrintCompactionLogDag` API and related code? ########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/om/CompactionLogDagPrinter.java: ########## @@ -0,0 +1,202 @@ +/* + * 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.debug.om; + +import static org.apache.hadoop.ozone.OzoneConsts.COMPACTION_LOG_TABLE; + +import com.google.common.graph.GraphBuilder; +import com.google.common.graph.MutableGraph; +import com.google.protobuf.InvalidProtocolBufferException; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.hadoop.hdds.cli.AbstractSubcommand; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos; +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB; +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksIterator; +import org.apache.hadoop.ozone.debug.RocksDBUtils; +import org.apache.ozone.compaction.log.CompactionDagHelper; +import org.apache.ozone.compaction.log.CompactionFileInfo; +import org.apache.ozone.compaction.log.CompactionLogEntry; +import org.apache.ozone.graph.PrintableGraph; +import org.apache.ozone.rocksdiff.CompactionNode; +import org.rocksdb.ColumnFamilyDescriptor; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.RocksDBException; +import picocli.CommandLine; + +/** + * Handler to generate image for current compaction DAG in the OM leader node. + * ozone debug om print-compaction-dag. + */ [email protected]( + name = "print-compaction-dag", + aliases = "pcd", + description = "Create an image of the current compaction log DAG. " + + "This command is an offline command. i.e., it can run on any instance of om.db " + + "and does not require OM to be up.") +public class CompactionLogDagPrinter extends AbstractSubcommand implements Callable<Void> { + + @CommandLine.Option(names = {"-o", "--output-file"}, + required = true, + description = "Path to location at which image will be downloaded. " + + "Should include the image file name with \".png\" extension.") + private String imageLocation; + + @CommandLine.Option(names = {"--db"}, + required = true, + scope = CommandLine.ScopeType.INHERIT, + description = "Path to OM RocksDB") + private String dbPath; + + @CommandLine.Option(names = {"--compaction-log"}, + scope = CommandLine.ScopeType.INHERIT, + description = "Path to compaction-log directory.") + private String compactionLogDir; + + // TODO: Change graphType to enum. + @CommandLine.Option(names = {"-t", "--graph-type"}, + description = "Type of node name to use in the graph image. (optional)\n Accepted values are: \n" + + " FILE_NAME (default) : to use file name as node name in DAG,\n" + + " KEY_SIZE: to show the no. of keys in the file along with file name in the DAG node name,\n" + + " CUMULATIVE_SIZE: to show the cumulative size along with file name in the DAG node name.", + defaultValue = "FILE_NAME") + private String graphType; + + @Override + public Void call() throws Exception { + try { + CreateCompactionDag createCompactionDag = new CreateCompactionDag(dbPath, compactionLogDir); + createCompactionDag.pngPrintMutableGraph(imageLocation, PrintableGraph.GraphType.valueOf(graphType)); + out().println("Graph was generated at '" + imageLocation + "'."); + } catch (RocksDBException ex) { + err().println("Failed to open RocksDB: " + ex); + throw new IOException(ex); + } + return null; + } + + class CreateCompactionDag { + // Hash table to track CompactionNode for a given SST File. + private final ConcurrentHashMap<String, CompactionNode> compactionNodeMap = + new ConcurrentHashMap<>(); + private final MutableGraph<CompactionNode> backwardCompactionDAG = + GraphBuilder.directed().build(); + + private ColumnFamilyHandle compactionLogTableCFHandle; + private ManagedRocksDB activeRocksDB; + private CompactionDagHelper compactionDagHelper; + + CreateCompactionDag(String dbPath, String compactDir) throws RocksDBException { + final List<ColumnFamilyHandle> cfHandleList = new ArrayList<>(); + List<ColumnFamilyDescriptor> cfDescList = RocksDBUtils.getColumnFamilyDescriptors(dbPath); + activeRocksDB = ManagedRocksDB.openReadOnly(dbPath, cfDescList, cfHandleList); + compactionLogTableCFHandle = RocksDBUtils.getColumnFamilyHandle(COMPACTION_LOG_TABLE, cfHandleList); + compactionDagHelper = new CompactionDagHelper(compactDir, activeRocksDB, compactionLogTableCFHandle); + } + + public void pngPrintMutableGraph(String filePath, PrintableGraph.GraphType gType) + throws IOException, RocksDBException { + Objects.requireNonNull(filePath, "Image file path is required."); + Objects.requireNonNull(gType, "Graph type is required."); + + loadAllCompactionLogs(); + + PrintableGraph graph; + graph = new PrintableGraph(backwardCompactionDAG, gType); + graph.generateImage(filePath); + } + + public void loadAllCompactionLogs() { + compactionDagHelper.preconditionChecksForLoadAllCompactionLogs(false); Review Comment: Why are we doing `preconditionChecksForLoadAllCompactionLogs` twice, one here with `false` value and another one inside `addEntriesFromLogFilesToCompactionLogTable` with `true` value? ########## hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdiff/RocksDBCheckpointDiffer.java: ########## @@ -322,10 +271,6 @@ public void close() { private final MutableGraph<CompactionNode> backwardCompactionDAG = GraphBuilder.directed().build(); - public static final Integer DEBUG_DAG_BUILD_UP = 2; - public static final Integer DEBUG_DAG_TRAVERSAL = 3; - public static final Integer DEBUG_DAG_LIVE_NODES = 4; - public static final Integer DEBUG_READ_ALL_DB_KEYS = 5; private static final HashSet<Integer> DEBUG_LEVEL = new HashSet<>(); Review Comment: nit: can you please move this test util code to `TestRocksDBCheckpointDiffer`? I don't know why we have it here. -- 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]
