-
Notifications
You must be signed in to change notification settings - Fork 84
/
memory_partition.go
282 lines (252 loc) · 6.8 KB
/
memory_partition.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
package tstorage
import (
"fmt"
"sort"
"sync"
"sync/atomic"
"time"
)
// A memoryPartition implements a partition to store data points on heap.
// It offers a goroutine safe capabilities.
type memoryPartition struct {
// The number of data points
numPoints int64
// minT is immutable.
minT int64
maxT int64
// A hash map from metric name to memoryMetric.
metrics sync.Map
// Write ahead log.
wal wal
// The timestamp range of partitions after which they get persisted
partitionDuration int64
timestampPrecision TimestampPrecision
once sync.Once
}
func newMemoryPartition(wal wal, partitionDuration time.Duration, precision TimestampPrecision) partition {
if wal == nil {
wal = &nopWAL{}
}
var d int64
switch precision {
case Nanoseconds:
d = partitionDuration.Nanoseconds()
case Microseconds:
d = partitionDuration.Microseconds()
case Milliseconds:
d = partitionDuration.Milliseconds()
case Seconds:
d = int64(partitionDuration.Seconds())
default:
d = partitionDuration.Nanoseconds()
}
return &memoryPartition{
partitionDuration: d,
wal: wal,
timestampPrecision: precision,
}
}
// insertRows inserts the given rows to partition.
func (m *memoryPartition) insertRows(rows []Row) ([]Row, error) {
if len(rows) == 0 {
return nil, fmt.Errorf("no rows given")
}
// FIXME: Just emitting log is enough
err := m.wal.append(operationInsert, rows)
if err != nil {
return nil, fmt.Errorf("failed to write to WAL: %w", err)
}
// Set min timestamp at only first.
m.once.Do(func() {
min := rows[0].Timestamp
for i := range rows {
row := rows[i]
if row.Timestamp < min {
min = row.Timestamp
}
}
atomic.StoreInt64(&m.minT, min)
})
outdatedRows := make([]Row, 0)
maxTimestamp := rows[0].Timestamp
var rowsNum int64
for i := range rows {
row := rows[i]
if row.Timestamp < m.minTimestamp() {
outdatedRows = append(outdatedRows, row)
continue
}
if row.Timestamp == 0 {
row.Timestamp = toUnix(time.Now(), m.timestampPrecision)
}
if row.Timestamp > maxTimestamp {
maxTimestamp = row.Timestamp
}
name := marshalMetricName(row.Metric, row.Labels)
mt := m.getMetric(name)
mt.insertPoint(&row.DataPoint)
rowsNum++
}
atomic.AddInt64(&m.numPoints, rowsNum)
// Make max timestamp up-to-date.
if atomic.LoadInt64(&m.maxT) < maxTimestamp {
atomic.SwapInt64(&m.maxT, maxTimestamp)
}
return outdatedRows, nil
}
func toUnix(t time.Time, precision TimestampPrecision) int64 {
switch precision {
case Nanoseconds:
return t.UnixNano()
case Microseconds:
return t.UnixNano() / 1e3
case Milliseconds:
return t.UnixNano() / 1e6
case Seconds:
return t.Unix()
default:
return t.UnixNano()
}
}
func (m *memoryPartition) selectDataPoints(metric string, labels []Label, start, end int64) ([]*DataPoint, error) {
name := marshalMetricName(metric, labels)
mt := m.getMetric(name)
return mt.selectPoints(start, end), nil
}
// getMetric gives back the reference to the metrics list whose name is the given one.
// If none, it creates a new one.
func (m *memoryPartition) getMetric(name string) *memoryMetric {
value, ok := m.metrics.Load(name)
if !ok {
value = &memoryMetric{
name: name,
points: make([]*DataPoint, 0, 1000),
outOfOrderPoints: make([]*DataPoint, 0),
}
m.metrics.Store(name, value)
}
return value.(*memoryMetric)
}
func (m *memoryPartition) minTimestamp() int64 {
return atomic.LoadInt64(&m.minT)
}
func (m *memoryPartition) maxTimestamp() int64 {
return atomic.LoadInt64(&m.maxT)
}
func (m *memoryPartition) size() int {
return int(atomic.LoadInt64(&m.numPoints))
}
func (m *memoryPartition) active() bool {
return m.maxTimestamp()-m.minTimestamp()+1 < m.partitionDuration
}
func (m *memoryPartition) clean() error {
// What all data managed by memoryPartition is on heap that is automatically removed by GC.
// So do nothing.
return nil
}
func (m *memoryPartition) expired() bool {
return false
}
// memoryMetric has a list of ordered data points that belong to the memoryMetric
type memoryMetric struct {
name string
size int64
minTimestamp int64
maxTimestamp int64
// points must kept in order
points []*DataPoint
outOfOrderPoints []*DataPoint
mu sync.RWMutex
}
func (m *memoryMetric) insertPoint(point *DataPoint) {
size := atomic.LoadInt64(&m.size)
// TODO: Consider to stop using mutex every time.
// Instead, fix the capacity of points slice, kind of like:
/*
m.points := make([]*DataPoint, 1000)
for i := 0; i < 1000; i++ {
m.points[i] = point
}
*/
m.mu.Lock()
defer m.mu.Unlock()
// First insertion
if size == 0 {
m.points = append(m.points, point)
atomic.StoreInt64(&m.minTimestamp, point.Timestamp)
atomic.StoreInt64(&m.maxTimestamp, point.Timestamp)
atomic.AddInt64(&m.size, 1)
return
}
// Insert point in order
if m.points[size-1].Timestamp < point.Timestamp {
m.points = append(m.points, point)
atomic.StoreInt64(&m.maxTimestamp, point.Timestamp)
atomic.AddInt64(&m.size, 1)
return
}
m.outOfOrderPoints = append(m.outOfOrderPoints, point)
}
// selectPoints returns a new slice by re-slicing with [startIdx:endIdx].
func (m *memoryMetric) selectPoints(start, end int64) []*DataPoint {
size := atomic.LoadInt64(&m.size)
minTimestamp := atomic.LoadInt64(&m.minTimestamp)
maxTimestamp := atomic.LoadInt64(&m.maxTimestamp)
var startIdx, endIdx int
if end <= minTimestamp {
return []*DataPoint{}
}
m.mu.RLock()
defer m.mu.RUnlock()
if start <= minTimestamp {
startIdx = 0
} else {
// Use binary search because points are in-order.
startIdx = sort.Search(int(size), func(i int) bool {
return m.points[i].Timestamp >= start
})
}
if end > maxTimestamp {
endIdx = int(size)
} else {
// Use binary search because points are in-order.
endIdx = sort.Search(int(size), func(i int) bool {
return m.points[i].Timestamp >= end
})
}
return m.points[startIdx:endIdx]
}
// encodeAllPoints uses the given seriesEncoder to encode all metric data points in order by timestamp,
// including outOfOrderPoints.
func (m *memoryMetric) encodeAllPoints(encoder seriesEncoder) error {
sort.Slice(m.outOfOrderPoints, func(i, j int) bool {
return m.outOfOrderPoints[i].Timestamp < m.outOfOrderPoints[j].Timestamp
})
var oi, pi int
for oi < len(m.outOfOrderPoints) && pi < len(m.points) {
if m.outOfOrderPoints[oi].Timestamp < m.points[pi].Timestamp {
if err := encoder.encodePoint(m.outOfOrderPoints[oi]); err != nil {
return err
}
oi++
} else {
if err := encoder.encodePoint(m.points[pi]); err != nil {
return err
}
pi++
}
}
for oi < len(m.outOfOrderPoints) {
if err := encoder.encodePoint(m.outOfOrderPoints[oi]); err != nil {
return err
}
oi++
}
for pi < len(m.points) {
if err := encoder.encodePoint(m.points[pi]); err != nil {
return err
}
pi++
}
return nil
}