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

     1  // Copyright 2019 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  
    21  	"github.com/pingcap/tiflow/dm/ctl/common"
    22  	"github.com/pingcap/tiflow/dm/pb"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  // NewUnlockDDLLockCmd creates a UnlockDDLLock command.
    27  func NewUnlockDDLLockCmd() *cobra.Command {
    28  	cmd := &cobra.Command{
    29  		Use:    "unlock-ddl-lock <lock-ID>",
    30  		Short:  "Unlocks DDL lock forcefully",
    31  		Hidden: true,
    32  		RunE:   unlockDDLLockFunc,
    33  	}
    34  	cmd.Flags().StringP("owner", "o", "", "source to replace the default owner")
    35  	cmd.Flags().BoolP("force-remove", "f", false, "force to remove DDL lock")
    36  	cmd.Flags().StringP("action", "a", "skip", "accept skip/exec values which means whether to skip or execute ddls")
    37  	cmd.Flags().StringP("database", "d", "", "database name of the table")
    38  	cmd.Flags().StringP("table", "t", "", "table name")
    39  	return cmd
    40  }
    41  
    42  // unlockDDLLockFunc does unlock DDL lock.
    43  func unlockDDLLockFunc(cmd *cobra.Command, _ []string) error {
    44  	if len(cmd.Flags().Args()) != 1 {
    45  		cmd.SetOut(os.Stdout)
    46  		common.PrintCmdUsage(cmd)
    47  		return errors.New("please check output to see error")
    48  	}
    49  	owner, err := cmd.Flags().GetString("owner")
    50  	if err != nil {
    51  		common.PrintLinesf("error in parse `--owner`")
    52  		return err
    53  	}
    54  
    55  	lockID := cmd.Flags().Arg(0)
    56  
    57  	sources, err := common.GetSourceArgs(cmd)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	forceRemove, err := cmd.Flags().GetBool("force-remove")
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	database, err := cmd.Flags().GetString("database")
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	table, err := cmd.Flags().GetString("table")
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	action, err := cmd.Flags().GetString("action")
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	var op pb.UnlockDDLLockOp
    83  	switch action {
    84  	case "exec":
    85  		op = pb.UnlockDDLLockOp_ExecLock
    86  	case "skip":
    87  		op = pb.UnlockDDLLockOp_SkipLock
    88  	default:
    89  		return errors.New("please check --action argument, only exec/skip are acceptable")
    90  	}
    91  
    92  	ctx, cancel := context.WithCancel(context.Background())
    93  	defer cancel()
    94  
    95  	resp := &pb.UnlockDDLLockResponse{}
    96  	err = common.SendRequest(
    97  		ctx,
    98  		"UnlockDDLLock",
    99  		&pb.UnlockDDLLockRequest{
   100  			ID:           lockID,
   101  			ReplaceOwner: owner,
   102  			ForceRemove:  forceRemove,
   103  			Sources:      sources,
   104  			Database:     database,
   105  			Table:        table,
   106  			Op:           op,
   107  		},
   108  		&resp,
   109  	)
   110  
   111  	if err != nil {
   112  		common.PrintLinesf("can not unlock DDL lock %s", lockID)
   113  		return err
   114  	}
   115  
   116  	common.PrettyPrintResponse(resp)
   117  	return nil
   118  }