ashishkumar50 commented on code in PR #7465: URL: https://github.com/apache/ozone/pull/7465#discussion_r1853390102
########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/KeyPathRetriever.java: ########## @@ -0,0 +1,295 @@ +/* + * 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; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.stream.Stream; +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.SubcommandWithParent; +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.Table.KeyValue; +import org.apache.hadoop.hdds.utils.db.TableIterator; +import org.apache.hadoop.ozone.om.OMMetadataManager; +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; +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.kohsuke.MetaInfServices; +import picocli.CommandLine; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +/** + * Tool that retrieve key full path from OM db file table with pattern match. + */ [email protected]( + name = "retrieve-key-fullpath", + description = "retrieve full key path with matching criteria") +@MetaInfServices(SubcommandWithParent.class) +public class KeyPathRetriever implements Callable<Void>, SubcommandWithParent { + private static final DBColumnFamilyDefinition<Long, String> DIRTREE_COLUMN_FAMILY + = new DBColumnFamilyDefinition<>("dirTreeTable", LongCodec.get(), StringCodec.get()); + + @CommandLine.Spec + private static CommandLine.Model.CommandSpec spec; + + @CommandLine.Option(names = {"--db"}, + required = true, + description = "Database File") + private String dbFile; + + @CommandLine.Option(names = {"--containers"}, + required = false, + description = "Comma separated Container Ids") + private String strContainerIds; + + @CommandLine.Option(names = {"--container-file"}, + required = false, + description = "file with container id in new line") + private String containerFileName; + + @CommandLine.Option(names = {"--out", "-o"}, + required = false, + description = "out file name") + private String fileName; + + private DBStore dirTreeDbStore = null; + private Table<Long, String> dirTreeTable = null; + + @Override + public Class<?> getParentType() { + return OzoneDebug.class; + } + + public void setDirTreeTable(Table<Long, String> table) { + this.dirTreeTable = table; + } + + @Override + public Void call() throws Exception { + // get containerIds filter + Set<Long> containerIds = new HashSet<>(); + if (!StringUtils.isEmpty(strContainerIds)) { + if (!StringUtils.isEmpty(strContainerIds)) { + String[] split = strContainerIds.split(","); + for (String id : split) { + containerIds.add(Long.parseLong(id)); + } + } + } else if (!StringUtils.isEmpty(containerFileName)) { + try (Stream<String> stream = Files.lines(Paths.get(containerFileName))) { + stream.forEach(e -> containerIds.add(Long.parseLong(e))); + } + } + if (containerIds.isEmpty()) { + System.out.println("No containers to be filtered"); + return null; + } + // out stream + PrintWriter writer; + if (StringUtils.isEmpty(fileName)) { + writer = out(); + } else { + writer = new PrintWriter(new BufferedWriter(new PrintWriter(fileName, UTF_8.name()))); + } + + // db handler + OMMetadataManager metadataManager = getOmMetadataManager(dbFile); + if (metadataManager == null) { + writer.close(); + return null; + } + + dirTreeDbStore = openDb(new File(dbFile).getParentFile()); + if (dirTreeDbStore == null) { + writer.close(); + metadataManager.stop(); + return null; + } + dirTreeTable = dirTreeDbStore.getTable(DIRTREE_COLUMN_FAMILY.getName(), Long.class, String.class); + + try { + retrieve(metadataManager, writer, containerIds); + } finally { + dirTreeDbStore.close(); + writer.close(); + metadataManager.stop(); + } + return null; + } + + public void retrieve( + OMMetadataManager metadataManager, PrintWriter writer, Set<Long> containerIds) { + // build dir tree + Map<Long, Pair<Long, String>> bucketVolMap = new HashMap<>(); + try { + prepareDirIdTree(metadataManager, bucketVolMap); + } catch (Exception e) { + System.out.println("Exception occurred reading directory Table, " + e); + return; + } + + // iterate file table and filter for container + try (TableIterator<String, ? extends KeyValue<String, OmKeyInfo>> fileItr + = metadataManager.getFileTable().iterator()) { + while (fileItr.hasNext()) { + KeyValue<String, OmKeyInfo> next = fileItr.next(); + boolean found = next.getValue().getKeyLocationVersions().stream().anyMatch( + e -> e.getLocationList().stream().anyMatch( + blk -> containerIds.contains(blk.getBlockID().getContainerID()))); + if (found) { + StringBuilder sb = new StringBuilder(next.getValue().getKeyName()); + Long prvParent = next.getValue().getParentObjectID(); + while (prvParent != null) { + // check reached for bucket volume level + if (bucketVolMap.containsKey(prvParent)) { + Pair<Long, String> nameParentPair = bucketVolMap.get(prvParent); + sb.insert(0, nameParentPair.getValue() + OM_KEY_PREFIX); + prvParent = nameParentPair.getKey(); + if (null == prvParent) { + // add to output as reached till volume + writer.println(sb); + break; + } + continue; + } + + // check dir tree + Pair<Long, String> nameParentPair = getFromDirTree(prvParent); + if (nameParentPair == null) { + break; + } + sb.insert(0, nameParentPair.getValue() + OM_KEY_PREFIX); + prvParent = nameParentPair.getKey(); + } + } + } + } catch (Exception e) { + System.out.println("Exception occurred reading file Table, " + e); Review Comment: Errors can be printed in err instead of out, out can be just used for actual output. ########## hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneDebugShell.java: ########## @@ -175,6 +180,37 @@ public void testLdbCliForOzoneSnapshot() throws Exception { assertThat(cmdOut).contains(keyName); } + @Test + public void testKeyPathRetriever() throws Exception { + final String volumeName = UUID.randomUUID().toString(); + final String bucketName = UUID.randomUUID().toString(); + StringWriter stdout = new StringWriter(); + PrintWriter pstdout = new PrintWriter(stdout); + String[] args; + CommandLine cmd; + try (OzoneClient client = OzoneClientFactory.getRpcClient(conf)) { + TestDataUtil.createVolumeAndBucket(client, volumeName, bucketName, BucketLayout.FILE_SYSTEM_OPTIMIZED); + OzoneBucket bucket = client.getObjectStore().getVolume(volumeName).getBucket(bucketName); + bucket.createDirectory("dir1"); + TestDataUtil.createKey(bucket, "dir1/file1", "test"); + TestDataUtil.createKey(bucket, "dir1/file2", "test"); + TestDataUtil.createKey(bucket, "dir1/file3", "test"); + OzoneKeyDetails key1 = bucket.getKey("dir1/file1"); + OzoneKeyDetails key3 = bucket.getKey("dir1/file2"); + StringBuilder sb = new StringBuilder(); + key1.getOzoneKeyLocations().forEach(e -> sb.append(e.getContainerID())); + key3.getOzoneKeyLocations().forEach(e -> sb.append(",").append(e.getContainerID())); + String dbFile = OMStorage.getOmDbDir(conf).getPath() + "/om.db"; + cmd = new CommandLine(new OzoneDebug()).addSubcommand(new CommandLine(new KeyPathRetriever())).setOut(pstdout); + args = new String[] {"retrieve-key-fullpath", "--db", dbFile, "--containers", sb.toString()}; + } + int exitCode = cmd.execute(args); + assertEquals(0, exitCode); + String keyPrefix = volumeName + "/" + bucketName + "/dir1/"; + assertTrue(stdout.toString().contains(keyPrefix + "file1") && stdout.toString().contains(keyPrefix + "file2")); Review Comment: Use `assertThat` instead of `assertTrue` ########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/KeyPathRetriever.java: ########## @@ -0,0 +1,295 @@ +/* + * 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; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.stream.Stream; +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.SubcommandWithParent; +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.Table.KeyValue; +import org.apache.hadoop.hdds.utils.db.TableIterator; +import org.apache.hadoop.ozone.om.OMMetadataManager; +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; +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.kohsuke.MetaInfServices; +import picocli.CommandLine; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +/** + * Tool that retrieve key full path from OM db file table with pattern match. + */ [email protected]( + name = "retrieve-key-fullpath", + description = "retrieve full key path with matching criteria") +@MetaInfServices(SubcommandWithParent.class) +public class KeyPathRetriever implements Callable<Void>, SubcommandWithParent { + private static final DBColumnFamilyDefinition<Long, String> DIRTREE_COLUMN_FAMILY + = new DBColumnFamilyDefinition<>("dirTreeTable", LongCodec.get(), StringCodec.get()); + + @CommandLine.Spec + private static CommandLine.Model.CommandSpec spec; + + @CommandLine.Option(names = {"--db"}, + required = true, + description = "Database File") + private String dbFile; + + @CommandLine.Option(names = {"--containers"}, + required = false, + description = "Comma separated Container Ids") + private String strContainerIds; + + @CommandLine.Option(names = {"--container-file"}, + required = false, + description = "file with container id in new line") + private String containerFileName; + + @CommandLine.Option(names = {"--out", "-o"}, + required = false, + description = "out file name") + private String fileName; + + private DBStore dirTreeDbStore = null; + private Table<Long, String> dirTreeTable = null; + + @Override + public Class<?> getParentType() { + return OzoneDebug.class; + } + + public void setDirTreeTable(Table<Long, String> table) { + this.dirTreeTable = table; + } + + @Override + public Void call() throws Exception { + // get containerIds filter + Set<Long> containerIds = new HashSet<>(); + if (!StringUtils.isEmpty(strContainerIds)) { + if (!StringUtils.isEmpty(strContainerIds)) { + String[] split = strContainerIds.split(","); + for (String id : split) { + containerIds.add(Long.parseLong(id)); + } + } + } else if (!StringUtils.isEmpty(containerFileName)) { + try (Stream<String> stream = Files.lines(Paths.get(containerFileName))) { + stream.forEach(e -> containerIds.add(Long.parseLong(e))); + } + } + if (containerIds.isEmpty()) { + System.out.println("No containers to be filtered"); + return null; + } + // out stream + PrintWriter writer; + if (StringUtils.isEmpty(fileName)) { + writer = out(); + } else { + writer = new PrintWriter(new BufferedWriter(new PrintWriter(fileName, UTF_8.name()))); + } + + // db handler + OMMetadataManager metadataManager = getOmMetadataManager(dbFile); + if (metadataManager == null) { + writer.close(); + return null; + } + + dirTreeDbStore = openDb(new File(dbFile).getParentFile()); + if (dirTreeDbStore == null) { + writer.close(); + metadataManager.stop(); + return null; + } + dirTreeTable = dirTreeDbStore.getTable(DIRTREE_COLUMN_FAMILY.getName(), Long.class, String.class); + + try { + retrieve(metadataManager, writer, containerIds); + } finally { + dirTreeDbStore.close(); Review Comment: Temporary db can be deleted as well, as it not used again. -- 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]
