-
Notifications
You must be signed in to change notification settings - Fork 1
/
sender.go
455 lines (363 loc) · 7.8 KB
/
sender.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
package memcache
import (
"io"
"sync"
"github.com/QuangTung97/go-memcache/memcache/netconn"
)
type closerInterface = io.Closer
type readCloserInterface = io.ReadCloser
// FlushWriter ...
type FlushWriter = netconn.FlushWriter
//go:generate moq -out sender_mocks_test.go . FlushWriter closerInterface readCloserInterface
// =====================
// Pool of Bytes
// =====================
var requestBytesPool = bytesPool{
pool: sync.Pool{
New: func() any {
return make([]byte, 0, 256)
},
},
}
var responseBytesPool = bytesPool{
pool: sync.Pool{
New: func() any {
return make([]byte, 0, 1024)
},
},
}
type bytesPool struct {
pool sync.Pool
}
func (p *bytesPool) get() []byte {
return p.pool.Get().([]byte)
}
func (p *bytesPool) put(data []byte) {
data = data[:0]
p.pool.Put(data)
}
type commandData struct {
cmdCount int
requestData []byte
responseData []byte
completed bool
epoch uint64
reader io.ReadCloser
lastErr error
mut sync.Mutex
cond *sync.Cond
}
func newCommand() *commandData {
c := &commandData{
requestData: requestBytesPool.get(),
responseData: responseBytesPool.get(),
}
c.cond = sync.NewCond(&c.mut)
return c
}
func (c *commandData) cloneShareRequest() *commandData {
result := &commandData{
cmdCount: c.cmdCount,
requestData: c.requestData,
}
result.cond = sync.NewCond(&result.mut)
return result
}
func freeCommandData(cmd *commandData) {
requestBytesPool.put(cmd.requestData)
responseBytesPool.put(cmd.responseData)
}
func (c *commandData) waitCompleted() {
c.mut.Lock()
for !c.completed {
c.cond.Wait()
}
c.mut.Unlock()
}
func (c *commandData) setErrorOnly(err error) {
c.mut.Lock()
c.lastErr = err
c.mut.Unlock()
}
func (c *commandData) setCompleted(err error) {
c.mut.Lock()
if c.completed {
c.mut.Unlock()
return
}
c.completed = true
// not override error
if c.lastErr == nil {
c.lastErr = err
}
c.mut.Unlock()
c.cond.Signal()
}
type sender struct {
//---- protected by ncMut ------
nc netconn.NetConn
alreadyClosed bool
ncMut sync.Mutex
tmpBuf []*commandData
epoch uint64
cmdEpoch uint64
lastErr error
ncErrorCond *sync.Cond
epochWaitCond *sync.Cond
//---- end ncMut protection ----
send sendBuffer
recv recvBuffer
}
// ----------------------------------
// Send Buffer
// ----------------------------------
type sendBuffer struct {
buf []*commandData
maxLen int
mut sync.Mutex
cond *sync.Cond
}
func initSendBuffer(b *sendBuffer, bufSizeLog int) {
maxSize := 1 << bufSizeLog
b.buf = make([]*commandData, 0, maxSize)
b.maxLen = maxSize
b.cond = sync.NewCond(&b.mut)
}
func (b *sendBuffer) popAll(output []*commandData) []*commandData {
b.mut.Lock()
output = append(output, b.buf...)
b.buf = clearCmdList(b.buf)
b.mut.Unlock()
b.cond.Broadcast()
return output
}
func (b *sendBuffer) push(cmd *commandData) (isLeader bool) {
b.mut.Lock()
for len(b.buf) >= b.maxLen {
b.cond.Wait()
}
prevLen := len(b.buf)
b.buf = append(b.buf, cmd)
b.mut.Unlock()
return prevLen == 0
}
// ----------------------------------
// Receiving Buffer
// ----------------------------------
type recvBuffer struct {
buf []*commandData
mut sync.Mutex
mask uint64
begin uint64
end uint64
closed bool
sendCond *sync.Cond
recvCond *sync.Cond
}
func initRecvBuffer(b *recvBuffer, sizeLog int) {
b.buf = make([]*commandData, 1<<sizeLog)
b.mask = 1<<sizeLog - 1
b.begin = 0
b.end = 0
b.sendCond = sync.NewCond(&b.mut)
b.recvCond = sync.NewCond(&b.mut)
}
func (b *recvBuffer) push(cmdList []*commandData) (remaining []*commandData, isClosed bool) {
max := uint64(len(b.buf))
for len(cmdList) > 0 {
b.mut.Lock()
// Wait until: begin + max - end > 0
for !b.closed && b.begin+max <= b.end {
b.sendCond.Wait()
}
if b.closed {
b.mut.Unlock()
return cmdList, true
}
n := uint64(len(cmdList))
if b.begin+max < b.end+n {
n = b.begin + max - b.end
}
for i, cmd := range cmdList[:n] {
index := (b.end + uint64(i)) & b.mask
b.buf[index] = cmd
}
b.end += n
b.mut.Unlock()
b.recvCond.Signal()
cmdList = cmdList[n:]
}
return nil, false
}
func (b *recvBuffer) read(cmdList []*commandData) int {
b.mut.Lock()
for !b.closed && b.end == b.begin {
b.recvCond.Wait()
}
n := uint64(len(cmdList))
if b.end-b.begin < n {
n = b.end - b.begin
}
for i := uint64(0); i < n; i++ {
index := (b.begin + i) & b.mask
cmdList[i] = b.buf[index]
}
b.begin += n
b.mut.Unlock()
b.sendCond.Signal()
return int(n)
}
func (b *recvBuffer) closeBuffer() {
b.mut.Lock()
b.closed = true
b.mut.Unlock()
b.sendCond.Signal()
b.recvCond.Signal()
}
func newSender(nc netconn.NetConn, bufSizeLog int) *sender {
s := &sender{
nc: nc,
lastErr: nil,
}
initSendBuffer(&s.send, bufSizeLog)
initRecvBuffer(&s.recv, bufSizeLog+1)
s.ncErrorCond = sync.NewCond(&s.ncMut)
s.epochWaitCond = sync.NewCond(&s.ncMut)
return s
}
func clearCmdList(cmdList []*commandData) []*commandData {
for i := range cmdList {
cmdList[i] = nil
}
return cmdList[:0]
}
func (s *sender) setLastErrorAndClose(err error) error {
s.lastErr = err
s.ncErrorCond.Signal()
if !s.alreadyClosed {
s.alreadyClosed = true
return s.nc.Closer.Close()
}
return nil
}
func (s *sender) writeAndFlush() error {
if s.lastErr != nil {
return s.lastErr
}
for _, cmd := range s.tmpBuf {
_, err := s.nc.Writer.Write(cmd.requestData)
if err != nil {
_ = s.setLastErrorAndClose(err)
return err
}
}
err := s.nc.Writer.Flush()
if err != nil {
_ = s.setLastErrorAndClose(err)
return err
}
return nil
}
func (s *sender) sendToWriter() {
s.ncMut.Lock()
s.tmpBuf = s.send.popAll(s.tmpBuf)
for _, cmd := range s.tmpBuf {
cmd.reader = s.nc.Reader
cmd.epoch = s.cmdEpoch
}
remainingCommands, closed := s.recv.push(s.tmpBuf)
if closed {
for _, cmd := range remainingCommands {
cmd.setCompleted(ErrConnClosed)
}
s.tmpBuf = clearCmdList(s.tmpBuf)
s.ncMut.Unlock()
return
}
// flush to tcp socket
err := s.writeAndFlush()
if err != nil {
for _, cmd := range s.tmpBuf {
cmd.setErrorOnly(err)
}
s.tmpBuf = clearCmdList(s.tmpBuf)
s.ncMut.Unlock()
return
}
s.tmpBuf = clearCmdList(s.tmpBuf)
s.ncMut.Unlock()
}
func (s *sender) publish(cmd *commandData) {
isLeader := s.send.push(cmd)
if !isLeader {
return
}
s.sendToWriter()
}
func (s *sender) readSentCommands(cmdList []*commandData) int {
return s.recv.read(cmdList)
}
func (s *sender) waitForError() {
s.ncMut.Lock()
for s.lastErr == nil {
s.ncErrorCond.Wait()
}
s.ncMut.Unlock()
}
// setNetConnError used inside *sender.go*
func (s *sender) setNetConnError(err error, prevReader io.ReadCloser) {
s.ncMut.Lock()
if s.lastErr != ErrConnClosed {
if s.nc.Reader == prevReader {
_ = s.setLastErrorAndClose(err)
}
}
s.ncMut.Unlock()
}
func (s *sender) forceSetNetConnError(err error) {
s.ncMut.Lock()
if s.lastErr != ErrConnClosed {
_ = s.setLastErrorAndClose(err)
}
s.ncMut.Unlock()
}
// waitForNewEpoch used inside *pipeline.go*
func (s *sender) waitForNewEpoch(waitEpoch uint64) error {
s.ncMut.Lock()
for s.lastErr != ErrConnClosed && s.epoch <= waitEpoch {
s.epochWaitCond.Wait()
}
err := s.lastErr
s.ncMut.Unlock()
return err
}
// increaseEpochAndSetError used inside *conn.go*
func (s *sender) increaseEpochAndSetError(err error) {
s.ncMut.Lock()
if s.lastErr != ErrConnClosed {
s.epoch++
_ = s.setLastErrorAndClose(err)
}
s.ncMut.Unlock()
s.epochWaitCond.Broadcast()
}
func (s *sender) resetNetConn(nc netconn.NetConn) {
s.ncMut.Lock()
if s.lastErr != ErrConnClosed {
s.epoch++
s.cmdEpoch = s.epoch
s.nc = nc
s.alreadyClosed = false
s.lastErr = nil
}
s.ncMut.Unlock()
s.epochWaitCond.Broadcast()
}
func (s *sender) closeNetConn() error {
s.ncMut.Lock()
s.recv.closeBuffer()
err := s.setLastErrorAndClose(ErrConnClosed)
s.ncMut.Unlock()
s.epochWaitCond.Broadcast()
return err
}