Skip to content

Commit

Permalink
CLI 'show cluster' to sort rows by pod names with primary on top
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Aizman <alex.aizman@gmail.com>
  • Loading branch information
alex-aizman committed Oct 4, 2024
1 parent a59f921 commit e469684
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cmd/cli/teb/daeclu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/teb/init.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/teb/mountpath.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/teb/performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/teb/print.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
91 changes: 91 additions & 0 deletions cmd/cli/teb/sort.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 0 additions & 22 deletions cmd/cli/teb/stats.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/cli/teb/table.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit e469684

Please sign in to comment.