diff --git a/models/alias.go b/models/alias.go new file mode 100644 index 0000000..b0e0c80 --- /dev/null +++ b/models/alias.go @@ -0,0 +1,53 @@ +package models + +import ( + etcdpbv2 "github.com/milvus-io/birdwatcher/proto/v2.2/etcdpb" +) + +type Alias struct { + Name string + CollectionID int64 + State AliasState + CreateTS uint64 + DBID int64 + + key string +} + +func NewAlias(info *etcdpbv2.AliasInfo, key string) *Alias { + a := &Alias{ + Name: info.GetAliasName(), + CollectionID: info.GetCollectionId(), + State: AliasState(info.GetState()), + CreateTS: info.GetCreatedTime(), + DBID: info.GetDbId(), + } + return a +} + +type AliasState int32 + +const ( + AliasStateAliasCreated AliasState = 0 + AliasStateAliasCreating AliasState = 1 + AliasStateAliasDropping AliasState = 2 + AliasStateAliasDropped AliasState = 3 +) + +var AliasStateName = map[int32]string{ + 0: "AliasCreated", + 1: "AliasCreating", + 2: "AliasDropping", + 3: "AliasDropped", +} + +var AliasStateValue = map[string]int32{ + "AliasCreated": 0, + "AliasCreating": 1, + "AliasDropping": 2, + "AliasDropped": 3, +} + +func (x AliasState) String() string { + return EnumName(AliasStateName, int32(x)) +} diff --git a/states/etcd/common/alias.go b/states/etcd/common/alias.go new file mode 100644 index 0000000..a36845f --- /dev/null +++ b/states/etcd/common/alias.go @@ -0,0 +1,55 @@ +package common + +import ( + "context" + "errors" + "path" + + "github.com/milvus-io/birdwatcher/models" + etcdpbv2 "github.com/milvus-io/birdwatcher/proto/v2.2/etcdpb" + "github.com/samber/lo" + clientv3 "go.etcd.io/etcd/client/v3" +) + +const ( + // AliasPrefixBefore210 is the legacy meta prefix for milvus before 2.2.0 + AliasPrefixBefore210 = `root-coord/collection-alias` + + // AliasPrefixWithoutDB is the meta prefix for alias before database. + AliasPrefixWithoutDB = `root-coord/aliases` + + // AliasPrefixDB iis the meta prefix for alias with database feature + AliasPrefixDB = `root-coord/database/alias` +) + +func ListAliasVersion(ctx context.Context, cli clientv3.KV, basePath string, version string, filters ...func(*models.Alias) bool) ([]*models.Alias, error) { + prefixes := []string{ + path.Join(basePath, AliasPrefixWithoutDB), + path.Join(basePath, AliasPrefixDB), + } + + switch version { + case models.GTEVersion2_2: + var result []*models.Alias + for _, prefix := range prefixes { + infos, keys, err := ListProtoObjects[etcdpbv2.AliasInfo](ctx, cli, prefix) + if err != nil { + return nil, err + } + + result = append(result, lo.FilterMap(infos, func(info etcdpbv2.AliasInfo, idx int) (*models.Alias, bool) { + value := models.NewAlias(&info, keys[idx]) + for _, filter := range filters { + if !filter(value) { + return nil, false + } + } + return value, true + })...) + } + + return result, nil + default: + return nil, errors.New("not supported version") + } +} diff --git a/states/etcd/show/alias.go b/states/etcd/show/alias.go new file mode 100644 index 0000000..68c12db --- /dev/null +++ b/states/etcd/show/alias.go @@ -0,0 +1,42 @@ +package show + +import ( + "context" + "fmt" + + "github.com/milvus-io/birdwatcher/framework" + "github.com/milvus-io/birdwatcher/models" + "github.com/milvus-io/birdwatcher/states/etcd/common" + etcdversion "github.com/milvus-io/birdwatcher/states/etcd/version" + "github.com/samber/lo" +) + +type AliasParam struct { + framework.ParamBase `use:"show alias" desc:"list alias meta info" alias:"aliases"` + DBID int64 `name:"dbid" default:"-1" desc:"database id to filter with"` +} + +// AliasCommand implements `show alias` command. +func (c *ComponentShow) AliasCommand(ctx context.Context, p *AliasParam) error { + aliases, err := common.ListAliasVersion(ctx, c.client, c.basePath, etcdversion.GetVersion(), func(a *models.Alias) bool { + return p.DBID == -1 || p.DBID == a.DBID + }) + + if err != nil { + return err + } + + for dbid, aliases := range lo.GroupBy(aliases, func(a *models.Alias) int64 { return a.DBID }) { + fmt.Println("==========================") + fmt.Println("Database ID: ", dbid) + for _, alias := range aliases { + c.PrintAlias(alias) + } + } + + return nil +} + +func (c *ComponentShow) PrintAlias(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/partition.go b/states/etcd/show/partition.go index 3b8c212..4e1e5a4 100644 --- a/states/etcd/show/partition.go +++ b/states/etcd/show/partition.go @@ -9,7 +9,7 @@ import ( ) type PartitionParam struct { - framework.ParamBase `use:"partition" desc:"list partitions of provided collection"` + framework.ParamBase `use:"show partition" desc:"list partitions of provided collection"` CollectionID int64 `name:"collection" default:"0" desc:"collection id to list"` }