Skip to content

Commit

Permalink
Add show alias command (#167)
Browse files Browse the repository at this point in the history
Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia authored Jul 13, 2023
1 parent 2702b90 commit 83f9854
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
53 changes: 53 additions & 0 deletions models/alias.go
Original file line number Diff line number Diff line change
@@ -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))
}
55 changes: 55 additions & 0 deletions states/etcd/common/alias.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
42 changes: 42 additions & 0 deletions states/etcd/show/alias.go
Original file line number Diff line number Diff line change
@@ -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())
}
2 changes: 1 addition & 1 deletion states/etcd/show/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit 83f9854

Please sign in to comment.