github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_unsafe_reset.go (about) 1 // Copyright 2021 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 cli 15 16 import ( 17 "github.com/pingcap/errors" 18 "github.com/pingcap/tiflow/pkg/cmd/context" 19 "github.com/pingcap/tiflow/pkg/cmd/factory" 20 "github.com/pingcap/tiflow/pkg/cmd/util" 21 "github.com/pingcap/tiflow/pkg/etcd" 22 "github.com/pingcap/tiflow/pkg/txnutil/gc" 23 "github.com/spf13/cobra" 24 pd "github.com/tikv/pd/client" 25 ) 26 27 // unsafeResetOptions defines flags for the `cli unsafe reset` command. 28 type unsafeResetOptions struct { 29 clusterID string 30 etcdClient *etcd.CDCEtcdClientImpl 31 pdClient pd.Client 32 } 33 34 // newUnsafeResetOptions creates new unsafeResetOptions for the `cli unsafe reset` command. 35 func newUnsafeResetOptions() *unsafeResetOptions { 36 return &unsafeResetOptions{} 37 } 38 39 // complete adapts from the command line args to the data and client required. 40 func (o *unsafeResetOptions) complete(f factory.Factory) (err error) { 41 pdClient, err := f.PdClient() 42 if err != nil { 43 return err 44 } 45 defer func() { 46 if err != nil { 47 pdClient.Close() 48 } 49 }() 50 51 o.pdClient = pdClient 52 53 etcdClient, err := f.EtcdClient() 54 if err != nil { 55 return err 56 } 57 etcdClient.ClusterID = o.clusterID 58 o.etcdClient = etcdClient 59 60 return nil 61 } 62 63 func (o *unsafeResetOptions) addFlags(cmd *cobra.Command) { 64 cmd.Flags().StringVar(&o.clusterID, "cluster-id", "default", "cdc cluster id") 65 } 66 67 // run runs the `cli unsafe reset` command. 68 func (o *unsafeResetOptions) run(cmd *cobra.Command) error { 69 ctx := context.GetDefaultContext() 70 defer o.pdClient.Close() 71 defer o.etcdClient.Close() 72 73 leases, err := o.etcdClient.GetCaptureLeases(ctx) 74 if err != nil { 75 return errors.Trace(err) 76 } 77 78 err = o.etcdClient.ClearAllCDCInfo(ctx) 79 if err != nil { 80 return errors.Trace(err) 81 } 82 83 err = o.etcdClient.RevokeAllLeases(ctx, leases) 84 if err != nil { 85 return errors.Trace(err) 86 } 87 88 err = gc.RemoveServiceGCSafepoint(ctx, o.pdClient, o.etcdClient.GetGCServiceID()) 89 if err != nil { 90 return errors.Trace(err) 91 } 92 93 cmd.Println("reset and all metadata truncated in PD!") 94 95 return nil 96 } 97 98 // newCmdReset creates the `cli unsafe reset` command. 99 func newCmdReset(f factory.Factory, commonOptions *unsafeCommonOptions) *cobra.Command { 100 o := newUnsafeResetOptions() 101 102 command := &cobra.Command{ 103 Use: "reset", 104 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", 105 Args: cobra.NoArgs, 106 Run: func(cmd *cobra.Command, args []string) { 107 util.CheckErr(commonOptions.confirmMetaDelete(cmd)) 108 util.CheckErr(o.complete(f)) 109 util.CheckErr(o.run(cmd)) 110 }, 111 } 112 o.addFlags(command) 113 114 return command 115 }