github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/task/cn_selector.go (about) 1 // Copyright 2024 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package task 16 17 import ( 18 "container/heap" 19 "strings" 20 21 "github.com/matrixorigin/matrixone/pkg/hakeeper" 22 pb "github.com/matrixorigin/matrixone/pkg/pb/logservice" 23 ) 24 25 type rule func(pb.CNStoreInfo) bool 26 27 func containsLabel(key, label string) func(info pb.CNStoreInfo) bool { 28 return func(info pb.CNStoreInfo) bool { 29 return contains(info.Labels[key].Labels, label) 30 } 31 } 32 33 func withMemory(requirement uint64) func(info pb.CNStoreInfo) bool { 34 return func(info pb.CNStoreInfo) bool { 35 return info.Resource.MemTotal >= requirement 36 } 37 } 38 39 func withCPU(requirement uint64) func(info pb.CNStoreInfo) bool { 40 return func(info pb.CNStoreInfo) bool { 41 return info.Resource.CPUTotal >= requirement 42 } 43 } 44 45 func notExpired(cfg hakeeper.Config, currentTick uint64) func(info pb.CNStoreInfo) bool { 46 return func(info pb.CNStoreInfo) bool { 47 return !cfg.CNStoreExpired(info.Tick, currentTick) 48 } 49 } 50 51 func matchAllRules(cn pb.CNStoreInfo, rules ...rule) bool { 52 for _, rule := range rules { 53 if !rule(cn) { 54 return false 55 } 56 } 57 return true 58 } 59 60 func (p *cnPool) selectCNs(rules ...rule) *cnPool { 61 newPool := newCNPool() 62 for _, cn := range p.sortedCN { 63 if matchAllRules(cn.info, rules...) { 64 heap.Push(newPool, cn) 65 } 66 } 67 return newPool 68 } 69 70 func contains(slice []string, val string) bool { 71 for _, v := range slice { 72 if strings.EqualFold(val, v) { 73 return true 74 } 75 } 76 return false 77 }