Skip to content

Commit

Permalink
Change to a reasonable way
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuqi-lucas committed Sep 7, 2024
1 parent a9aae38 commit 706dca0
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions pkg/scheduler/objects/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,12 @@ func newDynamicQueueInternal(name string, leaf bool, parent *Queue) (*Queue, err
func (sq *Queue) applyTemplate(childTemplate *template.Template) {
sq.maxRunningApps = childTemplate.GetMaxApplications()
sq.properties = childTemplate.GetProperties()
// update metrics for guaranteed and max resource
// update before setting the resources in case the resources are nil
sq.updateGuaranteedResourceMetrics(childTemplate.GetGuaranteedResource() == nil)
sq.updateMaxResourceMetrics(childTemplate.GetMaxResource() == nil)
// the resources in template are already checked
sq.guaranteedResource = childTemplate.GetGuaranteedResource()
sq.maxResource = childTemplate.GetMaxResource()
// update metrics for guaranteed and max resource
sq.updateGuaranteedResourceMetrics()
sq.updateMaxResourceMetrics()
}

// getProperties returns a copy of the properties for this queue
Expand Down Expand Up @@ -404,7 +403,7 @@ func (sq *Queue) setResources(guaranteedResource, maxResource *resources.Resourc
sq.queueEvents.SendMaxResourceChangedEvent(sq.QueuePath, maxResource)
}
sq.maxResource = maxResource
sq.updateMaxResourceMetrics(false)
sq.updateMaxResourceMetrics()
case sq.maxResource != nil:
log.Log(log.SchedQueue).Debug("setting max resources",
zap.String("queue", sq.QueuePath),
Expand All @@ -413,9 +412,8 @@ func (sq *Queue) setResources(guaranteedResource, maxResource *resources.Resourc
if sq.queueEvents != nil {
sq.queueEvents.SendMaxResourceChangedEvent(sq.QueuePath, maxResource)
}
// update before setting the resources in case the resources are nil
sq.updateMaxResourceMetrics(true)
sq.maxResource = nil
sq.updateMaxResourceMetrics()
default:
log.Log(log.SchedQueue).Debug("max resources setting ignored: cannot set zero max resources",
zap.String("queue", sq.QueuePath))
Expand All @@ -431,7 +429,7 @@ func (sq *Queue) setResources(guaranteedResource, maxResource *resources.Resourc
sq.queueEvents.SendGuaranteedResourceChangedEvent(sq.QueuePath, guaranteedResource)
}
sq.guaranteedResource = guaranteedResource
sq.updateGuaranteedResourceMetrics(false)
sq.updateGuaranteedResourceMetrics()
case sq.guaranteedResource != nil:
log.Log(log.SchedQueue).Debug("setting guaranteed resources",
zap.String("queue", sq.QueuePath),
Expand All @@ -440,8 +438,6 @@ func (sq *Queue) setResources(guaranteedResource, maxResource *resources.Resourc
if sq.queueEvents != nil {
sq.queueEvents.SendGuaranteedResourceChangedEvent(sq.QueuePath, guaranteedResource)
}
// update before setting the resources in case the resources are nil
sq.updateGuaranteedResourceMetrics(true)
sq.guaranteedResource = nil
default:
log.Log(log.SchedQueue).Debug("guaranteed resources setting ignored: cannot set zero guaranteed resources",
Expand Down Expand Up @@ -1367,7 +1363,7 @@ func (sq *Queue) SetMaxResource(max *resources.Resource) {
sq.queueEvents.SendMaxResourceChangedEvent(sq.QueuePath, sq.maxResource)
}
sq.maxResource = max.Clone()
sq.updateMaxResourceMetrics(false)
sq.updateMaxResourceMetrics()
case sq.maxResource != nil:
log.Log(log.SchedQueue).Debug("setting max resources",
zap.String("queue", sq.QueuePath),
Expand All @@ -1376,9 +1372,8 @@ func (sq *Queue) SetMaxResource(max *resources.Resource) {
if sq.queueEvents != nil {
sq.queueEvents.SendMaxResourceChangedEvent(sq.QueuePath, sq.maxResource)
}
// update before setting to nil to get the metrics right
sq.updateMaxResourceMetrics(true)
sq.maxResource = nil
sq.updateMaxResourceMetrics()
default:
log.Log(log.SchedQueue).Debug("max resources setting ignored: cannot set zero max resources",
zap.String("queue", sq.QueuePath))
Expand Down Expand Up @@ -1660,30 +1655,32 @@ func (sq *Queue) SupportTaskGroup() bool {
}

// updateGuaranteedResourceMetrics updates guaranteed resource metrics.
func (sq *Queue) updateGuaranteedResourceMetrics(isNil bool) {
func (sq *Queue) updateGuaranteedResourceMetrics() {
if sq.guaranteedResource != nil {
for k, v := range sq.guaranteedResource.Resources {
if !isNil {
metrics.GetQueueMetrics(sq.QueuePath).SetQueueGuaranteedResourceMetrics(k, float64(v))
} else {
metrics.GetQueueMetrics(sq.QueuePath).SetQueueGuaranteedResourceMetrics(k, float64(0))
}
metrics.GetQueueMetrics(sq.QueuePath).SetQueueGuaranteedResourceMetrics(k, float64(v))
}
} else {
// We need to update the resource, because we support update the guaranteed resource to nil or zero resources
// for the queue. We need to update the metrics to reflect the change.
// Here, we just set the guaranteed resource to zero for cpu and memory.
metrics.GetQueueMetrics(sq.QueuePath).SetQueueGuaranteedResourceMetrics("cpu", float64(0))
metrics.GetQueueMetrics(sq.QueuePath).SetQueueGuaranteedResourceMetrics("memory", float64(0))
}
}

// updateMaxResourceMetrics updates max resource metrics.
func (sq *Queue) updateMaxResourceMetrics(isNil bool) {
func (sq *Queue) updateMaxResourceMetrics() {
if sq.maxResource != nil {
if !isNil {
for k, v := range sq.maxResource.Resources {
metrics.GetQueueMetrics(sq.QueuePath).SetQueueMaxResourceMetrics(k, float64(v))
}
} else {
for k := range sq.maxResource.Resources {
metrics.GetQueueMetrics(sq.QueuePath).SetQueueMaxResourceMetrics(k, float64(0))
}
for k, v := range sq.maxResource.Resources {
metrics.GetQueueMetrics(sq.QueuePath).SetQueueMaxResourceMetrics(k, float64(v))
}
} else {
// We need to update the resource, because we support update the max resource to nil or zero resources
// for the queue. We need to update the metrics to reflect the change.
// Here, we just set the max resource to zero for cpu and memory.
metrics.GetQueueMetrics(sq.QueuePath).SetQueueMaxResourceMetrics("cpu", float64(0))
metrics.GetQueueMetrics(sq.QueuePath).SetQueueMaxResourceMetrics("memory", float64(0))
}
}

Expand Down

0 comments on commit 706dca0

Please sign in to comment.