-
Notifications
You must be signed in to change notification settings - Fork 175
/
ic.go
459 lines (426 loc) · 11.3 KB
/
ic.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Package ais provides core functionality for the AIStore object storage.
/*
* Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
*/
package ais
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/cmn/debug"
"github.com/NVIDIA/aistore/cmn/nlog"
"github.com/NVIDIA/aistore/core/meta"
"github.com/NVIDIA/aistore/nl"
"github.com/NVIDIA/aistore/xact"
jsoniter "github.com/json-iterator/go"
)
// Information Center (IC) is a group of proxies that take care of ownership of
// (Job, Task, eXtended action) entities. IC manages their lifecycle and monitors
// status. When a job, task or xaction is created, it gets registered with all IC
// members. Henceforth, IC acts as information source as far as status (running,
// aborted, finished), progress, and statistics.
//
// Non-IC AIS proxies, on the other hand, redirect all corresponding requests to
// one (anyone) of the IC (proxy) members.
const (
// Implies equal ownership by all IC members and applies to all async ops
// that have no associated cache other than start/end timestamps and stats counters
// (case in point: list/query-objects that MAY be cached, etc.)
equalIC = "\x00"
)
type (
regIC struct {
nl nl.Listener
smap *smapX
query url.Values
msg any
}
xactRegMsg struct {
UUID string `json:"uuid"`
Kind string `json:"kind"`
Srcs []string `json:"srcs"` // list of daemonIDs
}
icBundle struct {
Smap *smapX `json:"smap"`
OwnershipTbl jsoniter.RawMessage `json:"ownership_table"`
}
ic struct {
p *proxy
}
)
func (ic *ic) init(p *proxy) {
ic.p = p
}
func (ic *ic) reverseToOwner(w http.ResponseWriter, r *http.Request, uuid string, msg any) (reversedOrFailed bool) {
retry := true
begin:
var (
smap = ic.p.owner.smap.get()
selfIC = smap.IsIC(ic.p.si)
owner, exists = ic.p.notifs.getOwner(uuid)
psi *meta.Snode
)
if exists {
goto outer
}
if selfIC {
if !exists && !retry {
err := fmt.Errorf("x-[%s] not found (%s)", uuid, smap.StrIC(ic.p.si))
ic.p.writeErr(w, r, err, http.StatusNotFound, Silent)
return true
}
if retry {
withRetry(cmn.Rom.CplaneOperation(), func() bool {
owner, exists = ic.p.notifs.getOwner(uuid)
return exists
})
if !exists {
retry = false
_ = ic.syncICBundle() // TODO handle error
goto begin
}
}
} else {
hrwOwner, err := smap.HrwIC(uuid)
if err != nil {
ic.p.writeErr(w, r, err, http.StatusInternalServerError)
return true
}
owner = hrwOwner.ID()
}
outer:
switch owner {
case "": // not owned
return
case equalIC:
if selfIC {
owner = ic.p.SID()
} else {
for pid, si := range smap.Pmap {
if !smap.IsIC(psi) {
continue
}
owner = pid
psi = si
break outer
}
}
default: // cached owned
psi = smap.GetProxy(owner)
if psi == nil || !smap.IsIC(psi) {
var err error
if psi, err = smap.HrwIC(uuid); err != nil {
ic.p.writeErr(w, r, err, http.StatusInternalServerError)
return true
}
}
debug.Assertf(smap.IsIC(psi), "%s, %s", psi, smap.StrIC(ic.p.si))
}
if owner == ic.p.SID() {
return
}
// otherwise, hand it over
if msg != nil {
body := cos.MustMarshal(msg)
r.ContentLength = int64(len(body))
r.Body = io.NopCloser(bytes.NewReader(body))
}
ic.p.reverseNodeRequest(w, r, psi)
return true
}
func (ic *ic) redirectToIC(w http.ResponseWriter, r *http.Request) bool {
smap := ic.p.owner.smap.get()
if smap.IsIC(ic.p.si) {
return false
}
var node *meta.Snode
for _, psi := range smap.Pmap {
if smap.IsIC(psi) {
node = psi
break
}
}
redirectURL := ic.p.redirectURL(r, node, time.Now(), cmn.NetIntraControl)
http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
return true
}
func (ic *ic) xstatusAll(w http.ResponseWriter, r *http.Request, query url.Values) {
msg := &xact.QueryMsg{}
if err := cmn.ReadJSON(w, r, msg); err != nil {
return
}
flt := nlFilter{ID: msg.ID, Kind: msg.Kind, Bck: (*meta.Bck)(&msg.Bck), OnlyRunning: msg.OnlyRunning}
if !msg.Bck.IsEmpty() {
flt.Bck = (*meta.Bck)(&msg.Bck)
}
var (
vec nl.StatusVec
nls = ic.p.notifs.findAll(flt)
)
if cos.IsParseBool(query.Get(apc.QparamForce)) {
// (force just-in-time)
// for each args-selected xaction:
// check if any of the targets delayed updating the corresponding status,
// and query those targets directly
var (
config = cmn.GCO.Get()
interval = config.Periodic.NotifTime.D()
)
for _, nl := range nls {
ic.p.notifs.bcastGetStats(nl, interval)
status := nl.Status()
if err := nl.Err(); err != nil {
status.ErrMsg = err.Error()
}
vec = append(vec, *status)
}
} else {
for _, nl := range nls {
vec = append(vec, *nl.Status())
}
}
b := cos.MustMarshal(vec)
w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(b)))
w.Write(b)
}
func (ic *ic) xstatusOne(w http.ResponseWriter, r *http.Request) {
var (
nl nl.Listener
bck *meta.Bck
msg = &xact.QueryMsg{}
)
if err := cmn.ReadJSON(w, r, msg); err != nil {
return
}
msg.Kind, _ = xact.GetKindName(msg.Kind) // display name => kind
if msg.ID == "" && msg.Kind == "" {
ic.p.writeErrStatusf(w, r, http.StatusBadRequest, "invalid %s", msg)
return
}
// for queries of the type {Kind: apc.ActRebalance}
if msg.ID == "" && ic.redirectToIC(w, r) {
return
}
if msg.ID != "" && ic.reverseToOwner(w, r, msg.ID, msg) {
return
}
if msg.Bck.Name != "" {
bck = meta.CloneBck(&msg.Bck)
if err := bck.Init(ic.p.owner.bmd); err != nil {
ic.p.writeErr(w, r, err, http.StatusNotFound, Silent)
return
}
}
flt := nlFilter{ID: msg.ID, Kind: msg.Kind, Bck: bck, OnlyRunning: msg.OnlyRunning}
withRetry(cmn.Rom.CplaneOperation(), func() bool {
nl = ic.p.notifs.find(flt)
return nl != nil
})
if nl == nil {
smap := ic.p.owner.smap.get()
err := fmt.Errorf("nl not found: %s, %s", smap.StrIC(ic.p.si), msg)
ic.p.writeErr(w, r, err, http.StatusNotFound, Silent)
return
}
if msg.Kind != "" && nl.Kind() != msg.Kind {
ic.p.writeErrf(w, r, "kind mismatch: %s, expected kind=%s", msg, nl.Kind())
return
}
// refresh NotifStatus
var (
config = cmn.GCO.Get()
interval = config.Periodic.NotifTime.D()
)
ic.p.notifs.bcastGetStats(nl, interval)
status := nl.Status()
if err := nl.Err(); err != nil {
status.ErrMsg = err.Error()
}
b := cos.MustMarshal(status) // TODO: include stats, e.g., progress when ready
w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(b)))
w.Write(b)
}
// verb /v1/ic
func (ic *ic) handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
ic.handleGet(w, r)
case http.MethodPost:
ic.handlePost(w, r)
default:
debug.Assert(false)
}
}
// GET /v1/ic
func (ic *ic) handleGet(w http.ResponseWriter, r *http.Request) {
var (
smap = ic.p.owner.smap.get()
what = r.URL.Query().Get(apc.QparamWhat)
)
if !smap.IsIC(ic.p.si) {
ic.p.writeErrf(w, r, "%s: not an IC member", ic.p.si)
return
}
switch what {
case apc.WhatICBundle:
bundle := icBundle{Smap: smap, OwnershipTbl: cos.MustMarshal(&ic.p.notifs)}
ic.p.writeJSON(w, r, bundle, what)
default:
ic.p.writeErrf(w, r, fmtUnknownQue, what)
}
}
// POST /v1/ic
func (ic *ic) handlePost(w http.ResponseWriter, r *http.Request) {
var (
smap = ic.p.owner.smap.get()
msg = &actMsgExt{}
)
if err := cmn.ReadJSON(w, r, msg); err != nil {
return
}
if !smap.IsIC(ic.p.si) {
if !withRetry(cmn.Rom.CplaneOperation(), func() bool {
smap = ic.p.owner.smap.get()
return smap.IsIC(ic.p.si)
}) {
ic.p.writeErrf(w, r, "%s: not an IC member", ic.p.si)
return
}
}
switch msg.Action {
case apc.ActMergeOwnershipTbl:
if err := cos.MorphMarshal(msg.Value, &ic.p.notifs); err != nil {
ic.p.writeErrf(w, r, cmn.FmtErrMorphUnmarshal, ic.p.si, msg.Action, msg.Value, err)
return
}
case apc.ActListenToNotif:
nlMsg := ¬ifListenMsg{}
if err := cos.MorphMarshal(msg.Value, nlMsg); err != nil {
ic.p.writeErrf(w, r, cmn.FmtErrMorphUnmarshal, ic.p.si, msg.Action, msg.Value, err)
return
}
if err := ic.p.notifs.add(nlMsg.nl); err != nil {
ic.p.writeErr(w, r, err)
return
}
case apc.ActRegGlobalXaction:
var (
regMsg = &xactRegMsg{}
tmap meta.NodeMap
callerSver = r.Header.Get(apc.HdrCallerSmapVer)
err error
)
if err = cos.MorphMarshal(msg.Value, regMsg); err != nil {
ic.p.writeErrf(w, r, cmn.FmtErrMorphUnmarshal, ic.p.si, msg.Action, msg.Value, err)
return
}
debug.Assert(len(regMsg.Srcs) != 0)
withRetry(cmn.Rom.CplaneOperation(), func() bool {
smap = ic.p.owner.smap.get()
tmap, err = smap.NewTmap(regMsg.Srcs)
return err == nil && callerSver == smap.vstr
})
if err != nil {
ic.p.writeErrStatusf(w, r, http.StatusNotFound, "%s: failed to %q: %v", ic.p, msg.Action, err)
return
}
nl := xact.NewXactNL(regMsg.UUID, regMsg.Kind, &smap.Smap, tmap)
if err = ic.p.notifs.add(nl); err != nil {
ic.p.writeErr(w, r, err)
return
}
default:
ic.p.writeErrAct(w, r, msg.Action)
}
}
func (ic *ic) registerEqual(a regIC) {
if a.query != nil {
a.query.Set(apc.QparamNotifyMe, equalIC)
}
if a.smap.IsIC(ic.p.si) {
err := ic.p.notifs.add(a.nl)
debug.AssertNoErr(err)
}
if a.smap.ICCount() > 1 {
ic.bcastListenIC(a.nl)
}
}
func (ic *ic) bcastListenIC(nl nl.Listener) {
var (
actMsg = apc.ActMsg{Action: apc.ActListenToNotif, Value: newNLMsg(nl)}
msg = ic.p.newAmsg(&actMsg, nil)
)
ic.p.bcastAsyncIC(msg)
}
func (ic *ic) sendOwnershipTbl(si *meta.Snode, smap *smapX) error {
if ic.p.notifs.size() == 0 {
if cmn.Rom.FastV(4, cos.SmoduleAIS) {
nlog.Infof("%s: notifs empty, not sending to %s", ic.p, si)
}
return nil
}
msg := ic.p.newAmsgActVal(apc.ActMergeOwnershipTbl, &ic.p.notifs)
cargs := allocCargs()
{
cargs.si = si
cargs.req = cmn.HreqArgs{Method: http.MethodPost, Path: apc.URLPathIC.S, Body: cos.MustMarshal(msg)}
cargs.timeout = cmn.Rom.CplaneOperation()
}
res := ic.p.call(cargs, smap)
freeCargs(cargs)
return res.err
}
// sync ownership table; TODO: review control flows and revisit impl.
func (ic *ic) syncICBundle() error {
smap := ic.p.owner.smap.get()
si := ic.p.si
for _, psi := range smap.Pmap {
if smap.IsIC(psi) && psi.ID() != si.ID() {
si = psi
break
}
}
if si.Eq(ic.p.si) {
return nil
}
cargs := allocCargs()
{
cargs.si = si
cargs.req = cmn.HreqArgs{
Method: http.MethodGet,
Path: apc.URLPathIC.S,
Query: url.Values{apc.QparamWhat: []string{apc.WhatICBundle}},
}
cargs.timeout = cmn.Rom.CplaneOperation()
cargs.cresv = cresIC{} // -> icBundle
}
res := ic.p.call(cargs, smap)
freeCargs(cargs)
if res.err != nil {
return res.err
}
bundle := res.v.(*icBundle)
debug.Assertf(smap.UUID == bundle.Smap.UUID, "%s vs %s", smap.StringEx(), bundle.Smap.StringEx())
if err := ic.p.owner.smap.synchronize(ic.p.si, bundle.Smap, nil /*ms payload*/, ic.p.htrun.smapUpdatedCB); err != nil {
if !isErrDowngrade(err) {
nlog.Errorln(cmn.NewErrFailedTo(ic.p, "sync", bundle.Smap, err))
}
} else {
smap = ic.p.owner.smap.get()
nlog.Infof("%s: synch %s", ic.p, smap)
}
if !smap.IsIC(ic.p.si) {
return nil
}
if err := jsoniter.Unmarshal(bundle.OwnershipTbl, &ic.p.notifs); err != nil {
return fmt.Errorf(cmn.FmtErrUnmarshal, ic.p, "ownership table", cos.BHead(bundle.OwnershipTbl), err)
}
return nil
}