From e469684085998f3f235456a1e10cbb3094ed6b7b Mon Sep 17 00:00:00 2001 From: Alex Aizman Date: Thu, 3 Oct 2024 20:12:09 -0400 Subject: [PATCH] CLI 'show cluster' to sort rows by pod names with primary on top Signed-off-by: Alex Aizman --- cmd/cli/teb/daeclu.go | 4 +- cmd/cli/teb/init.go | 2 +- cmd/cli/teb/mountpath.go | 4 +- cmd/cli/teb/performance.go | 2 +- cmd/cli/teb/print.go | 2 +- cmd/cli/teb/sort.go | 91 ++++++++++++++++++++++++++++++++++++++ cmd/cli/teb/stats.go | 22 --------- cmd/cli/teb/table.go | 2 +- 8 files changed, 99 insertions(+), 30 deletions(-) create mode 100644 cmd/cli/teb/sort.go delete mode 100644 cmd/cli/teb/stats.go diff --git a/cmd/cli/teb/daeclu.go b/cmd/cli/teb/daeclu.go index 19f3fd463e..57d792365a 100644 --- a/cmd/cli/teb/daeclu.go +++ b/cmd/cli/teb/daeclu.go @@ -88,7 +88,7 @@ func newTableProxies(ps StstMap, smap *meta.Smap, units string) *Table { table = newTable(cols...) ) - ids := ps.sortedSIDs() + ids := ps.sortPODs(smap, true) for _, sid := range ids { ds := ps[sid] @@ -194,7 +194,7 @@ func newTableTargets(ts StstMap, smap *meta.Smap, units string) *Table { } table = newTable(cols...) ) - ids := ts.sortedSIDs() + ids := ts.sortPODs(smap, false) for _, sid := range ids { ds := ts[sid] diff --git a/cmd/cli/teb/init.go b/cmd/cli/teb/init.go index cb96093737..0f5581dc5a 100644 --- a/cmd/cli/teb/init.go +++ b/cmd/cli/teb/init.go @@ -1,6 +1,6 @@ // Package teb contains templates and (templated) tables to format CLI output. /* - * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. */ package teb diff --git a/cmd/cli/teb/mountpath.go b/cmd/cli/teb/mountpath.go index 878b2ab042..8f424805ed 100644 --- a/cmd/cli/teb/mountpath.go +++ b/cmd/cli/teb/mountpath.go @@ -1,6 +1,6 @@ // Package teb contains templates and (templated) tables to format CLI output. /* - * Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2023-2024, NVIDIA CORPORATION. All rights reserved. */ package teb @@ -63,7 +63,7 @@ func NewMpathCapTab(st StstMap, c *PerfTabCtx, showMpaths bool) *Table { } table := newTable(cols...) - tids := st.sortedSIDs() + tids := st.sortSIDs() for _, tid := range tids { ds := st[tid] if c.Sid != "" && c.Sid != tid { diff --git a/cmd/cli/teb/performance.go b/cmd/cli/teb/performance.go index 55a6420913..773dfa2f20 100644 --- a/cmd/cli/teb/performance.go +++ b/cmd/cli/teb/performance.go @@ -114,7 +114,7 @@ func NewPerformanceTab(st StstMap, c *PerfTabCtx) (*Table, int /*numNZ non-zero } // 9. sort targets by IDs - tids := st.sortedSIDs() + tids := st.sortSIDs() // 10. construct an empty table table := newTable(printedColumns...) diff --git a/cmd/cli/teb/print.go b/cmd/cli/teb/print.go index c357d00972..7e1f7077ff 100644 --- a/cmd/cli/teb/print.go +++ b/cmd/cli/teb/print.go @@ -1,6 +1,6 @@ // Package teb contains templates and (templated) tables to format CLI output. /* - * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. */ package teb diff --git a/cmd/cli/teb/sort.go b/cmd/cli/teb/sort.go new file mode 100644 index 0000000000..e6162f60cf --- /dev/null +++ b/cmd/cli/teb/sort.go @@ -0,0 +1,91 @@ +// Package teb contains templates and (templated) tables to format CLI output. +/* + * Copyright (c) 2023-2024, NVIDIA CORPORATION. All rights reserved. + */ +package teb + +import ( + "sort" + + "github.com/NVIDIA/aistore/cmn/debug" + "github.com/NVIDIA/aistore/core/meta" + "github.com/NVIDIA/aistore/stats" +) + +type StstMap map[string]*stats.NodeStatus // by node ID (SID) + +func (psts StstMap) sortSIDs() (ids []string) { + ids = make([]string, 0, len(psts)) + for sid := range psts { + ids = append(ids, sid) + } + sort.Strings(ids) + return ids +} + +// NOTE: using stats.NodeStatus reserved field +func (psts StstMap) sortPODs(smap *meta.Smap, proxies bool) (ids []string) { + var ( + pods = make([]*stats.NodeStatus, 0, len(psts)) + pid = smap.Primary.ID() + hasp bool + ) + if len(psts) == 0 { + return + } + ids = make([]string, 0, len(psts)) + for sid, ds := range psts { + debug.Assert(sid == ds.Node.Snode.ID(), sid, " vs ", ds.Node.Snode.ID()) // (unlikely) + ids = append(ids, sid) + if l := len(ds.K8sPodName); l > 3 && ds.K8sPodName[l-1] >= '0' && ds.K8sPodName[l-1] <= '9' { + pods = append(pods, ds) + } + if proxies && pid == sid { + hasp = true + } + } + if len(ids) == 1 { + return ids + } + if len(ids) != len(pods) { + sort.Strings(ids) + return ids + } + + for _, ds := range pods { + var ( + s = ds.K8sPodName + ten = 1 + n int + ) + for i := len(s) - 1; i >= 0; i-- { + if s[i] < '0' || s[i] > '9' { + break + } + n += ten * int(s[i]-'0') + ten *= 10 + } + ds.Reserved4 = int64(n) + } + less := func(i, j int) bool { return pods[i].Reserved4 < pods[j].Reserved4 } + sort.Slice(pods, less) + + // ids in the pod-names order + for i, ds := range pods { + ids[i] = ds.Node.Snode.ID() + } + + // finally: primary on top + if proxies && hasp && ids[0] != pid { + for i := range ids { + if ids[i] != pid { + continue + } + copy(ids[1:], ids[0:i]) + ids[0] = pid + break + } + } + + return ids +} diff --git a/cmd/cli/teb/stats.go b/cmd/cli/teb/stats.go deleted file mode 100644 index dd3b945456..0000000000 --- a/cmd/cli/teb/stats.go +++ /dev/null @@ -1,22 +0,0 @@ -// Package teb contains templates and (templated) tables to format CLI output. -/* - * Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. - */ -package teb - -import ( - "sort" - - "github.com/NVIDIA/aistore/stats" -) - -type StstMap map[string]*stats.NodeStatus // by node ID (SID) - -func (ds StstMap) sortedSIDs() (ids []string) { - ids = make([]string, 0, len(ds)) - for sid := range ds { - ids = append(ids, sid) - } - sort.Strings(ids) - return -} diff --git a/cmd/cli/teb/table.go b/cmd/cli/teb/table.go index 49e6cee92a..a6919c0827 100644 --- a/cmd/cli/teb/table.go +++ b/cmd/cli/teb/table.go @@ -1,6 +1,6 @@ // Package teb contains templates and (templated) tables to format CLI output. /* - * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. */ package teb