|
| 1 | +// Copyright 2025 NVIDIA CORPORATION |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package nodelocalgreedy |
| 5 | + |
| 6 | +import ( |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/kai-scheduler/KAI-scheduler/pkg/common/constants" |
| 11 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/actions/common/solvers" |
| 12 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/actions/common/solvers/scenario" |
| 13 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api" |
| 14 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/common_info" |
| 15 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/pod_info" |
| 16 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/podgroup_info" |
| 17 | + "github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/framework" |
| 18 | +) |
| 19 | + |
| 20 | +type nodeLocalGreedyGenerator struct { |
| 21 | + solveCtx *solvers.SolveContext |
| 22 | + generateVictimsQueue solvers.GenerateVictimsQueue |
| 23 | + builder *solvers.PodAccumulatedScenarioBuilder |
| 24 | + scenarios []*scenario.ByNodeScenario |
| 25 | + shouldAdvanceAccumulatedScenario bool |
| 26 | +} |
| 27 | + |
| 28 | +func NewNodeLocalGreedyGenerator(ctx framework.ScenarioGeneratorContext) framework.ScenarioGenerator { |
| 29 | + solveCtx, generateVictimsQueue, ok := solvers.ValidateScenarioGeneratorContext(ctx) |
| 30 | + if !ok { |
| 31 | + return nil |
| 32 | + } |
| 33 | + return &nodeLocalGreedyGenerator{ |
| 34 | + solveCtx: solveCtx, |
| 35 | + generateVictimsQueue: generateVictimsQueue, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (g *nodeLocalGreedyGenerator) Name() string { |
| 40 | + return constants.GeneratorNodeLocalGreedy |
| 41 | +} |
| 42 | + |
| 43 | +func (g *nodeLocalGreedyGenerator) Next() api.ScenarioInfo { |
| 44 | + if !g.ensureBuilder() { |
| 45 | + return nil |
| 46 | + } |
| 47 | + for { |
| 48 | + if sn := g.popScenario(); sn != nil { |
| 49 | + return sn |
| 50 | + } |
| 51 | + accumulated := g.nextValidAccumulatedScenario() |
| 52 | + if accumulated == nil { |
| 53 | + return nil |
| 54 | + } |
| 55 | + g.scenarios = nodeLocalScenarios(g.solveCtx.Session, accumulated) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (g *nodeLocalGreedyGenerator) ensureBuilder() bool { |
| 60 | + if g.builder != nil { |
| 61 | + return true |
| 62 | + } |
| 63 | + victimsQueue := g.generateVictimsQueue() |
| 64 | + if victimsQueue == nil { |
| 65 | + return false |
| 66 | + } |
| 67 | + g.builder = solvers.NewPodAccumulatedScenarioBuilder( |
| 68 | + g.solveCtx.Session, |
| 69 | + g.solveCtx.PartialPendingJob, |
| 70 | + g.solveCtx.RecordedVictimsJobs, |
| 71 | + victimsQueue, |
| 72 | + g.solveCtx.FeasibleNodes, |
| 73 | + ) |
| 74 | + return true |
| 75 | +} |
| 76 | + |
| 77 | +func (g *nodeLocalGreedyGenerator) popScenario() *scenario.ByNodeScenario { |
| 78 | + if len(g.scenarios) == 0 { |
| 79 | + return nil |
| 80 | + } |
| 81 | + sn := g.scenarios[0] |
| 82 | + g.scenarios = g.scenarios[1:] |
| 83 | + return sn |
| 84 | +} |
| 85 | + |
| 86 | +func (g *nodeLocalGreedyGenerator) nextValidAccumulatedScenario() *scenario.ByNodeScenario { |
| 87 | + if g.shouldAdvanceAccumulatedScenario { |
| 88 | + return g.builder.GetNextAccumulatedScenario() |
| 89 | + } |
| 90 | + g.shouldAdvanceAccumulatedScenario = true |
| 91 | + |
| 92 | + return g.builder.GetValidAccumulatedScenario() |
| 93 | +} |
| 94 | + |
| 95 | +func nodeLocalScenarios(session *framework.Session, base *scenario.ByNodeScenario) []*scenario.ByNodeScenario { |
| 96 | + if base == nil { |
| 97 | + return nil |
| 98 | + } |
| 99 | + if len(base.PotentialVictimsTasks()) == 0 { |
| 100 | + if len(base.RecordedVictimsTasks()) == 0 { |
| 101 | + return nil |
| 102 | + } |
| 103 | + return []*scenario.ByNodeScenario{base} |
| 104 | + } |
| 105 | + |
| 106 | + var scenarios []*scenario.ByNodeScenario |
| 107 | + seen := map[string]struct{}{} |
| 108 | + for _, nodeName := range nodeNamesOfJob(base.LatestPotentialVictim()) { |
| 109 | + victimTasks := base.VictimsTasksFromNodes([]string{nodeName}) |
| 110 | + if len(victimTasks) == 0 { |
| 111 | + continue |
| 112 | + } |
| 113 | + key := victimUIDSetKey(victimTasks) |
| 114 | + if _, found := seen[key]; found { |
| 115 | + continue |
| 116 | + } |
| 117 | + seen[key] = struct{}{} |
| 118 | + sn := scenario.NewByNodeScenario( |
| 119 | + session, |
| 120 | + base.GetPreemptor(), |
| 121 | + base.PendingTasks(), |
| 122 | + nil, |
| 123 | + base.RecordedVictimsJobs(), |
| 124 | + ) |
| 125 | + addPotentialVictimsGroupedByJob(sn, victimTasks) |
| 126 | + scenarios = append(scenarios, sn) |
| 127 | + } |
| 128 | + return scenarios |
| 129 | +} |
| 130 | + |
| 131 | +func nodeNamesOfJob(job *podgroup_info.PodGroupInfo) []string { |
| 132 | + if job == nil { |
| 133 | + return nil |
| 134 | + } |
| 135 | + seen := map[string]struct{}{} |
| 136 | + for _, task := range job.GetAllPodsMap() { |
| 137 | + if task.NodeName == "" { |
| 138 | + continue |
| 139 | + } |
| 140 | + seen[task.NodeName] = struct{}{} |
| 141 | + } |
| 142 | + nodeNames := make([]string, 0, len(seen)) |
| 143 | + for nodeName := range seen { |
| 144 | + nodeNames = append(nodeNames, nodeName) |
| 145 | + } |
| 146 | + sort.Strings(nodeNames) |
| 147 | + return nodeNames |
| 148 | +} |
| 149 | + |
| 150 | +func victimUIDSetKey(tasks []*pod_info.PodInfo) string { |
| 151 | + uids := make([]string, 0, len(tasks)) |
| 152 | + for _, task := range tasks { |
| 153 | + uids = append(uids, string(task.UID)) |
| 154 | + } |
| 155 | + sort.Strings(uids) |
| 156 | + return strings.Join(uids, "\x00") |
| 157 | +} |
| 158 | + |
| 159 | +func addPotentialVictimsGroupedByJob(sn *scenario.ByNodeScenario, tasks []*pod_info.PodInfo) { |
| 160 | + groupedTasks := map[common_info.PodGroupID][]*pod_info.PodInfo{} |
| 161 | + var jobOrder []common_info.PodGroupID |
| 162 | + for _, task := range tasks { |
| 163 | + if _, found := groupedTasks[task.Job]; !found { |
| 164 | + jobOrder = append(jobOrder, task.Job) |
| 165 | + } |
| 166 | + groupedTasks[task.Job] = append(groupedTasks[task.Job], task) |
| 167 | + } |
| 168 | + for _, jobID := range jobOrder { |
| 169 | + sn.AddPotentialVictimsTasks(groupedTasks[jobID]) |
| 170 | + } |
| 171 | +} |
0 commit comments