-
Notifications
You must be signed in to change notification settings - Fork 44
/
buffer.go
698 lines (622 loc) · 17.7 KB
/
buffer.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package buffer
import (
"container/list"
"errors"
"io"
"sync"
"time"
"github.com/apache/skywalking-rover/pkg/tools/enums"
"github.com/apache/skywalking-rover/pkg/tools/host"
)
var (
ErrNotComplete = errors.New("socket: not complete event")
)
type SocketDataBuffer interface {
// GenerateConnectionID for identity the buffer belong which connection
GenerateConnectionID() string
// BufferData of the buffer
BufferData() []byte
// TotalSize of socket data, the data may exceed the size of the BufferData()
TotalSize() uint64
// Direction of the data, send or receive
Direction() enums.SocketDataDirection
// BufferStartPosition the buffer start index
BufferStartPosition() int
// BufferLen the buffer data length
BufferLen() int
// DataID data id of the buffer
DataID() uint64
// DataSequence the data sequence under same data id
DataSequence() int
// IsStart this buffer is start of the same data id
IsStart() bool
// IsFinished this buffer is finish of the same data id
IsFinished() bool
// HaveReduceDataAfterChunk check have reduced data after current buffer
HaveReduceDataAfterChunk() bool
// StartTime the data start timestamp
StartTime() uint64
// EndTime the data end timestamp
EndTime() uint64
}
type SocketDataDetail interface {
// DataID data id of the buffer
DataID() uint64
}
type Buffer struct {
dataEvents *list.List
detailEvents *list.List
validated bool // the events list is validated or not
eventLocker sync.RWMutex
head *Position
current *Position
// record the latest expired data id in connection for expire the older socket detail
// because the older socket detail may not be received in buffer
latestExpiredDataID uint64
}
type SocketDataEventLimited struct {
SocketDataBuffer
From int
Size int
}
func (s *SocketDataEventLimited) BufferData() []byte {
return s.SocketDataBuffer.BufferData()[s.From:s.Size]
}
func (s *SocketDataEventLimited) BufferLen() int {
return s.Size - s.From
}
func (s *SocketDataEventLimited) BufferStartPosition() int {
return s.From
}
type Position struct {
// element of the event list
element *list.Element
// bufIndex the buffer index of the element
bufIndex int
}
func (p *Position) Clone() *Position {
return &Position{element: p.element, bufIndex: p.bufIndex}
}
func (p *Position) DataID() uint64 {
return p.element.Value.(SocketDataBuffer).DataID()
}
func (p *Position) Seq() int {
return p.element.Value.(SocketDataBuffer).DataSequence()
}
func NewBuffer() *Buffer {
return &Buffer{
dataEvents: list.New(),
detailEvents: list.New(),
validated: false,
}
}
func (r *Buffer) FindFirstDataBuffer(dataID uint64) SocketDataBuffer {
for e := r.dataEvents.Front(); e != nil; e = e.Next() {
cur := e.Value.(SocketDataBuffer)
if cur.DataID() == dataID {
return cur
}
}
return nil
}
func (r *Buffer) Position() *Position {
return r.current.Clone()
}
func (r *Buffer) Clean() {
r.eventLocker.Lock()
defer r.eventLocker.Unlock()
r.dataEvents = list.New()
r.detailEvents = list.New()
r.head = nil
r.current = nil
}
func (r *Buffer) Slice(validated bool, start, end *Position) *Buffer {
dataEvents := list.New()
detailEvents := list.New()
var firstDetailElement *list.Element
for nextElement := start.element; nextElement != end.element; nextElement = nextElement.Next() {
if nextElement == nil {
break
}
// found first matches detail event
if detailEvents.Len() == 0 || firstDetailElement == nil {
for e := r.detailEvents.Front(); e != nil; e = e.Next() {
if e.Value.(SocketDataDetail).DataID() >= nextElement.Value.(SocketDataBuffer).DataID() {
detailEvents.PushBack(e.Value)
firstDetailElement = e
break
}
}
}
dataEvents.PushBack(nextElement.Value)
}
lastBuffer := end.element.Value.(SocketDataBuffer)
dataEvents.PushBack(&SocketDataEventLimited{SocketDataBuffer: lastBuffer, Size: end.bufIndex})
// if the first detail element been found, append the details until the last buffer data id
if firstDetailElement == nil {
for e := r.detailEvents.Front(); e != nil; e = e.Next() {
if e.Value.(SocketDataDetail).DataID() == lastBuffer.DataID() {
detailEvents.PushBack(e.Value)
break
}
}
} else if firstDetailElement != nil && firstDetailElement.Value.(SocketDataDetail).DataID() != lastBuffer.DataID() {
for tmp := firstDetailElement.Next(); tmp != nil; tmp = tmp.Next() {
if tmp.Value.(SocketDataDetail).DataID() > lastBuffer.DataID() {
break
}
detailEvents.PushBack(tmp.Value)
}
}
return &Buffer{
dataEvents: dataEvents,
detailEvents: detailEvents,
validated: validated,
head: &Position{element: dataEvents.Front(), bufIndex: start.bufIndex},
current: &Position{element: dataEvents.Front(), bufIndex: start.bufIndex},
}
}
func (r *Buffer) Len() int {
if r == nil || r.head == nil {
return 0
}
var result int
var startIndex = r.head.bufIndex
for e := r.head.element; e != nil; e = e.Next() {
result += r.head.element.Value.(SocketDataBuffer).BufferLen() - startIndex
startIndex = 0
}
return result
}
func (r *Buffer) Details() *list.List {
return r.detailEvents
}
func (r *Buffer) DataSize() int64 {
if r == nil {
return 0
}
var result int64
var headPosition = r.head
if headPosition == nil {
if !r.PrepareForReading() {
return 0
}
defer func() {
r.ResetForLoopReading()
}()
headPosition = r.head
}
isFirst := true
for e := headPosition.element; e != nil; e = e.Next() {
if isFirst {
result += int64(e.Value.(SocketDataBuffer).BufferLen() - headPosition.bufIndex)
isFirst = false
} else {
result += int64(e.Value.(SocketDataBuffer).BufferLen())
}
}
return result
}
func (r *Buffer) FirstSocketBuffer() SocketDataBuffer {
if r.dataEvents == nil || r.dataEvents.Len() == 0 {
return nil
}
return r.dataEvents.Front().Value.(SocketDataBuffer)
}
func (r *Buffer) LastSocketBuffer() SocketDataBuffer {
if r.dataEvents.Len() == 0 {
return nil
}
return r.dataEvents.Back().Value.(SocketDataBuffer)
}
// DetectNotSendingLastPosition detect the buffer contains not sending data: the BPF limited socket data count
func (r *Buffer) DetectNotSendingLastPosition() *Position {
if r.dataEvents.Len() == 0 {
return nil
}
for e := r.dataEvents.Front(); e != nil; e = e.Next() {
buf := e.Value.(SocketDataBuffer)
// the buffer is sent finished but still have reduced data not send
if buf.IsFinished() && buf.HaveReduceDataAfterChunk() {
return &Position{element: e, bufIndex: buf.BufferLen()}
}
}
return nil
}
func CombineSlices(validated bool, buffers ...*Buffer) *Buffer {
if len(buffers) == 0 {
return nil
}
if len(buffers) == 1 {
return buffers[0]
}
dataEvents := list.New()
detailEvents := list.New()
for _, b := range buffers {
if b == nil {
continue
}
if b.head.bufIndex > 0 {
headBuffer := b.dataEvents.Front().Value.(SocketDataBuffer)
dataEvents.PushBack(&SocketDataEventLimited{SocketDataBuffer: headBuffer,
From: b.head.bufIndex, Size: headBuffer.BufferLen()})
for next := b.dataEvents.Front().Next(); next != nil; next = next.Next() {
dataEvents.PushBack(next.Value)
}
} else {
dataEvents.PushBackList(b.dataEvents)
}
detailEvents.PushBackList(b.detailEvents)
}
return &Buffer{
dataEvents: dataEvents,
detailEvents: detailEvents,
validated: validated,
head: &Position{element: dataEvents.Front(), bufIndex: 0},
current: &Position{element: dataEvents.Front(), bufIndex: 0},
}
}
func (r *Buffer) Peek(p []byte) (n int, err error) {
// save the index temporary
tmpPosition := r.current.Clone()
// restore the index
defer func() {
r.current = tmpPosition
}()
readIndex := 0
for readIndex < len(p) {
count, err := r.Read(p[readIndex:])
if err != nil {
return 0, err
}
readIndex += count
}
return readIndex, nil
}
func (r *Buffer) OffsetPosition(offset int) *Position {
var nextElement func(e *list.Element) *list.Element
if offset == 0 {
return r.current.Clone()
} else if offset > 0 {
nextElement = func(e *list.Element) *list.Element {
return e.Next()
}
} else {
nextElement = func(e *list.Element) *list.Element {
return e.Prev()
}
}
var curEle = r.current.element
var curIndex = r.current.bufIndex
for ; curEle != nil; curEle = nextElement(curEle) {
nextOffset := curIndex + offset
bufferLen := curEle.Value.(SocketDataBuffer).BufferLen()
if nextOffset >= 0 && nextOffset < bufferLen {
curIndex += offset
break
}
if offset > 0 {
offset -= bufferLen - curIndex
curIndex = 0
} else {
offset += curIndex
next := nextElement(curEle)
if next == nil {
curEle = next
break
}
curIndex = curEle.Value.(SocketDataBuffer).BufferLen()
}
}
if curEle == nil {
return nil
}
return &Position{element: curEle, bufIndex: curIndex}
}
func (r *Buffer) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if r.current == nil || r.current.element == nil {
return 0, io.EOF
}
element, n := r.ReadFromCurrent(p)
if n > 0 {
return n, nil
}
curEvent := element.Value.(SocketDataBuffer)
next := r.nextElement(element)
if next == nil {
return 0, io.EOF
}
nextEvent := next.Value.(SocketDataBuffer)
var shouldRead = false
if r.validated {
shouldRead = true
// same data id and sequence orders
} else if (curEvent.DataID() == nextEvent.DataID() && curEvent.DataSequence()+1 == nextEvent.DataSequence()) ||
// cur event is finished and next event is start
(nextEvent.IsStart() && curEvent.IsFinished()) ||
// same data id and sequence but have difference buffer index
(curEvent.DataID() == nextEvent.DataID() && curEvent.DataSequence() == nextEvent.DataSequence() &&
r.current.bufIndex <= nextEvent.BufferStartPosition()) {
shouldRead = true
}
if !shouldRead {
return 0, ErrNotComplete
}
return r.read0(next, nextEvent, p)
}
func (r *Buffer) ReadFromCurrent(p []byte) (element *list.Element, n int) {
element = r.current.element
curEvent := element.Value.(SocketDataBuffer)
residueSize := curEvent.BufferLen() - r.current.bufIndex
if residueSize > 0 {
readLen := len(p)
if residueSize < readLen {
readLen = residueSize
}
n = copy(p, curEvent.BufferData()[r.current.bufIndex:r.current.bufIndex+readLen])
r.current.bufIndex += n
return element, n
}
return element, 0
}
func (r *Buffer) ReadUntilBufferFull(data []byte) error {
reduceLen := len(data)
currentIndex := 0
for reduceLen > 0 {
readCount, err := r.Read(data[:reduceLen])
if err != nil {
return err
}
reduceLen -= readCount
currentIndex += readCount
}
return nil
}
func (r *Buffer) Merge(other *Buffer) {
if other == nil {
return
}
for e := other.dataEvents.Front(); e != nil; e = e.Next() {
if v, ok := e.Value.(SocketDataBuffer); ok && v != nil {
r.AppendDataEvent(v)
}
}
for e := other.detailEvents.Front(); e != nil; e = e.Next() {
if v, ok := e.Value.(SocketDataDetail); ok && v != nil {
r.AppendDetailEvent(v)
}
}
}
func (r *Buffer) read0(currentElement *list.Element, currentBuffer SocketDataBuffer, p []byte) (n int, err error) {
readLen := len(p)
if currentBuffer.BufferLen() < readLen {
readLen = currentBuffer.BufferLen()
}
copy(p, currentBuffer.BufferData()[:readLen])
r.current.element = currentElement
r.current.bufIndex = readLen
return readLen, nil
}
// IsCurrentPacketReadFinished means to validate the current reading package is reading finished
func (r *Buffer) IsCurrentPacketReadFinished() bool {
return r.current.bufIndex == r.current.element.Value.(SocketDataBuffer).BufferLen()
}
func (r *Buffer) ResetForLoopReading() {
r.head = nil
r.current = nil
}
func (r *Buffer) PrepareForReading() bool {
if r.dataEvents.Len() == 0 {
return false
}
if r.head == nil || r.head.element == nil {
// read in the first element
r.eventLocker.RLock()
defer r.eventLocker.RUnlock()
r.head = &Position{element: r.dataEvents.Front(), bufIndex: 0}
r.current = r.head.Clone()
} else {
// make sure we can read from head
r.current = r.head.Clone()
}
return true
}
// nolint
func (r *Buffer) RemoveReadElements() bool {
r.eventLocker.Lock()
defer r.eventLocker.Unlock()
// delete until the last data id
if r.head.element != nil && r.current.element != nil {
firstDataID := r.head.element.Value.(SocketDataBuffer).DataID()
currentBuffer := r.current.element.Value.(SocketDataBuffer)
lastDataID := currentBuffer.DataID()
startDelete := false
for e := r.detailEvents.Front(); e != nil; {
event := e.Value.(SocketDataDetail)
if firstDataID == lastDataID && currentBuffer.BufferLen() != r.current.bufIndex {
// current buffer is not finished, so the detail cannot be deleted
break
}
if !startDelete && event.DataID() >= firstDataID && event.DataID() <= lastDataID {
startDelete = true
} else if startDelete && event.DataID() > lastDataID {
// out of the data id, just break
break
}
if startDelete {
tmp := e.Next()
r.detailEvents.Remove(e)
e = tmp
} else {
e = e.Next()
}
}
}
// delete until to current position
next := r.head.element
stillCurrent := true
for ; next != nil && next != r.current.element; next = r.removeElement0(next) {
stillCurrent = false
}
// not enough data, then return
if !stillCurrent && next == nil {
return true
}
if next != nil && next.Value.(SocketDataBuffer).BufferLen() == r.current.bufIndex { // all buffer finished, then delete it
// the last event already read finished, then delete it
r.head.element = r.removeElement0(next)
r.head.bufIndex = 0
} else {
if !stillCurrent {
r.head.element = next
}
// still have reduced buffer, then keep reading from current index in next loop
r.head.bufIndex = r.current.bufIndex
}
return false
}
// SkipCurrentElement skip current element in reader, if return true means have read finished
func (r *Buffer) SkipCurrentElement() bool {
r.head.element = r.nextElement(r.current.element)
r.current.bufIndex = 0
return r.head.element == nil
}
func (r *Buffer) removeElement0(element *list.Element) *list.Element {
if element == nil {
return nil
}
result := element.Next()
r.dataEvents.Remove(element)
return result
}
func (r *Buffer) AppendDetailEvent(event SocketDataDetail) {
r.eventLocker.Lock()
defer r.eventLocker.Unlock()
if r.detailEvents.Len() == 0 {
r.detailEvents.PushFront(event)
return
}
if r.detailEvents.Back().Value.(SocketDataDetail).DataID() < event.DataID() {
r.detailEvents.PushBack(event)
return
}
beenAdded := false
for element := r.detailEvents.Front(); element != nil; element = element.Next() {
existEvent := element.Value.(SocketDataDetail)
if existEvent.DataID() > event.DataID() {
// data id needs order
beenAdded = true
}
if beenAdded {
r.detailEvents.InsertBefore(event, element)
break
}
}
if !beenAdded {
r.detailEvents.PushBack(event)
}
}
// AppendDataEvent insert the event to the event list following the order
func (r *Buffer) AppendDataEvent(event SocketDataBuffer) {
r.eventLocker.Lock()
defer r.eventLocker.Unlock()
if r.dataEvents.Len() == 0 {
r.dataEvents.PushFront(event)
return
}
if r.dataEvents.Back().Value.(SocketDataBuffer).DataID() < event.DataID() {
r.dataEvents.PushBack(event)
return
}
beenAdded := false
for element := r.dataEvents.Front(); element != nil; element = element.Next() {
existEvent := element.Value.(SocketDataBuffer)
if existEvent.DataID() > event.DataID() {
// data id needs order
beenAdded = true
} else if existEvent.DataID() == event.DataID() && existEvent.DataSequence() > event.DataSequence() {
// following the sequence order
beenAdded = true
}
if beenAdded {
r.dataEvents.InsertBefore(event, element)
break
}
}
if !beenAdded {
r.dataEvents.PushBack(event)
}
}
func (r *Buffer) DeleteExpireEvents(expireDuration time.Duration) int {
r.eventLocker.Lock()
defer r.eventLocker.Unlock()
expireTime := time.Now().Add(-expireDuration)
// data event queue
count := r.deleteEventsWithJudgement(r.dataEvents, func(element *list.Element) bool {
buffer := element.Value.(SocketDataBuffer)
startTime := host.Time(buffer.StartTime())
if expireTime.After(startTime) {
r.latestExpiredDataID = buffer.DataID()
return true
}
return false
})
// detail event queue
count += r.deleteEventsWithJudgement(r.detailEvents, func(element *list.Element) bool {
return r.latestExpiredDataID > 0 && element.Value.(SocketDataDetail).DataID() <= r.latestExpiredDataID
})
return count
}
func (r *Buffer) DataLength() int {
if r.dataEvents == nil {
return 0
}
return r.dataEvents.Len()
}
func (r *Buffer) DetailLength() int {
if r.detailEvents == nil {
return 0
}
return r.detailEvents.Len()
}
func (r *Buffer) deleteEventsWithJudgement(l *list.List, checker func(element *list.Element) bool) int {
count := 0
for e := l.Front(); e != nil; {
if checker(e) {
count++
cur := e
e = e.Next()
l.Remove(cur)
} else {
break
}
}
return count
}
func (r *Buffer) nextElement(e *list.Element) *list.Element {
if e == nil {
return nil
}
r.eventLocker.RLock()
defer r.eventLocker.RUnlock()
return e.Next()
}