fix(scheduler): separate eligibility from negative GPU scores

This commit is contained in:
Arnaud Dartois committed 2026-10-20 02:31:17 +02:00
1 parent 21f0530b6c
commit e2a7442175
2 files changed
+34 -5

No files matched your search

@@ -280,7 +280,7 @@ public sealed class AnalysisJobSchedulerService : IAnalysisJobSchedulerService
Agent = a,
Score = ComputeAgentScore(a, job, workloadMap)
})
.Where(x => x.Score >= 0) // -1 means disqualified
.Where(x => x.Score.HasValue)
.OrderBy(x => x.Score)
.ThenBy(x => workloadMap.TryGetValue(x.Agent.Id, out var c) ? c : 0)
.ToList();
@@ -300,19 +300,19 @@ public sealed class AnalysisJobSchedulerService : IAnalysisJobSchedulerService
/// - RAM: 0 if sufficient, +10 per missing GB
/// - Load: +count of active jobs (prefer less loaded agents)
/// </summary>
private static int ComputeAgentScore(Agent agent, AnalysisJob job, Dictionary<Guid, int> workload)
private static int? ComputeAgentScore(Agent agent, AnalysisJob job, Dictionary<Guid, int> workload)
{
// Disqualify: GPU required but agent doesn't have it
if (job.RequiresGpu && !agent.HasGpu)
return -1;
return null;
// Disqualify: CPU cores insufficient
if (job.RequiredCpuCores > 0 && agent.CpuCores < job.RequiredCpuCores)
return -1;
return null;
// Disqualify: RAM insufficient
if (job.RequiredMemoryMb > 0 && agent.MemoryMb < job.RequiredMemoryMb)
return -1;
return null;
var score = 0;
@@ -53,6 +53,35 @@ public class AnalysisJobSchedulerServiceTests : IAsyncLifetime
return new TestDbContextFactory(opts);
}
public static IEnumerable<object[]> GpuScenarios =>
from type in Enum.GetValues<AnalysisJobType>()
from sameSite in new[] { true, false }
from loaded in new[] { true, false }
select new object[] { type, sameSite, loaded };
[Theory]
[MemberData(nameof(GpuScenarios))]
public async Task SelectBestAgent_SoleGpu_RemainsEligible(AnalysisJobType type, bool sameSite, bool loaded)
{
var (siteId, cameraId) = await SeedSiteAndCameraAsync();
await using var db = await _dbFactory.CreateDbContextAsync();
var otherSite = new Site("remote", "remote", "UTC");
if (!sameSite) db.Sites.Add(otherSite);
var agent = new Agent(sameSite ? siteId : otherSite.Id, "gpu", "gpu", "1.0");
agent.Register("hash", "prefix");
agent.UpdateCapabilities(true, 8, 8192, GpuAcceleration.NvidiaCuda);
db.Agents.Add(agent);
if (loaded)
{
var active = new AnalysisJob(cameraId, siteId, type, AnalysisJobPriority.Periodic);
active.Dispatch(agent.Id);
db.AnalysisJobs.Add(active);
}
await db.SaveChangesAsync();
var created = await _service.CreateJobAsync(cameraId, type, AnalysisJobPriority.Periodic);
(await _service.SelectBestAgentAsync(created.Data!, CancellationToken.None))!.Id.ShouldBe(agent.Id);
}
// ── CreateJob ───────────────────────────────────────────────────
[Fact]