-
Notifications
You must be signed in to change notification settings - Fork 175
/
plstcx.go
227 lines (208 loc) · 5.19 KB
/
plstcx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Package ais provides core functionality for the AIStore object storage.
/*
* Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
*/
package ais
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/atomic"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/cmn/debug"
"github.com/NVIDIA/aistore/cmn/nlog"
"github.com/NVIDIA/aistore/core"
"github.com/NVIDIA/aistore/core/meta"
"github.com/NVIDIA/aistore/xact"
"github.com/NVIDIA/aistore/xact/xs"
)
type (
lstca struct {
a map[string]*lstcx
mu sync.Mutex
}
lstcx struct {
p *proxy
// arg
bckFrom *meta.Bck
bckTo *meta.Bck
amsg *apc.ActMsg // orig
config *cmn.Config
smap *smapX
hdr http.Header
// work
tsi *meta.Snode
xid string // x-tco
cnt int
lsmsg apc.LsoMsg
altmsg apc.ActMsg
tcomsg cmn.TCOMsg
stopped atomic.Bool
}
)
func (a *lstca) add(c *lstcx) {
a.mu.Lock()
if a.a == nil {
a.a = make(map[string]*lstcx, 4)
}
a.a[c.xid] = c
a.mu.Unlock()
}
func (a *lstca) del(c *lstcx) {
a.mu.Lock()
delete(a.a, c.xid)
a.mu.Unlock()
}
func (a *lstca) abort(xargs *xact.ArgsMsg) {
switch {
case xargs.ID != "":
if !strings.HasPrefix(xargs.ID, xs.PrefixTcoID) {
return
}
a.mu.Lock()
if c, ok := a.a[xargs.ID]; ok {
c.stopped.Store(true)
}
a.mu.Unlock()
nlog.Infoln(xargs.ID, "aborted")
case xargs.Kind == apc.ActCopyObjects || xargs.Kind == apc.ActETLObjects:
var ids []string
a.mu.Lock()
for uuid, c := range a.a {
c.stopped.Store(true)
ids = append(ids, uuid)
}
clear(a.a)
a.mu.Unlock()
if len(ids) > 0 {
nlog.Infoln(ids, "aborted")
}
}
}
func (c *lstcx) do() (string, error) {
// 1. lsmsg
c.lsmsg = apc.LsoMsg{
UUID: cos.GenUUID(),
Prefix: c.tcomsg.TCBMsg.Prefix,
Props: apc.GetPropsName,
PageSize: 0, // i.e., backend.MaxPageSize()
}
c.lsmsg.SetFlag(apc.LsNameOnly | apc.LsNoDirs)
c.smap = c.p.owner.smap.get()
tsi, err := c.smap.HrwTargetTask(c.lsmsg.UUID)
if err != nil {
return "", err
}
c.tsi = tsi
c.lsmsg.SID = tsi.ID()
// 2. ls 1st page
var lst *cmn.LsoRes
lst, err = c.p.lsObjsR(c.bckFrom, &c.lsmsg, c.hdr, c.smap, tsi /*designated target*/, c.config, true)
if err != nil {
return "", err
}
if len(lst.Entries) == 0 {
//
// TODO: return http.StatusNoContent to indicate exactly that (#6393)
//
nlog.Infoln(c.amsg.Action, c.bckFrom.Cname(""), " to ", c.bckTo.Cname("") ": lso counts zero - nothing to do")
return c.lsmsg.UUID, nil
}
// 3. assign txn UUID here, and use it to communicate with x-tco directly across pages (ref050724)
c.tcomsg.TxnUUID = cos.GenUUID()
// 4. tcomsg
c.tcomsg.ToBck = c.bckTo.Clone()
lr, cnt := &c.tcomsg.ListRange, len(lst.Entries)
lr.ObjNames = make([]string, 0, cnt)
for _, e := range lst.Entries {
if e.IsDir() { // NOTE: always skip virtual dir (apc.EntryIsDir)
continue
}
lr.ObjNames = append(lr.ObjNames, e.Name)
}
// 5. multi-obj action: transform/copy 1st page
c.altmsg.Value = &c.tcomsg
c.altmsg.Action = apc.ActCopyObjects
if c.amsg.Action == apc.ActETLBck {
c.altmsg.Action = apc.ActETLObjects
}
if c.xid, err = c.p.tcobjs(c.bckFrom, c.bckTo, c.config, &c.altmsg, &c.tcomsg); err != nil {
return "", err
}
nlog.Infoln("'ls --all' to execute [" c.amsg.Action " -> " c.altmsg.Action "]")
s := fmt.Sprintf("%s[%s] %s => %s", c.altmsg.Action, c.xid, c.bckFrom, c.bckTo)
// 6. more pages, if any
if lst.ContinuationToken != "" {
// Run
nlog.Infoln("run", s, "...")
c.lsmsg.ContinuationToken = lst.ContinuationToken
go c.pages(s, cnt)
} else {
nlog.Infoln(s, "count", cnt)
}
return c.xid, nil
}
func (c *lstcx) pages(s string, cnt int) {
c.cnt = cnt
c.p.lstca.add(c)
// pages 2, 3, ...
var err error
for !c.stopped.Load() && c.lsmsg.ContinuationToken != "" {
if cnt, err = c._page(); err != nil {
break
}
c.cnt = cnt
}
c.p.lstca.del(c)
nlog.Infoln(s, "count", c.cnt, "stopped", c.stopped.Load(), "c-token", c.lsmsg.ContinuationToken, "err", err)
}
// next page
func (c *lstcx) _page() (int, error) {
lst, err := c.p.lsObjsR(c.bckFrom, &c.lsmsg, c.hdr, c.smap, c.tsi, c.config, true)
if err != nil {
return 0, err
}
c.lsmsg.ContinuationToken = lst.ContinuationToken
if len(lst.Entries) == 0 {
debug.Assert(lst.ContinuationToken == "")
return 0, nil
}
lr := &c.tcomsg.ListRange
clear(lr.ObjNames)
lr.ObjNames = lr.ObjNames[:0]
for _, e := range lst.Entries {
if e.IsDir() { // NOTE: always skip virtual dir (apc.EntryIsDir)
continue
}
lr.ObjNames = append(lr.ObjNames, e.Name)
}
c.altmsg.Name = c.xid
c.altmsg.Value = &c.tcomsg
err = c.bcast()
return len(lr.ObjNames), err
}
// calls t.httpxpost (TODO: slice of names is the only "delta" - optimize)
func (c *lstcx) bcast() (err error) {
body := cos.MustMarshal(c.altmsg)
args := allocBcArgs()
{
args.req = cmn.HreqArgs{Method: http.MethodPost, Path: apc.URLPathXactions.S, Body: body}
args.to = core.Targets
args.timeout = cmn.Rom.MaxKeepalive()
}
if c.stopped.Load() {
return
}
results := c.p.bcastGroup(args)
freeBcArgs(args)
for _, res := range results {
if err = res.err; err != nil {
break
}
}
freeBcastRes(results)
return err
}