Skip to content

Commit

Permalink
Merge pull request #2 from antonio2368/findsupernodes
Browse files Browse the repository at this point in the history
Add command for finding super nodes
  • Loading branch information
antonio2368 authored May 25, 2023
2 parents fefb94d + 1cd07b5 commit 932b3e0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions core/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/go-zookeeper/zk"
Expand Down Expand Up @@ -245,6 +246,66 @@ func (c *Cmd) deleteall() (err error) {
return
}

func printsupernodes(c *Cmd, path string, threshold int32) (err error) {
path = cleanPath(path)

_, stat, err := c.Conn.Get(path)
if err != nil {
return
}

if stat.NumChildren >= threshold {
fmt.Printf("%+v\n%s\n", string(path), fmtStat(stat))
return
}

children, _, err := c.Conn.Children(path)
if err != nil {
return
}

for _, child := range children {
child_path := ""
if path == "/" {
child_path = fmt.Sprintf("/%s", child)
} else {
child_path = fmt.Sprintf("%s/%s", path, child)
}

_ = printsupernodes(c, child_path, threshold)
}

return
}

func (c *Cmd) findsupernodes() (err error) {
err = c.checkConn()
if err != nil {
return
}

p := "/"

options := c.Options
if len(options) != 1 {
return errors.New("Threshold missing as argument which is number of children that defines a super node")
}

num, err := strconv.Atoi(options[0])
if err != nil {
return err
}

threshold := int32(num)

err = printsupernodes(c, p, threshold)
if err != nil {
return
}

return
}

func (c *Cmd) deletestalebackups() (err error) {
err = c.checkConn()
if err != nil {
Expand Down Expand Up @@ -384,6 +445,8 @@ func (c *Cmd) run() (err error) {
return c.deleteall()
case "deletestalebackups":
return c.deletestalebackups()
case "findsupernodes":
return c.findsupernodes()
case "close":
return c.close()
case "connect":
Expand Down Expand Up @@ -420,6 +483,7 @@ set <path> [<data>]
delete <path>
deleteall <path>
deletestalebackups
findsupernodes <threshold>
connect <host:port>
addauth <scheme> <auth>
close
Expand Down
1 change: 1 addition & 0 deletions core/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var commands = []prompt.Suggest{
{Text: "delete", Description: "Delete a node"},
{Text: "deleteall", Description: "Delete a node recursively"},
{Text: "deletestalebackups", Description: "Delete ClickHouse nodes used for backups that are now inactive"},
{Text: "findsupernodes", Description: "Find nodes with number of children larger than some threshold"},
{Text: "close", Description: "Close connection"},
{Text: "connect", Description: "Connect servers"},
{Text: "addauth", Description: "Add auth info"},
Expand Down

0 comments on commit 932b3e0

Please sign in to comment.