sarvekshayr commented on code in PR #9469: URL: https://github.com/apache/ozone/pull/9469#discussion_r2606452624
########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/om/ContainerToKeyMapping.java: ########## @@ -0,0 +1,343 @@ +/* + * 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.Table; +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 = "Maps full key paths that use the specified containers. " + + "Note: A container can have both FSO and OBS keys. Currently this tool processes only FSO keys") +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 Table<Long, String> dirTreeTable; + + // TODO: Add support to OBS keys (HDDS-14118) + @Override + public Void call() throws Exception { + String dbPath = parent.getDbPath(); + // Parse container IDs + Set<Long> 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) { + 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); + } + } + + private void retrieve(PrintWriter writer, Set<Long> containerIds) { + // Build dir tree + Map<Long, Pair<Long, String>> bucketVolMap = new HashMap<>(); + try { + prepareDirIdTree(bucketVolMap); + } catch (Exception e) { + err().println("Exception occurred reading directory Table, " + e); + return; + } + + // Map to collect keys per container + Map<Long, List<String>> containerToKeysMap = new HashMap<>(); + for (Long containerId : containerIds) { + containerToKeysMap.put(containerId, new ArrayList<>()); + } + + // Iterate file table and filter for container + try (ManagedRocksIterator fileIterator = new ManagedRocksIterator( + rocksDB.get().newIterator(fileCFHandle))) { + fileIterator.get().seekToFirst(); + + while (fileIterator.get().isValid()) { Review Comment: Used freon command to create a million keys. ``` Number of Volumes created: 100 Number of Buckets created: 1000 Number of Keys added: 1000000 ``` Added logs only to track progress. ``` bash-5.1$ ozone debug om container-key-mapping --db /data/metadata/om.db --containers 1,2,3,4,5,6,7,8,9,10,11,12 > key.txt Note: A container can have both FSO and OBS keys. Currently this tool processes only FSO keys Total keys processed: 1000000 Total time: 3596 ms ``` ~3.6s taken to complete. -- 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]
