-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathcompaction_queue.go
425 lines (378 loc) · 9.35 KB
/
compaction_queue.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
package compactor
import (
"container/heap"
"slices"
"sync"
"sync/atomic"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/pyroscope/pkg/experiment/metastore/compaction"
"github.com/grafana/pyroscope/pkg/util"
)
const defaultBlockBatchSize = 20
type compactionKey struct {
// Order of the fields is not important.
// Can be generalized.
tenant string
shard uint32
level uint32
}
type compactionQueue struct {
config Config
registerer prometheus.Registerer
levels []*blockQueue
}
// blockQueue stages blocks as they are being added. Once a batch of blocks
// within the compaction key reaches a certain size or age, it is pushed to
// the linked list in the arrival order and to the compaction key queue.
//
// This allows to iterate over the blocks in the order of arrival within the
// compaction dimension, while maintaining an ability to remove blocks from the
// queue efficiently.
//
// No pop operation is needed for the block queue: the only way blocks leave
// the queue is through explicit removal. Batch and block iterators provide
// the read access.
type blockQueue struct {
config Config
registerer prometheus.Registerer
staged map[compactionKey]*stagedBlocks
// Batches ordered by arrival.
head, tail *batch
// Priority queue by last update: we need to flush
// incomplete batches once they stop updating.
updates *priorityBlockQueue
}
// stagedBlocks is a queue of blocks sharing the same compaction key.
type stagedBlocks struct {
key compactionKey
// Local queue (blocks sharing this compaction key).
head, tail *batch
// Parent block queue (global).
queue *blockQueue
// Incomplete batch of blocks.
batch *batch
// Map of block IDs to their locations in batches.
refs map[string]blockRef
stats *queueStats
collector *queueStatsCollector
// Parent block queue maintains a priority queue of
// incomplete batches by the last update time.
heapIndex int
updatedAt int64
}
type queueStats struct {
blocks atomic.Int32
batches atomic.Int32
rejected atomic.Int32
missed atomic.Int32
}
// blockRef points to the block in the batch.
type blockRef struct {
batch *batch
index int
}
type blockEntry struct {
id string // Block ID.
index uint64 // Index of the command in the raft log.
}
type batch struct {
flush sync.Once
size uint32
blocks []blockEntry
// Reference to the parent.
staged *stagedBlocks
// Links to the global batch queue items:
// the compaction key of batches may differ.
nextG, prevG *batch
// Links to the local batch queue items:
// batches that share the same compaction key.
next, prev *batch
createdAt int64
}
func newCompactionQueue(config Config, registerer prometheus.Registerer) *compactionQueue {
return &compactionQueue{
config: config,
registerer: registerer,
}
}
func (q *compactionQueue) reset() {
for _, level := range q.levels {
if level != nil {
for _, s := range level.staged {
level.removeStaged(s)
}
}
}
clear(q.levels)
q.levels = q.levels[:0]
}
func (q *compactionQueue) push(e compaction.BlockEntry) bool {
level := q.blockQueue(e.Level)
staged := level.stagedBlocks(compactionKey{
tenant: e.Tenant,
shard: e.Shard,
level: e.Level,
})
staged.updatedAt = e.AppendedAt
pushed := staged.push(blockEntry{
id: e.ID,
index: e.Index,
})
heap.Fix(level.updates, staged.heapIndex)
level.flushOldest(e.AppendedAt)
return pushed
}
func (q *compactionQueue) blockQueue(l uint32) *blockQueue {
s := l + 1 // Levels are 0-based.
if s > uint32(len(q.levels)) {
q.levels = slices.Grow(q.levels, int(s))[:s]
}
level := q.levels[l]
if level == nil {
level = newBlockQueue(q.config, q.registerer)
q.levels[l] = level
}
return level
}
func newBlockQueue(config Config, registerer prometheus.Registerer) *blockQueue {
return &blockQueue{
config: config,
registerer: registerer,
staged: make(map[compactionKey]*stagedBlocks),
updates: new(priorityBlockQueue),
}
}
func (q *blockQueue) stagedBlocks(k compactionKey) *stagedBlocks {
staged, ok := q.staged[k]
if !ok {
staged = &stagedBlocks{
queue: q,
key: k,
refs: make(map[string]blockRef),
stats: new(queueStats),
}
staged.resetBatch()
q.staged[k] = staged
heap.Push(q.updates, staged)
if q.registerer != nil {
staged.collector = newQueueStatsCollector(staged)
util.RegisterOrGet(q.registerer, staged.collector)
}
}
return staged
}
func (q *blockQueue) removeStaged(s *stagedBlocks) {
if s.collector != nil {
q.registerer.Unregister(s.collector)
}
delete(q.staged, s.key)
if s.heapIndex < 0 {
// We usually end up here since s has already been evicted
// from the priority queue via Pop due to its age.
return
}
if s.heapIndex >= q.updates.Len() {
// Should not be possible.
return
}
heap.Remove(q.updates, s.heapIndex)
}
func (s *stagedBlocks) push(block blockEntry) bool {
if _, found := s.refs[block.id]; found {
s.stats.rejected.Add(1)
return false
}
s.refs[block.id] = blockRef{batch: s.batch, index: len(s.batch.blocks)}
s.batch.blocks = append(s.batch.blocks, block)
if s.batch.size == 0 {
s.batch.createdAt = s.updatedAt
}
s.batch.size++
s.stats.blocks.Add(1)
if !s.queue.config.exceedsMaxSize(s.batch) &&
!s.queue.config.exceedsMaxAge(s.batch, s.updatedAt) {
// The batch is still valid.
return true
}
return s.flush()
}
func (s *stagedBlocks) flush() (flushed bool) {
s.batch.flush.Do(func() {
s.queue.pushBatch(s.batch)
flushed = true
})
s.resetBatch()
return flushed
}
func (s *stagedBlocks) resetBatch() {
s.batch = &batch{
blocks: make([]blockEntry, 0, defaultBlockBatchSize),
staged: s,
}
}
var zeroBlockEntry blockEntry
func (s *stagedBlocks) delete(block string) blockEntry {
ref, found := s.refs[block]
if !found {
s.stats.missed.Add(1)
return zeroBlockEntry
}
// We can't change the order of the blocks in the batch,
// because that would require updating all the block locations.
e := ref.batch.blocks[ref.index]
ref.batch.blocks[ref.index] = zeroBlockEntry
ref.batch.size--
s.stats.blocks.Add(-1)
if ref.batch.size == 0 {
s.queue.removeBatch(ref.batch)
}
delete(s.refs, block)
if len(s.refs) == 0 {
s.queue.removeStaged(s)
}
return e
}
func (q *blockQueue) pushBatch(b *batch) {
if q.tail != nil {
q.tail.nextG = b
b.prevG = q.tail
} else {
q.head = b
}
q.tail = b
// Same for the queue of batches
// with matching compaction key.
if b.staged.tail != nil {
b.staged.tail.next = b
b.prev = b.staged.tail
} else {
b.staged.head = b
}
b.staged.tail = b
b.staged.stats.batches.Add(1)
}
func (q *blockQueue) removeBatch(b *batch) {
if b.prevG != nil {
b.prevG.nextG = b.nextG
} else {
// This is the head.
q.head = b.nextG
}
if b.nextG != nil {
b.nextG.prevG = b.prevG
} else {
// This is the tail.
q.tail = b.prevG
}
b.nextG = nil
b.prevG = nil
// Same for the queue of batches
// with matching compaction key.
if b.prev != nil {
b.prev.next = b.next
} else {
// This is the head.
b.staged.head = b.next
}
if b.next != nil {
b.next.prev = b.prev
} else {
// This is the tail.
b.staged.tail = b.next
}
b.next = nil
b.prev = nil
b.staged.stats.batches.Add(-1)
}
func (q *blockQueue) flushOldest(now int64) {
if q.updates.Len() == 0 {
// Should not be possible.
return
}
oldest := (*q.updates)[0]
if !q.config.exceedsMaxAge(oldest.batch, now) {
return
}
heap.Pop(q.updates)
oldest.flush()
}
type priorityBlockQueue []*stagedBlocks
func (pq priorityBlockQueue) Len() int { return len(pq) }
func (pq priorityBlockQueue) Less(i, j int) bool {
return pq[i].updatedAt < pq[j].updatedAt
}
func (pq priorityBlockQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].heapIndex = i
pq[j].heapIndex = j
}
func (pq *priorityBlockQueue) Push(x interface{}) {
n := len(*pq)
staged := x.(*stagedBlocks)
staged.heapIndex = n
*pq = append(*pq, staged)
}
func (pq *priorityBlockQueue) Pop() interface{} {
old := *pq
n := len(old)
staged := old[n-1]
old[n-1] = nil
staged.heapIndex = -1
*pq = old[0 : n-1]
return staged
}
func newBatchIter(q *blockQueue) *batchIter { return &batchIter{batch: q.head} }
// batchIter iterates over the batches in the queue, in the order of arrival.
type batchIter struct{ batch *batch }
func (i *batchIter) next() (*batch, bool) {
if i.batch == nil {
return nil, false
}
b := i.batch
i.batch = i.batch.nextG
return b, b != nil
}
func (i *batchIter) reset(b *batch) { i.batch = b }
// batchIter iterates over the batches in the queue, in the order of arrival
// within the compaction key. It's guaranteed that returned blocks are unique
// across all batched.
type blockIter struct {
visited map[string]struct{}
batch *batch
i int
}
func newBlockIter() *blockIter {
visited := make(map[string]struct{}, 64)
visited[zeroBlockEntry.id] = struct{}{}
return &blockIter{visited: visited}
}
func (it *blockIter) setBatch(b *batch) {
it.batch = b
it.i = 0
}
func (it *blockIter) more() bool {
if it.batch == nil {
return false
}
return it.i < len(it.batch.blocks)
}
func (it *blockIter) peek() (string, bool) {
for it.batch != nil {
if it.i >= len(it.batch.blocks) {
it.setBatch(it.batch.next)
continue
}
entry := it.batch.blocks[it.i]
if _, visited := it.visited[entry.id]; visited {
it.i++
continue
}
return entry.id, true
}
return "", false
}
func (it *blockIter) advance() {
entry := it.batch.blocks[it.i]
it.visited[entry.id] = struct{}{}
it.i++
}