yangwwei commented on a change in pull request #145:
URL: 
https://github.com/apache/incubator-yunikorn-core/pull/145#discussion_r427744368



##########
File path: pkg/scheduler/sorters.go
##########
@@ -47,39 +39,101 @@ func sortQueue(queues []*SchedulingQueue, sortType 
SortType) {
                        return comp < 0
                })
        }
+       metrics.GetSchedulerMetrics().ObserveQueueSortingLatency(sortingStart)
 }
 
-func sortApplications(apps []*SchedulingApplication, sortType SortType, 
globalResource *resources.Resource) {
-       // TODO add latency metric
+func sortApplications(apps map[string]*SchedulingApplication, sortType 
policies.SortPolicy, globalResource *resources.Resource) 
[]*SchedulingApplication {
+       sortingStart := time.Now()
+       var sortedApps []*SchedulingApplication
        switch sortType {
-       case FairSortPolicy:
+       case policies.FairSortPolicy:
+               sortedApps = filterOnPendingResources(apps)
                // Sort by usage
-               sort.SliceStable(apps, func(i, j int) bool {
-                       l := apps[i]
-                       r := apps[j]
+               sort.SliceStable(sortedApps, func(i, j int) bool {
+                       l := sortedApps[i]
+                       r := sortedApps[j]
                        return resources.CompUsageRatio(l.getAssumeAllocated(), 
r.getAssumeAllocated(), globalResource) < 0
                })
-       case FifoSortPolicy:
+       case policies.FifoSortPolicy:
+               sortedApps = filterOnPendingResources(apps)
                // Sort by submission time oldest first
-               sort.SliceStable(apps, func(i, j int) bool {
-                       l := apps[i]
-                       r := apps[j]
+               sort.SliceStable(sortedApps, func(i, j int) bool {
+                       l := sortedApps[i]
+                       r := sortedApps[j]
                        return l.ApplicationInfo.SubmissionTime < 
r.ApplicationInfo.SubmissionTime
                })
+       case policies.StateAwarePolicy:
+               sortedApps = stateAwareFilter(apps)
+               // Sort by submission time oldest first
+               sort.SliceStable(sortedApps, func(i, j int) bool {
+                       l := sortedApps[i]
+                       r := sortedApps[j]
+                       return l.ApplicationInfo.SubmissionTime < 
r.ApplicationInfo.SubmissionTime
+               })
+       }
+       metrics.GetSchedulerMetrics().ObserveAppSortingLatency(sortingStart)
+       return sortedApps
+}
+
+func filterOnPendingResources(apps map[string]*SchedulingApplication) 
[]*SchedulingApplication {
+       filteredApps := make([]*SchedulingApplication, 0)
+       for _, app := range apps {
+               // Only look at app when pending-res > 0
+               if resources.StrictlyGreaterThanZero(app.GetPendingResource()) {
+                       filteredApps = append(filteredApps, app)
+               }
+       }
+       return filteredApps
+}
+
+// This filter only allows one (1) application with a state that is not 
running in the list of candidates.
+// The preference is a state of Starting. If we can not find an app with a 
starting state we will use an app
+// with an Accepted state. However if there is an app with a Starting state 
even with no pending resource
+// requests, no Accepted apps can be scheduled. An app in New state does not 
have any asks and can never be
+// scheduled.
+func stateAwareFilter(apps map[string]*SchedulingApplication) 
[]*SchedulingApplication {

Review comment:
       we need UT for this function




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to