github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/framework/common.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 framework 15 16 import ( 17 "github.com/pingcap/log" 18 "github.com/pingcap/tiflow/engine/framework/internal/master" 19 frameModel "github.com/pingcap/tiflow/engine/framework/model" 20 engineModel "github.com/pingcap/tiflow/engine/model" 21 "go.uber.org/zap" 22 ) 23 24 type ( 25 // WorkerType alias to model.WorkerType 26 WorkerType = frameModel.WorkerType 27 28 // WorkerConfig stores worker config in any type 29 WorkerConfig = interface{} 30 31 // WorkerHandle alias to master.WorkerHandle 32 WorkerHandle = master.WorkerHandle 33 34 // MockHandle is a mock for WorkerHandle. 35 // Re-exported for testing. 36 MockHandle = master.MockHandle 37 38 // MasterFailoverReasonCode is used as reason code 39 MasterFailoverReasonCode int32 40 ) 41 42 // Defines all reason codes 43 const ( 44 MasterTimedOut = MasterFailoverReasonCode(iota + 1) 45 MasterReportedError 46 ) 47 48 // MasterFailoverReason contains failover reason code and error message 49 type MasterFailoverReason struct { 50 Code MasterFailoverReasonCode 51 ErrorMsg string 52 } 53 54 // MustConvertWorkerType2JobType return the job type of worker type. Panic if it fail. 55 // TODO: let user register a unique identifier for the metric prefix 56 func MustConvertWorkerType2JobType(tp WorkerType) engineModel.JobType { 57 switch tp { 58 case frameModel.JobManager: 59 // jobmanager is the framework level job 60 return engineModel.JobTypeJobManager 61 case frameModel.CvsJobMaster, frameModel.CvsTask: 62 return engineModel.JobTypeCVSDemo 63 case frameModel.FakeJobMaster, frameModel.FakeTask: 64 return engineModel.JobTypeFakeJob 65 case frameModel.DMJobMaster, frameModel.DmTask, frameModel.WorkerDMDump, frameModel.WorkerDMLoad, frameModel.WorkerDMSync: 66 return engineModel.JobTypeDM 67 case frameModel.CdcJobMaster, frameModel.CdcTask: 68 return engineModel.JobTypeCDC 69 } 70 71 log.Panic("unexpected fail when convert worker type to job type", zap.Stringer("worker_type", tp)) 72 return engineModel.JobTypeInvalid 73 } 74 75 // ExitReason is the type for exit reason 76 type ExitReason int 77 78 // define some ExitReason 79 const ( 80 ExitReasonUnknown = ExitReason(iota) 81 ExitReasonFinished 82 ExitReasonCanceled 83 ExitReasonFailed 84 ) 85 86 // WorkerStateToExitReason translates WorkerState to ExitReason 87 // TODO: business logic should not sense 'WorkerState' 88 func WorkerStateToExitReason(code frameModel.WorkerState) ExitReason { 89 switch code { 90 case frameModel.WorkerStateFinished: 91 return ExitReasonFinished 92 case frameModel.WorkerStateStopped: 93 return ExitReasonCanceled 94 case frameModel.WorkerStateError: 95 return ExitReasonFailed 96 } 97 98 return ExitReasonUnknown 99 }