imbajin commented on code in PR #2945:
URL: https://github.com/apache/hugegraph/pull/2945#discussion_r4297424636


##########
hugegraph-server/hugegraph-dist/src/assembly/static/bin/arthas.sh:
##########
@@ -0,0 +1,60 @@
+#!/bin/bash
+#
+# 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.
+#
+# Offline attachment to a running Server or Store using the bundled agent.
+set -euo pipefail
+
+fail() { echo "Error: $*" >&2; exit 1; }
+usage() {
+    echo "Usage: $0 [--pid PID] [--telnet-port PORT] [--command COMMAND]"
+    echo "Run as the service OS user; defaults to bin/pid and loopback port 
3658."
+    echo "New agents disable HTTP/MCP; use the existing telnet port for an 
active agent."
+    echo "Use 'quit' to disconnect, 'stop' to stop Arthas."
+}
+
+BIN=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)
+TOP=$(cd -P "$BIN/.." && pwd)
+pid=""
+port=3658
+boot_args=()
+while [ "$#" -gt 0 ]; do
+    case "$1" in
+        --pid|--telnet-port|--command)
+            [ "$#" -ge 2 ] || fail "Missing value for $1"
+            case "$1" in
+                --pid) pid=$2 ;;
+                --telnet-port) port=$2 ;;
+                --command) boot_args=(--command "$2") ;;
+            esac
+            shift 2 ;;
+        -h|--help) usage; exit 0 ;;
+        *) usage >&2; fail "Unknown option: $1" ;;
+    esac
+done
+if [ -z "$pid" ]; then
+    [ -r "$BIN/pid" ] || fail "No bin/pid; start the service or supply --pid 
PID"
+    pid=$(cat "$BIN/pid")
+fi
+[[ "$pid" =~ ^[1-9][0-9]{0,9}$ ]] || fail "PID must be a positive integer"
+[[ "$port" =~ ^[1-9][0-9]{0,4}$ ]] && [ "$port" -le 65535 ] || fail "Invalid 
telnet port"
+JAVA=${JAVA_HOME:+$JAVA_HOME/bin/}java
+ARTHAS="$TOP/lib/arthas"
+[ -f "$ARTHAS/arthas-boot.jar" ] || fail "Bundled Arthas files are missing 
from lib/arthas"
+"$JAVA" --add-modules jdk.attach --class-path "$ARTHAS/arthas-core.jar" \

Review Comment:
   Updated the Server, HStore and Store runtime stages to Temurin JDK 17, which 
provides both jdk.attach and jdk.compiler. The bundled helper now works inside 
the container without an extra JDK or exposed diagnostic port. Verified native 
attachment and session cleanup for both distribution fixtures with the official 
Linux JDK runtime base, and documented container usage in this PR and 
apache/hugegraph-doc#528.



##########
hugegraph-server/hugegraph-dist/src/assembly/static/bin/ArthasAttach.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.regex.Pattern;
+
+import com.sun.tools.attach.VirtualMachine;
+import com.taobao.arthas.core.config.FeatureCodec;
+
+class ArthasAttach {
+    public static void main(String[] args) throws Exception {
+        Path home = Path.of(args[1]);
+        Path top = Path.of(args[3]);
+        VirtualMachine vm = VirtualMachine.attach(args[0]);
+        Path configFile = null;
+        try {
+            Properties target = vm.getSystemProperties();
+            String command = target.getProperty("sun.java.command", "");
+            String main = 
"org.apache.hugegraph.bootstrap.HugeGraphServerBootstrap";
+            boolean server = 
Files.isRegularFile(top.resolve("bin/start-hugegraph.sh")) &&
+                             (command.equals(main) || command.startsWith(main 
+ " "));
+            boolean store = 
Files.isRegularFile(top.resolve("bin/start-hugegraph-store.sh")) &&
+                            
(command.matches(Pattern.quote(top.resolve("lib/hg-store-node-").toString()) +
+                                             "[^/]+[.]jar(?: .*|$)") ||
+                             
(command.matches("org[.]springframework[.]boot[.]loader[.]JarLauncher(?: 
.*|$)") &&
+                              
"HugeGraphStore".equals(target.getProperty("name"))));
+            if (!server && !store) {
+                throw new IllegalArgumentException("PID does not identify a 
Server or Store in this distribution");
+            }
+            // FeatureCodec drops blank values. Pin a private properties file
+            // first so target system/env configuration cannot restore 
MCP/tunnel.
+            configFile = Files.createTempFile("hugegraph-arthas-", 
".properties");
+            Files.writeString(configFile, "arthas.config.overrideAll=true\n" +

Review Comment:
   The helper now stops the target Arthas agent when its client exits, 
including one-time commands, quit, and INT/TERM/HUP. Cleanup is enabled only 
after target validation and checks JAVA_PID before sending stop, so a different 
JVM on the requested port is left alone. Server and Store native tests verify 
listener closure while the application JVM stays alive. Documentation now 
states that local processes can access the unauthenticated listener during 
diagnosis, warns against concurrent helper sessions, and explains API-only 
activation and SIGKILL limitations.



##########
hugegraph-server/hugegraph-dist/src/assembly/static/bin/ArthasAttach.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.regex.Pattern;
+
+import com.sun.tools.attach.VirtualMachine;
+import com.taobao.arthas.core.config.FeatureCodec;
+
+class ArthasAttach {
+    public static void main(String[] args) throws Exception {
+        Path home = Path.of(args[1]);
+        Path top = Path.of(args[3]);
+        VirtualMachine vm = VirtualMachine.attach(args[0]);
+        Path configFile = null;
+        try {
+            Properties target = vm.getSystemProperties();
+            String command = target.getProperty("sun.java.command", "");
+            String main = 
"org.apache.hugegraph.bootstrap.HugeGraphServerBootstrap";
+            boolean server = 
Files.isRegularFile(top.resolve("bin/start-hugegraph.sh")) &&
+                             (command.equals(main) || command.startsWith(main 
+ " "));
+            boolean store = 
Files.isRegularFile(top.resolve("bin/start-hugegraph-store.sh")) &&

Review Comment:
   Added a positive Store fixture using an executable hg-store-node JAR and the 
distribution’s real bundled attach helper and Arthas agent. The Store CI job 
now runs the same native test after packaging. Both Server and Store cases 
passed locally, including command, quit, signal cleanup and 
hostile-configuration checks.



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