forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_set.go
305 lines (249 loc) · 8.94 KB
/
index_set.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
package compactor
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/grafana/loki/v3/pkg/compactor/retention"
"github.com/grafana/loki/v3/pkg/compression"
"github.com/grafana/loki/v3/pkg/storage/chunk/client/util"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/index"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/storage"
util_log "github.com/grafana/loki/v3/pkg/util/log"
)
type IndexSet interface {
GetTableName() string
ListSourceFiles() []storage.IndexFile
GetSourceFile(indexFile storage.IndexFile) (string, error)
GetLogger() log.Logger
GetWorkingDir() string
// SetCompactedIndex sets the CompactedIndex for upload/applying retention and making the compactor remove the source files.
// CompactedIndex can be nil only in case of all the source files in common index set being compacted away to per tenant index.
// It would return an error if the CompactedIndex is nil and removeSourceFiles is true in case of user index set since
// compaction should either create new files or can be a noop if there is nothing to compact.
// There is no need to call SetCompactedIndex if no changes were made to the index for this IndexSet.
SetCompactedIndex(compactedIndex CompactedIndex, removeSourceFiles bool) error
}
// CompactedIndex is built by TableCompactor for IndexSet after compaction.
// It would be used for:
// 1. applying custom retention, processing delete requests using IndexProcessor
// 2. uploading the compacted index to storage by converting it to index.Index using ToIndexFile
// After all the operations are successfully done or in case of failure, Cleanup would be called to cleanup the state.
type CompactedIndex interface {
// IndexProcessor is used for applying custom retention and processing delete requests.
retention.IndexProcessor
// Cleanup should clean up all the state built during compaction.
// It is typically called at the end or in case of an error.
Cleanup()
// ToIndexFile is used to convert the CompactedIndex to an IndexFile for uploading to the object store.
// Once the IndexFile is uploaded using Index.Reader, the file is closed using Index.Close and removed from disk using Index.Path.
ToIndexFile() (index.Index, error)
}
// indexSet helps with doing operations on a set of index files belonging to a single user or common index files shared by users.
type indexSet struct {
ctx context.Context
tableName, userID string
workingDir string
baseIndexSet storage.IndexSet
uploadCompactedDB bool
removeSourceObjects bool
compactedIndex CompactedIndex
sourceObjects []storage.IndexFile
logger log.Logger
}
// newUserIndexSet intializes a new index set for user index.
func newUserIndexSet(ctx context.Context, tableName, userID string, baseUserIndexSet storage.IndexSet, workingDir string, logger log.Logger) (*indexSet, error) {
if !baseUserIndexSet.IsUserBasedIndexSet() {
return nil, fmt.Errorf("base index set is not for user index")
}
return newIndexSet(ctx, tableName, userID, baseUserIndexSet, workingDir, log.With(logger, "user-id", userID))
}
// newCommonIndexSet intializes a new index set for common index.
func newCommonIndexSet(ctx context.Context, tableName string, baseUserIndexSet storage.IndexSet, workingDir string, logger log.Logger) (*indexSet, error) {
if baseUserIndexSet.IsUserBasedIndexSet() {
return nil, fmt.Errorf("base index set is not for common index")
}
return newIndexSet(ctx, tableName, "", baseUserIndexSet, workingDir, logger)
}
func newIndexSet(ctx context.Context, tableName, userID string, baseIndexSet storage.IndexSet, workingDir string, logger log.Logger) (*indexSet, error) {
if err := util.EnsureDirectory(workingDir); err != nil {
return nil, err
}
ui := &indexSet{
ctx: ctx,
tableName: tableName,
userID: userID,
workingDir: workingDir,
baseIndexSet: baseIndexSet,
logger: logger,
}
if userID != "" {
ui.logger = log.With(logger, "user-id", userID)
}
var err error
ui.sourceObjects, err = ui.baseIndexSet.ListFiles(ui.ctx, ui.tableName, ui.userID, false)
if err != nil {
return nil, err
}
return ui, nil
}
func (is *indexSet) GetTableName() string {
return is.tableName
}
func (is *indexSet) GetWorkingDir() string {
return is.workingDir
}
func (is *indexSet) ListSourceFiles() []storage.IndexFile {
return is.sourceObjects
}
func (is *indexSet) GetSourceFile(indexFile storage.IndexFile) (string, error) {
decompress := storage.IsCompressedFile(indexFile.Name)
dst := filepath.Join(is.workingDir, indexFile.Name)
if decompress {
dst = strings.Trim(dst, gzipExtension)
}
err := storage.DownloadFileFromStorage(dst, storage.IsCompressedFile(indexFile.Name),
false, storage.LoggerWithFilename(is.logger, indexFile.Name),
func() (io.ReadCloser, error) {
return is.baseIndexSet.GetFile(is.ctx, is.tableName, is.userID, indexFile.Name)
})
if err != nil {
return "", err
}
return dst, nil
}
func (is *indexSet) GetLogger() log.Logger {
return is.logger
}
func (is *indexSet) SetCompactedIndex(compactedIndex CompactedIndex, removeSourceFiles bool) error {
if compactedIndex == nil && removeSourceFiles && is.userID != "" {
return errors.New("compacted index can't be nil when remove source files is true for user index set")
}
is.setCompactedIndex(compactedIndex, compactedIndex != nil, removeSourceFiles)
return nil
}
func (is *indexSet) setCompactedIndex(compactedIndex CompactedIndex, uploadCompactedDB, removeSourceObjects bool) {
is.compactedIndex = compactedIndex
is.uploadCompactedDB = uploadCompactedDB
is.removeSourceObjects = removeSourceObjects
}
// runRetention runs the retention on index set
func (is *indexSet) runRetention(tableMarker retention.TableMarker) error {
if is.compactedIndex == nil {
return nil
}
empty, modified, err := tableMarker.MarkForDelete(is.ctx, is.tableName, is.userID, is.compactedIndex, is.logger)
if err != nil {
return err
}
if empty {
is.uploadCompactedDB = false
is.removeSourceObjects = true
} else if modified {
is.uploadCompactedDB = true
is.removeSourceObjects = true
}
return nil
}
// upload uploads the compacted index in compressed format.
func (is *indexSet) upload() error {
if is.compactedIndex == nil {
return errors.New("can't upload nil or empty compacted index")
}
// ToDo(Sandeep): move index uploading to a common function and share it with generic index-shipper
idx, err := is.compactedIndex.ToIndexFile()
if err != nil {
return err
}
defer func() {
filePath := idx.Path()
if err := idx.Close(); err != nil {
level.Error(is.logger).Log("msg", "failed to close indexFile", "err", err)
return
}
if err := os.Remove(filePath); err != nil {
level.Error(is.logger).Log("msg", "failed to remove indexFile", "err", err)
return
}
}()
fileName := idx.Name()
level.Debug(is.logger).Log("msg", fmt.Sprintf("uploading index %s", fileName))
idxPath := idx.Path()
filePath := fmt.Sprintf("%s%s", idxPath, ".temp")
f, err := os.Create(filePath)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
level.Error(util_log.Logger).Log("msg", "failed to close temp file", "path", filePath, "err", err)
}
if err := os.Remove(filePath); err != nil {
level.Error(util_log.Logger).Log("msg", "failed to remove temp file", "path", filePath, "err", err)
}
}()
gzipPool := compression.GetWriterPool(compression.GZIP)
compressedWriter := gzipPool.GetWriter(f)
defer gzipPool.PutWriter(compressedWriter)
idxReader, err := idx.Reader()
if err != nil {
return err
}
_, err = idxReader.Seek(0, 0)
if err != nil {
return err
}
_, err = io.Copy(compressedWriter, idxReader)
if err != nil {
return err
}
err = compressedWriter.Close()
if err != nil {
return err
}
// flush the file to disk and seek the file to the beginning.
if err := f.Sync(); err != nil {
return err
}
if _, err := f.Seek(0, 0); err != nil {
return err
}
return is.baseIndexSet.PutFile(is.ctx, is.tableName, is.userID, fmt.Sprintf("%s.gz", fileName), f)
}
// removeFilesFromStorage deletes source objects from storage.
func (is *indexSet) removeFilesFromStorage() error {
level.Info(is.logger).Log("msg", "removing source db files from storage", "count", len(is.sourceObjects))
for _, object := range is.sourceObjects {
err := is.baseIndexSet.DeleteFile(is.ctx, is.tableName, is.userID, object.Name)
if err != nil {
return err
}
}
return nil
}
// done takes care of file operations which includes:
// - recreate the compacted db if required.
// - upload the compacted db if required.
// - remove the source objects from storage if required.
func (is *indexSet) done() error {
if is.uploadCompactedDB {
if err := is.upload(); err != nil {
return err
}
}
if is.removeSourceObjects {
return is.removeFilesFromStorage()
}
return nil
}
func (is *indexSet) cleanup() {
if is.compactedIndex == nil {
return
}
is.compactedIndex.Cleanup()
}