blob: 1c52141404df7faeee16e5212b75e687e8b96188 [file] [edit]
############################################################################
# Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
############################################################################
"""Test srun --exclude for steps in an allocation.
srun --exclude must only remove nodes from those a step can use, so concurrent
"srun -x <node> -N1" steps each start on a free node instead of waiting for the
same one. An "srun -x <node> -N2" step runs when more than two nodes are left,
and an "srun -x <node>" step without --nodes runs on the nodes left. A
--nodelist shorter than --nodes gets its extra nodes from any free node, with
--exclude and without it as a control. --distribution=arbitrary with --exclude
and no node list fails in srun, as it does without --exclude, since --exclude
gives it no host list to lay tasks out from.
"""
import pytest
import atf
NODE_CPUS = 4
NODES = 4
@pytest.fixture(scope="module", autouse=True)
def setup():
atf.require_nodes(NODES, [("CPUs", NODE_CPUS), ("ThreadsPerCore", 1)])
atf.require_config_parameter("SelectType", "select/cons_tres")
atf.require_config_parameter("SelectTypeParameters", "CR_CPU")
# --stepmgr is silently dropped without it, see PrologFlags in slurm.conf(5)
atf.require_config_parameter_includes("PrologFlags", "Contain")
atf.require_slurm_running()
def _allocate(stepmgr):
"""Return a job with NODE_CPUS CPUs on each node and no step running."""
stepmgr_arg = "--stepmgr" if stepmgr else ""
job_id = atf.submit_job_sbatch(
f"-N{NODES} --ntasks-per-node={NODE_CPUS} {stepmgr_arg} "
"--wrap 'sleep infinity'",
fatal=True,
)
atf.wait_for_job_state(job_id, "RUNNING", fatal=True)
# TODO: Issue 50980. A step started too soon after RUNNING can be
# rejected, so retry a step that holds no resources until one runs.
probe = f"srun --jobid={job_id} --overlap --mem=0 true"
for _ in atf.timer(fatal=True):
if atf.run_command_exit(probe) == 0:
break
for _ in atf.timer(fatal=True):
if not _srun_steps(job_id):
break
return job_id
def _srun_steps(job_id):
"""Return the srun steps of a job by step id."""
return {
step_id: step
for step_id, step in atf.get_steps(job_id, quiet=True, fatal=True).items()
if step_id.split(".")[1] not in ("batch", "extern", "interactive")
}
def _other_nodes(job_id):
"""Return the batch host and the job's other nodes in node list order."""
batch_host = atf.get_job_parameter(job_id, "BatchHost", fatal=True)
nodes = atf.node_range_to_list(
atf.get_job_parameter(job_id, "NodeList", fatal=True)
)
return batch_host, [node for node in nodes if node != batch_host]
@pytest.mark.skipif(
atf.get_version("bin/srun") < (25, 11),
reason="Ticket 23311: srun hit an assertion excluding nodes in an allocation. Fixed in 25.11",
)
@pytest.mark.xfail(
atf.get_version("bin/srun") < (26, 11, 1),
reason="Ticket 26118: srun pinned --exclude steps to the same nodes. Fixed in 26.11.1",
)
@pytest.mark.parametrize("stepmgr", [False, True], ids=["slurmctld", "stepmgr"])
def test_concurrent_excluded_steps(stepmgr):
"""Test concurrent steps excluding the batch host each run on a free node."""
job_id = _allocate(stepmgr)
batch_host, other_nodes = _other_nodes(job_id)
# Each step takes every CPU of a node, so no two can share one
for _ in other_nodes:
atf.run_command(
f"srun --jobid={job_id} --mem=0 -x {batch_host} -N1 -n{NODE_CPUS} "
"sleep infinity",
background=True,
)
for _ in atf.timer():
steps = list(_srun_steps(job_id).values())
states = sorted(step["State"] for step in steps)
if states == ["RUNNING"] * len(other_nodes):
break
assert states == ["RUNNING"] * len(
other_nodes
), f"Every step excluding {batch_host} should run at once, got {states}"
assert sorted(step["NodeList"] for step in steps) == sorted(
other_nodes
), f"Each step should run on its own node other than {batch_host}"
@pytest.mark.skipif(
atf.get_version("bin/srun") < (25, 11),
reason="Ticket 23311: srun hit an assertion excluding nodes in an allocation. Fixed in 25.11",
)
@pytest.mark.parametrize("stepmgr", [False, True], ids=["slurmctld", "stepmgr"])
def test_multi_node_excluded_step(stepmgr):
"""Test an --exclude step needing fewer nodes than are left still runs."""
job_id = _allocate(stepmgr)
batch_host, other_nodes = _other_nodes(job_id)
result = atf.run_command(
f"srun --jobid={job_id} --mem=0 -x {batch_host} -N2 -n2 "
"printenv SLURMD_NODENAME"
)
assert (
result["exit_code"] == 0
), f"Step excluding {batch_host} should run, got: {result['stderr']}"
nodes = result["stdout"].split()
assert len(set(nodes)) == 2 and set(nodes) <= set(
other_nodes
), f"Step should run on two nodes other than {batch_host}, got {nodes}"
@pytest.mark.parametrize("stepmgr", [False, True], ids=["slurmctld", "stepmgr"])
@pytest.mark.parametrize(
"ntasks_arg",
[
pytest.param("", id="default_ntasks"),
pytest.param(
"-n1",
id="one_task",
marks=pytest.mark.skipif(
atf.get_version("bin/srun") < (25, 11),
reason="Ticket 23311: srun hit an assertion excluding nodes in an allocation. Fixed in 25.11",
),
),
],
)
def test_excluded_step_without_nodes(ntasks_arg, stepmgr):
"""Test an --exclude step without --nodes runs on the nodes left."""
job_id = _allocate(stepmgr)
batch_host, other_nodes = _other_nodes(job_id)
result = atf.run_command(
f"srun --jobid={job_id} --mem=0 -x {batch_host} {ntasks_arg} "
"printenv SLURMD_NODENAME"
)
assert (
result["exit_code"] == 0
), f"Step excluding {batch_host} should run, got: {result['stderr']}"
nodes = result["stdout"].split()
assert nodes and set(nodes) <= set(
other_nodes
), f"Step should run only on nodes other than {batch_host}, got {nodes}"
@pytest.mark.parametrize("stepmgr", [False, True], ids=["slurmctld", "stepmgr"])
@pytest.mark.parametrize(
"exclude",
[
pytest.param(False, id="nodelist"),
pytest.param(
True,
id="exclude",
marks=pytest.mark.xfail(
atf.get_version("bin/srun") < (26, 11, 1),
reason="Ticket 26118: srun added busy nodes to a short --nodelist with --exclude. Fixed in 26.11.1",
),
),
],
)
def test_short_nodelist_gets_free_node(exclude, stepmgr):
"""Test a step with fewer --nodelist nodes than --nodes adds a free node."""
job_id = _allocate(stepmgr)
batch_host, (listed, busy, free) = _other_nodes(job_id)
# Fill the node older srun padded the list with when using --exclude.
# With --exclude only "free" can be added, without it the batch host can too
atf.run_command(
f"srun --jobid={job_id} --mem=0 -w {busy} -N1 -n{NODE_CPUS} sleep infinity",
background=True,
)
for _ in atf.timer(fatal=True):
busy_steps = _srun_steps(job_id)
if [step["State"] for step in busy_steps.values()] == ["RUNNING"]:
break
exclude_arg = f"-x {batch_host}" if exclude else ""
atf.run_command(
f"srun --jobid={job_id} --mem=0 -w {listed} {exclude_arg} -N2 "
f"-n{2 * NODE_CPUS} sleep infinity",
background=True,
)
for _ in atf.timer():
steps = [
step
for step_id, step in _srun_steps(job_id).items()
if step_id not in busy_steps
]
states = [step["State"] for step in steps]
if states == ["RUNNING"]:
break
assert states == [
"RUNNING"
], f"Step on {listed} should not wait for busy {busy}, got {states}"
nodes = atf.node_range_to_list(steps[0]["NodeList"])
assert (
len(nodes) == 2 and listed in nodes and busy not in nodes
), f"Step should run on {listed} and a free node, got {nodes}"
if exclude:
assert nodes == [listed, free], f"Step should not run on excluded {batch_host}"
@pytest.mark.xfail(
atf.get_version("bin/srun") < (26, 11, 1),
reason="Ticket 26118: srun laid out --exclude arbitrary steps on the nodes left. Fixed in 26.11.1",
)
@pytest.mark.parametrize("stepmgr", [False, True], ids=["slurmctld", "stepmgr"])
def test_arbitrary_exclude_without_nodelist(stepmgr):
"""Test --distribution=arbitrary --exclude without a node list fails in srun."""
job_id = _allocate(stepmgr)
batch_host, other_nodes = _other_nodes(job_id)
result = atf.run_command(
f"srun --jobid={job_id} --mem=0 -m arbitrary -x {batch_host} "
f"-n{len(other_nodes)} printenv SLURMD_NODENAME"
)
assert (
result["exit_code"] != 0
), f"Step without a host list should not run, got: {result['stdout']}"
assert (
"hostlist specified 0 nodes" in result["stderr"]
), f"srun should reject the step itself, got: {result['stderr']}"