linyiqun commented on a change in pull request #1891: URL: https://github.com/apache/ozone/pull/1891#discussion_r592426705
########## File path: hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/PrefixParser.java ########## @@ -0,0 +1,189 @@ +/* + * 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.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.Callable; + +import java.nio.file.Path; +import org.apache.hadoop.hdds.cli.SubcommandWithParent; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.utils.MetadataKeyFilters; +import org.apache.hadoop.hdds.utils.db.Table; +import org.apache.hadoop.hdds.utils.db.Table.KeyValue; +import org.apache.hadoop.ozone.om.OMConfigKeys; +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; +import org.apache.hadoop.ozone.om.helpers.*; +import org.apache.hadoop.ozone.om.ratis.utils.OzoneManagerRatisUtils; +import org.kohsuke.MetaInfServices; +import picocli.CommandLine; +import picocli.CommandLine.Model.CommandSpec; +import picocli.CommandLine.Spec; + +/** + * Tool that parses OM db file for prefix table. + */ [email protected]( + name = "prefix", + description = "Parse prefix contents") +@MetaInfServices(SubcommandWithParent.class) +public class PrefixParser implements Callable<Void>, SubcommandWithParent { + + @Spec + private CommandSpec spec; + + @CommandLine.Option(names = {"--db"}, + required = true, + description = "Database File Path") + private String dbPath; + + @CommandLine.Option(names = {"--path"}, + required = true, + description = "prefixFile Path") + private String filePath; + + @CommandLine.Option(names = {"--bucket"}, + required = true, + description = "bucket in /vol/bucket format") + private String bucket; + + @CommandLine.Option(names = {"--volume"}, + required = true, + description = "volume in /vol/bucket format") + private String volume; + + public String getDbPath() { + return dbPath; + } + + public void setDbPath(String dbPath) { + this.dbPath = dbPath; + } + + @Override + public Class<?> getParentType() { + return OzoneDebug.class; + } + + @Override + public Void call() throws Exception { + parse(volume, bucket, dbPath, filePath); + return null; + } + + public static void main(String[] args) throws Exception { + new PrefixParser().call(); + } + + public void parse(String vol, String buck, String db, + String file) throws Exception { + if (!Files.exists(Paths.get(db))) { + System.out.println("DB path not exist:" + db); + return; + } + + System.out.println("FilePath is:" + file); + System.out.println("Db Path is:" + db); + + OzoneConfiguration conf = new OzoneConfiguration(); + conf.set(OMConfigKeys.OZONE_OM_DB_DIRS, db); + OzoneManagerRatisUtils.setBucketFSOptimized(true); + + OmMetadataManagerImpl metadataManager = + new OmMetadataManagerImpl(conf); + metadataManager.start(conf); + + org.apache.hadoop.fs.Path effectivePath = + new org.apache.hadoop.fs.Path("/"); + + Path p = Paths.get(file); + + // First get the info about the bucket + String bucketKey = metadataManager.getBucketKey(vol, buck); + OmBucketInfo info = metadataManager.getBucketTable().get(bucketKey); + long lastObjectId = info.getObjectID(); + WithParentObjectId objectBucketId = new WithParentObjectId(); + objectBucketId.setObjectID(lastObjectId); + dumpInfo("Bucket", effectivePath, objectBucketId, bucketKey); + + Iterator<Path> pathIterator = p.iterator(); + while(pathIterator.hasNext()) { + Path ss = pathIterator.next(); + String path = + metadataManager.getOzonePathKey(lastObjectId, ss.toString()); + OmDirectoryInfo directoryInfo = + metadataManager.getDirectoryTable().get(path); + effectivePath = getEffectivePath(effectivePath, ss.toString()); + + dumpInfo("Intermediate Dir", effectivePath, + directoryInfo, directoryInfo.getName()); Review comment: The file path to be parsed here is expected to be directory, right? If given path is a file, I don't think we will get the expected objID from dir table at the end of the while loop. If so, we should make sure input path is a directory. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
