github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cmd/client_unsafe.go (about)

     1  // Copyright 2020 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 cmd
    15  
    16  import (
    17  	"fmt"
    18  	"strings"
    19  
    20  	"github.com/pingcap/errors"
    21  	"github.com/pingcap/ticdc/cdc"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  func newUnsafeCommand() *cobra.Command {
    26  	command := &cobra.Command{
    27  		Use:    "unsafe",
    28  		Hidden: true,
    29  	}
    30  	command.AddCommand(
    31  		newDeleteServiceGcSafepointCommand(),
    32  		newResetCommand(),
    33  		newShowMetadataCommand(),
    34  	)
    35  	command.PersistentFlags().BoolVar(&noConfirm, "no-confirm", false, "Don't ask user whether to confirm executing meta command")
    36  	return command
    37  }
    38  
    39  func newResetCommand() *cobra.Command {
    40  	command := &cobra.Command{
    41  		Use:   "reset",
    42  		Short: "Reset the status of the TiCDC cluster, delete all meta data in etcd, confirm that you know what this command will do and use it at your own risk",
    43  		RunE: func(cmd *cobra.Command, args []string) error {
    44  			if err := confirmMetaDelete(cmd); err != nil {
    45  				return err
    46  			}
    47  			ctx := defaultContext
    48  
    49  			leases, err := cdcEtcdCli.GetCaptureLeases(ctx)
    50  			if err != nil {
    51  				return errors.Trace(err)
    52  			}
    53  
    54  			err = cdcEtcdCli.ClearAllCDCInfo(ctx)
    55  			if err != nil {
    56  				return errors.Trace(err)
    57  			}
    58  
    59  			err = cdcEtcdCli.RevokeAllLeases(ctx, leases)
    60  			if err != nil {
    61  				return errors.Trace(err)
    62  			}
    63  
    64  			_, err = pdCli.UpdateServiceGCSafePoint(ctx, cdc.CDCServiceSafePointID, 0, 0)
    65  			if err != nil {
    66  				return errors.Trace(err)
    67  			}
    68  
    69  			cmd.Println("reset and all metadata truncated in PD!")
    70  
    71  			return nil
    72  		},
    73  	}
    74  	return command
    75  }
    76  
    77  func newShowMetadataCommand() *cobra.Command {
    78  	command := &cobra.Command{
    79  		Use:   "show-metadata",
    80  		Short: "Show metadata stored in PD",
    81  		RunE: func(cmd *cobra.Command, args []string) error {
    82  			ctx := defaultContext
    83  			kvs, err := cdcEtcdCli.GetAllCDCInfo(ctx)
    84  			if err != nil {
    85  				return errors.Trace(err)
    86  			}
    87  			for _, kv := range kvs {
    88  				cmd.Printf("Key: %s, Value: %s\n", string(kv.Key), string(kv.Value))
    89  			}
    90  			cmd.Printf("Show %d KVs\n", len(kvs))
    91  			return nil
    92  		},
    93  	}
    94  	return command
    95  }
    96  
    97  func newDeleteServiceGcSafepointCommand() *cobra.Command {
    98  	command := &cobra.Command{
    99  		Use:   "delete-service-gc-safepoint",
   100  		Short: "Delete CDC service GC safepoint in PD, confirm that you know what this command will do and use it at your own risk",
   101  		RunE: func(cmd *cobra.Command, args []string) error {
   102  			if err := confirmMetaDelete(cmd); err != nil {
   103  				return err
   104  			}
   105  			ctx := defaultContext
   106  			_, err := pdCli.UpdateServiceGCSafePoint(ctx, cdc.CDCServiceSafePointID, 0, 0)
   107  			if err == nil {
   108  				cmd.Println("CDC service GC safepoint truncated in PD!")
   109  			}
   110  			return errors.Trace(err)
   111  		},
   112  	}
   113  	return command
   114  }
   115  
   116  func confirmMetaDelete(cmd *cobra.Command) error {
   117  	if noConfirm {
   118  		return nil
   119  	}
   120  	cmd.Printf("Confirm that you know what this command will do and use it at your own risk [Y/N]\n")
   121  	var yOrN string
   122  	_, err := fmt.Scan(&yOrN)
   123  	if err != nil {
   124  		return err
   125  	}
   126  	if strings.ToLower(strings.TrimSpace(yOrN)) != "y" {
   127  		return errors.NewNoStackError("abort meta command")
   128  	}
   129  	return nil
   130  }