sigs.k8s.io/kueue@v0.6.2/pkg/queue/cluster_queue_strict_fifo.go (about)

     1  /*
     2  Copyright 2022 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 queue
    18  
    19  import (
    20  	"k8s.io/utils/clock"
    21  
    22  	kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
    23  	utilpriority "sigs.k8s.io/kueue/pkg/util/priority"
    24  	"sigs.k8s.io/kueue/pkg/workload"
    25  )
    26  
    27  var (
    28  	realClock = clock.RealClock{}
    29  )
    30  
    31  // ClusterQueueStrictFIFO is the implementation for the ClusterQueue for
    32  // StrictFIFO.
    33  type ClusterQueueStrictFIFO struct {
    34  	*clusterQueueBase
    35  }
    36  
    37  var _ ClusterQueue = &ClusterQueueStrictFIFO{}
    38  
    39  func newClusterQueueStrictFIFO(cq *kueue.ClusterQueue, wo workload.Ordering) (ClusterQueue, error) {
    40  	cqImpl := newClusterQueueImpl(keyFunc, queueOrderingFunc(wo), realClock)
    41  	cqStrict := &ClusterQueueStrictFIFO{
    42  		clusterQueueBase: cqImpl,
    43  	}
    44  
    45  	err := cqStrict.Update(cq)
    46  	return cqStrict, err
    47  }
    48  
    49  // queueOrderingFunc returns a function used by the clusterQueue heap algorithm
    50  // to sort workloads. The function sorts workloads based on their priority.
    51  // When priorities are equal, it uses the workload's creation or eviction
    52  // time.
    53  func queueOrderingFunc(wo workload.Ordering) func(a, b interface{}) bool {
    54  	return func(a, b interface{}) bool {
    55  		objA := a.(*workload.Info)
    56  		objB := b.(*workload.Info)
    57  		p1 := utilpriority.Priority(objA.Obj)
    58  		p2 := utilpriority.Priority(objB.Obj)
    59  
    60  		if p1 != p2 {
    61  			return p1 > p2
    62  		}
    63  
    64  		tA := wo.GetQueueOrderTimestamp(objA.Obj)
    65  		tB := wo.GetQueueOrderTimestamp(objB.Obj)
    66  		return !tB.Before(tA)
    67  	}
    68  }
    69  
    70  // RequeueIfNotPresent requeues if the workload is not present.
    71  // If the reason for requeue is that the workload doesn't match the CQ's
    72  // namespace selector, then the requeue is not immediate.
    73  func (cq *ClusterQueueStrictFIFO) RequeueIfNotPresent(wInfo *workload.Info, reason RequeueReason) bool {
    74  	return cq.requeueIfNotPresent(wInfo, reason != RequeueReasonNamespaceMismatch)
    75  }