add explain index allocation

This commit is contained in:
T. von Dein
2026-05-18 13:58:08 +02:00
parent db50072a7b
commit 40e0612bef
6 changed files with 83 additions and 5 deletions

View File

@@ -38,6 +38,7 @@ func Index(conf *cfg.Config) *cli.Command {
IndexCreate(conf), IndexCreate(conf),
IndexDelete(conf), IndexDelete(conf),
IndexClose(conf), IndexClose(conf),
IndexAllocation(conf),
}, },
} }
} }
@@ -75,6 +76,33 @@ func IndexList(conf *cfg.Config) *cli.Command {
} }
} }
func IndexAllocation(conf *cfg.Config) *cli.Command {
return &cli.Command{
Name: "allocation",
Aliases: []string{"a"},
Usage: "explain index allocation",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "shard",
Usage: "shard number to explain for",
Destination: &conf.Shards,
Aliases: []string{"s"},
},
&cli.BoolFlag{
Name: "primary",
Usage: "explain primary allocation (default true)",
Destination: &conf.Primary,
Aliases: []string{"p"},
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
return es.IndexAllocation(conf, cmd.Args().Get(0))
},
}
}
func IndexShow(conf *cfg.Config) *cli.Command { func IndexShow(conf *cfg.Config) *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "show", Name: "show",

View File

@@ -47,8 +47,9 @@ type Config struct {
DefaultCluster *Cluster DefaultCluster *Cluster
Index string // index: -i Index string // index: -i
Failed, Partials bool // index: flags Failed, Partials bool // index: flags
Shards, Replicas int // index create: -s -r Shards, Replicas int // index create+allocation: -s -r
Wait bool // index create: -w Wait bool // index create: -w
Primary bool // index allocation: -p
From, To, MaxItems int // search: flags From, To, MaxItems int // search: flags
Filter []string // search: -F Filter []string // search: -F
Exclude string // cluster compare: -e (regexp) Exclude string // cluster compare: -e (regexp)

View File

@@ -116,7 +116,7 @@ func CcrRemoteInfo(conf *cfg.Config, index string) error {
} }
table := NewTable(2, 5) table := NewTable(2, 5)
table.Addheaders("field", "value") table.Addheaders("ccr remote property", "value")
table.entries = [][]string{ table.entries = [][]string{
{"Remote Cluster", remote}, {"Remote Cluster", remote},

View File

@@ -176,7 +176,7 @@ func CcrFollowerShow(conf *cfg.Config, index string) error {
follower := res.Indices[0].Shards[0] follower := res.Indices[0].Shards[0]
table := NewTable(2, 9) table := NewTable(2, 9)
table.Addheaders("field", "value") table.Addheaders("ccr follower property", "value")
table.entries = [][]string{ table.entries = [][]string{
{"name", index}, {"name", index},

View File

@@ -111,7 +111,7 @@ func IndexShow(conf *cfg.Config, index string) error {
slog.Debug("ES result", "index", res) slog.Debug("ES result", "index", res)
table := NewTable(2, 5) table := NewTable(2, 5)
table.Addheaders("field", "value") table.Addheaders("index property", "value")
ts, err := strconv.ParseInt(res[index].Settings.Index.CreationDate.(string), 10, 64) ts, err := strconv.ParseInt(res[index].Settings.Index.CreationDate.(string), 10, 64)
if err != nil { if err != nil {
@@ -216,3 +216,52 @@ func IndexClose(conf *cfg.Config, index string) error {
return nil return nil
} }
func IndexAllocation(conf *cfg.Config, index string) error {
if index == "" {
return fmt.Errorf("no index specified")
}
alloc := conf.DefaultCluster.ES.Cluster.AllocationExplain().
Index(index).
Primary(conf.Primary).
Shard(conf.Shards)
res, err := alloc.Header("content-type", "application/json").
Header("accept", "application/json").
Do(context.Background())
if err != nil {
return fmt.Errorf("failed to get index allocation explain: %s", err)
}
slog.Debug("ES result", "index", res)
currentNode := res.CurrentNode
table := NewTable(2, 10)
table.Addheaders("index allocation setting", "value")
roles := make([]string, len(currentNode.Roles))
for idx, role := range currentNode.Roles {
roles[idx] = role.Name
}
table.entries = [][]string{
{"Index", index},
{"Current node", currentNode.Name},
{"Current k8s node", currentNode.Attributes["k8s_node_name"]},
{"Current node address", currentNode.TransportAddress},
{"Current node id", currentNode.Id},
{"Current node weight", fmt.Sprintf("%d", currentNode.WeightRanking)},
{"Current node roles", strings.Join(roles, ",")},
{"Can rebalance cluster", res.CanRebalanceCluster.Name},
{"Can rebalance to another node", res.CanRebalanceToOtherNode.Name},
{"Can remain on current node", res.CanRemainOnCurrentNode.Name},
}
if err := table.PrintMarkdown(); err != nil {
return err
}
return nil
}

View File

@@ -117,7 +117,7 @@ func SnapshotShow(conf *cfg.Config, snapshot string) error {
} }
table := NewTable(2, 17) table := NewTable(2, 17)
table.Addheaders("field", "value") table.Addheaders("snapshot property", "value")
snap := res.Snapshots[0] snap := res.Snapshots[0]