-
Notifications
You must be signed in to change notification settings - Fork 173
/
sendobj.go
459 lines (424 loc) · 10 KB
/
sendobj.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 transport provides long-lived http/tcp connections for
// intra-cluster communications (see README for details and usage example).
/*
* Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
*/
package transport
import (
"fmt"
"io"
"runtime"
"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/memsys"
"github.com/pierrec/lz4/v3"
)
// object stream & private types
type (
Stream struct {
workCh chan *Obj // aka SQ: next object to stream
cmplCh chan cmpl // aka SCQ; note that SQ and SCQ together form a FIFO
callback ObjSentCB // to free SGLs, close files, etc.
lz4s *lz4Stream
sendoff sendoff
streamBase
}
lz4Stream struct {
s *Stream
zw *lz4.Writer // orig reader => zw
sgl *memsys.SGL // zw => bb => network
blockMaxSize int // *uncompressed* block max size
frameChecksum bool // true: checksum lz4 frames
}
sendoff struct {
obj Obj
off int64
ins int // in-send enum
}
cmpl struct {
err error
obj Obj
}
)
// interface guard
var _ streamer = (*Stream)(nil)
///////////////////
// object stream //
///////////////////
func (s *Stream) terminate(err error, reason string) (actReason string, actErr error) {
ok := s.term.done.CAS(false, true)
debug.Assert(ok, s.String())
s.term.mu.Lock()
if s.term.err == nil {
s.term.err = err
}
if s.term.reason == "" {
s.term.reason = reason
}
s.Stop()
err = s.term.err
actReason, actErr = s.term.reason, s.term.err
s.cmplCh <- cmpl{err, Obj{Hdr: ObjHdr{Opcode: opcFin}}}
s.term.mu.Unlock()
// Remove stream after lock because we could deadlock between `do()`
// (which checks for `Terminated` status) and this function which
// would be under lock.
gc.remove(&s.streamBase)
if s.compressed() {
s.lz4s.sgl.Free()
if s.lz4s.zw != nil {
s.lz4s.zw.Reset(nil)
}
}
return
}
func (s *Stream) initCompression(extra *Extra) {
s.lz4s = &lz4Stream{}
s.lz4s.s = s
s.lz4s.blockMaxSize = int(extra.Config.Transport.LZ4BlockMaxSize)
s.lz4s.frameChecksum = extra.Config.Transport.LZ4FrameChecksum
if s.lz4s.blockMaxSize >= memsys.MaxPageSlabSize {
s.lz4s.sgl = g.mm.NewSGL(memsys.MaxPageSlabSize, memsys.MaxPageSlabSize)
} else {
s.lz4s.sgl = g.mm.NewSGL(cos.KiB*64, cos.KiB*64)
}
}
func (s *Stream) compressed() bool { return s.lz4s != nil }
func (s *Stream) usePDU() bool { return s.pdu != nil }
func (s *Stream) resetCompression() {
s.lz4s.sgl.Reset()
s.lz4s.zw.Reset(nil)
}
func (s *Stream) cmplLoop() {
for {
cmpl, ok := <-s.cmplCh
obj := &cmpl.obj
if !ok || obj.Hdr.isFin() {
break
}
s.doCmpl(&cmpl.obj, cmpl.err)
}
s.wg.Done()
}
// handle the last interrupted transmission and pending SQ/SCQ
func (s *Stream) abortPending(err error, completions bool) {
for obj := range s.workCh {
s.doCmpl(obj, err)
}
if completions {
for cmpl := range s.cmplCh {
if !cmpl.obj.Hdr.isFin() {
s.doCmpl(&cmpl.obj, cmpl.err)
}
}
}
}
// refcount to invoke the has-been-sent callback only once
// and *always* close the reader (sic!)
func (s *Stream) doCmpl(obj *Obj, err error) {
var rc int64
if obj.prc != nil {
rc = obj.prc.Dec()
debug.Assert(rc >= 0)
}
if obj.Reader != nil {
if err != nil && cmn.IsFileAlreadyClosed(err) {
nlog.Errorf("%s %s: %v", s, obj, err)
} else {
cos.Close(obj.Reader) // otherwise, always closing
}
}
// SCQ completion callback
if rc == 0 {
if obj.Callback != nil {
obj.Callback(&obj.Hdr, obj.Reader, obj.CmplArg, err)
} else if s.callback != nil {
s.callback(&obj.Hdr, obj.Reader, obj.CmplArg, err)
}
}
freeSend(obj)
}
func (s *Stream) doRequest() error {
s.numCur, s.sizeCur = 0, 0
if !s.compressed() {
return s.do(s)
}
s.lz4s.sgl.Reset()
if s.lz4s.zw == nil {
s.lz4s.zw = lz4.NewWriter(s.lz4s.sgl)
} else {
s.lz4s.zw.Reset(s.lz4s.sgl)
}
// lz4 framing spec at http://fastcompression.blogspot.com/2013/04/lz4-streaming-format-final.html
s.lz4s.zw.Header.BlockChecksum = false
s.lz4s.zw.Header.NoChecksum = !s.lz4s.frameChecksum
s.lz4s.zw.Header.BlockMaxSize = s.lz4s.blockMaxSize
return s.do(s.lz4s)
}
// as io.Reader
func (s *Stream) Read(b []byte) (n int, err error) {
s.time.inSend.Store(true) // for collector to delay cleanup
if !s.inSend() { // true when transmitting s.sendoff.obj
goto repeat
}
switch s.sendoff.ins {
case inData:
obj := &s.sendoff.obj
if !obj.IsHeaderOnly() {
return s.sendData(b)
}
if obj.Hdr.isFin() {
err = io.EOF
return
}
s.eoObj(nil)
case inPDU:
for !s.pdu.done {
err = s.pdu.readFrom(&s.sendoff)
if s.pdu.done {
s.pdu.insHeader()
break
}
}
if s.pdu.rlength() > 0 {
n = s.sendPDU(b)
if s.pdu.rlength() == 0 {
s.sendoff.off += int64(s.pdu.slength())
if s.pdu.last {
s.eoObj(nil)
}
s.pdu.reset()
}
}
return
case inHdr:
return s.sendHdr(b)
}
repeat:
select {
case obj, ok := <-s.workCh: // next object OR idle tick
if !ok {
err = fmt.Errorf("%s closed prior to stopping", s)
nlog.Warningln(err)
return
}
s.sendoff.obj = *obj
obj = &s.sendoff.obj
if obj.Hdr.isIdleTick() {
if len(s.workCh) > 0 {
goto repeat
}
return s.deactivate()
}
l := insObjHeader(s.maxhdr, &obj.Hdr, s.usePDU())
s.header = s.maxhdr[:l]
s.sendoff.ins = inHdr
return s.sendHdr(b)
case <-s.stopCh.Listen():
if cmn.Rom.FastV(5, cos.SmoduleTransport) {
nlog.Infoln(s.String(), "stopped [", s.numCur, s.stats.Num.Load(), "]")
}
err = io.EOF
return
}
}
func (s *Stream) sendHdr(b []byte) (n int, err error) {
n = copy(b, s.header[s.sendoff.off:])
s.sendoff.off += int64(n)
if s.sendoff.off < int64(len(s.header)) {
return
}
debug.Assert(s.sendoff.off == int64(len(s.header)))
s.stats.Offset.Add(s.sendoff.off)
obj := &s.sendoff.obj
if s.usePDU() && !obj.IsHeaderOnly() {
s.sendoff.ins = inPDU
} else {
s.sendoff.ins = inData
}
if cmn.Rom.FastV(5, cos.SmoduleTransport) && s.numCur&0x3f == 2 {
nlog.Infoln(s.String(), obj.Hdr.Cname(), "[", s.numCur, s.stats.Num.Load(), "]")
}
s.sendoff.off = 0
if obj.Hdr.isFin() {
err = io.EOF
s.lastCh.Close()
}
return
}
func (s *Stream) sendData(b []byte) (n int, err error) {
var (
obj = &s.sendoff.obj
objSize = obj.Size()
)
n, err = obj.Reader.Read(b)
s.sendoff.off += int64(n)
if err != nil {
if err == io.EOF {
if s.sendoff.off < objSize {
return n, fmt.Errorf("%s: read (%d) shorter than size (%d)", s, s.sendoff.off, objSize)
}
err = nil
}
s.eoObj(err)
} else if s.sendoff.off >= objSize {
s.eoObj(err)
}
return
}
func (s *Stream) sendPDU(b []byte) (n int) {
n = s.pdu.read(b)
return
}
// end-of-object:
// - update stats, reset idle timeout, and post completion
// - note that reader.Close() is done by `doCmpl`
// TODO: ideally, there's a way to flush buffered data to the underlying connection :NOTE
func (s *Stream) eoObj(err error) {
obj := &s.sendoff.obj
objSize := obj.Size()
if obj.IsUnsized() {
objSize = s.sendoff.off
}
s.sizeCur += s.sendoff.off
s.stats.Offset.Add(s.sendoff.off)
if err != nil {
goto exit
}
if s.sendoff.off != objSize {
err = fmt.Errorf("%s: %s offset %d != size", s, obj, s.sendoff.off)
goto exit
}
// this stream stats
s.stats.Size.Add(objSize)
s.numCur++
s.stats.Num.Inc()
if cmn.Rom.FastV(5, cos.SmoduleTransport) && s.numCur&0x3f == 3 {
nlog.Infoln(s.String(), obj.Hdr.Cname(), "[", s.numCur, s.stats.Num.Load(), "]")
}
// target stats
g.tstats.Inc(cos.StreamsOutObjCount)
g.tstats.Add(cos.StreamsOutObjSize, objSize)
exit:
if err != nil {
nlog.Errorln(err)
}
// next completion => SCQ
s.cmplCh <- cmpl{err, s.sendoff.obj}
s.sendoff = sendoff{ins: inEOB}
}
func (s *Stream) inSend() bool { return s.sendoff.ins >= inHdr || s.sendoff.ins < inEOB }
func (s *Stream) dryrun() {
var (
body = io.NopCloser(s)
h = &hdl{trname: s.trname}
it = iterator{handler: h, body: body, hbuf: make([]byte, cmn.DfltTransportHeader)}
)
for {
hlen, flags, err := it.nextProtoHdr(s.String())
if err == io.EOF {
break
}
debug.AssertNoErr(err)
debug.Assert(flags&msgFl == 0)
obj, err := it.nextObj(s.String(), hlen)
if obj != nil {
cos.DrainReader(obj) // TODO: recycle `objReader` here
continue
}
if err != nil {
break
}
}
}
func (s *Stream) errCmpl(err error) {
if s.inSend() {
s.cmplCh <- cmpl{err, s.sendoff.obj}
}
}
// gc: drain terminated stream
func (s *Stream) drain(err error) {
for {
select {
case obj := <-s.workCh:
s.doCmpl(obj, err)
default:
return
}
}
}
// gc:
func (s *Stream) closeAndFree() {
close(s.workCh)
close(s.cmplCh)
g.mm.Free(s.maxhdr)
if s.pdu != nil {
s.pdu.free(g.mm)
}
}
// gc: post idle tick if idle
func (s *Stream) idleTick() {
if len(s.workCh) == 0 && s.sessST.CAS(active, inactive) {
s.workCh <- &Obj{Hdr: ObjHdr{Opcode: opcIdleTick}}
if cmn.Rom.FastV(5, cos.SmoduleTransport) {
nlog.Infoln(s.String(), "active => inactive")
}
}
}
///////////
// Stats //
///////////
func (stats *Stats) CompressionRatio() float64 {
bytesRead := stats.Offset.Load()
bytesSent := stats.CompressedSize.Load()
return float64(bytesRead) / float64(bytesSent)
}
///////////////
// lz4Stream //
///////////////
func (lz4s *lz4Stream) Read(b []byte) (n int, err error) {
var (
sendoff = &lz4s.s.sendoff
last = sendoff.obj.Hdr.isFin()
retry = maxInReadRetries // insist on returning n > 0 (note that lz4 compresses /blocks/)
)
if lz4s.sgl.Len() > 0 {
lz4s.zw.Flush()
n, err = lz4s.sgl.Read(b)
if err == io.EOF { // reusing/rewinding this buf multiple times
err = nil
}
goto ex
}
re:
n, err = lz4s.s.Read(b)
_, _ = lz4s.zw.Write(b[:n])
if last {
lz4s.zw.Flush()
retry = 0
} else if lz4s.s.sendoff.ins == inEOB || err != nil {
lz4s.zw.Flush()
retry = 0
}
n, _ = lz4s.sgl.Read(b)
if n == 0 {
if retry > 0 {
retry--
runtime.Gosched()
goto re
}
lz4s.zw.Flush()
n, _ = lz4s.sgl.Read(b)
}
ex:
lz4s.s.stats.CompressedSize.Add(int64(n))
if lz4s.sgl.Len() == 0 {
lz4s.sgl.Reset()
}
if last && err == nil {
err = io.EOF
}
return
}