This is an automated email from the ASF dual-hosted git repository.

michaelsmith pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git

commit f5aa242de6ae78bab7f0532204637797ba8b84c0
Author: Michael Smith <[email protected]>
AuthorDate: Thu Jun 1 14:13:10 2023 -0700

    IMPALA-12158: Restore process name for daemons
    
    IMPALA-10794 introduced names for threads, which messed up process names
    too for catalogd and impalad.
    
    Sets a pseudo-property sun.java.command that is used by jps to read the
    Java process name. Use that property to replace the name of the "main"
    thread (which, when updated, also updates the process name).
    
    The process name is also used for renames in ThreadNameAnnotator so that
    the original process name is always present. This ensures tools like
    pgrep can find impalad and catalogd.
    
    Change-Id: I7469b4f595acc1949286520a8e6086716bec0b1f
    Reviewed-on: http://gerrit.cloudera.org:8080/19990
    Reviewed-by: Impala Public Jenkins <[email protected]>
    Tested-by: Michael Smith <[email protected]>
---
 be/src/common/init.cc                              | 28 ++++++++++++++++++++++
 .../apache/impala/util/ThreadNameAnnotator.java    |  7 +++++-
 tests/common/impala_service.py                     |  2 +-
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/be/src/common/init.cc b/be/src/common/init.cc
index fa3f3a01f..6671c3eb9 100644
--- a/be/src/common/init.cc
+++ b/be/src/common/init.cc
@@ -380,6 +380,31 @@ static Status JavaAddOpens() {
   return Status::OK();
 }
 
+static Status JavaSetProcessName(string name) {
+  string current_val;
+  char* current_val_c = getenv("JAVA_TOOL_OPTIONS");
+  if (current_val_c != NULL) {
+    current_val = current_val_c;
+  }
+
+  if (!current_val.empty() && current_val.find("-Dsun.java.command") != 
string::npos) {
+    LOG(WARNING) << "Overriding sun.java.command in JAVA_TOOL_OPTIONS to " << 
name;
+  }
+
+  stringstream val_out;
+  if (!current_val.empty()) {
+    val_out << current_val << " ";
+  }
+  // Set sun.java.command so jps reports the name correctly, and 
ThreadNameAnnotator can
+  // use the process name for the main thread (and correctly restore the 
process name).
+  val_out << "-Dsun.java.command=" << name;
+
+  if (setenv("JAVA_TOOL_OPTIONS", val_out.str().c_str(), 1) < 0) {
+    return Status(Substitute("Could not update JAVA_TOOL_OPTIONS: $0", 
GetStrErrMsg()));
+  }
+  return Status::OK();
+}
+
 void impala::InitCommonRuntime(int argc, char** argv, bool init_jvm,
     TestInfo::Mode test_mode, bool external_fe) {
   srand(time(NULL));
@@ -537,6 +562,9 @@ void impala::InitCommonRuntime(int argc, char** argv, bool 
init_jvm,
     // Add JAVA_TOOL_OPTIONS for ehcache
     ABORT_IF_ERROR(JavaAddOpens());
 
+    ABORT_IF_ERROR(JavaSetProcessName(
+        boost::filesystem::path(argv[0]).filename().string()));
+
     if (!external_fe) {
       JniUtil::InitLibhdfs();
     }
diff --git a/fe/src/main/java/org/apache/impala/util/ThreadNameAnnotator.java 
b/fe/src/main/java/org/apache/impala/util/ThreadNameAnnotator.java
index c235986cb..db770ef20 100644
--- a/fe/src/main/java/org/apache/impala/util/ThreadNameAnnotator.java
+++ b/fe/src/main/java/org/apache/impala/util/ThreadNameAnnotator.java
@@ -46,7 +46,12 @@ public class ThreadNameAnnotator implements AutoCloseable {
 
   public ThreadNameAnnotator(String annotation) {
     thr_ = Thread.currentThread();
-    oldName_ = thr_.getName();
+    if ("main".equals(thr_.getName())) {
+      // Use the process name from sun.java.command.
+      oldName_ = System.getProperty("sun.java.command", thr_.getName());
+    } else {
+      oldName_ = thr_.getName();
+    }
     newName_ = oldName_ + " [" + annotation + "]";
     thr_.setName(newName_);
   }
diff --git a/tests/common/impala_service.py b/tests/common/impala_service.py
index 17cb917d6..0dee27429 100644
--- a/tests/common/impala_service.py
+++ b/tests/common/impala_service.py
@@ -187,7 +187,7 @@ class BaseImpalaService(object):
     # as it will be preserved along with everything else in the log directory.
     impalad_pids = subprocess.check_output(["pgrep", "impalad"],
         universal_newlines=True).split("\n")[:-1]
-    catalogd_pids = subprocess.check_output(["pgrep", "-f", "catalogd"],
+    catalogd_pids = subprocess.check_output(["pgrep", "catalogd"],
         universal_newlines=True).split("\n")[:-1]
     minidump_diag_string = "Dumping minidumps for impalads/catalogds...\n"
     for pid in impalad_pids:

Reply via email to