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

@@ -111,7 +111,7 @@ func IndexShow(conf *cfg.Config, index string) error {
slog.Debug("ES result", "index", res)
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)
if err != nil {
@@ -216,3 +216,52 @@ func IndexClose(conf *cfg.Config, index string) error {
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
}