forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compactor.go
388 lines (328 loc) · 12 KB
/
compactor.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
package tsdb
import (
"context"
"fmt"
"math"
"os"
"sync"
"time"
"unsafe"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/concurrency"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/storage/chunk"
"github.com/grafana/loki/pkg/storage/config"
"github.com/grafana/loki/pkg/storage/stores/indexshipper/compactor"
"github.com/grafana/loki/pkg/storage/stores/indexshipper/compactor/retention"
index_shipper "github.com/grafana/loki/pkg/storage/stores/indexshipper/index"
"github.com/grafana/loki/pkg/storage/stores/tsdb/index"
)
const readDBsConcurrency = 50
type indexProcessor struct{}
func NewIndexCompactor() compactor.IndexCompactor {
return indexProcessor{}
}
func (i indexProcessor) NewTableCompactor(ctx context.Context, commonIndexSet compactor.IndexSet, existingUserIndexSet map[string]compactor.IndexSet, userIndexSetFactoryFunc compactor.MakeEmptyUserIndexSetFunc, periodConfig config.PeriodConfig) compactor.TableCompactor {
return newTableCompactor(ctx, commonIndexSet, existingUserIndexSet, userIndexSetFactoryFunc, periodConfig)
}
func (i indexProcessor) OpenCompactedIndexFile(ctx context.Context, path, tableName, userID, workingDir string, periodConfig config.PeriodConfig, logger log.Logger) (compactor.CompactedIndex, error) {
indexFile, err := OpenShippableTSDB(path)
if err != nil {
return nil, err
}
defer func() {
if err := indexFile.Close(); err != nil {
level.Error(logger).Log("msg", "failed to close index file", "err", err)
}
}()
builder := NewBuilder()
err = indexFile.(*TSDBFile).Index.(*TSDBIndex).forSeries(ctx, nil, func(lbls labels.Labels, fp model.Fingerprint, chks []index.ChunkMeta) {
builder.AddSeries(lbls.Copy(), fp, chks)
}, labels.MustNewMatcher(labels.MatchEqual, "", ""))
if err != nil {
return nil, err
}
builder.chunksFinalized = true
return newCompactedIndex(ctx, tableName, userID, workingDir, periodConfig, builder), nil
}
type tableCompactor struct {
commonIndexSet compactor.IndexSet
existingUserIndexSet map[string]compactor.IndexSet
userIndexSetFactoryFunc compactor.MakeEmptyUserIndexSetFunc
ctx context.Context
periodConfig config.PeriodConfig
compactedIndexes map[string]compactor.CompactedIndex
}
func newTableCompactor(
ctx context.Context,
commonIndexSet compactor.IndexSet,
existingUserIndexSet map[string]compactor.IndexSet,
userIndexSetFactoryFunc compactor.MakeEmptyUserIndexSetFunc,
periodConfig config.PeriodConfig,
) *tableCompactor {
return &tableCompactor{
ctx: ctx,
commonIndexSet: commonIndexSet,
existingUserIndexSet: existingUserIndexSet,
userIndexSetFactoryFunc: userIndexSetFactoryFunc,
periodConfig: periodConfig,
}
}
func (t *tableCompactor) CompactTable() error {
multiTenantIndexes := t.commonIndexSet.ListSourceFiles()
var multiTenantIndices []Index
indicesMtx := sync.Mutex{}
// concurrently download and open all the multi-tenant indexes
err := concurrency.ForEachJob(t.ctx, len(multiTenantIndexes), readDBsConcurrency, func(ctx context.Context, job int) error {
downloadedAt, err := t.commonIndexSet.GetSourceFile(multiTenantIndexes[job])
if err != nil {
return err
}
defer func() {
if err := os.Remove(downloadedAt); err != nil {
level.Error(t.commonIndexSet.GetLogger()).Log("msg", "failed to remove downloaded index file", "path", downloadedAt, "err", err)
}
}()
idx, err := OpenShippableTSDB(downloadedAt)
if err != nil {
return err
}
indicesMtx.Lock()
defer indicesMtx.Unlock()
multiTenantIndices = append(multiTenantIndices, idx.(Index))
return nil
})
if err != nil {
return err
}
var multiTenantIndex Index = NoopIndex{}
if len(multiTenantIndices) > 0 {
var err error
multiTenantIndex, err = NewMultiIndex(multiTenantIndices...)
if err != nil {
return err
}
}
// find all the user ids from the multi-tenant indexes using TenantLabel.
userIDs, err := multiTenantIndex.LabelValues(t.ctx, "", 0, math.MaxInt64, TenantLabel)
if err != nil {
return err
}
// go through all the users having index in the multi-tenant indexes and setup builder for each user
// builder would combine users index from multi-tenant indexes and the existing compacted index(es)
t.compactedIndexes = make(map[string]compactor.CompactedIndex, len(userIDs))
for _, userID := range userIDs {
existingUserIndexSet, ok := t.existingUserIndexSet[userID]
if !ok {
var err error
existingUserIndexSet, err = t.userIndexSetFactoryFunc(userID)
if err != nil {
return err
}
}
builder, err := setupBuilder(t.ctx, userID, existingUserIndexSet, multiTenantIndices)
if err != nil {
return err
}
compactedIndex := newCompactedIndex(t.ctx, existingUserIndexSet.GetTableName(), userID, existingUserIndexSet.GetWorkingDir(), t.periodConfig, builder)
t.compactedIndexes[userID] = compactedIndex
if err := existingUserIndexSet.SetCompactedIndex(compactedIndex, true); err != nil {
return err
}
}
// go through existingUserIndexSet and find the ones that were not initialized now due to no updates and
// have multiple index files in the storage to merge them into a single index file.
for userID, srcIdxSet := range t.existingUserIndexSet {
if _, ok := t.compactedIndexes[userID]; ok || len(srcIdxSet.ListSourceFiles()) <= 1 {
continue
}
builder, err := setupBuilder(t.ctx, userID, srcIdxSet, []Index{})
if err != nil {
return err
}
compactedIndex := newCompactedIndex(t.ctx, srcIdxSet.GetTableName(), userID, srcIdxSet.GetWorkingDir(), t.periodConfig, builder)
t.compactedIndexes[userID] = compactedIndex
if err := srcIdxSet.SetCompactedIndex(compactedIndex, true); err != nil {
return err
}
}
if len(multiTenantIndices) > 0 {
if err := t.commonIndexSet.SetCompactedIndex(nil, true); err != nil {
return err
}
}
return nil
}
// setupBuilder creates a Builder for a single user.
// It combines the users index from multiTenantIndexes and its existing compacted index(es)
func setupBuilder(ctx context.Context, userID string, sourceIndexSet compactor.IndexSet, multiTenantIndexes []Index) (*Builder, error) {
sourceIndexes := sourceIndexSet.ListSourceFiles()
builder := NewBuilder()
// add users index from multi-tenant indexes to the builder
for _, idx := range multiTenantIndexes {
err := idx.(*TSDBFile).Index.(*TSDBIndex).forSeries(ctx, nil, func(lbls labels.Labels, fp model.Fingerprint, chks []index.ChunkMeta) {
builder.AddSeries(withoutTenantLabel(lbls.Copy()), fp, chks)
}, withTenantLabelMatcher(userID, []*labels.Matcher{})...)
if err != nil {
return nil, err
}
}
// download all the existing compacted indexes and add them to the builder
for _, sourceIndex := range sourceIndexes {
path, err := sourceIndexSet.GetSourceFile(sourceIndex)
if err != nil {
return nil, err
}
defer func() {
if err := os.Remove(path); err != nil {
level.Error(sourceIndexSet.GetLogger()).Log("msg", "error removing source index file", "err", err)
}
}()
indexFile, err := OpenShippableTSDB(path)
if err != nil {
return nil, err
}
defer func() {
if err := indexFile.Close(); err != nil {
level.Error(sourceIndexSet.GetLogger()).Log("msg", "failed to close index file", "err", err)
}
}()
err = indexFile.(*TSDBFile).Index.(*TSDBIndex).forSeries(ctx, nil, func(lbls labels.Labels, fp model.Fingerprint, chks []index.ChunkMeta) {
builder.AddSeries(lbls.Copy(), fp, chks)
}, labels.MustNewMatcher(labels.MatchEqual, "", ""))
if err != nil {
return nil, err
}
}
// finalize the chunks to remove the duplicates and sort them
builder.FinalizeChunks()
return builder, nil
}
type compactedIndex struct {
ctx context.Context
userID string
builder *Builder
workingDir string
tableInterval model.Interval
periodConfig config.PeriodConfig
indexChunks []chunk.Chunk
deleteChunks map[string][]index.ChunkMeta
seriesToCleanup map[string]struct{}
}
func newCompactedIndex(ctx context.Context, tableName, userID, workingDir string, periodConfig config.PeriodConfig, builder *Builder) *compactedIndex {
return &compactedIndex{
ctx: ctx,
userID: userID,
builder: builder,
workingDir: workingDir,
periodConfig: periodConfig,
tableInterval: retention.ExtractIntervalFromTableName(tableName),
deleteChunks: map[string][]index.ChunkMeta{},
seriesToCleanup: map[string]struct{}{},
}
}
// ForEachChunk iterates over all the chunks in the builder and calls the callback function.
func (c *compactedIndex) ForEachChunk(ctx context.Context, callback retention.ChunkEntryCallback) error {
schemaCfg := config.SchemaConfig{
Configs: []config.PeriodConfig{c.periodConfig},
}
chunkEntry := retention.ChunkEntry{
ChunkRef: retention.ChunkRef{
UserID: getUnsafeBytes(c.userID),
},
}
logprotoChunkRef := logproto.ChunkRef{
UserID: c.userID,
}
for seriesID, stream := range c.builder.streams {
logprotoChunkRef.Fingerprint = uint64(stream.fp)
chunkEntry.SeriesID = getUnsafeBytes(seriesID)
chunkEntry.Labels = withoutTenantLabel(stream.labels)
for i := 0; i < len(stream.chunks) && ctx.Err() == nil; i++ {
chk := stream.chunks[i]
logprotoChunkRef.From = chk.From()
logprotoChunkRef.Through = chk.Through()
logprotoChunkRef.Checksum = chk.Checksum
chunkEntry.ChunkID = getUnsafeBytes(schemaCfg.ExternalKey(logprotoChunkRef))
chunkEntry.From = logprotoChunkRef.From
chunkEntry.Through = logprotoChunkRef.Through
deleteChunk, err := callback(chunkEntry)
if err != nil {
return err
}
if deleteChunk {
// add the chunk to the list of chunks to delete which would be taken care of while building the index.
c.deleteChunks[seriesID] = append(c.deleteChunks[seriesID], chk)
}
}
}
return ctx.Err()
}
// IndexChunk adds the chunk to the list of chunks to index.
// Before accepting the chunk it checks if it falls within the tableInterval and rejects it if not.
func (c *compactedIndex) IndexChunk(chk chunk.Chunk) (bool, error) {
if chk.From > c.tableInterval.End || c.tableInterval.Start > chk.Through {
return false, nil
}
c.indexChunks = append(c.indexChunks, chk)
return true, nil
}
// CleanupSeries removes the series from the builder(including its chunks) and deletes the list of chunks lined up for deletion.
func (c *compactedIndex) CleanupSeries(_ []byte, lbls labels.Labels) error {
seriesID := lbls.String()
if _, ok := c.builder.streams[seriesID]; !ok {
return fmt.Errorf("series cleanup not allowed on non-existing series %s", seriesID)
}
delete(c.builder.streams, seriesID)
delete(c.deleteChunks, seriesID)
return nil
}
func (c *compactedIndex) Cleanup() {}
// ToIndexFile creates an indexFile from the chunksmetas stored in the builder.
// Before building the index, it takes care of the lined up updates i.e deletes and adding of new chunks.
func (c *compactedIndex) ToIndexFile() (index_shipper.Index, error) {
for seriesID, chks := range c.deleteChunks {
for _, chk := range chks {
chunkFound, err := c.builder.DropChunk(seriesID, chk)
if err != nil {
return nil, err
}
if !chunkFound {
return nil, fmt.Errorf("could not drop non-existent chunk %x from series %s", chk, seriesID)
}
}
}
c.deleteChunks = nil
for _, chk := range c.indexChunks {
err := c.builder.InsertChunk(chk.Metric.String(), index.ChunkMeta{
Checksum: chk.Checksum,
MinTime: int64(chk.From),
MaxTime: int64(chk.Through),
KB: uint32(chk.Size()) / (1 << 10),
Entries: uint32(chk.Data.Entries()),
})
if err != nil {
return nil, err
}
}
c.indexChunks = nil
id, err := c.builder.Build(c.ctx, c.workingDir, func(from, through model.Time, checksum uint32) Identifier {
id := SingleTenantTSDBIdentifier{
TS: time.Now(),
From: from,
Through: through,
Checksum: checksum,
}
return newPrefixedIdentifier(id, c.workingDir, "")
})
if err != nil {
return nil, err
}
return NewShippableTSDBFile(id, false)
}
func getUnsafeBytes(s string) []byte {
return *((*[]byte)(unsafe.Pointer(&s)))
}