From d6af04f7f862bf8e0c3f862f164781c622975500 Mon Sep 17 00:00:00 2001 From: Sadanand Shenoy Date: Mon, 7 Aug 2023 21:38:49 +0530 Subject: [PATCH] HDDS-9119. Fix LDB Cli for Ozone Snapshot DB (#5151) Co-authored-by: Sadanand Shenoy --- .../ozone/shell/TestOzoneDebugShell.java | 50 +++++++++++++++++++ .../ozone/debug/DBDefinitionFactory.java | 6 +++ 2 files changed, 56 insertions(+) diff --git a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneDebugShell.java b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneDebugShell.java index da96bd1c83f..72b14ddd408 100644 --- a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneDebugShell.java +++ b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/shell/TestOzoneDebugShell.java @@ -31,16 +31,25 @@ import org.apache.hadoop.ozone.TestDataUtil; import org.apache.hadoop.ozone.client.OzoneClient; import org.apache.hadoop.ozone.client.OzoneClientFactory; +import org.apache.hadoop.ozone.client.OzoneSnapshot; +import org.apache.hadoop.ozone.debug.DBScanner; import org.apache.hadoop.ozone.debug.OzoneDebug; +import org.apache.hadoop.ozone.debug.RDBParser; import org.apache.hadoop.ozone.om.OMConfigKeys; +import org.apache.hadoop.ozone.om.OMStorage; import org.apache.hadoop.ozone.om.helpers.OmKeyArgs; import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo; +import org.apache.ozone.test.GenericTestUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import picocli.CommandLine; +import java.io.File; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.time.Duration; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -52,6 +61,9 @@ import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_PIPELINE_REPORT_INTERVAL; import static org.apache.hadoop.hdds.HddsConfigKeys.HDDS_COMMAND_STATUS_REPORT_INTERVAL; import static org.apache.hadoop.hdds.scm.ScmConfigKeys.OZONE_SCM_HEARTBEAT_PROCESS_INTERVAL; +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; +import static org.apache.hadoop.ozone.OzoneConsts.OM_SNAPSHOT_CHECKPOINT_DIR; +import static org.apache.hadoop.ozone.OzoneConsts.OM_DB_NAME; /** * Test Ozone Debug shell. @@ -118,6 +130,44 @@ public void testChunkInfoCmdBeforeAfterCloseContainer() throws Exception { Assertions.assertEquals(0, exitCode); } + @Test + public void testLdbCliForOzoneSnapshot() throws Exception { + StringWriter stdout = new StringWriter(); + PrintWriter pstdout = new PrintWriter(stdout); + CommandLine cmd = new CommandLine(new RDBParser()) + .addSubcommand(new DBScanner()) + .setOut(pstdout); + final String volumeName = UUID.randomUUID().toString(); + final String bucketName = UUID.randomUUID().toString(); + final String keyName = UUID.randomUUID().toString(); + + writeKey(volumeName, bucketName, keyName); + + String snapshotName = + client.getObjectStore().createSnapshot(volumeName, bucketName, "snap1"); + OzoneSnapshot snapshot = + client.getObjectStore().listSnapshot(volumeName, bucketName, null, null) + .next(); + Assertions.assertEquals(snapshotName, snapshot.getName()); + String dbPath = getSnapshotDBPath(snapshot.getCheckpointDir()); + String snapshotCurrent = dbPath + OM_KEY_PREFIX + "CURRENT"; + GenericTestUtils + .waitFor(() -> new File(snapshotCurrent).exists(), 1000, 120000); + String[] args = + new String[] {"--db=" + dbPath, "scan", "--cf", "keyTable"}; + int exitCode = cmd.execute(args); + Assertions.assertEquals(0, exitCode); + String cmdOut = stdout.toString(); + Assertions.assertTrue(cmdOut.contains(keyName)); + } + + private static String getSnapshotDBPath(String checkPointDir) { + return OMStorage.getOmDbDir(conf) + + OM_KEY_PREFIX + OM_SNAPSHOT_CHECKPOINT_DIR + OM_KEY_PREFIX + + OM_DB_NAME + checkPointDir; + } + + private static void writeKey(String volumeName, String bucketName, String keyName) throws IOException { try (OzoneClient client = OzoneClientFactory.getRpcClient(conf)) { diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/DBDefinitionFactory.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/DBDefinitionFactory.java index e2836bbd1a3..a163cda2502 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/DBDefinitionFactory.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/DBDefinitionFactory.java @@ -35,6 +35,8 @@ import com.amazonaws.services.kms.model.InvalidArnException; import com.google.common.base.Preconditions; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_DB_NAME; import static org.apache.hadoop.ozone.recon.ReconConstants.RECON_CONTAINER_KEY_DB; import static org.apache.hadoop.ozone.recon.ReconConstants.RECON_OM_SNAPSHOT_DB; @@ -60,6 +62,10 @@ private DBDefinitionFactory() { } public static DBDefinition getDefinition(String dbName) { + // OM snapshot DB name starts with this prefix. + if (!dbName.equals(OM_DB_NAME) && dbName.startsWith(OM_DB_NAME)) { + dbName = OM_DB_NAME; + } if (dbMap.containsKey(dbName)) { return dbMap.get(dbName); }