ashishkumar50 commented on code in PR #9469:
URL: https://github.com/apache/ozone/pull/9469#discussion_r2605304892


##########
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/om/ContainerToKeyMapping.java:
##########
@@ -0,0 +1,344 @@
+/*
+ * 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.OM_KEY_PREFIX;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.stream.Collectors;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.hadoop.hdds.cli.AbstractSubcommand;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.utils.db.DBColumnFamilyDefinition;
+import org.apache.hadoop.hdds.utils.db.DBStore;
+import org.apache.hadoop.hdds.utils.db.DBStoreBuilder;
+import org.apache.hadoop.hdds.utils.db.LongCodec;
+import org.apache.hadoop.hdds.utils.db.StringCodec;
+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.hadoop.ozone.om.codec.OMDBDefinition;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmVolumeArgs;
+import org.rocksdb.ColumnFamilyDescriptor;
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDBException;
+import picocli.CommandLine;
+
+/**
+ * Tool to map full key paths that use the specified containers.
+ * Note: Currently only processes FSO layout buckets.
+ */
[email protected](
+    name = "container-key-mapping",
+    aliases = "ckm",
+    description = "Map full key paths that use the specified containers. " +
+        "Note: Currently processes FSO layout buckets.")
+public class ContainerToKeyMapping extends AbstractSubcommand implements 
Callable<Void> {
+  private static final String DIRTREE_DB_NAME = "omdirtree.db";
+  private static final String DIRTREE_TABLE_NAME = "dirTreeTable";
+  private static final DBColumnFamilyDefinition<Long, String> 
DIRTREE_TABLE_DEFINITION =
+      new DBColumnFamilyDefinition<>(DIRTREE_TABLE_NAME,
+          LongCodec.get(),
+          StringCodec.get());
+
+  @CommandLine.ParentCommand
+  private OMDebug parent;
+
+  @CommandLine.Option(names = {"--containers"},
+      required = true,
+      description = "Comma separated Container IDs")
+  private String containers;
+
+  private ManagedRocksDB rocksDB;
+  private ColumnFamilyHandle volumeCFHandle;
+  private ColumnFamilyHandle bucketCFHandle;
+  private ColumnFamilyHandle directoryCFHandle;
+  private ColumnFamilyHandle fileCFHandle;
+  private DBStore dirTreeDbStore;
+  private org.apache.hadoop.hdds.utils.db.Table<Long, String> dirTreeTable;
+
+  @Override
+  public Void call() throws Exception {
+    String dbPath = parent.getDbPath();
+    // Parse container IDs
+    Set<Long> containerIDs = new HashSet<>();
+    if (!StringUtils.isEmpty(containers)) {
+      containerIDs = Arrays.stream(containers.split(","))
+          .map(String::trim)
+          .map(Long::parseLong)
+          .collect(Collectors.toSet());
+    }
+
+    List<ColumnFamilyDescriptor> cfDescList = 
RocksDBUtils.getColumnFamilyDescriptors(dbPath);
+    List<ColumnFamilyHandle> cfHandleList = new ArrayList<>();
+
+    try (PrintWriter writer = out()) {
+      rocksDB = ManagedRocksDB.openReadOnly(dbPath, cfDescList, cfHandleList);
+      volumeCFHandle = RocksDBUtils.getColumnFamilyHandle(
+          OMDBDefinition.VOLUME_TABLE_DEF.getName(), cfHandleList);
+      bucketCFHandle = RocksDBUtils.getColumnFamilyHandle(
+          OMDBDefinition.BUCKET_TABLE_DEF.getName(), cfHandleList);
+      directoryCFHandle = RocksDBUtils.getColumnFamilyHandle(
+          OMDBDefinition.DIRECTORY_TABLE_DEF.getName(), cfHandleList);
+      fileCFHandle = RocksDBUtils.getColumnFamilyHandle(
+          OMDBDefinition.FILE_TABLE_DEF.getName(), cfHandleList);
+
+      openDirTreeDB(dbPath);
+      retrieve(writer, containerIDs);
+    } catch (RocksDBException e) {
+      System.err.println("Failed to open RocksDB: " + e);
+      throw e;
+    } finally {
+      closeDirTreeDB(dbPath);
+      if (rocksDB != null) {
+        rocksDB.close();
+      }
+    }
+    return null;
+  }
+
+  private void openDirTreeDB(String dbPath) throws IOException {
+    File dirTreeDbPath = new File(new File(dbPath).getParentFile(), 
DIRTREE_DB_NAME);
+    // Delete the DB from the last run if it exists.
+    if (dirTreeDbPath.exists()) {
+      FileUtils.deleteDirectory(dirTreeDbPath);
+    }
+
+    ConfigurationSource conf = new OzoneConfiguration();
+    dirTreeDbStore = DBStoreBuilder.newBuilder(conf)
+        .setName(DIRTREE_DB_NAME)
+        .setPath(dirTreeDbPath.getParentFile().toPath())
+        .addTable(DIRTREE_TABLE_DEFINITION.getName())
+        .build();
+    dirTreeTable = dirTreeDbStore.getTable(DIRTREE_TABLE_DEFINITION.getName(),
+        DIRTREE_TABLE_DEFINITION.getKeyCodec(), 
DIRTREE_TABLE_DEFINITION.getValueCodec());
+  }
+
+  private void closeDirTreeDB(String dbPath) throws IOException {
+    if (dirTreeDbStore != null) {
+      dirTreeDbStore.close();
+    }
+    File dirTreeDbPath = new File(new File(dbPath).getParentFile(), 
DIRTREE_DB_NAME);
+    if (dirTreeDbPath.exists()) {
+      FileUtils.deleteDirectory(dirTreeDbPath);
+    }
+  }
+
+  public void retrieve(PrintWriter writer, Set<Long> containerIds) {

Review Comment:
   Change to `private`



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