github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/framework/model/typedefs.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 model 15 16 import ( 17 "encoding/json" 18 "fmt" 19 20 "github.com/pingcap/tiflow/pkg/errors" 21 ) 22 23 type ( 24 // MasterID is master id in master worker framework. 25 // - It is job manager id when master is job manager and worker is job master. 26 // - It is job master id when master is job master and worker is worker. 27 MasterID = string 28 // WorkerID is worker id in master worker framework. 29 // - It is job master id when master is job manager and worker is job master. 30 // - It is worker id when master is job master and worker is worker. 31 WorkerID = string 32 // WorkerType represents task type, such as DM worker, DM master, etc. 33 WorkerType int16 34 // Epoch is an increasing only value. 35 Epoch = int64 36 ) 37 38 // Defines all task type 39 // TODO: Refine me.Currently, when adding a new worker type or job type, we need to modify many code places, 40 // NOTICE: DO NOT CHANGE the previous worker type 41 // Modify the comment in model IF you add some new worker type 42 const ( 43 JobManager = WorkerType(iota + 1) 44 // job master 45 CvsJobMaster 46 FakeJobMaster 47 DMJobMaster 48 CdcJobMaster 49 // task 50 CvsTask 51 FakeTask 52 DmTask 53 CdcTask 54 // worker 55 WorkerDMDump 56 WorkerDMLoad 57 WorkerDMSync 58 // extend the worker type here 59 ) 60 61 var typesStringify = [...]string{ 62 0: "", 63 JobManager: "JobManager", 64 CvsJobMaster: "CVSJobMaster", 65 FakeJobMaster: "FakeJobMaster", 66 DMJobMaster: "DMJobMaster", 67 CdcJobMaster: "CDCJobMaster", 68 CvsTask: "CVSTask", 69 FakeTask: "FakeTask", 70 DmTask: "DMTask", 71 CdcTask: "CDCTask", 72 WorkerDMDump: "DMDumpTask", 73 WorkerDMLoad: "DMLoadTask", 74 WorkerDMSync: "DMSyncTask", 75 } 76 77 var toWorkerType map[string]WorkerType 78 79 func init() { 80 toWorkerType = make(map[string]WorkerType, len(typesStringify)) 81 for i, s := range typesStringify { 82 toWorkerType[s] = WorkerType(i) 83 } 84 } 85 86 // String implements fmt.Stringer interface 87 func (wt WorkerType) String() string { 88 if int(wt) >= len(typesStringify) || wt < 0 { 89 return fmt.Sprintf("Unknown WorkerType %d", wt) 90 } 91 return typesStringify[wt] 92 } 93 94 // MarshalJSON marshals the enum as a quoted json string 95 func (wt WorkerType) MarshalJSON() ([]byte, error) { 96 return json.Marshal(wt.String()) 97 } 98 99 // UnmarshalJSON unmashals a quoted json string to the enum value 100 func (wt *WorkerType) UnmarshalJSON(b []byte) error { 101 var ( 102 j string 103 ok bool 104 ) 105 if err := json.Unmarshal(b, &j); err != nil { 106 return err 107 } 108 *wt, ok = toWorkerType[j] 109 if !ok { 110 return errors.Errorf("Unknown WorkerType %s", j) 111 } 112 return nil 113 }