github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/dm/topic.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  	"encoding/json"
    18  	"fmt"
    19  
    20  	"github.com/pingcap/tiflow/dm/pb"
    21  	"github.com/pingcap/tiflow/dm/pkg/shardddl/optimism"
    22  	frameModel "github.com/pingcap/tiflow/engine/framework/model"
    23  	"github.com/pingcap/tiflow/engine/jobmaster/dm/metadata"
    24  	"github.com/pingcap/tiflow/engine/pkg/p2p"
    25  	"github.com/pingcap/tiflow/pkg/errors"
    26  )
    27  
    28  // Defines topics here
    29  const (
    30  	OperateTask  p2p.Topic = "OperateTask"
    31  	QueryStatus  p2p.Topic = "QueryStatus"
    32  	StopWorker   p2p.Topic = "StopWorker"
    33  	GetJobCfg    p2p.Topic = "GetJobCfg"
    34  	Binlog       p2p.Topic = "Binlog"
    35  	BinlogSchema p2p.Topic = "BinlogSchema"
    36  
    37  	// internal
    38  	BinlogTask       p2p.Topic = "BinlogTask"
    39  	BinlogSchemaTask p2p.Topic = "BinlogSchemaTask"
    40  	CoordinateDDL    p2p.Topic = "CoordinateDDL"
    41  )
    42  
    43  // OperateType represents internal operate type in DM
    44  // TODO: use OperateType in lib or move OperateType to lib.
    45  type OperateType int
    46  
    47  // These op may updated in later pr.
    48  const (
    49  	None OperateType = iota
    50  	Create
    51  	Pause
    52  	Resume
    53  	Update
    54  	Delete
    55  	// internal
    56  	Deleting
    57  )
    58  
    59  var typesStringify = [...]string{
    60  	0:        "",
    61  	Create:   "Create",
    62  	Pause:    "Pause",
    63  	Resume:   "Resume",
    64  	Update:   "Update",
    65  	Delete:   "Delete",
    66  	Deleting: "Deleting",
    67  }
    68  
    69  var toOperateType map[string]OperateType
    70  
    71  func init() {
    72  	toOperateType = make(map[string]OperateType, len(typesStringify))
    73  	for i, s := range typesStringify {
    74  		toOperateType[s] = OperateType(i)
    75  	}
    76  }
    77  
    78  // String implements fmt.Stringer interface
    79  func (op OperateType) String() string {
    80  	if int(op) >= len(typesStringify) || op < 0 {
    81  		return fmt.Sprintf("Unknown OperateType %d", op)
    82  	}
    83  	return typesStringify[op]
    84  }
    85  
    86  // MarshalJSON marshals the enum as a quoted json string
    87  func (op OperateType) MarshalJSON() ([]byte, error) {
    88  	return json.Marshal(op.String())
    89  }
    90  
    91  // UnmarshalJSON unmashals a quoted json string to the enum value
    92  func (op *OperateType) UnmarshalJSON(b []byte) error {
    93  	var (
    94  		j  string
    95  		ok bool
    96  	)
    97  	if err := json.Unmarshal(b, &j); err != nil {
    98  		return err
    99  	}
   100  	*op, ok = toOperateType[j]
   101  	if !ok {
   102  		return errors.Errorf("Unknown OperateType %s", j)
   103  	}
   104  	return nil
   105  }
   106  
   107  // OperateTaskMessage is operate task message
   108  type OperateTaskMessage struct {
   109  	Task string
   110  	Op   OperateType
   111  }
   112  
   113  // StopWorkerMessage is stop worker message
   114  type StopWorkerMessage struct {
   115  	Task string
   116  }
   117  
   118  // QueryStatusRequest is query status request
   119  type QueryStatusRequest struct {
   120  	Task string
   121  }
   122  
   123  // ProcessError copies pb.ProcessError expect for JSON tag.
   124  type ProcessError struct {
   125  	ErrCode    int32  `json:"error_code,omitempty"`
   126  	ErrClass   string `json:"error_class,omitempty"`
   127  	ErrScope   string `json:"error_scope,omitempty"`
   128  	ErrLevel   string `json:"error_level,omitempty"`
   129  	Message    string `json:"message,omitempty"`
   130  	RawCause   string `json:"raw_cause,omitempty"`
   131  	Workaround string `json:"workaround,omitempty"`
   132  }
   133  
   134  // ProcessResult copies pb.ProcessResult expect for JSON tag.
   135  type ProcessResult struct {
   136  	IsCanceled bool            `protobuf:"varint,1,opt,name=isCanceled,proto3" json:"is_canceled,omitempty"`
   137  	Errors     []*ProcessError `protobuf:"bytes,2,rep,name=errors,proto3" json:"errors,omitempty"`
   138  	Detail     []byte          `protobuf:"bytes,3,opt,name=detail,proto3" json:"detail,omitempty"`
   139  }
   140  
   141  // NewProcessResultFromPB converts ProcessResult from pb.ProcessResult.
   142  func NewProcessResultFromPB(result *pb.ProcessResult) *ProcessResult {
   143  	if result == nil {
   144  		return nil
   145  	}
   146  	ret := &ProcessResult{
   147  		IsCanceled: result.IsCanceled,
   148  		Detail:     result.Detail,
   149  	}
   150  	for _, err := range result.Errors {
   151  		ret.Errors = append(ret.Errors, &ProcessError{
   152  			ErrCode:    err.ErrCode,
   153  			ErrClass:   err.ErrClass,
   154  			ErrScope:   err.ErrScope,
   155  			ErrLevel:   err.ErrLevel,
   156  			Message:    err.Message,
   157  			RawCause:   err.RawCause,
   158  			Workaround: err.Workaround,
   159  		})
   160  	}
   161  	return ret
   162  }
   163  
   164  // QueryStatusResponse is query status response
   165  type QueryStatusResponse struct {
   166  	ErrorMsg         string                `json:"error_message"`
   167  	Unit             frameModel.WorkerType `json:"unit"`
   168  	Stage            metadata.TaskStage    `json:"stage"`
   169  	Result           *ProcessResult        `json:"result"`
   170  	Status           json.RawMessage       `json:"status"`
   171  	IoTotalBytes     uint64                `json:"io_total_bytes"`
   172  	DumpIoTotalBytes uint64                `json:"dump_io_total_bytes"`
   173  }
   174  
   175  // BinlogRequest is binlog request
   176  type BinlogRequest pb.HandleErrorRequest
   177  
   178  // BinlogResponse is binlog response
   179  type BinlogResponse struct {
   180  	ErrorMsg string
   181  	// taskID -> task response
   182  	Results map[string]*CommonTaskResponse
   183  }
   184  
   185  // BinlogTaskRequest is binlog task request
   186  type BinlogTaskRequest pb.HandleWorkerErrorRequest
   187  
   188  // BinlogSchemaRequest is binlog schema request
   189  type BinlogSchemaRequest pb.OperateSchemaRequest
   190  
   191  // BinlogSchemaResponse is binlog schema response
   192  type BinlogSchemaResponse struct {
   193  	ErrorMsg string
   194  	// taskID -> task response
   195  	Results map[string]*CommonTaskResponse
   196  }
   197  
   198  // BinlogSchemaTaskRequest is binlog schema task request
   199  type BinlogSchemaTaskRequest pb.OperateWorkerSchemaRequest
   200  
   201  // CommonTaskResponse is common task response
   202  type CommonTaskResponse struct {
   203  	ErrorMsg string
   204  	Msg      string
   205  }
   206  
   207  // CoordinateDDLRequest is coordinate DDL request
   208  type CoordinateDDLRequest metadata.DDLItem
   209  
   210  // CoordinateDDLResponse is coordinate DDL response
   211  type CoordinateDDLResponse struct {
   212  	ErrorMsg      string
   213  	DDLs          []string
   214  	ConflictStage optimism.ConflictStage
   215  }