refactor: replace yarn CLI shell-out with YARN REST API

yarn_client.py no longer invokes the 'yarn' binary via subprocess; it uses
httpx against /ws/v1/cluster/apps/* endpoints. This means the runtime image
no longer needs the Hadoop client installation — the only YARN-side
dependency left in the container is the config dir consumed by
spark-submit itself.

New model field:
  - Job.yarn_rm_url: str | None
  - PendingSubmission.yarn_rm_url: str | None (snapshotted at prepare)

prepare_submit_job snapshots Connection.yarn_rm_url into the pending
record (consistent with the existing master/deploy_mode/spark_conf
snapshot pattern); confirm_submit_job copies it onto the Job so
status/logs/kill can use it without re-looking-up the connection.

Resolution order for the RM URL at runtime:
  1. Job.yarn_rm_url (preferred — survives connection edits/deletes)
  2. Connection.yarn_rm_url fallback (if a future tool is added that
     doesn't go through a Job)
  3. YARN_RESOURCE_MANAGER_URL env var

Errors:
  - YarnConfigError (HTTP 4xx semantics) when URL is missing/malformed
  - YarnError for HTTP 4xx/5xx from the RM, network failures, missing
    state field, or unparseable log responses

10 new tests in test_yarn_client.py cover the REST surface:
success, 404, 5xx, missing state field, env-var fallback, malformed
URL, log 404 with log-aggregation hint, kill PUT body shape, and
httpx connection-error wrapping.
This commit is contained in:
Claude
2026-06-24 17:33:36 +08:00
parent 0d7b1d1ec3
commit b3564737f7
12 changed files with 252 additions and 91 deletions
+106 -37
View File
@@ -1,9 +1,11 @@
# coding=utf-8
from unittest.mock import MagicMock, patch
from unittest.mock import patch
import httpx
import pytest
from spark_executor.core.yarn_client import (
YarnConfigError,
YarnError,
get_application_logs,
get_application_status,
@@ -11,53 +13,120 @@ from spark_executor.core.yarn_client import (
)
def _fake_proc(returncode: int, stdout: str = "", stderr: str = ""):
p = MagicMock()
p.returncode = returncode
p.stdout = stdout
p.stderr = stderr
return p
RM = "http://rm:8088"
def test_status_parses_state_line():
fake = _fake_proc(
0,
stdout="Application Report :\n State : RUNNING\n ...\n",
)
with patch("spark_executor.core.yarn_client.subprocess.run", return_value=fake):
state, raw = get_application_status("application_1")
def _resp(status: int, *, json_data=None, text: str | None = None) -> httpx.Response:
if json_data is not None:
return httpx.Response(status, json=json_data)
return httpx.Response(status, text=text or "")
# --- get_application_status ---
def test_status_parses_app_state():
fake = _resp(200, json_data={"app": {"id": "application_1", "state": "RUNNING"}})
with patch("spark_executor.core.yarn_client.httpx.request", return_value=fake) as m:
state, raw = get_application_status("application_1", RM)
assert state == "RUNNING"
assert "RUNNING" in raw
args = m.call_args.args
assert args == ("GET", f"{RM}/ws/v1/cluster/apps/application_1")
def test_status_raises_on_nonzero_return():
fake = _fake_proc(1, stderr="not found")
with patch("spark_executor.core.yarn_client.subprocess.run", return_value=fake):
with pytest.raises(YarnError):
get_application_status("application_x")
def test_status_raises_on_404():
with patch("spark_executor.core.yarn_client.httpx.request", return_value=_resp(404)):
with pytest.raises(YarnError, match="not found"):
get_application_status("application_x", RM)
def test_logs_returns_stdout():
fake = _fake_proc(0, stdout="log line 1\nlog line 2\n")
with patch("spark_executor.core.yarn_client.subprocess.run", return_value=fake) as m:
out = get_application_logs("application_1")
def test_status_raises_on_5xx():
with patch(
"spark_executor.core.yarn_client.httpx.request",
return_value=_resp(503, text="upstream down"),
):
with pytest.raises(YarnError, match="503"):
get_application_status("application_1", RM)
def test_status_raises_when_state_field_missing():
fake = _resp(200, json_data={"app": {"id": "application_1"}})
with patch("spark_executor.core.yarn_client.httpx.request", return_value=fake):
with pytest.raises(YarnError, match="Could not parse YARN state"):
get_application_status("application_1", RM)
def test_status_requires_rm_url():
with patch.dict("os.environ", {}, clear=True):
with pytest.raises(YarnConfigError):
get_application_status("application_1", None)
def test_status_falls_back_to_env_var():
fake = _resp(200, json_data={"app": {"state": "FINISHED"}})
with patch.dict("os.environ", {"YARN_RESOURCE_MANAGER_URL": "http://env-rm:8088"}, clear=False):
with patch("spark_executor.core.yarn_client.httpx.request", return_value=fake) as m:
state, _ = get_application_status("application_1", None)
assert state == "FINISHED"
assert "env-rm:8088" in m.call_args.args[1]
def test_status_rejects_non_http_url():
with pytest.raises(YarnConfigError, match="must start with"):
get_application_status("application_1", "rm:8088")
# --- get_application_logs ---
def test_logs_returns_text():
fake = _resp(200, text="log line 1\nlog line 2\n")
with patch("spark_executor.core.yarn_client.httpx.request", return_value=fake) as m:
out = get_application_logs("application_1", RM)
assert out == "log line 1\nlog line 2\n"
args = m.call_args.args[0]
assert args[:3] == ["yarn", "logs", "-applicationId"]
assert args[3] == "application_1"
assert m.call_args.args == ("GET", f"{RM}/ws/v1/cluster/apps/application_1/aggregated-logs")
def test_kill_invokes_yarn_application_kill():
fake = _fake_proc(0)
with patch("spark_executor.core.yarn_client.subprocess.run", return_value=fake) as m:
kill_application("application_1")
args = m.call_args.args[0]
assert args[:3] == ["yarn", "application", "-kill"]
assert args[3] == "application_1"
def test_logs_raises_on_404_with_explanation():
with patch("spark_executor.core.yarn_client.httpx.request", return_value=_resp(404)):
with pytest.raises(YarnError, match="log-aggregation-enable"):
get_application_logs("application_1", RM)
def test_kill_raises_on_nonzero_return():
fake = _fake_proc(1, stderr="denied")
with patch("spark_executor.core.yarn_client.subprocess.run", return_value=fake):
def test_logs_raises_on_5xx():
with patch(
"spark_executor.core.yarn_client.httpx.request",
return_value=_resp(500, text="boom"),
):
with pytest.raises(YarnError):
kill_application("application_1")
get_application_logs("application_1", RM)
# --- kill_application ---
def test_kill_sends_put_with_killed_state():
fake = _resp(200, json_data={"app": {"state": "KILLED"}})
with patch("spark_executor.core.yarn_client.httpx.request", return_value=fake) as m:
kill_application("application_1", RM)
args = m.call_args.args
assert args == ("PUT", f"{RM}/ws/v1/cluster/apps/application_1/state")
assert m.call_args.kwargs["json"] == {"state": "KILLED"}
def test_kill_raises_on_5xx():
with patch(
"spark_executor.core.yarn_client.httpx.request",
return_value=_resp(403, text="forbidden"),
):
with pytest.raises(YarnError, match="403"):
kill_application("application_1", RM)
# --- connection errors ---
def test_status_wraps_httpx_errors_as_yarn_error():
with patch(
"spark_executor.core.yarn_client.httpx.request",
side_effect=httpx.ConnectError("connection refused"),
):
with pytest.raises(YarnError, match="connection failed"):
get_application_status("application_1", RM)