github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/executor/dm/api.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 dm 15 16 import ( 17 "bytes" 18 "context" 19 "fmt" 20 21 "github.com/gogo/protobuf/jsonpb" 22 "github.com/gogo/protobuf/proto" 23 "github.com/pingcap/tiflow/engine/framework" 24 dmpkg "github.com/pingcap/tiflow/engine/pkg/dm" 25 "github.com/pingcap/tiflow/pkg/errors" 26 ) 27 28 // QueryStatus implements the api of query status request. 29 // QueryStatus is called by refection of commandHandler. 30 func (w *dmWorker) QueryStatus(ctx context.Context, req *dmpkg.QueryStatusRequest) *dmpkg.QueryStatusResponse { 31 if w.taskID != req.Task { 32 return &dmpkg.QueryStatusResponse{ErrorMsg: fmt.Sprintf("task id mismatch, get %s, actually %s", req.Task, w.taskID)} 33 } 34 // get status from unit 35 status := w.unitHolder.Status(ctx) 36 stage, result := w.unitHolder.Stage() 37 // copy status via json 38 mar := jsonpb.Marshaler{EmitDefaults: true} 39 var buf bytes.Buffer 40 err := mar.Marshal(&buf, status.(proto.Message)) 41 if err != nil { 42 return &dmpkg.QueryStatusResponse{ErrorMsg: err.Error()} 43 } 44 return &dmpkg.QueryStatusResponse{ 45 Unit: w.workerType, 46 Stage: stage, 47 Result: dmpkg.NewProcessResultFromPB(result), 48 Status: buf.Bytes(), 49 IoTotalBytes: w.cfg.IOTotalBytes.Load(), 50 DumpIoTotalBytes: w.cfg.DumpIOTotalBytes.Load(), 51 } 52 } 53 54 // StopWorker implements the api of stop worker message which kill itself. 55 // StopWorker is called by refection of commandHandler. 56 func (w *dmWorker) StopWorker(ctx context.Context, msg *dmpkg.StopWorkerMessage) error { 57 if w.taskID != msg.Task { 58 return errors.Errorf("task id mismatch, get %s, actually %s", msg.Task, w.taskID) 59 } 60 61 workerStatus := w.workerStatus(ctx) 62 return w.Exit(ctx, framework.ExitReasonCanceled, nil, workerStatus.ExtBytes) 63 } 64 65 // OperateTask implements the api of operate task message. 66 // OperateTask is called by refection of commandHandler. 67 func (w *dmWorker) OperateTask(ctx context.Context, msg *dmpkg.OperateTaskMessage) error { 68 if w.taskID != msg.Task { 69 return errors.Errorf("task id mismatch, get %s, actually %s", msg.Task, w.taskID) 70 } 71 switch msg.Op { 72 case dmpkg.Pause: 73 return w.unitHolder.Pause(ctx) 74 case dmpkg.Resume: 75 return w.unitHolder.Resume(ctx) 76 default: 77 return errors.Errorf("unsupported op type %d for task %s", msg.Op, w.taskID) 78 } 79 } 80 81 // BinlogTask implements the api of binlog task request. 82 // BinlogTask is called by refection of commandHandler. 83 func (w *dmWorker) BinlogTask(ctx context.Context, req *dmpkg.BinlogTaskRequest) *dmpkg.CommonTaskResponse { 84 msg, err := w.unitHolder.Binlog(ctx, req) 85 if err != nil { 86 return &dmpkg.CommonTaskResponse{ErrorMsg: err.Error()} 87 } 88 return &dmpkg.CommonTaskResponse{Msg: msg} 89 } 90 91 // BinlogSchemaTask implements the api of binlog schema request. 92 // BinlogSchemaTask is called by refection of commandHandler. 93 func (w *dmWorker) BinlogSchemaTask(ctx context.Context, req *dmpkg.BinlogSchemaTaskRequest) *dmpkg.CommonTaskResponse { 94 if w.taskID != req.Source { 95 return &dmpkg.CommonTaskResponse{ErrorMsg: fmt.Sprintf("task id mismatch, get %s, actually %s", req.Source, w.taskID)} 96 } 97 msg, err := w.unitHolder.BinlogSchema(ctx, req) 98 if err != nil { 99 return &dmpkg.CommonTaskResponse{ErrorMsg: err.Error()} 100 } 101 return &dmpkg.CommonTaskResponse{Msg: msg} 102 }