-
Notifications
You must be signed in to change notification settings - Fork 173
/
base.go
352 lines (306 loc) · 8.62 KB
/
base.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
// 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"
"net/url"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
"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"
)
// stream TCP/HTTP session: inactive <=> active transitions
const (
inactive = iota
active
)
// in-send states
const (
inHdr = iota + 1
inPDU
inData
inEOB
)
const maxInReadRetries = 64 // Tx: lz4 stream read; Rx: partial object header
// termination: reasons
const (
reasonError = "error"
endOfStream = "end-of-stream"
reasonStopped = "stopped"
connErrWait = time.Second // ECONNREFUSED | ECONNRESET | EPIPE
termErrWait = time.Second
)
type (
streamer interface {
compressed() bool
dryrun()
terminate(error, string) (string, error)
doRequest() error
inSend() bool
abortPending(error, bool)
errCmpl(error)
resetCompression()
// gc
closeAndFree()
drain(err error)
idleTick()
}
streamBase struct {
streamer streamer
client Client // stream's http client
stopCh cos.StopCh // stop/abort stream
lastCh cos.StopCh // end-of-stream
pdu *spdu // PDU buffer
postCh chan struct{} // to indicate that workCh has work
trname string // http endpoint: (trname, dstURL, dstID)
dstURL string
dstID string
lid string // log prefix
maxhdr []byte // header buf must be large enough to accommodate max-size for this stream
header []byte // object header (slice of the maxhdr with bucket/objName, etc. fields packed/serialized)
term struct {
err error
reason string
mu sync.Mutex
done atomic.Bool
}
stats Stats // stream stats (send side - compare with rxStats)
time struct {
idleTeardown time.Duration // idle timeout
inSend atomic.Bool // true upon Send() or Read() - info for Collector to delay cleanup
ticks int // num 1s ticks until idle timeout
index int // heap stuff
}
wg sync.WaitGroup
sessST atomic.Int64 // state of the TCP/HTTP session: active (connected) | inactive (disconnected)
sessID int64 // stream session ID
numCur int64 // gets reset to zero upon each timeout
sizeCur int64 // ditto
chanFull atomic.Int64
}
)
////////////////
// streamBase //
////////////////
func newBase(client Client, dstURL, dstID string, extra *Extra) (s *streamBase) {
var (
sid string
u, err = url.Parse(dstURL)
)
debug.AssertNoErr(err)
s = &streamBase{client: client, dstURL: dstURL, dstID: dstID}
s.sessID = nextSessionID.Inc()
s.trname = path.Base(u.Path)
s.lastCh.Init()
s.stopCh.Init()
s.postCh = make(chan struct{}, 1)
// default overrides
if extra.SenderID != "" {
sid = "-" + extra.SenderID
}
// NOTE: PDU-based traffic - a MUST-have for "unsized" transmissions
if extra.UsePDU() {
if extra.SizePDU > maxSizePDU {
debug.Assert(false)
extra.SizePDU = maxSizePDU
}
buf, _ := g.mm.AllocSize(int64(extra.SizePDU))
s.pdu = newSendPDU(buf)
}
if extra.IdleTeardown > 0 {
s.time.idleTeardown = extra.IdleTeardown
} else {
s.time.idleTeardown = cos.NonZero(extra.Config.Transport.IdleTeardown.D(), dfltIdleTeardown)
}
debug.Assert(s.time.idleTeardown >= dfltTick, s.time.idleTeardown, " vs ", dfltTick)
s.time.ticks = int(s.time.idleTeardown / dfltTick)
s._lid(sid, dstID, extra)
s.maxhdr, _ = g.mm.AllocSize(_sizeHdr(extra.Config, int64(extra.MaxHdrSize)))
s.sessST.Store(inactive) // initiate HTTP session upon the first arrival
return
}
func (s *streamBase) _lid(sid, dstID string, extra *Extra) {
var (
sb strings.Builder
l = 2 + len(s.trname) + len(sid) + 32 + len(dstID)
)
sb.Grow(l)
sb.WriteString("s-")
sb.WriteString(s.trname)
sb.WriteString(sid)
sb.WriteByte('[')
sb.WriteString(strconv.FormatInt(s.sessID, 10))
extra.Lid(&sb)
sb.WriteString("]=>")
sb.WriteString(dstID)
s.lid = sb.String() // "s-%s%s[%d]=>%s"
}
// (used on the receive side as well)
func _sizeHdr(config *cmn.Config, size int64) int64 {
if size != 0 {
debug.Assert(size <= cmn.MaxTransportHeader, size)
size = min(size, cmn.MaxTransportHeader)
} else {
size = cos.NonZero(int64(config.Transport.MaxHeaderSize), int64(cmn.DfltTransportHeader))
}
return size
}
func (s *streamBase) startSend(streamable fmt.Stringer) (err error) {
s.time.inSend.Store(true) // StreamCollector to postpone cleanups
if s.IsTerminated() {
// slow path
reason, errT := s.TermInfo()
err = cmn.NewErrStreamTerminated(s.String(), errT, reason, "dropping "+streamable.String())
nlog.Errorln(err)
return
}
if s.sessST.CAS(inactive, active) {
s.postCh <- struct{}{}
if cmn.Rom.FastV(5, cos.SmoduleTransport) {
nlog.Infoln(s.String(), "inactive => active")
}
}
return
}
func (s *streamBase) Stop() { s.stopCh.Close() }
func (s *streamBase) URL() string { return s.dstURL }
func (s *streamBase) ID() (string, int64) { return s.trname, s.sessID }
func (s *streamBase) String() string { return s.lid }
func (s *streamBase) Abort() { s.Stop() } // (DM =>) SB => s.Abort() sequence (e.g. usage see otherXreb.Abort())
func (s *streamBase) IsTerminated() bool { return s.term.done.Load() }
func (s *streamBase) TermInfo() (reason string, err error) {
// to account for an unlikely delay between done.CAS() and mu.Lock - see terminate()
sleep := cos.ProbingFrequency(termErrWait)
for elapsed := time.Duration(0); elapsed < termErrWait; elapsed += sleep {
s.term.mu.Lock()
reason, err = s.term.reason, s.term.err
s.term.mu.Unlock()
if reason != "" {
break
}
time.Sleep(sleep)
}
return
}
func (s *streamBase) GetStats() (stats Stats) {
// byte-num transfer stats
stats.Num.Store(s.stats.Num.Load())
stats.Offset.Store(s.stats.Offset.Load())
stats.Size.Store(s.stats.Size.Load())
stats.CompressedSize.Store(s.stats.CompressedSize.Load())
return
}
func (s *streamBase) isNextReq() (reason string) {
for {
select {
case <-s.lastCh.Listen():
if cmn.Rom.FastV(5, cos.SmoduleTransport) {
nlog.Infoln(s.String(), "end-of-stream")
}
reason = endOfStream
return
case <-s.stopCh.Listen():
if cmn.Rom.FastV(5, cos.SmoduleTransport) {
nlog.Infoln(s.String(), "stopped")
}
reason = reasonStopped
return
case <-s.postCh:
s.sessST.Store(active)
if cmn.Rom.FastV(5, cos.SmoduleTransport) {
nlog.Infoln(s.String(), "active <- posted")
}
return
}
}
}
func (s *streamBase) deactivate() (n int, err error) {
err = io.EOF
if cmn.Rom.FastV(5, cos.SmoduleTransport) {
nlog.Infoln(s.String(), "connection teardown: [", s.numCur, s.stats.Num.Load(), "]")
}
return
}
func (s *streamBase) sendLoop(dryrun bool) {
var (
err error
reason string
retried bool
)
for {
if s.sessST.Load() == active {
if dryrun {
s.streamer.dryrun()
} else if errR := s.streamer.doRequest(); errR != nil {
if !cos.IsRetriableConnErr(err) || retried {
reason = reasonError
err = errR
s.streamer.errCmpl(err)
break
}
retried = true
nlog.Errorln(s.String(), "err: ", errR, "- retrying...")
time.Sleep(connErrWait)
}
}
if reason = s.isNextReq(); reason != "" {
break
}
}
reason, err = s.streamer.terminate(err, reason)
s.wg.Done()
if reason == endOfStream {
return
}
// termination is caused by anything other than Fin()
// (reasonStopped is, effectively, abort via Stop() - totally legit)
if reason != reasonStopped {
nlog.Errorf("%s: terminating (%s, %v)", s, reason, err)
}
// wait for the SCQ/cmplCh to empty
s.wg.Wait()
// cleanup
s.streamer.abortPending(err, false /*completions*/)
verbose := cmn.Rom.FastV(5, cos.SmoduleTransport)
if cnt := s.chanFull.Load(); (cnt >= 10 && cnt <= 20) || (cnt > 0 && verbose) {
nlog.Errorln(s.String(), cos.ErrWorkChanFull, "cnt:", cnt)
}
}
///////////
// Extra //
///////////
func (extra *Extra) UsePDU() bool { return extra.SizePDU > 0 }
func (extra *Extra) Compressed() bool {
return extra.Compression != "" && extra.Compression != apc.CompressNever
}
func (extra *Extra) Lid(sb *strings.Builder) {
if extra.Compressed() {
sb.WriteByte('[')
sb.WriteString(cos.ToSizeIEC(int64(extra.Config.Transport.LZ4BlockMaxSize), 0))
sb.WriteByte(']')
}
}
//
// misc
//
func dryrun() (dryrun bool) {
var err error
if a := os.Getenv("AIS_STREAM_DRY_RUN"); a != "" {
if dryrun, err = strconv.ParseBool(a); err != nil {
nlog.Errorln(err)
}
}
return
}