-
Notifications
You must be signed in to change notification settings - Fork 594
HDDS-14118. Support OBS keys and additional flags in ContainerToKeyMapping tool #9599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sarvekshayr
wants to merge
3
commits into
apache:master
Choose a base branch
from
sarvekshayr:HDDS-14118
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−60
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,13 +57,12 @@ | |
|
|
||
| /** | ||
| * Tool to map full key paths that use the specified containers. | ||
| * Note: Currently only processes FSO layout buckets. | ||
| * Supports both FSO (File System Optimized) and OBS (Object Store) bucket layouts. | ||
| */ | ||
| @CommandLine.Command( | ||
| 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") | ||
| description = "Maps full key paths that use the specified containers.") | ||
| 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"; | ||
|
|
@@ -80,22 +79,26 @@ public class ContainerToKeyMapping extends AbstractSubcommand implements Callabl | |
| description = "Comma separated Container IDs") | ||
| private String containers; | ||
|
|
||
| @CommandLine.Option(names = {"--onlyFileNames"}, | ||
| defaultValue = "false", | ||
| description = "Only display file names without full path") | ||
| private boolean onlyFileNames; | ||
|
|
||
| private DBStore omDbStore; | ||
| private Table<String, OmVolumeArgs> volumeTable; | ||
| private Table<String, OmBucketInfo> bucketTable; | ||
| private Table<String, OmDirectoryInfo> directoryTable; | ||
| private Table<String, OmKeyInfo> fileTable; | ||
| private Table<String, OmKeyInfo> keyTable; | ||
| private DBStore dirTreeDbStore; | ||
| private Table<Long, String> dirTreeTable; | ||
| // Cache volume IDs to avoid repeated lookups | ||
| private final Map<String, Long> volumeCache = new HashMap<>(); | ||
| private ConfigurationSource conf; | ||
|
|
||
| // TODO: Add support to OBS keys (HDDS-14118) | ||
| @Override | ||
| public Void call() throws Exception { | ||
| err().println("Note: A container can have both FSO and OBS keys. Currently this tool processes only FSO keys"); | ||
|
|
||
|
|
||
| String dbPath = parent.getDbPath(); | ||
| // Parse container IDs | ||
| Set<Long> containerIDs = Arrays.stream(containers.split(",")) | ||
|
|
@@ -122,9 +125,9 @@ public Void call() throws Exception { | |
| bucketTable = OMDBDefinition.BUCKET_TABLE_DEF.getTable(omDbStore, CacheType.NO_CACHE); | ||
| directoryTable = OMDBDefinition.DIRECTORY_TABLE_DEF.getTable(omDbStore, CacheType.NO_CACHE); | ||
| fileTable = OMDBDefinition.FILE_TABLE_DEF.getTable(omDbStore, CacheType.NO_CACHE); | ||
| keyTable = OMDBDefinition.KEY_TABLE_DEF.getTable(omDbStore, CacheType.NO_CACHE); | ||
|
|
||
| openDirTreeDB(dbPath); | ||
| retrieve(writer, containerIDs); | ||
| retrieve(dbPath, writer, containerIDs); | ||
| } catch (Exception e) { | ||
| err().println("Failed to open RocksDB: " + e); | ||
| throw e; | ||
|
|
@@ -163,59 +166,102 @@ private void closeDirTreeDB(String dbPath) throws IOException { | |
| } | ||
| } | ||
|
|
||
| private void retrieve(PrintWriter writer, Set<Long> containerIds) { | ||
| // Build dir tree | ||
| private void retrieve(String dbPath, PrintWriter writer, Set<Long> containerIds) { | ||
| Map<Long, Pair<Long, String>> bucketVolMap = new HashMap<>(); | ||
| try { | ||
| prepareDirIdTree(bucketVolMap); | ||
| } catch (Exception e) { | ||
| err().println("Exception occurred reading directory Table, " + e); | ||
| return; | ||
| // Build dir tree for FSO keys only if we need full paths | ||
| if (!onlyFileNames) { | ||
| try { | ||
| openDirTreeDB(dbPath); | ||
| 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<>(); | ||
| // Track unreferenced keys count per container | ||
| // Track unreferenced keys count per container (FSO only) | ||
| Map<Long, Long> unreferencedCountMap = new HashMap<>(); | ||
| for (Long containerId : containerIds) { | ||
| containerToKeysMap.put(containerId, new ArrayList<>()); | ||
| unreferencedCountMap.put(containerId, 0L); | ||
| } | ||
|
|
||
| // Iterate file table and filter for container | ||
| try (TableIterator<String, ? extends Table.KeyValue<String, OmKeyInfo>> fileIterator = | ||
| // Process FSO keys (fileTable) | ||
| processFSOKeys(containerIds, containerToKeysMap, unreferencedCountMap, bucketVolMap); | ||
|
|
||
| // Process OBS keys (keyTable) | ||
| processOBSKeys(containerIds, containerToKeysMap); | ||
|
|
||
| jsonOutput(writer, containerToKeysMap, unreferencedCountMap); | ||
| } | ||
|
Comment on lines
+191
to
+198
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit - please remove unnecessary new lines in the code. I can see after calling every method a new line is added. |
||
|
|
||
| private void processFSOKeys(Set<Long> containerIds, Map<Long, List<String>> containerToKeysMap, | ||
| Map<Long, Long> unreferencedCountMap, Map<Long, Pair<Long, String>> bucketVolMap) { | ||
| try (TableIterator<String, ? extends Table.KeyValue<String, OmKeyInfo>> fileIterator = | ||
| fileTable.iterator()) { | ||
|
|
||
| while (fileIterator.hasNext()) { | ||
| Table.KeyValue<String, OmKeyInfo> entry = fileIterator.next(); | ||
| OmKeyInfo keyInfo = entry.getValue(); | ||
|
|
||
| // Find which containers this key uses | ||
| Set<Long> keyContainers = new HashSet<>(); | ||
| keyInfo.getKeyLocationVersions().forEach( | ||
| e -> e.getLocationList().forEach( | ||
| blk -> { | ||
| long cid = blk.getBlockID().getContainerID(); | ||
| if (containerIds.contains(cid)) { | ||
| keyContainers.add(cid); | ||
| } | ||
| })); | ||
| Set<Long> keyContainers = getKeyContainers(keyInfo, containerIds); | ||
|
|
||
| if (!keyContainers.isEmpty()) { | ||
| // Reconstruct full path | ||
| String fullPath = reconstructFullPath(keyInfo, bucketVolMap, unreferencedCountMap, keyContainers); | ||
| if (fullPath != null) { | ||
| // For FSO keys, reconstruct the full path | ||
| // Or extract just the key name if onlyFileNames is true | ||
| String keyPath = onlyFileNames ? keyInfo.getKeyName() : | ||
| reconstructFullPath(keyInfo, bucketVolMap, unreferencedCountMap, keyContainers); | ||
| if (keyPath != null) { | ||
| for (Long containerId : keyContainers) { | ||
| containerToKeysMap.get(containerId).add(fullPath); | ||
| containerToKeysMap.get(containerId).add(keyPath); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| err().println("Exception occurred reading file Table, " + e); | ||
| return; | ||
| err().println("Exception occurred reading fileTable (FSO keys), " + e); | ||
| } | ||
| jsonOutput(writer, containerToKeysMap, unreferencedCountMap); | ||
| } | ||
|
|
||
| private void processOBSKeys(Set<Long> containerIds, Map<Long, List<String>> containerToKeysMap) { | ||
| try (TableIterator<String, ? extends Table.KeyValue<String, OmKeyInfo>> keyIterator = | ||
| keyTable.iterator()) { | ||
|
|
||
| while (keyIterator.hasNext()) { | ||
| Table.KeyValue<String, OmKeyInfo> entry = keyIterator.next(); | ||
| OmKeyInfo keyInfo = entry.getValue(); | ||
|
|
||
| // Find which containers this key uses | ||
| Set<Long> keyContainers = getKeyContainers(keyInfo, containerIds); | ||
|
|
||
| if (!keyContainers.isEmpty()) { | ||
| // For OBS keys, use the database key directly (already in /volume/bucket/key format) | ||
| // Or extract just the key name if onlyFileNames is true | ||
| String keyPath = onlyFileNames ? keyInfo.getKeyName() : entry.getKey(); | ||
| for (Long containerId : keyContainers) { | ||
| containerToKeysMap.get(containerId).add(keyPath); | ||
| } | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| err().println("Exception occurred reading keyTable (OBS keys), " + e); | ||
| } | ||
| } | ||
|
|
||
| private Set<Long> getKeyContainers(OmKeyInfo keyInfo, Set<Long> targetContainerIds) { | ||
| Set<Long> keyContainers = new HashSet<>(); | ||
| keyInfo.getKeyLocationVersions().forEach( | ||
| e -> e.getLocationList().forEach( | ||
| blk -> { | ||
| long cid = blk.getBlockID().getContainerID(); | ||
| if (targetContainerIds.contains(cid)) { | ||
| keyContainers.add(cid); | ||
| } | ||
| })); | ||
| return keyContainers; | ||
| } | ||
|
|
||
| private void prepareDirIdTree(Map<Long, Pair<Long, String>> bucketVolMap) throws Exception { | ||
|
|
@@ -263,19 +309,19 @@ private String reconstructFullPath(OmKeyInfo keyInfo, Map<Long, Pair<Long, Strin | |
| sb.insert(0, nameParentPair.getValue() + OM_KEY_PREFIX); | ||
| prvParent = nameParentPair.getKey(); | ||
| if (null == prvParent) { | ||
| return sb.toString(); | ||
| return OM_KEY_PREFIX + sb; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| // Check dir tree | ||
| Pair<Long, String> nameParentPair = getFromDirTree(prvParent); | ||
| if (nameParentPair == null) { | ||
| // Parent not found - increment unreferenced count for all containers this key uses | ||
| // If parent is not found, mark the key as unreferenced and increment its count | ||
| for (Long containerId : keyContainers) { | ||
| unreferencedCountMap.put(containerId, unreferencedCountMap.get(containerId) + 1); | ||
| } | ||
| break; | ||
| return "[unreferenced] " + keyInfo.getKeyName(); | ||
| } | ||
| sb.insert(0, nameParentPair.getValue() + OM_KEY_PREFIX); | ||
| prvParent = nameParentPair.getKey(); | ||
|
|
@@ -342,7 +388,7 @@ private void jsonOutput(PrintWriter writer, Map<Long, List<String>> containerToK | |
| } | ||
|
|
||
| containerNode.set("keys", keysArray); | ||
| containerNode.put("numOfKeys", entry.getValue().size()); | ||
| containerNode.put("totalKeys", entry.getValue().size()); // includes unreferenced keys | ||
|
|
||
| // Add unreferenced count if > 0 | ||
| long unreferencedCount = unreferencedCountMap.get(containerId); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - avoid this new line