-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathbuffer_mem.go
190 lines (150 loc) · 3.42 KB
/
buffer_mem.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
package models
import (
"sync"
"github.com/influxdata/telegraf"
)
// MemoryBuffer stores metrics in a circular buffer.
type MemoryBuffer struct {
sync.Mutex
BufferStats
buf []telegraf.Metric
first int // index of the first/oldest metric
last int // one after the index of the last/newest metric
size int // number of metrics currently in the buffer
cap int // the capacity of the buffer
batchFirst int // index of the first metric in the batch
batchSize int // number of metrics currently in the batch
}
func NewMemoryBuffer(capacity int, stats BufferStats) (*MemoryBuffer, error) {
return &MemoryBuffer{
BufferStats: stats,
buf: make([]telegraf.Metric, capacity),
cap: capacity,
}, nil
}
func (b *MemoryBuffer) Len() int {
b.Lock()
defer b.Unlock()
return b.length()
}
func (b *MemoryBuffer) Add(metrics ...telegraf.Metric) int {
b.Lock()
defer b.Unlock()
dropped := 0
for i := range metrics {
if n := b.addMetric(metrics[i]); n != 0 {
dropped += n
}
}
b.BufferSize.Set(int64(b.length()))
return dropped
}
func (b *MemoryBuffer) Batch(batchSize int) []telegraf.Metric {
b.Lock()
defer b.Unlock()
outLen := min(b.size, batchSize)
out := make([]telegraf.Metric, outLen)
if outLen == 0 {
return out
}
b.batchFirst = b.first
b.batchSize = outLen
batchIndex := b.batchFirst
for i := range out {
out[i] = b.buf[batchIndex]
b.buf[batchIndex] = nil
batchIndex = b.next(batchIndex)
}
b.first = b.nextby(b.first, b.batchSize)
b.size -= outLen
return out
}
func (b *MemoryBuffer) Accept(batch []telegraf.Metric) {
b.Lock()
defer b.Unlock()
for _, m := range batch {
b.metricWritten(m)
}
b.resetBatch()
b.BufferSize.Set(int64(b.length()))
}
func (b *MemoryBuffer) Reject(batch []telegraf.Metric) {
b.Lock()
defer b.Unlock()
if len(batch) == 0 {
return
}
free := b.cap - b.size
restore := min(len(batch), free)
skip := len(batch) - restore
b.first = b.prevby(b.first, restore)
b.size = min(b.size+restore, b.cap)
re := b.first
// Copy metrics from the batch back into the buffer
for i := range batch {
if i < skip {
b.metricDropped(batch[i])
} else {
b.buf[re] = batch[i]
re = b.next(re)
}
}
b.resetBatch()
b.BufferSize.Set(int64(b.length()))
}
func (b *MemoryBuffer) Close() error {
return nil
}
func (b *MemoryBuffer) Stats() BufferStats {
return b.BufferStats
}
func (b *MemoryBuffer) length() int {
return min(b.size+b.batchSize, b.cap)
}
func (b *MemoryBuffer) addMetric(m telegraf.Metric) int {
dropped := 0
// Check if Buffer is full
if b.size == b.cap {
b.metricDropped(b.buf[b.last])
dropped++
if b.batchSize > 0 {
b.batchSize--
b.batchFirst = b.next(b.batchFirst)
}
}
b.metricAdded()
b.buf[b.last] = m
b.last = b.next(b.last)
if b.size == b.cap {
b.first = b.next(b.first)
}
b.size = min(b.size+1, b.cap)
return dropped
}
// next returns the next index with wrapping.
func (b *MemoryBuffer) next(index int) int {
index++
if index == b.cap {
return 0
}
return index
}
// nextby returns the index that is count newer with wrapping.
func (b *MemoryBuffer) nextby(index, count int) int {
index += count
index %= b.cap
return index
}
// prevby returns the index that is count older with wrapping.
func (b *MemoryBuffer) prevby(index, count int) int {
index -= count
for index < 0 {
index += b.cap
}
index %= b.cap
return index
}
func (b *MemoryBuffer) resetBatch() {
b.batchFirst = 0
b.batchSize = 0
}