mladjan-gadzic commented on code in PR #5724: URL: https://github.com/apache/ozone/pull/5724#discussion_r1447584728
########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/ContainerKeyScanner.java: ########## @@ -0,0 +1,421 @@ +/* + * 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 com.google.common.collect.Sets; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.hdds.cli.SubcommandWithParent; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.utils.db.DBColumnFamilyDefinition; +import org.apache.hadoop.hdds.utils.db.DBDefinition; +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB; +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksIterator; +import org.apache.hadoop.ozone.OzoneConsts; +import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup; +import org.kohsuke.MetaInfServices; +import org.rocksdb.ColumnFamilyDescriptor; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.RocksDBException; +import picocli.CommandLine; + +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.stream.Collectors; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; +import static org.apache.hadoop.ozone.OzoneConsts.ROOT_PATH; + +/** + * Parser for a list of container IDs, to scan for keys. + */ [email protected]( + name = "ckscanner", + description = "Find keys that reference a container" +) +@MetaInfServices(SubcommandWithParent.class) +public class ContainerKeyScanner implements Callable<Void>, + SubcommandWithParent { + + private static final String FILE_TABLE = "fileTable"; + private static final String KEY_TABLE = "keyTable"; + private static final String DIRECTORY_TABLE = "directoryTable"; + + @CommandLine.Spec + private static CommandLine.Model.CommandSpec spec; + + @CommandLine.ParentCommand + private RDBParser parent; + + @CommandLine.Option(names = {"-ids", "--container-ids"}, + split = ",", + paramLabel = "containerIDs", + required = true, + description = "Set of container IDs to be used for getting all " + + "their keys. Example-usage: 1,11,2 (Separated by ',').") + private Set<Long> containerIds; + + private static Map<String, OmDirectoryInfo> directoryTable; + private static boolean isDirTableLoaded = false; + + @Override + public Void call() throws Exception { + ContainerKeyInfoWrapper containerKeyInfoWrapper = + scanDBForContainerKeys(parent.getDbPath()); + + printOutput(containerKeyInfoWrapper); + + closeStdChannels(); + + return null; + } + + private void closeStdChannels() { + out().close(); + err().close(); + } + + private Map<String, OmDirectoryInfo> getDirectoryTableData(String dbPath) + throws RocksDBException, IOException { + Map<String, OmDirectoryInfo> directoryTableData = new HashMap<>(); + + // Get all tables from RocksDB + List<ColumnFamilyDescriptor> columnFamilyDescriptors = + RocksDBUtils.getColumnFamilyDescriptors(dbPath); + final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>(); + + // Get all table handles + try (ManagedRocksDB db = ManagedRocksDB.openReadOnly(dbPath, + columnFamilyDescriptors, columnFamilyHandles)) { + dbPath = removeTrailingSlashIfNeeded(dbPath); + DBDefinition dbDefinition = DBDefinitionFactory.getDefinition( + Paths.get(dbPath), new OzoneConfiguration()); + if (dbDefinition == null) { + throw new IllegalStateException("Incorrect DB Path"); + } + + // Get directory table + DBColumnFamilyDefinition<?, ?> columnFamilyDefinition = + dbDefinition.getColumnFamily(DIRECTORY_TABLE); + if (columnFamilyDefinition == null) { + throw new IllegalStateException( + "Table with name" + DIRECTORY_TABLE + " not found"); + } + + // Get directory table handle + ColumnFamilyHandle columnFamilyHandle = getColumnFamilyHandle( + columnFamilyDefinition.getName().getBytes(UTF_8), + columnFamilyHandles); + if (columnFamilyHandle == null) { + throw new IllegalStateException("columnFamilyHandle is null"); + } + + // Get iterator for directory table + try (ManagedRocksIterator iterator = new ManagedRocksIterator( + db.get().newIterator(columnFamilyHandle))) { + iterator.get().seekToFirst(); + while (iterator.get().isValid()) { + directoryTableData.put(new String(iterator.get().key(), UTF_8), + ((OmDirectoryInfo) columnFamilyDefinition.getValueCodec() + .fromPersistedFormat(iterator.get().value()))); + iterator.get().next(); + } + } + } + + return directoryTableData; + } + + @Override + public Class<?> getParentType() { + return RDBParser.class; + } + + private static PrintWriter err() { + return spec.commandLine().getErr(); + } + + private static PrintWriter out() { + return spec.commandLine().getOut(); + } + + public Map<Long, Path> getAbsolutePathForObjectIDs( Review Comment: I tried that, but couldn't find a way to squeeze functionality from [FSODirectoryPathResolver#getAbsolutePathForObjectIDs](https://github.com/apache/ozone/blob/15c77c6e9bb22a0bbf5d0080f81c9310051229a9/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/FSODirectoryPathResolver.java#L72) that is needed here. There is a significant difference in accesing the tables between methods (iterators used etc.). -- 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]
