github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/ha/worker.go (about) 1 // Copyright 2020 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 ha 15 16 import ( 17 "context" 18 "encoding/json" 19 "fmt" 20 21 "github.com/pingcap/tiflow/dm/common" 22 "github.com/pingcap/tiflow/dm/pkg/etcdutil" 23 "github.com/pingcap/tiflow/dm/pkg/terror" 24 clientv3 "go.etcd.io/etcd/client/v3" 25 ) 26 27 // WorkerInfo represents the node information of the DM-worker. 28 type WorkerInfo struct { 29 Name string `json:"name"` // the name of the node. 30 Addr string `json:"addr"` // the client address of the node to advertise. 31 } 32 33 // NewWorkerInfo creates a new WorkerInfo instance. 34 func NewWorkerInfo(name, addr string) WorkerInfo { 35 return WorkerInfo{ 36 Name: name, 37 Addr: addr, 38 } 39 } 40 41 // String implements Stringer interface. 42 func (i WorkerInfo) String() string { 43 s, _ := i.toJSON() 44 return s 45 } 46 47 // toJSON returns the string of JSON represent. 48 func (i WorkerInfo) toJSON() (string, error) { 49 data, err := json.Marshal(i) 50 if err != nil { 51 return "", terror.ErrHAInvalidItem.Delegate(err, fmt.Sprintf("failed to marshal worker info: %+v", i)) 52 } 53 return string(data), nil 54 } 55 56 // workerInfoFromJSON constructs WorkerInfo from its JSON represent. 57 func workerInfoFromJSON(s string) (i WorkerInfo, err error) { 58 if err = json.Unmarshal([]byte(s), &i); err != nil { 59 err = terror.ErrHAInvalidItem.Delegate(err, fmt.Sprintf("failed to unmarshal worker info: %s", s)) 60 } 61 return 62 } 63 64 // PutWorkerInfo puts the DM-worker info into etcd. 65 // k/v: worker-name -> worker information. 66 func PutWorkerInfo(cli *clientv3.Client, info WorkerInfo) (int64, error) { 67 value, err := info.toJSON() 68 if err != nil { 69 return 0, err 70 } 71 key := common.WorkerRegisterKeyAdapter.Encode(info.Name) 72 _, rev, err := etcdutil.DoTxnWithRepeatable(cli, etcdutil.ThenOpFunc(clientv3.OpPut(key, value))) 73 return rev, err 74 } 75 76 // GetAllWorkerInfo gets all DM-worker info in etcd currently. 77 // k/v: worker-name -> worker information. 78 func GetAllWorkerInfo(cli *clientv3.Client) (map[string]WorkerInfo, int64, error) { 79 ctx, cancel := context.WithTimeout(cli.Ctx(), etcdutil.DefaultRequestTimeout) 80 defer cancel() 81 82 resp, err := cli.Get(ctx, common.WorkerRegisterKeyAdapter.Path(), clientv3.WithPrefix()) 83 if err != nil { 84 return nil, 0, terror.ErrHAFailTxnOperation.Delegate(err, "failed to get all worker info") 85 } 86 87 ifm := make(map[string]WorkerInfo) 88 for _, kv := range resp.Kvs { 89 info, err2 := workerInfoFromJSON(string(kv.Value)) 90 if err2 != nil { 91 return nil, 0, err2 92 } 93 94 ifm[info.Name] = info 95 } 96 97 return ifm, resp.Header.Revision, nil 98 } 99 100 // DeleteWorkerInfoRelayConfig deletes the specified DM-worker information and its relay config. 101 func DeleteWorkerInfoRelayConfig(cli *clientv3.Client, worker string) (int64, error) { 102 ops := []clientv3.Op{ 103 clientv3.OpDelete(common.WorkerRegisterKeyAdapter.Encode(worker)), 104 clientv3.OpDelete(common.UpstreamRelayWorkerKeyAdapter.Encode(worker)), 105 } 106 _, rev, err := etcdutil.DoTxnWithRepeatable(cli, etcdutil.ThenOpFunc(ops...)) 107 return rev, err 108 }