KatInterview

Fixing Priority Preemption in a GPU Lease Scheduler

System DesignModel answer
MediumModel answerSystem Designconcurrencyschedulingdebugging

Last updated

Diagnose why a GPU lease manager preempts the wrong tenant, rewrite victim selection to evict the weakest lease, and design regression tests.

A cluster control plane hands out exclusive GPU leases to training jobs. The GPUManager class keeps a map from gpu_id to the current tenant (job_id plus a numeric priority); a fresh request takes an unused GPU if one exists, and otherwise may displace a running tenant.

Operators report that high-priority jobs are sometimes denied even though less important work is still holding hardware. The allocation code in its current shape:

class GPUManager:
    def __init__(self, total_gpus):
        self.total_gpus = total_gpus
        self.active_leases = {}  # gpu_id -> {'job_id': ..., 'priority': ...}

    def request_gpu(self, job_id, priority):
        free_gpus = [i for i in range(self.total_gpus) if i not in self.active_leases]
        if free_gpus:
            self.active_leases[free_gpus[0]] = {'job_id': job_id, 'priority': priority}
            return True

        for gpu_id, lease in self.active_leases.items():
            if priority > lease['priority']:
                self.active_leases[gpu_id] = {'job_id': job_id, 'priority': priority}
                return True
        return False

Concrete symptom: on a machine with one GPU and jobs arriving in priority order 1, then 2, then 3, the third request is rejected although it outranks the incumbent.

Deliver three things: (1) explain the defect in the preemption path, including why the reported single-GPU sequence may not reproduce it from this snippet alone; (2) rewrite request_gpu so preemption follows a stated, defensible policy; (3) describe unit tests — including adversarial ones — that would have caught this class of bug.

Hints

Hint 1

Trace the reported sequence line by line before trusting it; if the snippet already passes that scenario, the production code probably differs in a way the report hints at.

Hint 2

Ask what the loop is actually deciding: it commits to whichever lease it meets first rather than answering 'is any tenant beatable?'

Hint 3

Preemption destroys live work, so the policy should both check the minimum priority and evict the least valuable lease — with a total order for ties.