Copilot commented on code in PR #11017:
URL: https://github.com/apache/ozone/pull/11017#discussion_r3780058900


##########
hadoop-ozone/cli-repair/src/main/java/org/apache/hadoop/ozone/repair/om/DownloadOMDB.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.repair.om;
+
+import static org.apache.hadoop.ozone.OzoneConsts.OM_DB_NAME;
+import static 
org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_DB_CHECKPOINT_USE_INODE_BASED_KEY;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import org.apache.commons.io.FileUtils;
+import org.apache.hadoop.hdds.cli.HddsVersionProvider;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.utils.db.DBCheckpoint;
+import org.apache.hadoop.ozone.OmUtils;
+import org.apache.hadoop.ozone.om.helpers.OMNodeDetails;
+import org.apache.hadoop.ozone.om.ratis_snapshot.OmRatisSnapshotProvider;
+import org.apache.hadoop.ozone.repair.RepairTool;
+import picocli.CommandLine;
+
+/**
+ * Tool to download and construct OM DB using follower bootstrap flow.
+ */
[email protected](
+    name = "download",
+    description = "Downloads and constructs om.db from an OM node using the 
same checkpoint transfer flow as follower bootstrap.",
+    mixinStandardHelpOptions = true,
+    versionProvider = HddsVersionProvider.class
+)
+public class DownloadOMDB extends RepairTool {
+
+  @CommandLine.Option(
+      names = {"--service-id", "--om-service-id"},
+      description = "Ozone Manager Service ID",
+      required = false
+  )
+  private String omServiceId;
+
+  @CommandLine.Option(
+      names = {"--node-id"},
+      description = "Node ID of the OM to download om.db from. Required when 
OM HA is configured.",
+      required = false
+  )
+  private String nodeId;
+
+  @CommandLine.Option(
+      names = {"--output-dir"},
+      description = "Path where the constructed om.db directory will be 
written.",
+      required = true
+  )
+  private Path outputDir;
+
+  @CommandLine.Option(
+      names = {"--overwrite"},
+      description = "Overwrite output directory if it already exists."
+  )
+  private boolean overwrite;
+
+  @Override
+  public void execute() throws Exception {
+    OzoneConfiguration conf = getOzoneConf();
+
+    if (nodeId == null && OmUtils.isServiceIdsDefined(conf)) {
+      error("This is an HA OM cluster; specify --node-id to select which OM to 
download from.");
+      return;
+    }
+
+    OMNodeDetails omNodeDetails =
+        OMNodeDetails.getOMNodeDetailsFromConf(conf, omServiceId, nodeId);
+    if (omNodeDetails == null) {
+      error("Couldn't determine OM node from the given service-id: %s and 
node-id: %s.",
+          omServiceId, nodeId);
+      return;
+    }
+
+    if (Files.exists(outputDir)) {
+      if (!overwrite) {
+        error("Output directory already exists: %s. Use --overwrite to replace 
it.",
+            outputDir.toAbsolutePath());
+        return;
+      }
+      FileUtils.forceDelete(outputDir.toFile());
+    }
+    Path parent = outputDir.toAbsolutePath().getParent();
+    if (parent != null) {
+      Files.createDirectories(parent);
+    }
+
+    // This tool intentionally follows the inode-based follower bootstrap 
transfer.
+    conf.setBoolean(OZONE_OM_DB_CHECKPOINT_USE_INODE_BASED_KEY, true);
+
+    Path snapshotWorkDir = Files.createTempDirectory("ozone-omdb-bootstrap-");
+    DBCheckpoint checkpoint = null;
+    try (OmRatisSnapshotProvider provider = new OmRatisSnapshotProvider(
+        conf, snapshotWorkDir.toFile(),
+        Collections.singletonMap(omNodeDetails.getNodeId(), omNodeDetails))) {
+      checkpoint = 
provider.downloadDBSnapshotFromLeader(omNodeDetails.getNodeId());
+      Path omDbPath = checkpoint.getCheckpointLocation().resolve(OM_DB_NAME);
+      if (!Files.isDirectory(omDbPath)) {
+        throw new IOException("Constructed OM DB directory not found in 
checkpoint: " + omDbPath);
+      }
+      FileUtils.moveDirectory(omDbPath.toFile(), outputDir.toFile());
+      info("Successfully downloaded and constructed om.db at: %s",
+          outputDir.toAbsolutePath());

Review Comment:
   The checkpoint transfer path includes snapshot data (db.snapshots) as well 
as om.db, but this tool moves only om.db and then cleans up the checkpoint 
directory, discarding the downloaded snapshots. This contradicts the intent of 
downloading OM metadata "with snapshots" and the follower-bootstrap layout 
(checkpointLocation/om.db and checkpointLocation/db.snapshots). Consider 
writing an output metadata directory that preserves both om.db and db.snapshots 
(and update the option description/tests accordingly).



##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/repair/om/TestDownloadOMDBTool.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.repair.om;
+
+import static 
org.apache.hadoop.ozone.OzoneConsts.OZONE_DB_CHECKPOINT_HTTP_ENDPOINT_V2;
+import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_ADDRESS_KEY;
+import static 
org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HTTP_ADDRESS_KEY;
+import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_NODES_KEY;
+import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.utils.IOUtils;
+import org.apache.hadoop.ozone.MiniOzoneHAClusterImpl;
+import org.apache.hadoop.ozone.MiniOzoneCluster;
+import org.apache.hadoop.ozone.OzoneConfigKeys;
+import org.apache.hadoop.ozone.ha.ConfUtils;
+import org.apache.hadoop.ozone.om.OzoneManager;
+import org.apache.hadoop.ozone.om.ratis_snapshot.OmRatisSnapshotProvider;
+import org.apache.hadoop.ozone.repair.OzoneRepair;
+import org.apache.ozone.test.GenericTestUtils;
+import org.apache.ozone.test.GenericTestUtils.LogCapturer;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Integration tests for `ozone repair om download`.
+ */
+public class TestDownloadOMDBTool {
+
+  private static final String OM_SERVICE_ID = "om-service-download-test";
+  private static MiniOzoneHAClusterImpl cluster;
+  private static OzoneConfiguration conf;
+
+  private GenericTestUtils.PrintStreamCapturer out;
+  private GenericTestUtils.PrintStreamCapturer err;
+
+  @BeforeAll
+  public static void init() throws Exception {
+    conf = new OzoneConfiguration();
+    cluster = MiniOzoneCluster.newHABuilder(conf)
+        .setOMServiceId(OM_SERVICE_ID)
+        .setNumOfOzoneManagers(3)
+        .setNumOfActiveOMs(3)
+        .build();
+    cluster.waitForClusterToBeReady();
+  }
+
+  @AfterAll
+  public static void cleanup() {
+    IOUtils.closeQuietly(cluster);
+  }
+
+  @BeforeEach
+  public void setup() {
+    out = GenericTestUtils.captureOut();
+    err = GenericTestUtils.captureErr();
+  }
+
+  @AfterEach
+  public void reset() {
+    IOUtils.closeQuietly(out, err);
+  }
+
+  @Test
+  public void testDownloadConstructsOmDbUsingV2Transfer(@TempDir Path tempDir) 
throws Exception {
+    OzoneManager leader = cluster.getOMLeader();
+    String leaderNodeId = leader.getOMNodeId();
+    Path outputDir = tempDir.resolve("downloaded-om.db");
+
+    LogCapturer providerLog = 
LogCapturer.captureLogs(OmRatisSnapshotProvider.class);
+    int exitCode = new OzoneRepair().getCmd().execute(withHAConf(new String[] {
+        "om", "download",
+        "--service-id", OM_SERVICE_ID,
+        "--node-id", leaderNodeId,
+        "--output-dir", outputDir.toString()
+    }));
+
+    assertEquals(0, exitCode, err.getOutput());
+    assertTrue(Files.isDirectory(outputDir), "Expected downloaded om.db 
directory to exist.");
+    assertTrue(Files.exists(outputDir.resolve("CURRENT")), "Expected RocksDB 
CURRENT file in downloaded om.db.");
+    
assertThat(providerLog.getOutput()).contains(OZONE_DB_CHECKPOINT_HTTP_ENDPOINT_V2);
+  }

Review Comment:
   LogCapturer installs a Log4j appender and needs to be stopped to avoid 
leaking appenders into other tests. Wrap the capture in a try/finally and call 
stopCapturing().



-- 
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]

Reply via email to