github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/jobmaster/example/master_impl.go (about) 1 // Copyright 2022 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package example 15 16 import ( 17 "context" 18 "sync" 19 20 "github.com/pingcap/log" 21 "github.com/pingcap/tiflow/engine/framework" 22 frameModel "github.com/pingcap/tiflow/engine/framework/model" 23 "github.com/pingcap/tiflow/engine/pkg/p2p" 24 "go.uber.org/zap" 25 ) 26 27 const ( 28 exampleWorkerType = 999 29 exampleWorkerCfg = "config" 30 ) 31 32 var _ framework.Master = &exampleMaster{} 33 34 type exampleMaster struct { 35 *framework.DefaultBaseMaster 36 37 worker struct { 38 mu sync.Mutex 39 40 id frameModel.WorkerID 41 handle framework.WorkerHandle 42 online bool 43 statusCode frameModel.WorkerState 44 receivedErr error 45 } 46 47 tickCount int 48 } 49 50 func (e *exampleMaster) InitImpl(ctx context.Context) (err error) { 51 log.Info("InitImpl") 52 e.worker.mu.Lock() 53 e.worker.id, err = e.CreateWorker(exampleWorkerType, exampleWorkerCfg) 54 e.worker.mu.Unlock() 55 return 56 } 57 58 func (e *exampleMaster) Tick(ctx context.Context) error { 59 log.Info("Tick") 60 e.tickCount++ 61 62 e.worker.mu.Lock() 63 defer e.worker.mu.Unlock() 64 handle := e.worker.handle 65 if handle == nil { 66 return nil 67 } 68 e.worker.statusCode = handle.Status().State 69 log.Info("status", zap.Any("status", handle.Status())) 70 return nil 71 } 72 73 func (e *exampleMaster) OnMasterRecovered(ctx context.Context) error { 74 log.Info("OnMasterRecovered") 75 return nil 76 } 77 78 func (e *exampleMaster) OnWorkerDispatched(worker framework.WorkerHandle, result error) error { 79 log.Info("OnWorkerDispatched") 80 e.worker.mu.Lock() 81 e.worker.handle = worker 82 e.worker.receivedErr = result 83 e.worker.mu.Unlock() 84 return nil 85 } 86 87 func (e *exampleMaster) OnWorkerOnline(worker framework.WorkerHandle) error { 88 log.Info("OnWorkerOnline") 89 e.worker.mu.Lock() 90 e.worker.handle = worker 91 e.worker.online = true 92 e.worker.mu.Unlock() 93 return nil 94 } 95 96 func (e *exampleMaster) OnWorkerOffline(worker framework.WorkerHandle, reason error) error { 97 log.Info("OnWorkerOffline") 98 return nil 99 } 100 101 func (e *exampleMaster) OnWorkerMessage(worker framework.WorkerHandle, topic p2p.Topic, message interface{}) error { 102 log.Info("OnWorkerMessage") 103 return nil 104 } 105 106 func (e *exampleMaster) CloseImpl(ctx context.Context) { 107 log.Info("CloseImpl") 108 } 109 110 func (e *exampleMaster) StopImpl(ctx context.Context) { 111 log.Info("StopImpl") 112 } 113 114 func (e *exampleMaster) OnWorkerStatusUpdated(worker framework.WorkerHandle, newStatus *frameModel.WorkerStatus) error { 115 log.Info("OnWorkerStatusUpdated") 116 return nil 117 }