volcano.sh/volcano@v1.9.0/pkg/scheduler/actions/enqueue/enqueue.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package enqueue 18 19 import ( 20 "time" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/util/sets" 24 "k8s.io/klog/v2" 25 26 "volcano.sh/apis/pkg/apis/scheduling" 27 "volcano.sh/volcano/pkg/scheduler/api" 28 "volcano.sh/volcano/pkg/scheduler/framework" 29 "volcano.sh/volcano/pkg/scheduler/util" 30 ) 31 32 type Action struct{} 33 34 func New() *Action { 35 return &Action{} 36 } 37 38 func (enqueue *Action) Name() string { 39 return "enqueue" 40 } 41 42 func (enqueue *Action) Initialize() {} 43 44 func (enqueue *Action) Execute(ssn *framework.Session) { 45 klog.V(5).Infof("Enter Enqueue ...") 46 defer klog.V(5).Infof("Leaving Enqueue ...") 47 48 queues := util.NewPriorityQueue(ssn.QueueOrderFn) 49 queueSet := sets.NewString() 50 jobsMap := map[api.QueueID]*util.PriorityQueue{} 51 52 for _, job := range ssn.Jobs { 53 if job.ScheduleStartTimestamp.IsZero() { 54 ssn.Jobs[job.UID].ScheduleStartTimestamp = metav1.Time{ 55 Time: time.Now(), 56 } 57 } 58 if queue, found := ssn.Queues[job.Queue]; !found { 59 klog.Errorf("Failed to find Queue <%s> for Job <%s/%s>", 60 job.Queue, job.Namespace, job.Name) 61 continue 62 } else if !queueSet.Has(string(queue.UID)) { 63 klog.V(5).Infof("Added Queue <%s> for Job <%s/%s>", 64 queue.Name, job.Namespace, job.Name) 65 66 queueSet.Insert(string(queue.UID)) 67 queues.Push(queue) 68 } 69 70 if job.IsPending() { 71 if _, found := jobsMap[job.Queue]; !found { 72 jobsMap[job.Queue] = util.NewPriorityQueue(ssn.JobOrderFn) 73 } 74 klog.V(5).Infof("Added Job <%s/%s> into Queue <%s>", job.Namespace, job.Name, job.Queue) 75 jobsMap[job.Queue].Push(job) 76 } 77 } 78 79 klog.V(3).Infof("Try to enqueue PodGroup to %d Queues", len(jobsMap)) 80 81 for { 82 if queues.Empty() { 83 break 84 } 85 86 queue := queues.Pop().(*api.QueueInfo) 87 88 // skip the Queue that has no pending job 89 jobs, found := jobsMap[queue.UID] 90 if !found || jobs.Empty() { 91 continue 92 } 93 job := jobs.Pop().(*api.JobInfo) 94 95 if job.PodGroup.Spec.MinResources == nil || ssn.JobEnqueueable(job) { 96 ssn.JobEnqueued(job) 97 job.PodGroup.Status.Phase = scheduling.PodGroupInqueue 98 ssn.Jobs[job.UID] = job 99 } 100 101 // Added Queue back until no job in Queue. 102 queues.Push(queue) 103 } 104 } 105 106 func (enqueue *Action) UnInitialize() {}