Skip to content

Commit 3ad7b3f

Browse files
author
Preetha Appan
committed
More review comments
1 parent 993b6a2 commit 3ad7b3f

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

scheduler/preemption.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func GetBasePreemptionResourceFactory() PreemptionResourceFactory {
9494
// Preemptor is used to track existing allocations
9595
// and find suitable allocations to preempt
9696
type Preemptor struct {
97+
9798
// currentPreemptions is a map computed when SetPreemptions is called
9899
// it tracks the number of preempted allocations per job/taskgroup
99100
currentPreemptions map[structs.NamespacedID]map[string]int
@@ -125,6 +126,7 @@ func NewPreemptor(jobPriority int) *Preemptor {
125126
// SetNode sets the node
126127
func (p *Preemptor) SetNode(node *structs.Node) {
127128
nodeRemainingResources := node.ComparableResources()
129+
128130
// Subtract the reserved resources of the node
129131
if c := node.ComparableReservedResources(); c != nil {
130132
nodeRemainingResources.Subtract(c)
@@ -148,6 +150,7 @@ func (p *Preemptor) SetCandidates(allocs []*structs.Allocation) {
148150
// SetPreemptions initializes a map tracking existing counts of preempted allocations
149151
// per job/task group. This is used while scoring preemption options
150152
func (p *Preemptor) SetPreemptions(allocs []*structs.Allocation) {
153+
151154
// Clear out existing values since this can be called more than once
152155
p.currentPreemptions = make(map[structs.NamespacedID]map[string]int)
153156

@@ -237,7 +240,7 @@ func (p *Preemptor) PreemptForTaskGroup(resourceAsk *structs.AllocatedResources)
237240
// This filters out allocs whose resources are already covered by another alloc
238241
basePreemptionResource := GetBasePreemptionResourceFactory()
239242
resourcesNeeded = resourceAsk.Comparable()
240-
filteredBestAllocs := filterSuperset(bestAllocs, p.nodeRemainingResources, resourcesNeeded, basePreemptionResource)
243+
filteredBestAllocs := p.filterSuperset(bestAllocs, p.nodeRemainingResources, resourcesNeeded, basePreemptionResource)
241244
return filteredBestAllocs
242245

243246
}
@@ -270,6 +273,7 @@ func (p *Preemptor) PreemptForNetwork(networkResourceAsk *structs.NetworkResourc
270273

271274
// Filter out alloc that's ineligible due to priority
272275
if p.jobPriority-alloc.Job.Priority < 10 {
276+
273277
// If this allocation uses a needed reserved port
274278
// preemption is impossible so we return early
275279
networks := alloc.ComparableResources().Flattened.Networks
@@ -403,7 +407,7 @@ OUTER:
403407
Networks: []*structs.NetworkResource{networkResourceAsk},
404408
},
405409
}
406-
filteredBestAllocs := filterSuperset(allocsToPreempt, nodeRemainingResources, resourcesNeeded, preemptionResourceFactory)
410+
filteredBestAllocs := p.filterSuperset(allocsToPreempt, nodeRemainingResources, resourcesNeeded, preemptionResourceFactory)
407411
return filteredBestAllocs
408412
}
409413

@@ -475,12 +479,11 @@ func scoreForNetwork(resourceUsed *structs.NetworkResource, resourceNeeded *stru
475479
return networkResourceDistance(resourceUsed, resourceNeeded) + maxParallelScorePenalty
476480
}
477481

478-
// filterAndGroupPreemptibleAllocs groups allocations by priority after removing any from jobs of
479-
// a higher priority than jobPriority
482+
// filterAndGroupPreemptibleAllocs groups allocations by priority after filtering allocs
483+
// that are not preemptible based on the jobPriority arg
480484
func filterAndGroupPreemptibleAllocs(jobPriority int, current []*structs.Allocation) []*groupedAllocs {
481485
allocsByPriority := make(map[int][]*structs.Allocation)
482486
for _, alloc := range current {
483-
// Why is alloc.Job even nil though?
484487
if alloc.Job == nil {
485488
continue
486489
}
@@ -517,7 +520,7 @@ func filterAndGroupPreemptibleAllocs(jobPriority int, current []*structs.Allocat
517520
// filterSuperset is used as a final step to remove
518521
// any allocations that meet a superset of requirements from
519522
// the set of allocations to preempt
520-
func filterSuperset(bestAllocs []*structs.Allocation,
523+
func (p *Preemptor) filterSuperset(bestAllocs []*structs.Allocation,
521524
nodeRemainingResources *structs.ComparableResources,
522525
resourceAsk *structs.ComparableResources,
523526
preemptionResourceFactory PreemptionResourceFactory) []*structs.Allocation {
@@ -529,20 +532,15 @@ func filterSuperset(bestAllocs []*structs.Allocation,
529532
return distance1 > distance2
530533
})
531534

532-
var preemptedResources *structs.ComparableResources
535+
availableResources := nodeRemainingResources.Copy()
533536
var filteredBestAllocs []*structs.Allocation
534537

535538
// Do another pass to eliminate allocations that are a superset of other allocations
536539
// in the preemption set
537540
for _, alloc := range bestAllocs {
538-
if preemptedResources == nil {
539-
preemptedResources = alloc.ComparableResources().Copy()
540-
} else {
541-
preemptedResources.Add(alloc.ComparableResources().Copy())
542-
}
543541
filteredBestAllocs = append(filteredBestAllocs, alloc)
544-
availableResources := preemptedResources.Copy()
545-
availableResources.Add(nodeRemainingResources)
542+
allocResources := p.allocDetails[alloc.ID].resources
543+
availableResources.Add(allocResources)
546544

547545
premptionResource := preemptionResourceFactory(availableResources, resourceAsk)
548546
requirementsMet := premptionResource.MeetsRequirements()
@@ -559,12 +557,14 @@ func filterSuperset(bestAllocs []*structs.Allocation,
559557
func (p *Preemptor) distanceComparatorForNetwork(allocs []*structs.Allocation, networkResourceAsk *structs.NetworkResource, i int, j int) bool {
560558
firstAlloc := allocs[i]
561559
currentPreemptionCount1 := p.getNumPreemptions(firstAlloc)
560+
562561
// Look up configured maxParallel value for these allocation's task groups
563562
var maxParallel1, maxParallel2 int
564563
tg1 := allocs[i].Job.LookupTaskGroup(firstAlloc.TaskGroup)
565564
if tg1 != nil && tg1.Migrate != nil {
566565
maxParallel1 = tg1.Migrate.MaxParallel
567566
}
567+
568568
// Dereference network usage on first alloc if its there
569569
firstAllocNetworks := firstAlloc.ComparableResources().Flattened.Networks
570570
var firstAllocNetResourceUsed *structs.NetworkResource
@@ -580,6 +580,7 @@ func (p *Preemptor) distanceComparatorForNetwork(allocs []*structs.Allocation, n
580580
if tg2 != nil && tg2.Migrate != nil {
581581
maxParallel2 = tg2.Migrate.MaxParallel
582582
}
583+
583584
// Dereference network usage on second alloc if its there
584585
secondAllocNetworks := secondAlloc.ComparableResources().Flattened.Networks
585586
var secondAllocNetResourceUsed *structs.NetworkResource

scheduler/testing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (h *Harness) SubmitPlan(plan *structs.Plan) (*structs.PlanResult, State, er
117117
}
118118
}
119119

120-
// Set create and modify time for preempted allocs and flatten them
120+
// Set modify time for preempted allocs and flatten them
121121
var preemptedAllocs []*structs.Allocation
122122
for _, preemptions := range result.NodePreemptions {
123123
for _, alloc := range preemptions {

0 commit comments

Comments
 (0)