github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_processor_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/tiflow/cdc/model"
    20  	apiv2client "github.com/pingcap/tiflow/pkg/api/v2"
    21  	cmdcontext "github.com/pingcap/tiflow/pkg/cmd/context"
    22  	"github.com/pingcap/tiflow/pkg/cmd/factory"
    23  	"github.com/pingcap/tiflow/pkg/cmd/util"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  type processorMeta struct {
    28  	Status   *model.TaskStatus   `json:"status"`
    29  	Position *model.TaskPosition `json:"position"`
    30  }
    31  
    32  // queryProcessorOptions defines flags for the `cli processor query` command.
    33  type queryProcessorOptions struct {
    34  	apiClient apiv2client.APIV2Interface
    35  
    36  	changefeedID string
    37  	namespace    string
    38  	captureID    string
    39  }
    40  
    41  // newQueryProcessorOptions creates new options for the `cli changefeed query` command.
    42  func newQueryProcessorOptions() *queryProcessorOptions {
    43  	return &queryProcessorOptions{}
    44  }
    45  
    46  // complete adapts from the command line args to the data and client required.
    47  func (o *queryProcessorOptions) complete(f factory.Factory) error {
    48  	apiClient, err := f.APIV2Client()
    49  	if err != nil {
    50  		return err
    51  	}
    52  	o.apiClient = apiClient
    53  	return nil
    54  }
    55  
    56  // addFlags receives a *cobra.Command reference and binds
    57  // flags related to template printing to it.
    58  func (o *queryProcessorOptions) addFlags(cmd *cobra.Command) {
    59  	cmd.PersistentFlags().StringVarP(&o.namespace, "namespace", "n", "default", "Replication task (changefeed) Namespace")
    60  	cmd.PersistentFlags().StringVarP(&o.changefeedID, "changefeed-id", "c", "", "Replication task (changefeed) ID")
    61  	cmd.PersistentFlags().StringVarP(&o.captureID, "capture-id", "p", "", "capture ID")
    62  	_ = cmd.MarkPersistentFlagRequired("changefeed-id")
    63  	_ = cmd.MarkPersistentFlagRequired("capture-id")
    64  }
    65  
    66  // run cli cmd with api client
    67  func (o *queryProcessorOptions) runCliWithAPIClient(ctx context.Context, cmd *cobra.Command) error {
    68  	processor, err := o.apiClient.Processors().Get(ctx, o.namespace, o.changefeedID, o.captureID)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	tables := make(map[int64]*model.TableReplicaInfo)
    74  	for _, tableID := range processor.Tables {
    75  		tables[tableID] = &model.TableReplicaInfo{
    76  			// to be compatible with old version `cli processor query`,
    77  			// set this field to 0
    78  			StartTs: 0,
    79  		}
    80  	}
    81  
    82  	meta := &processorMeta{
    83  		Status: &model.TaskStatus{
    84  			Tables: tables,
    85  			// Operations, AdminJobType and ModRevision are vacant
    86  		},
    87  	}
    88  
    89  	return util.JSONPrint(cmd, meta)
    90  }
    91  
    92  // run runs the `cli processor query` command.
    93  func (o *queryProcessorOptions) run(cmd *cobra.Command) error {
    94  	ctx := cmdcontext.GetDefaultContext()
    95  	return o.runCliWithAPIClient(ctx, cmd)
    96  }
    97  
    98  // newCmdQueryProcessor creates the `cli processor query` command.
    99  func newCmdQueryProcessor(f factory.Factory) *cobra.Command {
   100  	o := newQueryProcessorOptions()
   101  
   102  	command := &cobra.Command{
   103  		Use:   "query",
   104  		Short: "Query information and status of a sub replication task (processor)",
   105  		Args:  cobra.NoArgs,
   106  		Run: func(cmd *cobra.Command, args []string) {
   107  			util.CheckErr(o.complete(f))
   108  			util.CheckErr(o.run(cmd))
   109  		},
   110  	}
   111  
   112  	o.addFlags(command)
   113  
   114  	return command
   115  }