github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_changefeed_query.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 "context" 18 19 "github.com/pingcap/errors" 20 v2 "github.com/pingcap/tiflow/cdc/api/v2" 21 "github.com/pingcap/tiflow/cdc/model" 22 apiv2client "github.com/pingcap/tiflow/pkg/api/v2" 23 "github.com/pingcap/tiflow/pkg/cmd/factory" 24 "github.com/pingcap/tiflow/pkg/cmd/util" 25 cerror "github.com/pingcap/tiflow/pkg/errors" 26 "github.com/spf13/cobra" 27 ) 28 29 // cfMeta holds changefeed info and changefeed status. 30 type cfMeta struct { 31 UpstreamID uint64 `json:"upstream_id"` 32 Namespace string `json:"namespace"` 33 ID string `json:"id"` 34 SinkURI string `json:"sink_uri"` 35 Config *v2.ReplicaConfig `json:"config"` 36 CreateTime model.JSONTime `json:"create_time"` 37 StartTs uint64 `json:"start_ts"` 38 ResolvedTs uint64 `json:"resolved_ts"` 39 TargetTs uint64 `json:"target_ts"` 40 CheckpointTSO uint64 `json:"checkpoint_tso"` 41 CheckpointTime model.JSONTime `json:"checkpoint_time"` 42 Engine model.SortEngine `json:"sort_engine,omitempty"` 43 FeedState model.FeedState `json:"state"` 44 RunningError *v2.RunningError `json:"error,omitempty"` 45 ErrorHis []int64 `json:"error_history,omitempty"` 46 CreatorVersion string `json:"creator_version"` 47 TaskStatus []model.CaptureTaskStatus `json:"task_status,omitempty"` 48 } 49 50 // queryChangefeedOptions defines flags for the `cli changefeed query` command. 51 type queryChangefeedOptions struct { 52 apiClientV2 apiv2client.APIV2Interface 53 changefeedID string 54 simplified bool 55 namespace string 56 } 57 58 // newQueryChangefeedOptions creates new options for the `cli changefeed query` command. 59 func newQueryChangefeedOptions() *queryChangefeedOptions { 60 return &queryChangefeedOptions{} 61 } 62 63 // addFlags receives a *cobra.Command reference and binds 64 // flags related to template printing to it. 65 func (o *queryChangefeedOptions) addFlags(cmd *cobra.Command) { 66 cmd.PersistentFlags().StringVarP(&o.namespace, "namespace", "n", "default", "Replication task (changefeed) Namespace") 67 cmd.PersistentFlags().BoolVarP(&o.simplified, "simple", "s", false, "Output simplified replication status") 68 cmd.PersistentFlags().StringVarP(&o.changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID") 69 _ = cmd.MarkPersistentFlagRequired("changefeed-id") 70 } 71 72 // complete adapts from the command line args to the data and client required. 73 func (o *queryChangefeedOptions) complete(f factory.Factory) error { 74 clientV2, err := f.APIV2Client() 75 if err != nil { 76 return err 77 } 78 o.apiClientV2 = clientV2 79 return nil 80 } 81 82 // run the `cli changefeed query` command. 83 func (o *queryChangefeedOptions) run(cmd *cobra.Command) error { 84 ctx := context.Background() 85 if o.simplified { 86 infos, err := o.apiClientV2.Changefeeds().List(ctx, o.namespace, "all") 87 if err != nil { 88 return errors.Trace(err) 89 } 90 for _, info := range infos { 91 if info.ID == o.changefeedID { 92 return util.JSONPrint(cmd, info) 93 } 94 } 95 return cerror.ErrChangeFeedNotExists.GenWithStackByArgs(o.changefeedID) 96 } 97 98 detail, err := o.apiClientV2.Changefeeds().Get(ctx, o.namespace, o.changefeedID) 99 if err != nil && cerror.ErrChangeFeedNotExists.NotEqual(err) { 100 return err 101 } 102 meta := &cfMeta{ 103 UpstreamID: detail.UpstreamID, 104 Namespace: detail.Namespace, 105 ID: detail.ID, 106 SinkURI: detail.SinkURI, 107 Config: detail.Config, 108 CreateTime: model.JSONTime(detail.CreateTime), 109 StartTs: detail.StartTs, 110 ResolvedTs: detail.ResolvedTs, 111 TargetTs: detail.TargetTs, 112 CheckpointTSO: detail.CheckpointTs, 113 CheckpointTime: detail.CheckpointTime, 114 FeedState: detail.State, 115 RunningError: detail.Error, 116 CreatorVersion: detail.CreatorVersion, 117 TaskStatus: detail.TaskStatus, 118 } 119 return util.JSONPrint(cmd, meta) 120 } 121 122 // newCmdQueryChangefeed creates the `cli changefeed query` command. 123 func newCmdQueryChangefeed(f factory.Factory) *cobra.Command { 124 o := newQueryChangefeedOptions() 125 126 command := &cobra.Command{ 127 Use: "query", 128 Short: "Query information and status of a replication task (changefeed)", 129 Args: cobra.NoArgs, 130 Run: func(cmd *cobra.Command, args []string) { 131 util.CheckErr(o.complete(f)) 132 util.CheckErr(o.run(cmd)) 133 }, 134 } 135 136 o.addFlags(command) 137 138 return command 139 }