-
Notifications
You must be signed in to change notification settings - Fork 460
/
shipper.go
339 lines (294 loc) · 10.7 KB
/
shipper.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/thanos-io/thanos/blob/main/pkg/shipper/shipper.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Thanos Authors.
package ingester
import (
"context"
"encoding/json"
"os"
"path"
"path/filepath"
"sort"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/runutil"
"github.com/oklog/ulid"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/thanos-io/objstore"
mimir_tsdb "github.com/grafana/mimir/pkg/storage/tsdb"
"github.com/grafana/mimir/pkg/storage/tsdb/block"
)
// shipperMetrics holds the shipper metrics. Mimir runs 1 shipper for each tenant but
// the metrics instance is shared across all tenants.
type shipperMetrics struct {
uploads prometheus.Counter
uploadFailures prometheus.Counter
lastSuccessfulUploadTime prometheus.Gauge
}
func newShipperMetrics(reg prometheus.Registerer) *shipperMetrics {
return &shipperMetrics{
uploads: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "cortex_ingester_shipper_uploads_total",
Help: "Total number of uploaded TSDB blocks",
}),
uploadFailures: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "cortex_ingester_shipper_upload_failures_total",
Help: "Total number of TSDB block upload failures",
}),
lastSuccessfulUploadTime: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Name: "cortex_ingester_shipper_last_successful_upload_timestamp_seconds",
Help: "Unix timestamp (in seconds) of the last successful TSDB block uploaded to the object storage.",
}),
}
}
type ShipperConfigProvider interface {
OutOfOrderBlocksExternalLabelEnabled(userID string) bool
}
// shipper watches a directory for matching files and directories and uploads
// them to a remote data store.
// shipper implements BlocksUploader interface.
type shipper struct {
logger log.Logger
cfgProvider ShipperConfigProvider
userID string
dir string
metrics *shipperMetrics
bucket objstore.Bucket
source block.SourceType
}
// newShipper creates a new uploader that detects new TSDB blocks in dir and uploads them to
// remote if necessary. It attaches the Thanos metadata section in each meta JSON file.
// If uploadCompacted is enabled, it also uploads compacted blocks which are already in filesystem.
func newShipper(
logger log.Logger,
cfgProvider ShipperConfigProvider,
userID string,
metrics *shipperMetrics,
dir string,
bucket objstore.Bucket,
source block.SourceType,
) *shipper {
if logger == nil {
logger = log.NewNopLogger()
}
return &shipper{
logger: logger,
cfgProvider: cfgProvider,
userID: userID,
dir: dir,
bucket: bucket,
metrics: metrics,
source: source,
}
}
// Sync performs a single synchronization, which ensures all non-compacted local blocks have been uploaded
// to the object bucket once.
//
// It is not concurrency-safe, however it is compactor-safe (running concurrently with compactor is ok).
func (s *shipper) Sync(ctx context.Context) (shipped int, err error) {
shippedBlocks, err := readShippedBlocks(s.dir)
if err != nil {
// If we encounter any error, proceed with an new list of shipped blocks.
// The meta file will be overridden later. Note that the meta file is only
// used to avoid unnecessary bucket.Exists call, which are properly handled
// by the system if their occur anyway.
level.Warn(s.logger).Log("msg", "reading meta file failed, will override it", "err", err)
// Reset the shipped blocks slice, so we can rebuild it only with blocks that still exist locally.
shippedBlocks = map[ulid.ULID]time.Time{}
}
meta := shipperMeta{Version: shipperMetaVersion1, Shipped: map[ulid.ULID]model.Time{}}
var uploadErrs int
metas, err := s.blockMetasFromOldest()
if err != nil {
return 0, err
}
for _, m := range metas {
// Do not sync a block if we already shipped or ignored it. If it's no longer found in the bucket,
// it was generally removed by the compaction process.
if shippedTime, shipped := shippedBlocks[m.ULID]; shipped {
meta.Shipped[m.ULID] = model.TimeFromUnixNano(shippedTime.UnixNano())
continue
}
if m.Stats.NumSamples == 0 {
// Ignore empty blocks.
level.Debug(s.logger).Log("msg", "ignoring empty block", "block", m.ULID)
continue
}
// We only ship of the first compacted block level as normal flow.
if m.Compaction.Level > 1 {
continue
}
// Check against bucket if the meta file for this block exists.
ok, err := s.bucket.Exists(ctx, path.Join(m.ULID.String(), block.MetaFilename))
if err != nil {
return 0, errors.Wrap(err, "check exists")
}
if ok {
// We decide to be conservative here and assume it was just recently uploaded.
// It would be very rare to have blocks uploaded but not tracked in the shipper meta file.
// This could happen if process crashed while uploading the block.
meta.Shipped[m.ULID] = model.Now()
shipped++ // the last upload must have failed, report the block as if it was shipped successfully now
continue
}
level.Info(s.logger).Log("msg", "uploading new block to long-term storage", "block", m.ULID)
if err := s.upload(ctx, m); err != nil {
// No error returned, just log line. This is because we want other blocks to be shipped even
// though this one failed. It will be retried on second Sync iteration.
level.Error(s.logger).Log("msg", "uploading new block to long-term storage failed", "block", m.ULID, "err", err)
uploadErrs++
continue
}
level.Info(s.logger).Log("msg", "finished uploading new block to long-term storage", "block", m.ULID)
meta.Shipped[m.ULID] = model.Now()
shipped++
s.metrics.uploads.Inc()
s.metrics.lastSuccessfulUploadTime.SetToCurrentTime()
}
if err := writeShipperMetaFile(s.logger, s.dir, meta); err != nil {
level.Warn(s.logger).Log("msg", "updating meta file failed", "err", err)
}
if uploadErrs > 0 {
s.metrics.uploadFailures.Add(float64(uploadErrs))
return shipped, errors.Errorf("failed to sync %v blocks", uploadErrs)
}
return shipped, nil
}
// upload method uploads the block to blocks storage. Block is uploaded with updated meta.json file with extra details.
// This updated version of meta.json is however not persisted locally on the disk, to avoid race condition when TSDB
// library could actually unload the block if it found meta.json file missing.
func (s *shipper) upload(ctx context.Context, meta *block.Meta) error {
blockDir := filepath.Join(s.dir, meta.ULID.String())
meta.Thanos.Source = s.source
meta.Thanos.SegmentFiles = block.GetSegmentFiles(blockDir)
if meta.Compaction.FromOutOfOrder() && s.cfgProvider.OutOfOrderBlocksExternalLabelEnabled(s.userID) {
// At this point the OOO data was already ingested and compacted, so there's no point in checking for the OOO feature flag
meta.Thanos.Labels[mimir_tsdb.OutOfOrderExternalLabel] = mimir_tsdb.OutOfOrderExternalLabelValue
}
// Upload block with custom metadata.
return block.Upload(ctx, s.logger, s.bucket, blockDir, meta)
}
// blockMetasFromOldest returns the block meta of each block found in dir
// sorted by minTime asc.
func (s *shipper) blockMetasFromOldest() (metas []*block.Meta, _ error) {
fis, err := os.ReadDir(s.dir)
if err != nil {
return nil, errors.Wrap(err, "read dir")
}
names := make([]string, 0, len(fis))
for _, fi := range fis {
names = append(names, fi.Name())
}
for _, n := range names {
if _, ok := block.IsBlockDir(n); !ok {
continue
}
dir := filepath.Join(s.dir, n)
fi, err := os.Stat(dir)
if err != nil {
return nil, errors.Wrapf(err, "stat block %v", dir)
}
if !fi.IsDir() {
continue
}
m, err := block.ReadMetaFromDir(dir)
if err != nil {
return nil, errors.Wrapf(err, "read metadata for block %v", dir)
}
metas = append(metas, m)
}
sort.Slice(metas, func(i, j int) bool {
return metas[i].BlockMeta.MinTime < metas[j].BlockMeta.MinTime
})
return metas, nil
}
func readShippedBlocks(dir string) (map[ulid.ULID]time.Time, error) {
meta, err := readShipperMetaFile(dir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// If the meta file doesn't exist it means the shipper hasn't run yet.
meta = shipperMeta{}
} else {
return nil, err
}
}
shippedBlocks := make(map[ulid.ULID]time.Time, len(meta.Shipped))
for blockID, shippedTime := range meta.Shipped {
shippedBlocks[blockID] = shippedTime.Time()
}
return shippedBlocks, nil
}
// shipperMeta defines the format mimir.shipper.json file that the shipper places in the data directory.
type shipperMeta struct {
Version int `json:"version"`
Shipped map[ulid.ULID]model.Time `json:"shipped"`
}
const (
// shipperMetaFilename is the known JSON filename for meta information.
shipperMetaFilename = "mimir.shipper.json"
// shipperMetaVersion1 represents 1 version of meta.
shipperMetaVersion1 = 1
)
// writeShipperMetaFile writes the given meta into <dir>/mimir.shipper.json.
func writeShipperMetaFile(logger log.Logger, dir string, meta shipperMeta) error {
path := filepath.Join(dir, shipperMetaFilename)
tmp := path + ".tmp"
f, err := os.Create(tmp)
if err != nil {
return err
}
enc := json.NewEncoder(f)
enc.SetIndent("", "\t")
if err := enc.Encode(meta); err != nil {
runutil.CloseWithLogOnErr(logger, f, "write meta file close")
return err
}
if err := f.Close(); err != nil {
return err
}
err = renameFile(logger, tmp, path)
if err != nil {
return errors.Wrap(err, "writing mimir shipped meta file")
}
return nil
}
// readShipperMetaFile reads the given meta from <dir>/mimir.shipper.json.
func readShipperMetaFile(dir string) (shipperMeta, error) {
fpath := filepath.Join(dir, filepath.Clean(shipperMetaFilename))
b, err := os.ReadFile(fpath)
if err != nil {
return shipperMeta{}, errors.Wrapf(err, "failed to read %s", fpath)
}
var m shipperMeta
if err := json.Unmarshal(b, &m); err != nil {
return shipperMeta{}, errors.Wrapf(err, "failed to parse %s as JSON: %q", fpath, string(b))
}
if m.Version != shipperMetaVersion1 {
return shipperMeta{}, errors.Errorf("unexpected meta file version %d", m.Version)
}
return m, nil
}
func renameFile(logger log.Logger, from, to string) error {
if err := os.RemoveAll(to); err != nil {
return err
}
if err := os.Rename(from, to); err != nil {
return err
}
// Directory was renamed; sync parent dir to persist rename.
pdir, err := fileutil.OpenDir(filepath.Dir(to))
if err != nil {
return err
}
if err = fileutil.Fdatasync(pdir); err != nil {
runutil.CloseWithLogOnErr(logger, pdir, "rename file dir close")
return err
}
return pdir.Close()
}