Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduler: make demand cap a share not a cost #3891

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/scheduler/context/scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,17 @@ func (sctx *SchedulingContext) UpdateFairShares() {
cappedShare float64
}

totalCost := sctx.TotalCost()
queueInfos := make([]*queueInfo, 0, len(sctx.QueueSchedulingContexts))
for queueName, qctx := range sctx.QueueSchedulingContexts {
cappedShare := 1.0
if !sctx.TotalResources.IsZero() {
cappedShare = sctx.FairnessCostProvider.UnweightedCostFromAllocation(qctx.CappedDemand)
if !sctx.TotalResources.IsZero() && totalCost > 0 {
cappedCost := sctx.FairnessCostProvider.UnweightedCostFromAllocation(qctx.CappedDemand)
cappedShare = cappedCost / totalCost
} else if qctx.CappedDemand.IsZero() {
cappedShare = 0
}

queueInfos = append(queueInfos, &queueInfo{
queueName: queueName,
adjustedShare: 0,
Expand Down
97 changes: 56 additions & 41 deletions internal/scheduler/context/scheduling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,12 @@ func TestSchedulingContextAccounting(t *testing.T) {
}

func TestCalculateFairShares(t *testing.T) {
zeroCpu := schedulerobjects.ResourceList{
Resources: map[string]resource.Quantity{"cpu": resource.MustParse("0")},
}
oneCpu := schedulerobjects.ResourceList{
Resources: map[string]resource.Quantity{"cpu": resource.MustParse("1")},
}
fortyCpu := schedulerobjects.ResourceList{
Resources: map[string]resource.Quantity{"cpu": resource.MustParse("40")},
}
oneHundredCpu := schedulerobjects.ResourceList{
Resources: map[string]resource.Quantity{"cpu": resource.MustParse("100")},
}
oneThousandCpu := schedulerobjects.ResourceList{
Resources: map[string]resource.Quantity{"cpu": resource.MustParse("1000")},
}
zeroCpu := cpu("0")
oneCpu := cpu("1")
fortyCpu := cpu("40")
oneHundredCpu := cpu("100")
oneThousandCpu := cpu("1000")

tests := map[string]struct {
availableResources schedulerobjects.ResourceList
queueCtxs map[string]*QueueSchedulingContext
Expand All @@ -82,103 +73,121 @@ func TestCalculateFairShares(t *testing.T) {
"one queue, demand exceeds capacity": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneThousandCpu},
"queueA": {Weight: 1.0, Demand: oneThousandCpu, Allocated: oneHundredCpu},
},
expectedFairShares: map[string]float64{"queueA": 1.0},
expectedAdjustedFairShares: map[string]float64{"queueA": 1.0},
},
"one queue, demand less than capacity": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneCpu},
"queueA": {Weight: 1.0, Demand: oneCpu, Allocated: oneCpu},
},
expectedFairShares: map[string]float64{"queueA": 1.0},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.01},
expectedAdjustedFairShares: map[string]float64{"queueA": 1.0},
},
"two queues, equal weights, demand exceeds capacity": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneThousandCpu},
"queueB": {Weight: 1.0, Demand: oneThousandCpu},
"queueA": {Weight: 1.0, Demand: oneThousandCpu, Allocated: cpu("50")},
"queueB": {Weight: 1.0, Demand: oneThousandCpu, Allocated: cpu("50")},
},
expectedFairShares: map[string]float64{"queueA": 0.5, "queueB": 0.5},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.5, "queueB": 0.5},
},
"two queues, equal weights, demand less than capacity for both queues": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneCpu},
"queueB": {Weight: 1.0, Demand: oneCpu},
"queueA": {Weight: 1.0, Demand: oneCpu, Allocated: oneCpu},
"queueB": {Weight: 1.0, Demand: oneCpu, Allocated: oneCpu},
},
expectedFairShares: map[string]float64{"queueA": 0.5, "queueB": 0.5},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.01, "queueB": 0.01},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.5, "queueB": 0.5},
},
"two queues, equal weights, demand less than capacity for one queue": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneCpu},
"queueB": {Weight: 1.0, Demand: oneThousandCpu},
"queueA": {Weight: 1.0, Demand: oneCpu, Allocated: oneCpu},
"queueB": {Weight: 1.0, Demand: oneThousandCpu, Allocated: cpu("99")},
},
expectedFairShares: map[string]float64{"queueA": 0.5, "queueB": 0.5},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.01, "queueB": 0.99},
},
"two queues, non equal weights, demand exceeds capacity for both queues": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneThousandCpu},
"queueB": {Weight: 3.0, Demand: oneThousandCpu},
"queueA": {Weight: 1.0, Demand: oneThousandCpu, Allocated: cpu("25")},
"queueB": {Weight: 3.0, Demand: oneThousandCpu, Allocated: cpu("75")},
},
expectedFairShares: map[string]float64{"queueA": 0.25, "queueB": 0.75},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.25, "queueB": 0.75},
},
"two queues, non equal weights, demand exceeds capacity for higher priority queue only": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneCpu},
"queueB": {Weight: 3.0, Demand: oneThousandCpu},
"queueA": {Weight: 1.0, Demand: oneCpu, Allocated: oneCpu},
"queueB": {Weight: 3.0, Demand: oneThousandCpu, Allocated: cpu("99")},
},
expectedFairShares: map[string]float64{"queueA": 0.25, "queueB": 0.75},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.01, "queueB": 0.99},
},
"two queues, non equal weights, demand exceeds capacity for lower priority queue only": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneThousandCpu},
"queueB": {Weight: 3.0, Demand: oneCpu},
"queueA": {Weight: 1.0, Demand: oneThousandCpu, Allocated: cpu("99")},
"queueB": {Weight: 3.0, Demand: oneCpu, Allocated: oneCpu},
},
expectedFairShares: map[string]float64{"queueA": 0.25, "queueB": 0.75},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.99, "queueB": 0.01},
},
"three queues, equal weights. Adjusted fair share requires multiple iterations": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneCpu},
"queueB": {Weight: 1.0, Demand: fortyCpu},
"queueC": {Weight: 1.0, Demand: oneThousandCpu},
"queueA": {Weight: 1.0, Demand: oneCpu, Allocated: oneCpu},
"queueB": {Weight: 1.0, Demand: fortyCpu, Allocated: fortyCpu},
"queueC": {Weight: 1.0, Demand: oneThousandCpu, Allocated: cpu("59")},
},
expectedFairShares: map[string]float64{"queueA": 1.0 / 3, "queueB": 1.0 / 3, "queueC": 1.0 / 3},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.01, "queueB": 0.4, "queueC": 0.59},
},
"No demand": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: zeroCpu},
"queueB": {Weight: 1.0, Demand: zeroCpu},
"queueC": {Weight: 1.0, Demand: zeroCpu},
"queueA": {Weight: 1.0, Demand: zeroCpu, Allocated: zeroCpu},
"queueB": {Weight: 1.0, Demand: zeroCpu, Allocated: zeroCpu},
"queueC": {Weight: 1.0, Demand: zeroCpu, Allocated: zeroCpu},
},
expectedFairShares: map[string]float64{"queueA": 1.0 / 3, "queueB": 1.0 / 3, "queueC": 1.0 / 3},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.0, "queueB": 0.0, "queueC": 0.0},
},
"No capacity": {
availableResources: zeroCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneCpu},
"queueB": {Weight: 1.0, Demand: oneCpu},
"queueC": {Weight: 1.0, Demand: oneCpu},
"queueA": {Weight: 1.0, Demand: oneCpu, Allocated: zeroCpu},
"queueB": {Weight: 1.0, Demand: oneCpu, Allocated: zeroCpu},
"queueC": {Weight: 1.0, Demand: oneCpu, Allocated: zeroCpu},
},
expectedFairShares: map[string]float64{"queueA": 1.0 / 3, "queueB": 1.0 / 3, "queueC": 1.0 / 3},
expectedAdjustedFairShares: map[string]float64{"queueA": 1.0 / 3, "queueB": 1.0 / 3, "queueC": 1.0 / 3},
},
"Demand but not allocated yet": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneCpu, Allocated: zeroCpu},
"queueB": {Weight: 1.0, Demand: oneThousandCpu, Allocated: zeroCpu},
},
expectedFairShares: map[string]float64{"queueA": 0.5, "queueB": 0.5},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.5, "queueB": 0.5},
},
"Demand partially allocated": {
availableResources: oneHundredCpu,
queueCtxs: map[string]*QueueSchedulingContext{
"queueA": {Weight: 1.0, Demand: oneCpu, Allocated: zeroCpu},
"queueB": {Weight: 1.0, Demand: oneThousandCpu, Allocated: cpu("50")},
},
expectedFairShares: map[string]float64{"queueA": 0.5, "queueB": 0.5},
expectedAdjustedFairShares: map[string]float64{"queueA": 0.02, "queueB": 0.98},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
Expand All @@ -192,7 +201,7 @@ func TestCalculateFairShares(t *testing.T) {
)
for qName, q := range tc.queueCtxs {
err = sctx.AddQueueSchedulingContext(
qName, q.Weight, schedulerobjects.QuantityByTAndResourceType[string]{}, q.Demand, q.Demand, nil)
qName, q.Weight, schedulerobjects.QuantityByTAndResourceType[string]{"priority-class-0": q.Allocated}, q.Demand, q.Demand, nil)
require.NoError(t, err)
}
sctx.UpdateFairShares()
Expand Down Expand Up @@ -226,3 +235,9 @@ func testSmallCpuJobSchedulingContext(queue, priorityClassName string) *JobSched
GangInfo: EmptyGangInfo(job),
}
}

func cpu(cpu string) schedulerobjects.ResourceList {
return schedulerobjects.ResourceList{
Resources: map[string]resource.Quantity{"cpu": resource.MustParse(cpu)},
}
}