github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/cpu/dynamicpolicy/cpueviction/cpu_eviciton.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 cpueviction 18 19 import ( 20 "context" 21 "fmt" 22 "sync" 23 24 "k8s.io/apimachinery/pkg/util/errors" 25 26 "github.com/kubewharf/katalyst-api/pkg/plugins/skeleton" 27 "github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/agent" 28 "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/cpueviction/strategy" 29 "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state" 30 "github.com/kubewharf/katalyst-core/pkg/config" 31 "github.com/kubewharf/katalyst-core/pkg/metaserver" 32 "github.com/kubewharf/katalyst-core/pkg/metrics" 33 ) 34 35 func init() { 36 RegisterCPUEvictionInitializer(strategy.EvictionNameLoad, strategy.NewCPUPressureLoadEviction) 37 RegisterCPUEvictionInitializer(strategy.EvictionNameSuppression, strategy.NewCPUPressureSuppressionEviction) 38 } 39 40 var cpuEvictionInitializers sync.Map 41 42 // InitFunc is used to initialize a particular cpu eviction plugin. 43 type InitFunc func(emitter metrics.MetricEmitter, metaServer *metaserver.MetaServer, 44 conf *config.Configuration, state state.ReadonlyState) (strategy.CPUPressureEviction, error) 45 46 func RegisterCPUEvictionInitializer(name string, initFunc InitFunc) { 47 cpuEvictionInitializers.Store(name, initFunc) 48 } 49 50 func GetRegisteredInitializers() map[string]InitFunc { 51 initializers := make(map[string]InitFunc) 52 cpuEvictionInitializers.Range(func(key, value interface{}) bool { 53 initializers[key.(string)] = value.(InitFunc) 54 return true 55 }) 56 return initializers 57 } 58 59 type CPUPressureEviction struct { 60 plugins map[string]agent.Component 61 } 62 63 func (c *CPUPressureEviction) Run(ctx context.Context) { 64 for _, p := range c.plugins { 65 go p.Run(ctx) 66 } 67 <-ctx.Done() 68 } 69 70 func NewCPUPressureEviction(emitter metrics.MetricEmitter, metaServer *metaserver.MetaServer, 71 conf *config.Configuration, state state.ReadonlyState, 72 ) (*CPUPressureEviction, error) { 73 eviction, err := newCPUPressureEviction(emitter, metaServer, conf, state) 74 if err != nil { 75 return nil, fmt.Errorf("create cpu eviction plugin failed: %s", err) 76 } 77 78 return eviction, nil 79 } 80 81 func newCPUPressureEviction(emitter metrics.MetricEmitter, metaServer *metaserver.MetaServer, 82 conf *config.Configuration, state state.ReadonlyState, 83 ) (*CPUPressureEviction, error) { 84 var errList []error 85 86 plugins := make(map[string]agent.Component) 87 for name, f := range GetRegisteredInitializers() { 88 plugin, err := f(emitter, metaServer, conf, state) 89 if err != nil { 90 errList = append(errList, err) 91 continue 92 } 93 94 wrappedEmitter := emitter.WithTags(name) 95 pluginWrapper, err := skeleton.NewRegistrationPluginWrapper(strategy.NewCPUPressureEvictionPlugin(plugin, wrappedEmitter), 96 []string{conf.PluginRegistrationDir}, 97 func(key string, value int64) { 98 _ = wrappedEmitter.StoreInt64(key, value, metrics.MetricTypeNameRaw) 99 }) 100 if err != nil { 101 errList = append(errList, err) 102 continue 103 } 104 105 plugins[name] = &agent.PluginWrapper{GenericPlugin: pluginWrapper} 106 } 107 108 if len(errList) > 0 { 109 return nil, errors.NewAggregate(errList) 110 } 111 112 return &CPUPressureEviction{ 113 plugins: plugins, 114 }, nil 115 }