github.com/matrixorigin/matrixone@v1.2.0/cmd/mo-service/setup.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 main 16 17 import ( 18 "context" 19 "sync" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 "github.com/matrixorigin/matrixone/pkg/common/mpool" 23 "github.com/matrixorigin/matrixone/pkg/common/runtime" 24 "github.com/matrixorigin/matrixone/pkg/common/stopper" 25 "github.com/matrixorigin/matrixone/pkg/logutil" 26 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 27 "github.com/matrixorigin/matrixone/pkg/txn/clock" 28 "go.uber.org/zap" 29 ) 30 31 const ( 32 localClockBackend = "LOCAL" 33 hlcClockBackend = "HLC" 34 ) 35 36 var ( 37 supportTxnClockBackends = map[string]struct{}{ 38 localClockBackend: {}, 39 hlcClockBackend: {}, 40 } 41 ) 42 43 var ( 44 logOnce sync.Once 45 setupRuntimeOnce sync.Once 46 ) 47 48 func setupProcessLevelRuntime(cfg *Config, stopper *stopper.Stopper) error { 49 var e error 50 setupRuntimeOnce.Do(func() { 51 mpool.InitCap(int64(cfg.Limit.Memory)) 52 r, err := newRuntime(cfg, stopper) 53 if err != nil { 54 e = err 55 return 56 } 57 runtime.SetupProcessLevelRuntime(r) 58 }) 59 return e 60 } 61 62 func getRuntime(st metadata.ServiceType, cfg *Config, stopper *stopper.Stopper) (runtime.Runtime, error) { 63 switch st { 64 case metadata.ServiceType_TN: 65 return newRuntime(cfg, stopper) 66 default: 67 return runtime.ProcessLevelRuntime(), nil 68 } 69 } 70 71 func newRuntime(cfg *Config, stopper *stopper.Stopper) (runtime.Runtime, error) { 72 clock, err := getClock(cfg, stopper) 73 if err != nil { 74 return nil, err 75 } 76 77 logger, err := getLogger(cfg) 78 if err != nil { 79 return nil, err 80 } 81 82 return runtime.NewRuntime(cfg.mustGetServiceType(), 83 cfg.mustGetServiceUUID(), 84 logger, 85 runtime.WithClock(clock)), nil 86 } 87 88 func getClock(cfg *Config, stopper *stopper.Stopper) (clock.Clock, error) { 89 var c clock.Clock 90 switch cfg.Clock.Backend { 91 case localClockBackend: 92 c = newLocalClock(cfg, stopper) 93 default: 94 return nil, moerr.NewInternalError(context.Background(), "not implment for %s", cfg.Clock.Backend) 95 } 96 c.SetNodeID(cfg.hashNodeID()) 97 return c, nil 98 } 99 100 func getLogger(cfg *Config) (*zap.Logger, error) { 101 initLogger(cfg) 102 logger := logutil.GetGlobalLogger() 103 return logger, nil 104 } 105 106 func newLocalClock(cfg *Config, stopper *stopper.Stopper) clock.Clock { 107 return clock.NewUnixNanoHLCClockWithStopper(stopper, cfg.Clock.MaxClockOffset.Duration) 108 } 109 110 func initLogger(cfg *Config) { 111 logOnce.Do(func() { 112 logutil.SetupMOLogger(&cfg.Log) 113 }) 114 } 115 116 func setupStatusServer(rt runtime.Runtime) { 117 ss, ok := rt.GetGlobalVariables(runtime.StatusServer) 118 if !ok || ss == nil { 119 rt.SetGlobalVariables(runtime.StatusServer, statusServer) 120 } 121 }