| ############################################################################ | |
| # Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | |
| ############################################################################ | |
| """Test that bf_allow_magnetic_slot makes backfill respect magnetic job priority.""" | |
| import re | |
| from datetime import datetime | |
| import pytest | |
| import atf | |
| RESV_NAME = "test_resv" | |
| MAGNETIC_SLOT = "bf_allow_magnetic_slot" | |
| HIGH_PRIORITY = 1000000000 | |
| # sdiag prints "Total cycles" for the main scheduler and for backfill | |
| BF_CYCLES_RE = re.compile(r"Backfilling stats.*?Total cycles:\s*(\d+)", re.DOTALL) | |
| # SETUP | |
| @pytest.fixture(scope="module", autouse=True) | |
| def setup(): | |
| atf.require_nodes(2) | |
| atf.require_config_parameter("SchedulerType", "sched/backfill") | |
| atf.require_config_parameter("PriorityType", "priority/multifactor") | |
| # Only backfill can start jobs or plan their start time | |
| atf.require_config_parameter_includes("SchedulerParameters", ("sched_interval", -1)) | |
| atf.require_config_parameter_includes("SchedulerParameters", "defer") | |
| atf.require_config_parameter_includes("SchedulerParameters", ("bf_interval", 1)) | |
| # Plan a backfill slot for every pending job | |
| atf.require_config_parameter_includes( | |
| "SchedulerParameters", ("bf_min_prio_reserve", 0) | |
| ) | |
| atf.require_slurm_running() | |
| # Module scope, so it is deleted after conftest has cancelled the jobs in it | |
| @pytest.fixture(scope="module") | |
| def magnetic_resv(): | |
| atf.run_command( | |
| f"scontrol create reservationname={RESV_NAME} nodes=ALL " | |
| f"user={atf.properties['test-user']} start=now duration=UNLIMITED " | |
| "flags=MAGNETIC", | |
| user=atf.properties["slurm-user"], | |
| fatal=True, | |
| ) | |
| for _ in atf.timer(fatal=True): | |
| if atf.get_reservation_parameter(RESV_NAME, "State") == "ACTIVE": | |
| break | |
| yield RESV_NAME | |
| atf.run_command( | |
| f"scontrol delete reservationname={RESV_NAME}", | |
| user=atf.properties["slurm-user"], | |
| fatal=True, | |
| ) | |
| # HELPER FUNCTIONS | |
| def set_magnetic_slot(enabled): | |
| """Set or unset bf_allow_magnetic_slot and wait until slurmctld uses it""" | |
| if enabled: | |
| atf.require_config_parameter_includes("SchedulerParameters", MAGNETIC_SLOT) | |
| else: | |
| atf.require_config_parameter_excludes("SchedulerParameters", MAGNETIC_SLOT) | |
| for _ in atf.timer(fatal=True): | |
| if ( | |
| atf.config_parameter_includes( | |
| "SchedulerParameters", MAGNETIC_SLOT, live=True | |
| ) | |
| == enabled | |
| ): | |
| break | |
| def submit_jobs(): | |
| """Run A on one node, then submit B on every node and C on one node. | |
| All of them are attracted by the magnetic reservation. B has a fixed | |
| higher priority than C, and C would delay B if it started now. | |
| """ | |
| job_a = atf.submit_job_sbatch( | |
| "-N1 --exclusive -t3 --wrap 'sleep infinity'", fatal=True | |
| ) | |
| atf.wait_for_job_state(job_a, "RUNNING", fatal=True) | |
| assert ( | |
| atf.get_job_parameter(job_a, "Reservation", fatal=True) == RESV_NAME | |
| ), "A should run in the magnetic reservation" | |
| node_cnt = atf.get_reservation_parameter(RESV_NAME, "NodeCnt") | |
| job_b = atf.submit_job_sbatch( | |
| f"-N{node_cnt} --exclusive -t1 --wrap 'sleep infinity'", fatal=True | |
| ) | |
| atf.run_command( | |
| f"scontrol update jobid={job_b} priority={HIGH_PRIORITY}", | |
| user=atf.properties["slurm-user"], | |
| fatal=True, | |
| ) | |
| job_c = atf.submit_job_sbatch( | |
| "-N1 --exclusive -t30 --wrap 'sleep infinity'", fatal=True | |
| ) | |
| prio_b = atf.get_job_parameter(job_b, "Priority", fatal=True) | |
| prio_c = atf.get_job_parameter(job_c, "Priority", fatal=True) | |
| assert prio_b > prio_c, "B should have a higher priority than C" | |
| return job_b, job_c | |
| def get_bf_cycles(): | |
| """Return the number of backfill cycles from sdiag""" | |
| output = atf.run_command_output("sdiag", fatal=True, quiet=True) | |
| match = BF_CYCLES_RE.search(output) | |
| assert match, "sdiag should report the backfill cycles" | |
| return int(match.group(1)) | |
| def has_future_start(job): | |
| """Return True if the job has an expected start time in the future""" | |
| start = str(job.get("StartTime")) | |
| if not re.fullmatch(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d", start): | |
| return False | |
| return datetime.fromisoformat(start) > datetime.now() | |
| # TESTS | |
| def test_default_no_magnetic_slot(magnetic_resv): | |
| """Verify that by default backfill starts C before the magnetic job B""" | |
| set_magnetic_slot(False) | |
| job_b, job_c = submit_jobs() | |
| assert atf.wait_for_job_state( | |
| job_c, "RUNNING" | |
| ), "C should start because B has no backfill slot" | |
| assert ( | |
| atf.get_job_parameter(job_c, "Reservation", fatal=True) == magnetic_resv | |
| ), "C should run in the magnetic reservation" | |
| assert ( | |
| atf.get_job_parameter(job_b, "JobState", fatal=True) == "PENDING" | |
| ), "B should still be pending" | |
| def test_magnetic_slot(magnetic_resv): | |
| """Verify that with bf_allow_magnetic_slot backfill keeps C behind B""" | |
| set_magnetic_slot(True) | |
| job_b, job_c = submit_jobs() | |
| # Wait until backfill has planned C | |
| cycles = get_bf_cycles() | |
| for _ in atf.timer(fatal=True): | |
| job = atf.get_job(job_c) | |
| if job["JobState"] != "PENDING": | |
| break | |
| if has_future_start(job) and get_bf_cycles() > cycles: | |
| break | |
| assert job["JobState"] == "PENDING", "C should wait for the slot of B" | |
| assert ( | |
| atf.get_job_parameter(job_b, "JobState", fatal=True) == "PENDING" | |
| ), "B should still be pending" | |
| assert has_future_start(atf.get_job(job_b)), "B should have a planned start" | |
| # Without B, C can start now | |
| atf.run_command(f"scancel {job_b}", fatal=True) | |
| assert atf.wait_for_job_state( | |
| job_c, "RUNNING" | |
| ), "C should start once B is cancelled" | |
| assert ( | |
| atf.get_job_parameter(job_c, "Reservation", fatal=True) == magnetic_resv | |
| ), "C should run in the magnetic reservation" |