volcano.sh/volcano@v1.9.0/pkg/scheduler/actions/shuffle/shuffle.go (about)

     1  /*
     2   Copyright 2022 The Volcano 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 shuffle
    18  
    19  import (
    20  	"k8s.io/klog/v2"
    21  
    22  	"volcano.sh/volcano/pkg/scheduler/api"
    23  	"volcano.sh/volcano/pkg/scheduler/framework"
    24  )
    25  
    26  const (
    27  	// Shuffle indicates the action name
    28  	Shuffle = "shuffle"
    29  )
    30  
    31  // Action defines the action
    32  type Action struct{}
    33  
    34  // New returns the action instance
    35  func New() *Action {
    36  	return &Action{}
    37  }
    38  
    39  // Name returns the action name
    40  func (shuffle *Action) Name() string {
    41  	return Shuffle
    42  }
    43  
    44  // Initialize inits the action
    45  func (shuffle *Action) Initialize() {}
    46  
    47  // Execute select evictees according given strategies and evict them.
    48  func (shuffle *Action) Execute(ssn *framework.Session) {
    49  	klog.V(5).Infoln("Enter Shuffle ...")
    50  	defer klog.V(5).Infoln("Leaving Shuffle ...")
    51  
    52  	// select pods that may be evicted
    53  	tasks := make([]*api.TaskInfo, 0)
    54  	for _, jobInfo := range ssn.Jobs {
    55  		for _, taskInfo := range jobInfo.Tasks {
    56  			if taskInfo.Status == api.Running {
    57  				tasks = append(tasks, taskInfo)
    58  			}
    59  		}
    60  	}
    61  
    62  	// Evict target workloads
    63  	victims := ssn.VictimTasks(tasks)
    64  	for victim := range victims {
    65  		klog.V(3).Infof("pod %s from namespace %s and job %s will be evicted.\n", victim.Name, victim.Namespace, string(victim.Job))
    66  		if err := ssn.Evict(victim, "shuffle"); err != nil {
    67  			klog.Errorf("Failed to evict Task <%s/%s>: %v\n", victim.Namespace, victim.Name, err)
    68  			continue
    69  		}
    70  	}
    71  }
    72  
    73  // UnInitialize releases resource which is not useful.
    74  func (shuffle *Action) UnInitialize() {}