|
| 1 | +// Package xs is a collection of eXtended actions (xactions), including multi-object |
| 2 | +// operations, list-objects, (cluster) rebalance and (target) resilver, ETL, and more. |
| 3 | +/* |
| 4 | + * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 5 | + */ |
| 6 | +package xs |
| 7 | + |
| 8 | +import ( |
| 9 | + "errors" |
| 10 | + "strconv" |
| 11 | + "sync" |
| 12 | + |
| 13 | + "github.com/NVIDIA/aistore/api/apc" |
| 14 | + "github.com/NVIDIA/aistore/cmn" |
| 15 | + "github.com/NVIDIA/aistore/cmn/archive" |
| 16 | + "github.com/NVIDIA/aistore/cmn/atomic" |
| 17 | + "github.com/NVIDIA/aistore/cmn/cos" |
| 18 | + "github.com/NVIDIA/aistore/core" |
| 19 | + "github.com/NVIDIA/aistore/core/meta" |
| 20 | + "github.com/NVIDIA/aistore/xact" |
| 21 | + "github.com/NVIDIA/aistore/xact/xreg" |
| 22 | +) |
| 23 | + |
| 24 | +type ( |
| 25 | + shardSummFactory struct { |
| 26 | + xctn *XactShardSumm |
| 27 | + msg *apc.ShardSummMsg |
| 28 | + xreg.RenewBase |
| 29 | + } |
| 30 | + XactShardSumm struct { |
| 31 | + p *shardSummFactory |
| 32 | + // live counters; snapshot into apc.ShardSummResult in Result() |
| 33 | + nTarObjs atomic.Uint64 |
| 34 | + nTarSize atomic.Uint64 |
| 35 | + nShards atomic.Uint64 |
| 36 | + nShardSize atomic.Uint64 |
| 37 | + nArchivedObjs atomic.Uint64 |
| 38 | + nStale atomic.Uint64 |
| 39 | + nInvalid atomic.Uint64 |
| 40 | + // TODO: include orphaned index count by sampling sysbucket indexes. |
| 41 | + xact.BckJogRunner |
| 42 | + } |
| 43 | +) |
| 44 | + |
| 45 | +var ( |
| 46 | + _ core.Xact = (*XactShardSumm)(nil) |
| 47 | + _ xreg.Renewable = (*shardSummFactory)(nil) |
| 48 | +) |
| 49 | + |
| 50 | +func (*shardSummFactory) New(args xreg.Args, bck *meta.Bck) xreg.Renewable { |
| 51 | + return &shardSummFactory{ |
| 52 | + RenewBase: xreg.RenewBase{Args: args, Bck: bck}, |
| 53 | + msg: args.Custom.(*apc.ShardSummMsg), |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func (p *shardSummFactory) Start() error { |
| 58 | + r := &XactShardSumm{p: p} |
| 59 | + err := r.BckJogRunner.Init(p.UUID(), p.Kind(), p.Bck, xact.BckJogRunnerOpts{ |
| 60 | + CbObj: r.visit, |
| 61 | + Prefix: p.msg.Prefix, |
| 62 | + NumWorkers: xact.NwpNone, |
| 63 | + }, cmn.GCO.Get()) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + p.xctn = r |
| 68 | + xact.GoRunW(r) |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func (*shardSummFactory) Kind() string { return apc.ActSummaryShard } |
| 73 | +func (p *shardSummFactory) Get() core.Xact { return p.xctn } |
| 74 | + |
| 75 | +func (*shardSummFactory) WhenPrevIsRunning(xreg.Renewable) (xreg.WPR, error) { |
| 76 | + return xreg.WprKeepAndStartNew, nil |
| 77 | +} |
| 78 | + |
| 79 | +func (r *XactShardSumm) Run(wg *sync.WaitGroup) { |
| 80 | + wg.Done() |
| 81 | + r.BckJogRunner.Run() |
| 82 | + if err := r.BckJogRunner.Wait(); err != nil { |
| 83 | + r.AddErr(err) |
| 84 | + } |
| 85 | + r.Finish() |
| 86 | +} |
| 87 | + |
| 88 | +func (r *XactShardSumm) visit(lom *core.LOM, _ []byte) error { |
| 89 | + mime, err := archive.Mime("", lom.ObjName) |
| 90 | + if err != nil || mime != archive.ExtTar { |
| 91 | + return nil |
| 92 | + } |
| 93 | + |
| 94 | + lom.Lock(false) |
| 95 | + defer lom.Unlock(false) |
| 96 | + if err := lom.Load(false /*cache it*/, true /*locked*/); err != nil { |
| 97 | + if cos.IsNotExist(err) { |
| 98 | + return nil |
| 99 | + } |
| 100 | + return err |
| 101 | + } |
| 102 | + if lom.IsCopy() { |
| 103 | + return nil |
| 104 | + } |
| 105 | + |
| 106 | + size := lom.Lsize() |
| 107 | + r.nTarObjs.Inc() |
| 108 | + r.nTarSize.Add(uint64(size)) |
| 109 | + r.ObjsAdd(1, size) |
| 110 | + if !lom.HasShardIdx() { |
| 111 | + return nil |
| 112 | + } |
| 113 | + |
| 114 | + idx, err := core.LoadShardIndex(lom) |
| 115 | + if err != nil { |
| 116 | + if errors.Is(err, archive.ErrShardIdxStale) { |
| 117 | + r.nStale.Inc() |
| 118 | + } else { |
| 119 | + r.nInvalid.Inc() |
| 120 | + } |
| 121 | + return nil |
| 122 | + } |
| 123 | + if idx == nil { |
| 124 | + return nil |
| 125 | + } |
| 126 | + r.nShards.Inc() |
| 127 | + r.nShardSize.Add(uint64(size)) |
| 128 | + r.nArchivedObjs.Add(uint64(len(idx.Entries))) |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func (r *XactShardSumm) Result() (*apc.ShardSummResult, error) { |
| 133 | + return &apc.ShardSummResult{ |
| 134 | + TarObjs: r.nTarObjs.Load(), |
| 135 | + TarSize: r.nTarSize.Load(), |
| 136 | + Shards: r.nShards.Load(), |
| 137 | + ShardSize: r.nShardSize.Load(), |
| 138 | + ArchivedObjs: r.nArchivedObjs.Load(), |
| 139 | + StaleIndexes: r.nStale.Load(), |
| 140 | + InvalidIndexes: r.nInvalid.Load(), |
| 141 | + }, r.Err() |
| 142 | +} |
| 143 | + |
| 144 | +func (r *XactShardSumm) Snap() *core.Snap { return r.Base.NewSnap(r) } |
| 145 | + |
| 146 | +func (r *XactShardSumm) CtlMsg() string { |
| 147 | + if r.p == nil || r.p.msg == nil { |
| 148 | + return "" |
| 149 | + } |
| 150 | + var sb cos.SB |
| 151 | + sb.Init(160) |
| 152 | + if r.p.msg.Prefix != "" { |
| 153 | + idxAppend(&sb, "prefix", r.p.msg.Prefix) |
| 154 | + } |
| 155 | + tarObjs := r.nTarObjs.Load() |
| 156 | + shards := r.nShards.Load() |
| 157 | + idxAppend(&sb, "tar-objs", strconv.FormatUint(tarObjs, 10)) |
| 158 | + idxAppend(&sb, "tar-size", strconv.FormatUint(r.nTarSize.Load(), 10)) |
| 159 | + idxAppend(&sb, "shards", strconv.FormatUint(shards, 10)) |
| 160 | + idxAppend(&sb, "shard-size", strconv.FormatUint(r.nShardSize.Load(), 10)) |
| 161 | + idxAppend(&sb, "archived-objs", strconv.FormatUint(r.nArchivedObjs.Load(), 10)) |
| 162 | + if n := r.nStale.Load(); n > 0 { |
| 163 | + idxAppend(&sb, "stale", strconv.FormatUint(n, 10)) |
| 164 | + } |
| 165 | + if n := r.nInvalid.Load(); n > 0 { |
| 166 | + idxAppend(&sb, "invalid-index", strconv.FormatUint(n, 10)) |
| 167 | + } |
| 168 | + if n := r.ErrCnt(); n > 0 { |
| 169 | + idxAppend(&sb, "errs", strconv.Itoa(n)) |
| 170 | + } |
| 171 | + return sb.String() |
| 172 | +} |
0 commit comments