From d013188032cb3e235a72744bd6d2a31410e0cbf9 Mon Sep 17 00:00:00 2001 From: Sarveksha Yeshavantha Raju <79865743+sarvekshayr@users.noreply.github.com> Date: Fri, 3 Jan 2025 16:47:59 +0530 Subject: [PATCH] HDDS-11727. Block `ozone repair om fso-tree` if OM is running (#7589) --- hadoop-ozone/dist/src/shell/ozone/ozone | 20 +++++++++++++++++ .../hadoop/ozone/repair/RepairTool.java | 22 +++++++++++++++++++ .../hadoop/ozone/repair/om/FSORepairCLI.java | 3 +++ 3 files changed, 45 insertions(+) diff --git a/hadoop-ozone/dist/src/shell/ozone/ozone b/hadoop-ozone/dist/src/shell/ozone/ozone index 0973ef81095..5bb05fee724 100755 --- a/hadoop-ozone/dist/src/shell/ozone/ozone +++ b/hadoop-ozone/dist/src/shell/ozone/ozone @@ -224,6 +224,7 @@ function ozonecmd_case OZONE_RUN_ARTIFACT_NAME="ozone-tools" ;; repair) + check_running_ozone_services OZONE_CLASSNAME=org.apache.hadoop.ozone.repair.OzoneRepair OZONE_DEBUG_OPTS="${OZONE_DEBUG_OPTS} ${RATIS_OPTS} ${OZONE_MODULE_ACCESS_ARGS}" OZONE_RUN_ARTIFACT_NAME="ozone-tools" @@ -245,6 +246,25 @@ function ozonecmd_case esac } +## @description Check for running Ozone services using PID files. +## @audience public +function check_running_ozone_services +{ + OZONE_PID_DIR="/tmp" + + local services=("om" "scm" "datanode") + + for service in "${services[@]}"; do + for pid_file in ${OZONE_PID_DIR}/ozone-*-${service}.pid; do + if [[ -f "${pid_file}" ]]; then + if kill -0 "$(cat "${pid_file}")" 2>/dev/null; then + export "OZONE_${service^^}_RUNNING=true" + fi + fi + done + done +} + ## @description turn off logging for CLI by default ## @audience private function ozone_suppress_shell_log diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/RepairTool.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/RepairTool.java index b94e1c52d82..d8a976b2fd8 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/RepairTool.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/RepairTool.java @@ -18,6 +18,7 @@ package org.apache.hadoop.ozone.repair; import org.apache.hadoop.hdds.cli.AbstractSubcommand; +import picocli.CommandLine; import java.io.PrintWriter; import java.util.concurrent.Callable; @@ -25,6 +26,10 @@ /** Parent class for all actionable repair commands. */ public abstract class RepairTool extends AbstractSubcommand implements Callable { + @CommandLine.Option(names = {"--force"}, + description = "Use this flag if you want to bypass the check in false-positive cases.") + private boolean force; + /** Hook method for subclasses for performing actual repair task. */ protected abstract void execute() throws Exception; @@ -34,6 +39,23 @@ public final Void call() throws Exception { return null; } + protected boolean checkIfServiceIsRunning(String serviceName) { + String envVariable = String.format("OZONE_%s_RUNNING", serviceName); + String runningServices = System.getenv(envVariable); + if ("true".equals(runningServices)) { + if (!force) { + error("Error: %s is currently running on this host. " + + "Stop the service before running the repair tool.", serviceName); + return true; + } else { + info("Warning: --force flag used. Proceeding despite %s being detected as running.", serviceName); + } + } else { + info("No running %s service detected. Proceeding with repair.", serviceName); + } + return false; + } + protected void info(String msg, Object... args) { out().println(formatMessage(msg, args)); } diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/om/FSORepairCLI.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/om/FSORepairCLI.java index 46af1e847be..fd6d75c7136 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/om/FSORepairCLI.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/om/FSORepairCLI.java @@ -55,6 +55,9 @@ public class FSORepairCLI extends RepairTool { @Override public void execute() throws Exception { + if (checkIfServiceIsRunning("OM")) { + return; + } if (repair) { info("FSO Repair Tool is running in repair mode"); } else {