Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: Add remove session command #291

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion models/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ type Session struct {
Address string `json:"Address,omitempty"`
Exclusive bool `json:"Exclusive,omitempty"`
Version string `json:"Version,omitempty"`

key string
}

func (s *Session) SetKey(key string) {
s.key = key
}

func (s *Session) GetKey() string {
return s.key
}

func (s Session) String() string {
return fmt.Sprintf("Session:%s, ServerID: %d, Version: %s, Address: %s", s.ServerName, s.ServerID, s.Version, s.Address)
}

func (s Session) IP() string {
func (s *Session) IP() string {
addr, err := net.ResolveTCPAddr("tcp", s.Address)
if err != nil {
return ""
Expand Down
1 change: 1 addition & 0 deletions states/etcd/common/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func ListSessionsByPrefix(cli clientv3.KV, prefix string) ([]*models.Session, er
if err != nil {
continue
}
session.SetKey(string(kv.Key))

sessions = append(sessions, session)
}
Expand Down
55 changes: 55 additions & 0 deletions states/etcd/remove/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package remove

import (
"context"
"fmt"
"strings"

"github.com/samber/lo"

"github.com/milvus-io/birdwatcher/framework"
"github.com/milvus-io/birdwatcher/models"
"github.com/milvus-io/birdwatcher/states/etcd/common"
)

type RemoveSessionParam struct {
framework.ParamBase `use:"remove session" desc:"remove session with specified component type & node id"`
Component string `name:"component" default:"" desc:"component type to remove"`
ID int64 `name:"sessionID" default:"0" desc:"session id to remove"`
Run bool `name:"run" default:"false" desc:"actual remove session, default in dry-run mode"`
}

func (c *ComponentRemove) RemoveSessionCommand(ctx context.Context, p *RemoveSessionParam) error {
sessions, err := common.ListSessions(c.client, c.basePath)
if err != nil {
return err
}

sessions = lo.Filter(sessions, func(s *models.Session, _ int) bool {
return strings.EqualFold(s.ServerName, p.Component) && s.ServerID == p.ID
})

if len(sessions) == 0 {
fmt.Printf("Session component type=%s session id=%d not found", p.Component, p.ID)
}

fmt.Printf("%d session item found:\n", len(sessions))
for _, session := range sessions {
fmt.Println(session.String())
fmt.Println(session.GetKey())
}

if p.Run {
fmt.Println("Start to remove session")
for _, session := range sessions {
_, err := c.client.Delete(ctx, session.GetKey())
if err != nil {
return err
}
}
} else {
fmt.Println("[Dry-mode] skip removing")
}

return nil
}
Loading