github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/ctl/master/update_validation.go (about)

     1  // Copyright 2023 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  
    19  	"github.com/pingcap/tiflow/dm/ctl/common"
    20  	"github.com/pingcap/tiflow/dm/pb"
    21  	"github.com/pingcap/tiflow/dm/pkg/binlog"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  const (
    26  	UpdateValidationOp = "update"
    27  )
    28  
    29  type validationUpdateArgs struct {
    30  	sources  []string
    31  	taskName string
    32  
    33  	cutoverBinlogPos  string
    34  	cutoverBinlogGTID string
    35  }
    36  
    37  func NewUpdateValidationCmd() *cobra.Command {
    38  	cmd := &cobra.Command{
    39  		Use:   "update [task-name]",
    40  		Short: "update validation config of the completeness of the data",
    41  		RunE:  updateValidation,
    42  	}
    43  	cmd.Flags().String("cutover-binlog-pos", "", "specify the cutover binlog name for validation, should include binlog name and pos in brackets, e.g. '(mysql-bin.000001, 5989)'")
    44  	cmd.Flags().String("cutover-binlog-gtid", "", "specify the cutover binlog gtid for validation, only valid when source config's gtid is enabled, e.g. '1642618e-cf65-11ec-9e3d-0242ac110002:1-30'")
    45  	return cmd
    46  }
    47  
    48  func updateValidation(cmd *cobra.Command, _ []string) error {
    49  	args, msg, ok := parseValidationUpdateArgs(cmd)
    50  	if !ok {
    51  		return printUsageAndFailWithMessage(cmd, msg)
    52  	}
    53  	req := &pb.UpdateValidationRequest{
    54  		TaskName:   args.taskName,
    55  		Sources:    args.sources,
    56  		BinlogPos:  args.cutoverBinlogPos,
    57  		BinlogGTID: args.cutoverBinlogGTID,
    58  	}
    59  
    60  	resp := &pb.UpdateValidationResponse{}
    61  	err := common.SendRequest(context.Background(), "UpdateValidation", req, &resp)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	common.PrettyPrintResponse(resp)
    66  	return nil
    67  }
    68  
    69  func parseValidationUpdateArgs(cmd *cobra.Command) (validationUpdateArgs, string, bool) {
    70  	var err error
    71  	args := validationUpdateArgs{}
    72  	if args.sources, err = common.GetSourceArgs(cmd); err != nil {
    73  		return args, err.Error(), false
    74  	}
    75  	if args.cutoverBinlogPos, err = cmd.Flags().GetString("cutover-binlog-pos"); err != nil {
    76  		return args, err.Error(), false
    77  	}
    78  	if len(args.cutoverBinlogPos) != 0 {
    79  		_, err = binlog.PositionFromPosStr(args.cutoverBinlogPos)
    80  		if err != nil {
    81  			return args, err.Error(), false
    82  		}
    83  	}
    84  
    85  	if args.cutoverBinlogGTID, err = cmd.Flags().GetString("cutover-binlog-gtid"); err != nil {
    86  		return args, err.Error(), false
    87  	}
    88  	if len(args.cutoverBinlogGTID) != 0 && len(args.cutoverBinlogPos) != 0 {
    89  		return args, "you must specify either one of '--cutover-binlog-pos' or '--cutover-binlog-pos'", false
    90  	}
    91  
    92  	if len(cmd.Flags().Args()) == 0 {
    93  		return args, "`task-name` should be set", false
    94  	} else if len(cmd.Flags().Args()) > 1 {
    95  		return args, "should specify only one `task-name`", false
    96  	}
    97  	args.taskName = cmd.Flags().Arg(0)
    98  	if len(args.taskName) == 0 {
    99  		return args, "`task-name` should be set", false
   100  	}
   101  	return args, "", true
   102  }