in pkg/scheduler/objects/application.go [1818:1894]
func (sa *Application) removeAllocationInternal(allocationKey string, releaseType si.TerminationType) *Allocation {
alloc := sa.allocations[allocationKey]
// When app has the allocation, update map, and update allocated resource of the app
if alloc == nil {
return nil
}
var event applicationEvent = EventNotNeeded
var eventWarning string
removeApp := false
// update correct allocation tracker
if alloc.IsPlaceholder() {
// make sure we account for the placeholders being removed in the tracking data
// update based on termination type: everything is counted as a timeout except for a real replace
if sa.placeholderData != nil {
if phData, ok := sa.placeholderData[alloc.taskGroupName]; ok {
if releaseType == si.TerminationType_PLACEHOLDER_REPLACED {
phData.Replaced++
} else {
phData.TimedOut++
}
}
}
// as and when every ph gets removed (for replacement), resource usage would be reduced.
// When real allocation happens as part of replacement, usage would be increased again with real alloc resource
sa.allocatedPlaceholder = resources.Sub(sa.allocatedPlaceholder, alloc.GetAllocatedResource())
sa.allocatedPlaceholder.Prune()
// if all the placeholders are replaced, clear the placeholder timer
if resources.IsZero(sa.allocatedPlaceholder) {
sa.clearPlaceholderTimer()
sa.hasPlaceholderAlloc = false
if (sa.IsCompleting() && sa.stateTimer == nil) || sa.IsFailing() || sa.IsResuming() || sa.hasZeroAllocations() {
removeApp = true
event = CompleteApplication
if sa.IsFailing() {
event = FailApplication
}
if sa.IsResuming() {
event = RunApplication
removeApp = false
}
eventWarning = "Application state not changed while removing a placeholder allocation"
}
}
// Aggregate the resources used by this alloc to the application's resource tracker
sa.trackCompletedResource(alloc)
sa.decUserResourceUsage(alloc.GetAllocatedResource(), removeApp)
} else {
sa.allocatedResource = resources.Sub(sa.allocatedResource, alloc.GetAllocatedResource())
sa.allocatedResource.Prune()
// Aggregate the resources used by this alloc to the application's resource tracker
sa.trackCompletedResource(alloc)
// When the resource trackers are zero we should not expect anything to come in later.
if sa.hasZeroAllocations() {
removeApp = true
event = CompleteApplication
eventWarning = "Application state not changed to Completing while removing an allocation"
}
sa.decUserResourceUsage(alloc.GetAllocatedResource(), removeApp)
}
if event != EventNotNeeded {
if err := sa.HandleApplicationEvent(event); err != nil {
log.Log(log.SchedApplication).Warn(eventWarning,
zap.String("currentState", sa.CurrentState()),
zap.Stringer("event", event),
zap.Error(err))
}
}
delete(sa.allocations, allocationKey)
sa.appEvents.SendRemoveAllocationEvent(sa.ApplicationID, alloc.allocationKey, alloc.GetAllocatedResource(), releaseType)
return alloc
}