github.com/matrixorigin/matrixone@v0.7.0/pkg/logservice/service_bootstrap.go (about) 1 // Copyright 2021 - 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 logservice 16 17 import ( 18 "context" 19 "time" 20 21 "github.com/lni/dragonboat/v4" 22 "github.com/matrixorigin/matrixone/pkg/common/runtime" 23 "github.com/matrixorigin/matrixone/pkg/pb/task" 24 "go.uber.org/zap" 25 ) 26 27 func (s *Service) BootstrapHAKeeper(ctx context.Context, cfg Config) error { 28 replicaID, bootstrapping := cfg.Bootstrapping() 29 if !bootstrapping { 30 return nil 31 } 32 members, err := cfg.GetInitHAKeeperMembers() 33 if err != nil { 34 return err 35 } 36 if err := s.store.startHAKeeperReplica(replicaID, members, false); err != nil { 37 // let's be a little less strict, when HAKeeper replica is already 38 // running as a result of store.startReplicas(), we just ignore the 39 // dragonboat.ErrShardAlreadyExist error below. 40 if err != dragonboat.ErrShardAlreadyExist { 41 s.runtime.SubLogger(runtime.SystemInit).Error("failed to start hakeeper replica", zap.Error(err)) 42 return err 43 } 44 } 45 numOfLogShards := cfg.BootstrapConfig.NumOfLogShards 46 numOfDNShards := cfg.BootstrapConfig.NumOfDNShards 47 numOfLogReplicas := cfg.BootstrapConfig.NumOfLogShardReplicas 48 for i := 0; i < checkBootstrapCycles; i++ { 49 select { 50 case <-ctx.Done(): 51 return nil 52 default: 53 } 54 if err := s.store.setInitialClusterInfo(numOfLogShards, 55 numOfDNShards, numOfLogReplicas); err != nil { 56 s.runtime.SubLogger(runtime.SystemInit).Error("failed to set initial cluster info", zap.Error(err)) 57 if err == dragonboat.ErrShardNotFound { 58 return nil 59 } 60 time.Sleep(time.Second) 61 continue 62 } 63 s.runtime.SubLogger(runtime.SystemInit).Info("initial cluster info set") 64 break 65 } 66 for i := 0; i < checkBootstrapCycles; i++ { 67 select { 68 case <-ctx.Done(): 69 return nil 70 default: 71 } 72 if err := s.createInitTasks(ctx); err == nil { 73 break 74 } 75 time.Sleep(time.Second) 76 } 77 return nil 78 } 79 80 func (s *Service) createInitTasks(ctx context.Context) error { 81 if err := s.store.taskScheduler.Create(ctx, []task.TaskMetadata{{ 82 ID: task.TaskCode_SystemInit.String(), 83 Executor: task.TaskCode_SystemInit, 84 }}); err != nil { 85 s.runtime.SubLogger(runtime.SystemInit).Error("failed to create init tasks", zap.Error(err)) 86 return err 87 } 88 s.runtime.SubLogger(runtime.SystemInit).Info("init tasks created") 89 return nil 90 }