volcano.sh/volcano@v1.9.0/pkg/scheduler/plugins/predicates/cache.go (about) 1 /* 2 Copyright 2020 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 predicates 18 19 import ( 20 "fmt" 21 "sync" 22 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/klog/v2" 25 26 batch "volcano.sh/apis/pkg/apis/batch/v1alpha1" 27 ) 28 29 type predicateCache struct { 30 sync.RWMutex 31 cache map[string]map[string]bool //key_1: nodename key_2:pod uid 32 } 33 34 // predicateCacheNew return cache map 35 func predicateCacheNew() *predicateCache { 36 return &predicateCache{ 37 cache: make(map[string]map[string]bool), 38 } 39 } 40 41 // getPodTemplateUID return pod template key 42 func getPodTemplateUID(pod *v1.Pod) string { 43 uid, found := pod.Annotations[batch.PodTemplateKey] 44 if !found { 45 return "" 46 } 47 48 return uid 49 } 50 51 // PredicateWithCache: check the predicate result existed in cache 52 func (pc *predicateCache) PredicateWithCache(nodeName string, pod *v1.Pod) (bool, error) { 53 podTemplateUID := getPodTemplateUID(pod) 54 if podTemplateUID == "" { 55 return false, fmt.Errorf("no anonation of volcano.sh/template-uid in pod %s", pod.Name) 56 } 57 58 pc.RLock() 59 defer pc.RUnlock() 60 if nodeCache, exist := pc.cache[nodeName]; exist { 61 if result, exist := nodeCache[podTemplateUID]; exist { 62 klog.V(4).Infof("Predicate node %s and pod %s result %v", nodeName, pod.Name, result) 63 return result, nil 64 } 65 } 66 67 return false, fmt.Errorf("no information of node %s and pod %s in predicate cache", nodeName, pod.Name) 68 } 69 70 // UpdateCache update cache data 71 func (pc *predicateCache) UpdateCache(nodeName string, pod *v1.Pod, fit bool) { 72 podTemplateUID := getPodTemplateUID(pod) 73 if podTemplateUID == "" { 74 klog.V(3).Infof("Don't find pod %s template uid", pod.Name) 75 return 76 } 77 78 pc.Lock() 79 defer pc.Unlock() 80 81 if _, exist := pc.cache[nodeName]; !exist { 82 podCache := make(map[string]bool) 83 podCache[podTemplateUID] = fit 84 pc.cache[nodeName] = podCache 85 } else { 86 pc.cache[nodeName][podTemplateUID] = fit 87 } 88 }