-
Notifications
You must be signed in to change notification settings - Fork 619
HDDS-15155. Add ozone debug ldb drop-column-family subcommand
#10167
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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
146 changes: 146 additions & 0 deletions
146
hadoop-ozone/cli-debug/src/main/java/org/apache/hadoop/ozone/debug/ldb/DropColumnFamily.java
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 |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * 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.ldb; | ||
|
|
||
| import static org.apache.hadoop.hdds.utils.db.DBStoreBuilder.DEFAULT_COLUMN_FAMILY_NAME; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
| import java.util.concurrent.Callable; | ||
| import org.apache.hadoop.hdds.cli.AbstractSubcommand; | ||
| import org.apache.hadoop.hdds.utils.IOUtils; | ||
| import org.apache.hadoop.hdds.utils.db.RocksDatabase; | ||
| import org.apache.hadoop.hdds.utils.db.managed.ManagedConfigOptions; | ||
| import org.apache.hadoop.hdds.utils.db.managed.ManagedDBOptions; | ||
| import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB; | ||
| import org.apache.hadoop.ozone.debug.RocksDBUtils; | ||
| import org.rocksdb.ColumnFamilyDescriptor; | ||
| import org.rocksdb.ColumnFamilyHandle; | ||
| import org.rocksdb.RocksDBException; | ||
| import picocli.CommandLine; | ||
|
|
||
| /** | ||
| * Drop a column family from a RocksDB. | ||
| */ | ||
| @CommandLine.Command( | ||
| name = "drop-column-family", | ||
| description = "Drop a column family from the DB.") | ||
| public class DropColumnFamily extends AbstractSubcommand | ||
| implements Callable<Void> { | ||
|
|
||
| private static final String WARNING_TO_STOP_SERVICE = | ||
| "WARNING: Dropping a column family mutates RocksDB and can be unsafe " + | ||
| "if the DB is already open by an Ozone process. Ensure the service " + | ||
| "that owns this DB is stopped before running this command. " + | ||
| "Do you want to continue (y/N)? "; | ||
|
|
||
| @CommandLine.ParentCommand | ||
| private RDBParser parent; | ||
|
|
||
| @CommandLine.Option(names = {"--column-family", "--cf"}, | ||
| required = true, | ||
| description = "Column family name") | ||
| private String columnFamilyName; | ||
|
|
||
| @CommandLine.Option(names = {"-y", "--yes"}, | ||
| description = "Continue without interactive user confirmation") | ||
| private boolean yes; | ||
|
|
||
| @Override | ||
| public Void call() throws Exception { | ||
| if (isDefaultColumnFamily(columnFamilyName)) { | ||
| throw new IllegalArgumentException( | ||
| "Default column family cannot be dropped."); | ||
| } | ||
| if (!columnFamilyExists()) { | ||
| throw new IllegalArgumentException(columnFamilyName | ||
| + " is not a column family in DB for the given path."); | ||
| } | ||
|
|
||
| confirm(); | ||
|
|
||
| ManagedConfigOptions configOptions = new ManagedConfigOptions(); | ||
| ManagedDBOptions dbOptions = new ManagedDBOptions(); | ||
| List<ColumnFamilyHandle> cfHandleList = new ArrayList<>(); | ||
| List<ColumnFamilyDescriptor> cfDescList = new ArrayList<>(); | ||
|
|
||
| try (ManagedRocksDB db = ManagedRocksDB.openWithLatestOptions( | ||
| configOptions, dbOptions, parent.getDbPath(), cfDescList, | ||
| cfHandleList)) { | ||
| ColumnFamilyHandle columnFamilyHandle = | ||
| RocksDBUtils.getColumnFamilyHandle(columnFamilyName, cfHandleList); | ||
| if (columnFamilyHandle == null) { | ||
| throw new IllegalArgumentException(columnFamilyName | ||
| + " is not a column family in DB for the given path."); | ||
| } | ||
|
|
||
| db.get().dropColumnFamily(columnFamilyHandle); | ||
| out().println("Dropped column family " + columnFamilyName); | ||
| } catch (RocksDBException exception) { | ||
| String errorMessage = "Failed to drop column family " + columnFamilyName | ||
| + " from RocksDB for the given path: " + parent.getDbPath(); | ||
| throw new IOException(errorMessage, exception); | ||
| } finally { | ||
| IOUtils.closeQuietly(cfHandleList); | ||
| closeDescriptors(cfDescList); | ||
| IOUtils.closeQuietly(configOptions, dbOptions); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static boolean isDefaultColumnFamily(String name) { | ||
| return DEFAULT_COLUMN_FAMILY_NAME.equals(name); | ||
| } | ||
|
|
||
| private void confirm() { | ||
| if (yes) { | ||
| return; | ||
| } | ||
|
|
||
| err().print(WARNING_TO_STOP_SERVICE); | ||
| err().flush(); | ||
| Scanner scanner = new Scanner(new InputStreamReader( | ||
| System.in, StandardCharsets.UTF_8)); | ||
| String confirmation = scanner.hasNextLine() | ||
| ? scanner.nextLine().trim() : ""; | ||
| if (!"y".equalsIgnoreCase(confirmation)) { | ||
| throw new IllegalStateException("Aborting command."); | ||
| } | ||
| } | ||
|
|
||
| private boolean columnFamilyExists() throws RocksDBException { | ||
| for (byte[] columnFamily : RocksDatabase.listColumnFamiliesEmptyOptions( | ||
| parent.getDbPath())) { | ||
| if (columnFamilyName.equals(new String(columnFamily, | ||
| StandardCharsets.UTF_8))) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static void closeDescriptors( | ||
| List<ColumnFamilyDescriptor> descriptors) { | ||
| descriptors.forEach(descriptor -> descriptor.getOptions().close()); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.