github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/ctl/master/operate_validation.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 master
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"os"
    20  	"strconv"
    21  
    22  	"github.com/pingcap/tiflow/dm/ctl/common"
    23  	"github.com/pingcap/tiflow/dm/pb"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func NewIgnoreValidationErrorCmd() *cobra.Command {
    28  	cmd := &cobra.Command{
    29  		Use:   "ignore-error <task-name> <error-id|--all>",
    30  		Short: "ignore validation error row change",
    31  		RunE:  operateValidationError(pb.ValidationErrOp_IgnoreErrOp),
    32  	}
    33  	cmd.Flags().Bool("all", false, "all errors")
    34  	return cmd
    35  }
    36  
    37  func NewResolveValidationErrorCmd() *cobra.Command {
    38  	cmd := &cobra.Command{
    39  		Use:   "resolve-error <task-name> <error-id|--all>",
    40  		Short: "resolve validation error row change",
    41  		RunE:  operateValidationError(pb.ValidationErrOp_ResolveErrOp),
    42  	}
    43  	cmd.Flags().Bool("all", false, "all errors")
    44  	return cmd
    45  }
    46  
    47  func NewClearValidationErrorCmd() *cobra.Command {
    48  	cmd := &cobra.Command{
    49  		Use:   "clear-error <task-name> <error-id|--all>",
    50  		Short: "clear validation error row change",
    51  		RunE:  operateValidationError(pb.ValidationErrOp_ClearErrOp),
    52  	}
    53  	cmd.Flags().Bool("all", false, "all errors")
    54  	return cmd
    55  }
    56  
    57  func operateValidationError(typ pb.ValidationErrOp) func(*cobra.Command, []string) error {
    58  	return func(cmd *cobra.Command, _ []string) error {
    59  		var (
    60  			resp     *pb.OperateValidationErrorResponse
    61  			taskName string
    62  			isAll    bool
    63  			errID    string
    64  			err      error
    65  			intErrID int
    66  		)
    67  		if len(cmd.Flags().Args()) < 1 {
    68  			cmd.SetOut(os.Stdout)
    69  			common.PrintCmdUsage(cmd)
    70  			return errors.New("task name should be specified")
    71  		}
    72  		if len(cmd.Flags().Args()) > 2 {
    73  			cmd.SetOut(os.Stdout)
    74  			common.PrintCmdUsage(cmd)
    75  			return errors.New("too many arguments are specified")
    76  		}
    77  		taskName = cmd.Flags().Arg(0)
    78  		if len(cmd.Flags().Args()) > 1 {
    79  			errID = cmd.Flags().Arg(1)
    80  		}
    81  		isAll, err = cmd.Flags().GetBool("all")
    82  		if err != nil {
    83  			return err
    84  		}
    85  		if (errID == "" && !isAll) || (errID != "" && isAll) {
    86  			cmd.SetOut(os.Stdout)
    87  			common.PrintCmdUsage(cmd)
    88  			return errors.New("either `--all` or `error-id` should be set")
    89  		}
    90  		if errID != "" {
    91  			intErrID, err = strconv.Atoi(errID)
    92  			if err != nil {
    93  				cmd.SetOut(os.Stdout)
    94  				common.PrintCmdUsage(cmd)
    95  				return errors.New("`error-id` should be integer when `--all` is not set")
    96  			}
    97  		}
    98  		ctx, cancel := context.WithCancel(context.Background())
    99  		defer cancel()
   100  		resp = &pb.OperateValidationErrorResponse{}
   101  		err = common.SendRequest(
   102  			ctx,
   103  			"OperateValidationError",
   104  			&pb.OperateValidationErrorRequest{
   105  				Op:         typ,
   106  				TaskName:   taskName,
   107  				ErrId:      uint64(intErrID),
   108  				IsAllError: isAll,
   109  			},
   110  			&resp,
   111  		)
   112  		if err != nil {
   113  			return err
   114  		}
   115  		common.PrettyPrintResponse(resp)
   116  		return nil
   117  	}
   118  }