| ############################################################################ | |
| # Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | |
| ############################################################################ | |
| """Clearing a pending job's GRES via scontrol update must fix its accounting. | |
| Ticket 24896: updating a held job's GRES to zero left the stale count in | |
| tres_req_cnt (hence ReqTRES) and gres_list_req, so the job was still scheduled | |
| and accounted as requesting the cleared GRES. These tests pin both sides of | |
| the fix: an update that removes the job's only GRES (the requested counts must | |
| be cleared explicitly) and one that clears a single GRES while keeping another | |
| (the retained count must survive). The first test also proves the cleared job | |
| can run while another job holds the GPU on the same node. | |
| """ | |
| import pytest | |
| import atf | |
| @pytest.fixture(scope="module", autouse=True) | |
| def setup(): | |
| atf.require_version( | |
| (26, 11), | |
| "sbin/slurmctld", | |
| reason="Ticket 24896: clearing a pending job's GRES to zero added in 26.11", | |
| ) | |
| atf.require_config_parameter("SelectType", "select/cons_tres") | |
| atf.require_config_parameter("SelectTypeParameters", "CR_CPU") | |
| atf.require_tty(0) | |
| atf.require_config_parameter( | |
| "Name", | |
| {"gpu": {"File": "/dev/tty0"}, "tmpfs": {"Count": 100}}, | |
| source="gres", | |
| ) | |
| atf.require_config_parameter_includes("GresTypes", "gpu") | |
| atf.require_config_parameter_includes("GresTypes", "tmpfs") | |
| atf.require_accounting() | |
| # Register both GRES as accounting TRES so their requested and allocated | |
| # counts are visible in ReqTRES and AllocTRES. | |
| atf.require_config_parameter_includes("AccountingStorageTRES", "gres/gpu") | |
| atf.require_config_parameter_includes("AccountingStorageTRES", "gres/tmpfs") | |
| nodes = atf.require_nodes( | |
| 1, [("Gres", "gpu:1,tmpfs:100"), ("CPUs", 2), ("RealMemory", 2)] | |
| ) | |
| atf.require_slurm_running() | |
| return nodes[0] | |
| def test_update_pending_job_clearing_only_gres_clears_accounting(setup): | |
| """Updating a pending job's only GRES to zero must remove it from the | |
| requested accounting TRES (ReqTRES), and the job must then be allocated and | |
| accounted without that GRES. | |
| This exercises the case where the update leaves no GRES at all, so the | |
| requested GRES counts have to be cleared explicitly. | |
| """ | |
| # Occupy the GPU but leave one CPU and 1 MB for the updated job. This | |
| # works even when the cluster has just one node and that node has a GPU. | |
| blocker_id = atf.submit_job_sbatch( | |
| f'-w {setup} -N1 -n1 -c1 --mem=1 --gres=gpu:1 -t5 --wrap "sleep 300"', | |
| fatal=True, | |
| ) | |
| job_ids = [blocker_id] | |
| try: | |
| atf.wait_for_job_state(blocker_id, "RUNNING", fatal=True) | |
| gpu_node = atf.get_job_parameter(blocker_id, "NodeList") | |
| assert gpu_node, f"running blocker {blocker_id} has no NodeList recorded" | |
| # Keep the job held until its GRES is cleared, and on the blocker's | |
| # node so it cannot avoid the occupied GPU by running elsewhere. | |
| job_id = atf.submit_job_sbatch( | |
| f'--hold -w {gpu_node} -N1 -n1 -c1 --mem=1 --gres=gpu:1 -t1 --wrap "true"', | |
| fatal=True, | |
| ) | |
| job_ids.append(job_id) | |
| req_tres = atf.get_job_parameter(job_id, "ReqTRES", "") | |
| assert ( | |
| "gres/gpu=1" in req_tres | |
| ), f"Submitted GPU request missing from ReqTRES: ReqTRES={req_tres}" | |
| atf.run_command(f"scontrol update JobId={job_id} Gres=gpu:0", fatal=True) | |
| # No GRES is left, so gres/gpu must disappear from the requested TRES. | |
| req_tres = atf.get_job_parameter(job_id, "ReqTRES", "") | |
| assert ( | |
| "gres/gpu=" not in req_tres | |
| ), f"gres/gpu should be cleared from ReqTRES after Gres=gpu:0: ReqTRES={req_tres}" | |
| # The job must finish without a GPU while the blocker still holds it. | |
| atf.run_command(f"scontrol release {job_id}", fatal=True) | |
| # Raised over the default: a loaded runner includes the whole epilog. | |
| atf.wait_for_job_state(job_id, "COMPLETED", timeout=120, fatal=True) | |
| job_node = atf.get_job_parameter(job_id, "NodeList") | |
| assert job_node, f"completed job {job_id} has no NodeList recorded" | |
| assert job_node == gpu_node, f"Job ran on {job_node}, expected {gpu_node}" | |
| assert ( | |
| atf.get_job_parameter(blocker_id, "JobState") == "RUNNING" | |
| ), "The blocker must still hold the GPU when the updated job completes" | |
| alloc_tres = atf.get_job_parameter(job_id, "AllocTRES", "") | |
| assert ( | |
| "gres/gpu=" not in alloc_tres | |
| ), f"A job updated to gpu:0 must not allocate any GPU: AllocTRES={alloc_tres}" | |
| finally: | |
| atf.cancel_jobs(job_ids, quiet=True) | |
| def test_update_pending_job_clearing_one_gres_keeps_another(): | |
| """Clearing one GRES of a pending job while keeping another must remove only | |
| the cleared GRES from the requested/allocated accounting TRES and preserve | |
| the retained GRES count. | |
| This exercises the case where the update still leaves a GRES requested, so | |
| the retained counts must survive while the cleared one is zeroed. | |
| """ | |
| job_id = atf.submit_job_sbatch( | |
| '--hold -N1 -n1 -c1 --mem=1 --gres=gpu:1,tmpfs:100 -t1 --wrap "true"', | |
| fatal=True, | |
| ) | |
| try: | |
| req_tres = atf.get_job_parameter(job_id, "ReqTRES", "") | |
| assert ( | |
| "gres/gpu=1" in req_tres and "gres/tmpfs=100" in req_tres | |
| ), f"Submitted GRES not fully reflected in ReqTRES: ReqTRES={req_tres}" | |
| # Clear the GPU but keep tmpfs at its submitted count. | |
| atf.run_command( | |
| f"scontrol update JobId={job_id} Gres=gpu:0,tmpfs:100", fatal=True | |
| ) | |
| req_tres = atf.get_job_parameter(job_id, "ReqTRES", "") | |
| assert ( | |
| "gres/gpu=" not in req_tres | |
| ), f"gres/gpu should be cleared from ReqTRES: ReqTRES={req_tres}" | |
| assert ( | |
| "gres/tmpfs=100" in req_tres | |
| ), f"gres/tmpfs should be preserved in ReqTRES: ReqTRES={req_tres}" | |
| # The job must be allocated with tmpfs but no GPU. | |
| atf.run_command(f"scontrol release {job_id}", fatal=True) | |
| atf.wait_for_job_state(job_id, "COMPLETED", timeout=120, fatal=True) | |
| alloc_tres = atf.get_job_parameter(job_id, "AllocTRES", "") | |
| assert ( | |
| "gres/gpu=" not in alloc_tres | |
| ), f"Cleared GPU must not appear in AllocTRES: AllocTRES={alloc_tres}" | |
| assert ( | |
| "gres/tmpfs=100" in alloc_tres | |
| ), f"Retained tmpfs must appear in AllocTRES: AllocTRES={alloc_tres}" | |
| finally: | |
| atf.cancel_jobs([job_id], quiet=True) |