|
| 1 | +// Package cli provides easy-to-use commands to manage, monitor, and utilize AIS clusters. |
| 2 | +/* |
| 3 | + * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 4 | + */ |
| 5 | +package cli |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/NVIDIA/aistore/api" |
| 13 | + "github.com/NVIDIA/aistore/api/apc" |
| 14 | + "github.com/NVIDIA/aistore/cmd/cli/teb" |
| 15 | + "github.com/NVIDIA/aistore/cmn" |
| 16 | + "github.com/NVIDIA/aistore/cmn/cos" |
| 17 | + |
| 18 | + "github.com/urfave/cli" |
| 19 | +) |
| 20 | + |
| 21 | +type ( |
| 22 | + shardSummCtx struct { |
| 23 | + c *cli.Context |
| 24 | + units string |
| 25 | + bck cmn.Bck |
| 26 | + prefix string |
| 27 | + msg apc.ShardSummMsg |
| 28 | + args api.ShardSummArgs |
| 29 | + l int |
| 30 | + n int |
| 31 | + } |
| 32 | + shardSummRow struct { |
| 33 | + Name string |
| 34 | + NotIndexed uint64 |
| 35 | + apc.ShardSummResult |
| 36 | + } |
| 37 | +) |
| 38 | + |
| 39 | +func shardIndexSummaryHandler(c *cli.Context) error { |
| 40 | + if c.NArg() == 0 { |
| 41 | + return incorrectUsageMsg(c, "missing bucket name") |
| 42 | + } |
| 43 | + if c.NArg() > 2 { |
| 44 | + return incorrectUsageMsg(c, "", c.Args()[2:]) |
| 45 | + } |
| 46 | + |
| 47 | + bck, objName, err := parseBckObjURI(c, c.Args().Get(0), true /*optObjName*/) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + prefix, err := parseBckObjPrefix(c, objName) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + ctx, err := newShardSummCtxMsg(c, bck, prefix) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // Optional JOB_ID means polling an existing summary job. |
| 61 | + isNew := true |
| 62 | + if xid := c.Args().Get(1); xid != "" { |
| 63 | + if !cos.IsValidUUID(xid) { |
| 64 | + return incorrectUsageMsg(c, "invalid job ID %q", xid) |
| 65 | + } |
| 66 | + if !ctx.args.DontWait { |
| 67 | + return incorrectUsageMsg(c, "JOB_ID requires %s", flprn(dontWaitFlag)) |
| 68 | + } |
| 69 | + ctx.msg.UUID = xid |
| 70 | + isNew = false |
| 71 | + } |
| 72 | + |
| 73 | + // Start or poll the summary, depending on msg.UUID and --dont-wait. |
| 74 | + xid, res, err := api.GetBucketShardSummary(apiBP, ctx.bck, &ctx.msg, ctx.args) |
| 75 | + |
| 76 | + // New --dont-wait starts the job; if no snapshot exists yet, print the xaction ID. |
| 77 | + dontWait := flagIsSet(c, dontWaitFlag) |
| 78 | + if err == nil && dontWait && isNew && res.IsEmpty() { |
| 79 | + actionDone(c, shardSummStartedMsg(xid, bck.Cname(prefix), isNew)) |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + // For --dont-wait polling, 202 means no snapshot yet; 206 prints the latest partial snapshot. |
| 84 | + var status int |
| 85 | + if err != nil { |
| 86 | + if herr := cmn.AsErrHTTP(err); herr != nil { |
| 87 | + status = herr.Status |
| 88 | + } |
| 89 | + if dontWait && status == http.StatusAccepted { |
| 90 | + actionDone(c, shardSummStartedMsg(xid, bck.Cname(prefix), isNew)) |
| 91 | + return nil |
| 92 | + } |
| 93 | + if dontWait && status == http.StatusPartialContent { |
| 94 | + msg := fmt.Sprintf("%s[%s] is still running - showing partial results:", apc.ActSummaryShard, ctx.msg.UUID) |
| 95 | + actionNote(c, msg) |
| 96 | + err = nil |
| 97 | + } |
| 98 | + } |
| 99 | + if err != nil { |
| 100 | + return V(err) |
| 101 | + } |
| 102 | + // Print the summary table. |
| 103 | + return ctx.print(res) |
| 104 | +} |
| 105 | + |
| 106 | +func shardSummStartedMsg(xid, bucket string, isNew bool) string { |
| 107 | + verb := "has started" |
| 108 | + if !isNew { |
| 109 | + verb = "is running" |
| 110 | + } |
| 111 | + return fmt.Sprintf("Job %s[%s] %s. To monitor, run 'ais bucket shard-index summary %s %s %s'", |
| 112 | + apc.ActSummaryShard, xid, verb, bucket, xid, flprn(dontWaitFlag)) |
| 113 | +} |
| 114 | + |
| 115 | +func newShardSummCtxMsg(c *cli.Context, bck cmn.Bck, prefix string) (*shardSummCtx, error) { |
| 116 | + units, err := parseUnitsFlag(c, unitsFlag) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + ctx := &shardSummCtx{ |
| 121 | + c: c, |
| 122 | + units: units, |
| 123 | + bck: bck, |
| 124 | + prefix: prefix, |
| 125 | + } |
| 126 | + ctx.msg.Prefix = prefix |
| 127 | + if ctx.args.DontWait = flagIsSet(c, dontWaitFlag); ctx.args.DontWait { |
| 128 | + return ctx, nil |
| 129 | + } |
| 130 | + |
| 131 | + ctx.args.CallAfter = _refreshRate(c) |
| 132 | + ctx.args.Callback = ctx.progress |
| 133 | + return ctx, nil |
| 134 | +} |
| 135 | + |
| 136 | +func (ctx *shardSummCtx) progress(res *apc.ShardSummResult, done bool) { |
| 137 | + if done { |
| 138 | + if ctx.n > 0 { |
| 139 | + fmt.Fprintln(ctx.c.App.Writer) |
| 140 | + } |
| 141 | + return |
| 142 | + } |
| 143 | + if res == nil || res.IsEmpty() { |
| 144 | + return |
| 145 | + } |
| 146 | + ctx.n++ |
| 147 | + |
| 148 | + s := fmt.Sprintf("%s: %s/%s indexed (%s indexed, %s total)", |
| 149 | + ctx.bck.Cname(ctx.prefix), |
| 150 | + cos.FormatBigI64(int64(res.Shards)), |
| 151 | + cos.FormatBigI64(int64(res.TarObjs)), |
| 152 | + teb.FmtSize(int64(res.ShardSize), ctx.units, 2), |
| 153 | + teb.FmtSize(int64(res.TarSize), ctx.units, 2)) |
| 154 | + if ctx.l < len(s) { |
| 155 | + ctx.l = len(s) + 4 |
| 156 | + } |
| 157 | + s += strings.Repeat(" ", ctx.l-len(s)) |
| 158 | + fmt.Fprintf(ctx.c.App.Writer, "\r%s", s) |
| 159 | +} |
| 160 | + |
| 161 | +func (ctx *shardSummCtx) print(res *apc.ShardSummResult) error { |
| 162 | + row := shardSummRow{ |
| 163 | + Name: ctx.bck.Cname(ctx.prefix), |
| 164 | + NotIndexed: shardSummNotIndexed(res), |
| 165 | + ShardSummResult: *res, |
| 166 | + } |
| 167 | + opts := teb.Opts{AltMap: teb.FuncMapUnits(ctx.units, false /*incl. calendar date*/)} |
| 168 | + if flagIsSet(ctx.c, noHeaderFlag) { |
| 169 | + return teb.Print([]shardSummRow{row}, teb.ShardSummariesBody, opts) |
| 170 | + } |
| 171 | + return teb.Print([]shardSummRow{row}, teb.ShardSummariesTmpl, opts) |
| 172 | +} |
| 173 | + |
| 174 | +func shardSummNotIndexed(res *apc.ShardSummResult) uint64 { |
| 175 | + if res.Shards > res.TarObjs { |
| 176 | + return 0 |
| 177 | + } |
| 178 | + return res.TarObjs - res.Shards |
| 179 | +} |
0 commit comments