From 9986cddf9b209a4cc44f60498085d63226dd4c89 Mon Sep 17 00:00:00 2001 From: congqixia Date: Mon, 24 Jul 2023 15:18:56 +0800 Subject: [PATCH] Add 'not supported' warning for legacy show segment-loaded command (#173) Signed-off-by: Congqi Xia --- states/etcd/commands.go | 2 - states/etcd/show/alias.go | 4 -- states/etcd/show/segment_loaded.go | 74 +++++++++++++++--------------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/states/etcd/commands.go b/states/etcd/commands.go index 67c50a3..c807ae9 100644 --- a/states/etcd/commands.go +++ b/states/etcd/commands.go @@ -23,8 +23,6 @@ func ShowCommand(cli clientv3.KV, basePath string) *cobra.Command { showCmd.AddCommand( // v2.1 legacy commands - // show segment-loaded - show.SegmentLoadedCommand(cli, basePath), // show querycoord-tasks show.QueryCoordTasks(cli, basePath), diff --git a/states/etcd/show/alias.go b/states/etcd/show/alias.go index b774d5f..0d760ce 100644 --- a/states/etcd/show/alias.go +++ b/states/etcd/show/alias.go @@ -52,10 +52,6 @@ func (rs *Aliases) PrintAs(format framework.Format) string { return "" } -func (rs *Aliases) Entities() any { - return rs.Data -} - func (rs *Aliases) PrintAlias(sb *strings.Builder, a *models.Alias) { fmt.Printf("Collection ID: %d\tAlias Name: %s\tState: %s\n", a.CollectionID, a.Name, a.State.String()) } diff --git a/states/etcd/show/segment_loaded.go b/states/etcd/show/segment_loaded.go index 3824f74..7e8f661 100644 --- a/states/etcd/show/segment_loaded.go +++ b/states/etcd/show/segment_loaded.go @@ -1,51 +1,53 @@ package show import ( + "context" "fmt" + "strings" + "github.com/cockroachdb/errors" + "github.com/milvus-io/birdwatcher/framework" + "github.com/milvus-io/birdwatcher/models" "github.com/milvus-io/birdwatcher/proto/v2.0/querypb" "github.com/milvus-io/birdwatcher/states/etcd/common" - "github.com/spf13/cobra" - clientv3 "go.etcd.io/etcd/client/v3" + etcdversion "github.com/milvus-io/birdwatcher/states/etcd/version" ) -// SegmentLoadedCommand returns show segment-loaded command. -func SegmentLoadedCommand(cli clientv3.KV, basePath string) *cobra.Command { - cmd := &cobra.Command{ - Use: "segment-loaded", - Short: "display segment information from querycoord", - Aliases: []string{"segments-loaded"}, - RunE: func(cmd *cobra.Command, args []string) error { +type SegmentLoadedParam struct { + framework.ParamBase `use:"show segment-loaded" desc:"display segment information from querycoordv1 meta" alias:"segments-loaded"` + CollectionID int64 `name:"collection" default:"0" desc:"collection id to filter with"` + SegmentID int64 `name:"segment" default:"0" desc:"segment id to filter with"` +} - collID, err := cmd.Flags().GetInt64("collection") - if err != nil { - return err - } - segmentID, err := cmd.Flags().GetInt64("segment") - if err != nil { - return err - } +func (c *ComponentShow) SegmentLoadedCommand(ctx context.Context, p *SegmentLoadedParam) (*LoadedSegments, error) { + if etcdversion.GetVersion() != models.LTEVersion2_1 { + return nil, errors.New("list segment-loaded from meta only support before 2.1.x, try use `show segment-loaded-grc` instead") + } + segments, err := common.ListLoadedSegments(c.client, c.basePath, func(info *querypb.SegmentInfo) bool { + return (p.CollectionID == 0 || info.CollectionID == p.CollectionID) && + (p.SegmentID == 0 || info.SegmentID == p.SegmentID) + }) + if err != nil { + return nil, errors.Wrap(err, "failed to list loaded segments") + } - segments, err := common.ListLoadedSegments(cli, basePath, func(info *querypb.SegmentInfo) bool { - return (collID == 0 || info.CollectionID == collID) && - (segmentID == 0 || info.SegmentID == segmentID) - }) - if err != nil { - fmt.Println("failed to list segments", err.Error()) - return nil - } + return framework.NewListResult[LoadedSegments](segments), nil +} - for _, info := range segments { - fmt.Printf("Segment ID: %d LegacyNodeID: %d NodeIds: %v,DmlChannel: %s\n", info.SegmentID, info.NodeID, info.NodeIds, info.DmChannel) - fmt.Printf("%#v\n", info.GetIndexInfos()) - } +type LoadedSegments struct { + framework.ListResultSet[querypb.SegmentInfo] +} - return nil - }, +func (rs *LoadedSegments) PrintAs(format framework.Format) string { + switch format { + case framework.FormatDefault, framework.FormatPlain: + builder := &strings.Builder{} + for _, info := range rs.Data { + fmt.Fprintf(builder, "Segment ID: %d LegacyNodeID: %d NodeIds: %v,DmlChannel: %s\n", info.SegmentID, info.NodeID, info.NodeIds, info.DmChannel) + fmt.Fprintf(builder, "%#v\n", info.GetIndexInfos()) + } + return builder.String() + default: } - cmd.Flags().Int64("collection", 0, "collection id to filter with") - cmd.Flags().String("format", "line", "segment display format") - cmd.Flags().Bool("detail", false, "flags indicating whether pring detail binlog info") - cmd.Flags().Int64("segment", 0, "segment id to filter with") - return cmd + return "" }