slfan1989 commented on code in PR #8742:
URL: https://github.com/apache/hadoop/pull/8742#discussion_r4092958951


##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/resources/diagnostics/diagnostics_collector.py:
##########
@@ -0,0 +1,358 @@
+# 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 argparse
+import logging
+import os
+import re
+import socket
+import subprocess
+import sys
+import time
+import xml.etree.ElementTree as ET
+from datetime import datetime, timedelta
+from typing import List, Optional, Tuple
+
+logger = logging.getLogger(__name__)
+
+_TEMP_DIR = "/tmp"
+_HADOOP_CONF_DIR: Optional[str] = None
+_YARN_SITE_XML = "yarn-site.xml"
+_RM_WEBAPP_HTTPS_ADDRESS_KEY = "yarn.resourcemanager.webapp.https.address"
+_RM_WEBAPP_HTTP_ADDRESS_KEY = "yarn.resourcemanager.webapp.address"
+_RM_ADDRESS: Optional[str] = None
+_NODE_SCHEME = "http"
+
+_RM_LOG_REGEX = r"(?<=\")\/logs.+?RESOURCEMANAGER.+?(?=\")"
+_NM_LOG_REGEX = r"(?<=\")\/logs.+?NODEMANAGER.+?(?=\")"
+_INPUT_TIME_FORMAT = '%a %b %d %H:%M:%S %Z %Y'  # e.g. Wed May 28 07:35:39 UTC 
2025
+_OUTPUT_TIME_FORMAT = '%Y-%m-%d %H:%M:%S,%f'    # e.g. 2025-05-28 11:57:05,435
+_OUTPUT_TIME_FORMAT_WITHOUT_SECOND = '%Y-%m-%d %H:%M'  # e.g. 2025-05-28 11:57
+_NUMBER_OF_JSTACK = 3
+
+args = None
+
+
+def list_issues() -> None:
+    print("application_diagnostic:appId", "scheduler_related_issue", sep="\n")
+
+
+def application_diagnostic() -> str:
+    """
+    Application Logs, Application Info, Application Attempts
+    Multiple JStack of Hanging Containers and NodeManager
+    ResourceManager logs during job duration.
+    NodeManager logs from NodeManager where hanging containers of jobs run 
during the duration of containers.
+    """
+    if args.arguments is None or len(args.arguments) == 0:
+        logger.error("Missing application or job id")
+        sys.exit(os.EX_USAGE)
+
+    app_id = args.arguments[0]
+    logger.info("Collecting application diagnostics for %s", app_id)
+    output_path = _create_output_dir(os.path.join(_TEMP_DIR, app_id))
+
+    nm_address = _get_nodemanager_address(app_id)
+    app_jstack = _create_request(
+        _web_url(_NODE_SCHEME, nm_address, 
"ws/v1/node/apps/{}/jstack/{}".format(app_id, _NUMBER_OF_JSTACK)),
+        False)
+    _write_output(output_path, "application_jstack", app_jstack)
+
+    nm_jstack = _create_request(
+        _web_url(_NODE_SCHEME, nm_address, 
"ws/v1/node/jstack/{}".format(_NUMBER_OF_JSTACK)),
+        False)
+    _write_output(output_path, "nm_{}_jstack".format(nm_address), nm_jstack)
+
+    app_info = _create_request(_rm_url("ws/v1/cluster/apps/{}".format(app_id)))
+    _write_output(output_path, "application_info", app_info)
+
+    app_attempts = 
_create_request(_rm_url("ws/v1/cluster/apps/{}/appattempts".format(app_id)))
+    _write_output(output_path, "application_attempts", app_attempts)
+
+    start_time, end_time = _get_application_time(app_info)
+
+    log_address = _get_node_log_address(_RM_ADDRESS, _RM_LOG_REGEX, 
_NODE_SCHEME)
+    _write_output(os.path.join(output_path, "node_log"), "resourcemanager_log",
+                  _filter_node_log(log_address, start_time, end_time, 
_NODE_SCHEME))
+
+    if "amHostHttpAddress" in app_info:
+        app_info_xml = ET.fromstring(app_info)
+        nm_address = app_info_xml.find("amHostHttpAddress").text
+        log_address = _get_node_log_address(nm_address, _NM_LOG_REGEX, 
_NODE_SCHEME)
+        _write_output(os.path.join(output_path, "node_log"), "nodemanager_log",
+                      _get_container_log(log_address, app_id, _NODE_SCHEME))
+
+    command = _run_cmd_and_save_output(os.path.join(output_path, "app_logs"), 
app_id,
+                                       "yarn", "logs", "-applicationId", 
app_id)
+    command.communicate()
+    logger.info("Application diagnostics written to %s", output_path)
+    return output_path
+
+
+def scheduler_related_issue() -> str:
+    """
+    ResourceManager Scheduler Logs with DEBUG enabled for 2 minutes.
+    Multiple Jstack of ResourceManager
+    YARN and Scheduler Configuration
+    Cluster Scheduler API /ws/v1/cluster/scheduler and Cluster Nodes API 
/ws/v1/cluster/nodes response
+    Scheduler Activities /ws/v1/cluster/scheduler/bulk-activities response
+    """
+    logger.info("Collecting scheduler-related diagnostics")
+    output_path = _create_output_dir(
+        os.path.join(_TEMP_DIR, "scheduler_related_issue" + 
str(time.time()).split(".")[0]))
+
+    rm_jstack = 
_create_request(_rm_url("ws/v1/node/jstack/{}".format(_NUMBER_OF_JSTACK)), 
False)
+    _write_output(output_path, "rm_{}_jstack".format(_RM_ADDRESS), rm_jstack)
+
+    scheduler_info = _create_request(_rm_url("ws/v1/cluster/scheduler"))
+    _write_output(output_path, "scheduler_info", scheduler_info)
+
+    nodes_info = _create_request(_rm_url("ws/v1/cluster/nodes"))
+    _write_output(output_path, "nodemanager_info", nodes_info)
+
+    scheduler_activities = 
_create_request(_rm_url("ws/v1/cluster/scheduler/bulk-activities"))
+    _write_output(output_path, "scheduler_activities", scheduler_activities)
+
+    scheduler_config = _create_request(_rm_url("ws/v1/cluster/scheduler-conf"))
+    _write_output(output_path, "scheduler_configuration", scheduler_config)
+
+    yarn_conf = _run_command("cat", os.path.join(_HADOOP_CONF_DIR, 
_YARN_SITE_XML))
+    _write_output(output_path, "yarn_site", yarn_conf)
+
+    enable_debug_log = _set_rm_scheduler_log_level("DEBUG")
+    logger.info("Set RM scheduler log level to DEBUG: %s", enable_debug_log)
+    log_address = _get_node_log_address(_RM_ADDRESS, _RM_LOG_REGEX, 
_NODE_SCHEME)
+    start_time, end_time = (_format_datetime_no_seconds(datetime.now() - 
timedelta(seconds=120)),

Review Comment:
   This collects the previous two minutes immediately after enabling DEBUG, 
rather than collecting two minutes of DEBUG output.
   
   Unless DEBUG was already enabled, most of this interval will not contain the 
scheduler diagnostics that this operation intends to capture.
   
   Could we record the start time when DEBUG is enabled, wait for the 
collection interval, and then retrieve that interval's logs?  Please also 
restore the previous log level in a `finally` block instead of unconditionally 
setting INFO, so failures do not leave DEBUG enabled or overwrite an existing 
setting.



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