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

@@ -47,8 +47,9 @@ type Config struct {
DefaultCluster *Cluster
Index string // index: -i
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
Primary bool // index allocation: -p
From, To, MaxItems int // search: flags
Filter []string // search: -F
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.Addheaders("field", "value")
table.Addheaders("ccr remote property", "value")
table.entries = [][]string{
{"Remote Cluster", remote},

View File

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

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
}

View File

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