@@ -94,6 +94,7 @@ func GetBasePreemptionResourceFactory() PreemptionResourceFactory {
94
94
// Preemptor is used to track existing allocations
95
95
// and find suitable allocations to preempt
96
96
type Preemptor struct {
97
+
97
98
// currentPreemptions is a map computed when SetPreemptions is called
98
99
// it tracks the number of preempted allocations per job/taskgroup
99
100
currentPreemptions map [structs.NamespacedID ]map [string ]int
@@ -125,6 +126,7 @@ func NewPreemptor(jobPriority int) *Preemptor {
125
126
// SetNode sets the node
126
127
func (p * Preemptor ) SetNode (node * structs.Node ) {
127
128
nodeRemainingResources := node .ComparableResources ()
129
+
128
130
// Subtract the reserved resources of the node
129
131
if c := node .ComparableReservedResources (); c != nil {
130
132
nodeRemainingResources .Subtract (c )
@@ -148,6 +150,7 @@ func (p *Preemptor) SetCandidates(allocs []*structs.Allocation) {
148
150
// SetPreemptions initializes a map tracking existing counts of preempted allocations
149
151
// per job/task group. This is used while scoring preemption options
150
152
func (p * Preemptor ) SetPreemptions (allocs []* structs.Allocation ) {
153
+
151
154
// Clear out existing values since this can be called more than once
152
155
p .currentPreemptions = make (map [structs.NamespacedID ]map [string ]int )
153
156
@@ -237,7 +240,7 @@ func (p *Preemptor) PreemptForTaskGroup(resourceAsk *structs.AllocatedResources)
237
240
// This filters out allocs whose resources are already covered by another alloc
238
241
basePreemptionResource := GetBasePreemptionResourceFactory ()
239
242
resourcesNeeded = resourceAsk .Comparable ()
240
- filteredBestAllocs := filterSuperset (bestAllocs , p .nodeRemainingResources , resourcesNeeded , basePreemptionResource )
243
+ filteredBestAllocs := p . filterSuperset (bestAllocs , p .nodeRemainingResources , resourcesNeeded , basePreemptionResource )
241
244
return filteredBestAllocs
242
245
243
246
}
@@ -270,6 +273,7 @@ func (p *Preemptor) PreemptForNetwork(networkResourceAsk *structs.NetworkResourc
270
273
271
274
// Filter out alloc that's ineligible due to priority
272
275
if p .jobPriority - alloc .Job .Priority < 10 {
276
+
273
277
// If this allocation uses a needed reserved port
274
278
// preemption is impossible so we return early
275
279
networks := alloc .ComparableResources ().Flattened .Networks
@@ -403,7 +407,7 @@ OUTER:
403
407
Networks : []* structs.NetworkResource {networkResourceAsk },
404
408
},
405
409
}
406
- filteredBestAllocs := filterSuperset (allocsToPreempt , nodeRemainingResources , resourcesNeeded , preemptionResourceFactory )
410
+ filteredBestAllocs := p . filterSuperset (allocsToPreempt , nodeRemainingResources , resourcesNeeded , preemptionResourceFactory )
407
411
return filteredBestAllocs
408
412
}
409
413
@@ -475,12 +479,11 @@ func scoreForNetwork(resourceUsed *structs.NetworkResource, resourceNeeded *stru
475
479
return networkResourceDistance (resourceUsed , resourceNeeded ) + maxParallelScorePenalty
476
480
}
477
481
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
480
484
func filterAndGroupPreemptibleAllocs (jobPriority int , current []* structs.Allocation ) []* groupedAllocs {
481
485
allocsByPriority := make (map [int ][]* structs.Allocation )
482
486
for _ , alloc := range current {
483
- // Why is alloc.Job even nil though?
484
487
if alloc .Job == nil {
485
488
continue
486
489
}
@@ -517,7 +520,7 @@ func filterAndGroupPreemptibleAllocs(jobPriority int, current []*structs.Allocat
517
520
// filterSuperset is used as a final step to remove
518
521
// any allocations that meet a superset of requirements from
519
522
// the set of allocations to preempt
520
- func filterSuperset (bestAllocs []* structs.Allocation ,
523
+ func ( p * Preemptor ) filterSuperset (bestAllocs []* structs.Allocation ,
521
524
nodeRemainingResources * structs.ComparableResources ,
522
525
resourceAsk * structs.ComparableResources ,
523
526
preemptionResourceFactory PreemptionResourceFactory ) []* structs.Allocation {
@@ -529,20 +532,15 @@ func filterSuperset(bestAllocs []*structs.Allocation,
529
532
return distance1 > distance2
530
533
})
531
534
532
- var preemptedResources * structs. ComparableResources
535
+ availableResources := nodeRemainingResources . Copy ()
533
536
var filteredBestAllocs []* structs.Allocation
534
537
535
538
// Do another pass to eliminate allocations that are a superset of other allocations
536
539
// in the preemption set
537
540
for _ , alloc := range bestAllocs {
538
- if preemptedResources == nil {
539
- preemptedResources = alloc .ComparableResources ().Copy ()
540
- } else {
541
- preemptedResources .Add (alloc .ComparableResources ().Copy ())
542
- }
543
541
filteredBestAllocs = append (filteredBestAllocs , alloc )
544
- availableResources := preemptedResources . Copy ()
545
- availableResources .Add (nodeRemainingResources )
542
+ allocResources := p . allocDetails [ alloc . ID ]. resources
543
+ availableResources .Add (allocResources )
546
544
547
545
premptionResource := preemptionResourceFactory (availableResources , resourceAsk )
548
546
requirementsMet := premptionResource .MeetsRequirements ()
@@ -559,12 +557,14 @@ func filterSuperset(bestAllocs []*structs.Allocation,
559
557
func (p * Preemptor ) distanceComparatorForNetwork (allocs []* structs.Allocation , networkResourceAsk * structs.NetworkResource , i int , j int ) bool {
560
558
firstAlloc := allocs [i ]
561
559
currentPreemptionCount1 := p .getNumPreemptions (firstAlloc )
560
+
562
561
// Look up configured maxParallel value for these allocation's task groups
563
562
var maxParallel1 , maxParallel2 int
564
563
tg1 := allocs [i ].Job .LookupTaskGroup (firstAlloc .TaskGroup )
565
564
if tg1 != nil && tg1 .Migrate != nil {
566
565
maxParallel1 = tg1 .Migrate .MaxParallel
567
566
}
567
+
568
568
// Dereference network usage on first alloc if its there
569
569
firstAllocNetworks := firstAlloc .ComparableResources ().Flattened .Networks
570
570
var firstAllocNetResourceUsed * structs.NetworkResource
@@ -580,6 +580,7 @@ func (p *Preemptor) distanceComparatorForNetwork(allocs []*structs.Allocation, n
580
580
if tg2 != nil && tg2 .Migrate != nil {
581
581
maxParallel2 = tg2 .Migrate .MaxParallel
582
582
}
583
+
583
584
// Dereference network usage on second alloc if its there
584
585
secondAllocNetworks := secondAlloc .ComparableResources ().Flattened .Networks
585
586
var secondAllocNetResourceUsed * structs.NetworkResource
0 commit comments