github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/checkers/cnservice/check.go (about) 1 // Copyright 2022 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 cnservice 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/common/runtime" 19 "github.com/matrixorigin/matrixone/pkg/hakeeper" 20 "github.com/matrixorigin/matrixone/pkg/hakeeper/operator" 21 pb "github.com/matrixorigin/matrixone/pkg/pb/logservice" 22 "go.uber.org/zap" 23 ) 24 25 func Check(cfg hakeeper.Config, infos pb.CNState, user pb.TaskTableUser, currentTick uint64) (operators []*operator.Operator) { 26 if user.Username == "" { 27 runtime.ProcessLevelRuntime().Logger().Warn("username is still empty.") 28 return 29 } 30 working, expired := parseCNStores(cfg, infos, currentTick) 31 if len(working)+len(expired) == 0 { 32 runtime.ProcessLevelRuntime().Logger().Error("there are no CNs yet.") 33 return 34 } 35 for _, store := range working { 36 if !infos.Stores[store].TaskServiceCreated { 37 runtime.ProcessLevelRuntime().Logger().Info("create task service for CN.", 38 zap.String("uuid", store)) 39 operators = append(operators, operator.CreateTaskServiceOp("", 40 store, pb.CNService, user)) 41 } 42 // If this instance has not joined gossip cluster, we generate join command. 43 if !infos.Stores[store].GossipJoined { 44 addresses := getGossipAddresses(cfg, infos, currentTick, store) 45 if len(addresses) > 0 { 46 runtime.ProcessLevelRuntime().Logger().Info("join gossip cluster for CN", 47 zap.String("uuid", store), 48 zap.Any("addresses", addresses)) 49 operators = append(operators, operator.JoinGossipClusterOp("", 50 store, addresses)) 51 } 52 } 53 } 54 for _, store := range expired { 55 runtime.ProcessLevelRuntime().Logger().Warn("expired CN.", 56 zap.String("uuid", store)) 57 operators = append(operators, operator.CreateDeleteCNOp("", store)) 58 } 59 return operators 60 } 61 62 // parseCNStores returns all expired stores' ids. 63 func parseCNStores(cfg hakeeper.Config, infos pb.CNState, currentTick uint64) ([]string, []string) { 64 working := make([]string, 0) 65 expired := make([]string, 0) 66 for uuid, storeInfo := range infos.Stores { 67 if cfg.CNStoreExpired(storeInfo.Tick, currentTick) { 68 expired = append(expired, uuid) 69 } else { 70 working = append(working, uuid) 71 } 72 } 73 74 return working, expired 75 } 76 77 // getGossipAddresses returns the gossip addresses of CN stores that are in working state. 78 func getGossipAddresses(cfg hakeeper.Config, infos pb.CNState, currentTick uint64, self string) []string { 79 var addresses []string 80 var count int 81 for uuid, storeInfo := range infos.Stores { 82 if !cfg.CNStoreExpired(storeInfo.Tick, currentTick) && uuid != self { 83 if len(storeInfo.GossipAddress) > 0 { 84 addresses = append(addresses, storeInfo.GossipAddress) 85 count++ 86 if count > 2 { 87 break 88 } 89 } 90 } 91 } 92 return addresses 93 }