github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_changefeed_remove.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 "strings" 18 19 apiv2client "github.com/pingcap/tiflow/pkg/api/v2" 20 "github.com/pingcap/tiflow/pkg/cmd/context" 21 "github.com/pingcap/tiflow/pkg/cmd/factory" 22 "github.com/pingcap/tiflow/pkg/cmd/util" 23 cerror "github.com/pingcap/tiflow/pkg/errors" 24 "github.com/spf13/cobra" 25 ) 26 27 // removeChangefeedOptions defines flags for the `cli changefeed remove` command. 28 type removeChangefeedOptions struct { 29 apiClient apiv2client.APIV2Interface 30 changefeedID string 31 namespace string 32 } 33 34 // newRemoveChangefeedOptions creates new options for the `cli changefeed remove` command. 35 func newRemoveChangefeedOptions() *removeChangefeedOptions { 36 return &removeChangefeedOptions{} 37 } 38 39 // addFlags receives a *cobra.Command reference and binds 40 // flags related to template printing to it. 41 func (o *removeChangefeedOptions) addFlags(cmd *cobra.Command) { 42 cmd.PersistentFlags().StringVarP(&o.namespace, "namespace", "n", "default", "Replication task (changefeed) Namespace") 43 cmd.PersistentFlags().StringVarP(&o.changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID") 44 _ = cmd.MarkPersistentFlagRequired("changefeed-id") 45 } 46 47 // complete adapts from the command line args to the data and client required. 48 func (o *removeChangefeedOptions) complete(f factory.Factory) error { 49 client, err := f.APIV2Client() 50 if err != nil { 51 return err 52 } 53 o.apiClient = client 54 return nil 55 } 56 57 // run the `cli changefeed remove` command. 58 func (o *removeChangefeedOptions) run(cmd *cobra.Command) error { 59 ctx := context.GetDefaultContext() 60 61 changefeedDetail, err := o.apiClient.Changefeeds().Get(ctx, o.namespace, o.changefeedID) 62 if err != nil { 63 if strings.Contains(err.Error(), "ErrChangeFeedNotExists") { 64 cmd.Printf("Changefeed not found.\nID: %s\n", o.changefeedID) 65 return nil 66 } 67 68 cmd.Printf("Changefeed remove failed.\nID: %s\nError: %s\n", o.changefeedID, 69 err.Error()) 70 return err 71 } 72 checkpointTs := changefeedDetail.CheckpointTs 73 sinkURI := changefeedDetail.SinkURI 74 75 err = o.apiClient.Changefeeds().Delete(ctx, o.namespace, o.changefeedID) 76 if err != nil { 77 cmd.Printf("Changefeed remove failed.\nID: %s\nError: %s\n", o.changefeedID, 78 err.Error()) 79 return err 80 } 81 82 _, err = o.apiClient.Changefeeds().Get(ctx, o.namespace, o.changefeedID) 83 // Should never happen here. This checking is for defending. 84 // The reason is that changefeed query to owner is invoked in the subsequent owner 85 // Tick and in that Tick, the in-memory data structure and the metadata stored in 86 // etcd is already deleted. 87 if err == nil { 88 err = cerror.ErrChangeFeedDeletionUnfinished.GenWithStackByArgs(o.changefeedID) 89 } 90 91 if strings.Contains(err.Error(), "ErrChangeFeedNotExists") { 92 err = nil 93 } 94 95 if err == nil { 96 cmd.Printf("Changefeed remove successfully.\nID: %s\nCheckpointTs: %d\nSinkURI: %s\n", 97 o.changefeedID, checkpointTs, sinkURI) 98 } else { 99 cmd.Printf("Changefeed remove failed.\nID: %s\nError: %s\n", o.changefeedID, 100 err.Error()) 101 } 102 103 return err 104 } 105 106 // newCmdRemoveChangefeed creates the `cli changefeed remove` command. 107 func newCmdRemoveChangefeed(f factory.Factory) *cobra.Command { 108 o := newRemoveChangefeedOptions() 109 110 command := &cobra.Command{ 111 Use: "remove", 112 Short: "Remove a replication task (changefeed)", 113 Args: cobra.NoArgs, 114 Run: func(cmd *cobra.Command, args []string) { 115 util.CheckErr(o.complete(f)) 116 util.CheckErr(o.run(cmd)) 117 }, 118 } 119 120 o.addFlags(command) 121 122 return command 123 }