-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconfig.go
More file actions
361 lines (310 loc) · 10.8 KB
/
config.go
File metadata and controls
361 lines (310 loc) · 10.8 KB
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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"net/http/pprof"
"os"
"strings"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"github.com/alecthomas/units"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/thanos-io/objstore"
"github.com/thanos-io/objstore/client"
"github.com/thanos-io/thanos/pkg/runutil"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/thanos-io/thanos-parquet-gateway/locate"
)
func setupInterrupt(ctx context.Context, g *run.Group, log *slog.Logger) {
ctx, cancel := context.WithCancel(ctx)
g.Add(func() error {
<-ctx.Done()
log.Info("Canceling actors")
return nil
}, func(error) {
cancel()
})
}
type bucketOpts struct {
// Thanos-style configuration
configFile string
config string
}
// registerThanosStyleFlags registers the standard Thanos objstore flags
func (opts *bucketOpts) registerThanosStyleFlags(cmd *kingpin.CmdClause, prefix string) {
configFlagSuffix := ".objstore.config"
configFlag := strings.TrimPrefix(prefix+configFlagSuffix, ".")
cmd.Flag(configFlag, "Alternative to 'objstore.config-file' flag (mutually exclusive). Content of YAML file that contains object store configuration. See format details: https://thanos.io/tip/thanos/storage.md/#configuration").
PlaceHolder("<content>").StringVar(&opts.config)
configFileFlagSuffix := ".objstore.config-file"
configFileFlag := strings.TrimPrefix(prefix+configFileFlagSuffix, ".")
cmd.Flag(configFileFlag, "Path to YAML file that contains object store configuration. See format details: https://thanos.io/tip/thanos/storage.md/#configuration").
PlaceHolder("<file-path>").StringVar(&opts.configFile)
}
func setupBucket(log *slog.Logger, opts bucketOpts) (objstore.Bucket, error) {
var confContentYaml []byte
var err error
if opts.configFile != "" {
confContentYaml, err = os.ReadFile(opts.configFile)
if err != nil {
return nil, fmt.Errorf("reading config file: %w", err)
}
} else if opts.config != "" {
confContentYaml = []byte(opts.config)
} else {
return nil, fmt.Errorf("no objstore configuration provided. Use --objstore.config-file or --objstore.config")
}
bkt, err := client.NewBucket(slogAdapter{log}, confContentYaml, "thanos-parquet-gateway", nil)
if err != nil {
return nil, fmt.Errorf("creating bucket client: %w", err)
}
return bkt, nil
}
type slogAdapter struct {
log *slog.Logger
}
func (s slogAdapter) Log(args ...any) error {
s.log.Debug("", args...)
return nil
}
type tracingOpts struct {
exporterType string
// jaeger opts
jaegerEndpoint string
samplingParam float64
samplingType string
}
func setupTracing(ctx context.Context, opts tracingOpts) error {
var (
exporter trace.SpanExporter
err error
)
switch opts.exporterType {
case "JAEGER":
exporter, err = jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(opts.jaegerEndpoint)))
if err != nil {
return err
}
case "STDOUT":
exporter, err = stdouttrace.New()
if err != nil {
return err
}
default:
return fmt.Errorf("invalid exporter type %s", opts.exporterType)
}
var sampler trace.Sampler
switch opts.samplingType {
case "PROBABILISTIC":
sampler = trace.TraceIDRatioBased(opts.samplingParam)
case "ALWAYS":
sampler = trace.AlwaysSample()
case "NEVER":
sampler = trace.NeverSample()
default:
return fmt.Errorf("invalid sampling type %s", opts.samplingType)
}
r, err := resource.New(ctx,
resource.WithAttributes(
semconv.ServiceName("parquet-gateway"),
semconv.ServiceVersion("v0.0.0"),
),
)
if err != nil {
return err
}
tracerProvider := trace.NewTracerProvider(
trace.WithSampler(trace.ParentBased(sampler)),
trace.WithBatcher(exporter),
trace.WithResource(r),
)
otel.SetTracerProvider(tracerProvider)
return nil
}
type apiOpts struct {
port int
shutdownTimeout time.Duration
}
func setupInternalAPI(g *run.Group, log *slog.Logger, reg *prometheus.Registry, opts apiOpts) {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
mux.HandleFunc("/-/healthy", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK")
})
mux.HandleFunc("/-/ready", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK")
})
server := &http.Server{Addr: fmt.Sprintf(":%d", opts.port), Handler: mux}
g.Add(func() error {
log.Info("Serving internal api", slog.Int("port", opts.port))
if err := server.ListenAndServe(); err != http.ErrServerClosed {
return err
}
return nil
}, func(error) {
log.Info("Shutting down internal api", slog.Int("port", opts.port))
ctx, cancel := context.WithTimeout(context.Background(), opts.shutdownTimeout)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Error("Error shutting down internal server", slog.Any("err", err))
}
})
}
type discoveryOpts struct {
discoveryInterval time.Duration
discoveryConcurrency int
}
func setupDiscovery(ctx context.Context, g *run.Group, log *slog.Logger, bkt objstore.Bucket, opts discoveryOpts) (*locate.Discoverer, error) {
discoverer := locate.NewDiscoverer(bkt, locate.MetaConcurrency(opts.discoveryConcurrency))
log.Info("Running initial discovery")
iterCtx, iterCancel := context.WithTimeout(ctx, opts.discoveryInterval)
defer iterCancel()
if err := discoverer.Discover(iterCtx); err != nil {
return nil, fmt.Errorf("unable to run initial discovery: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return runutil.Repeat(opts.discoveryInterval, ctx.Done(), func() error {
log.Debug("Running discovery")
iterCtx, iterCancel := context.WithTimeout(ctx, opts.discoveryInterval)
defer iterCancel()
if err := discoverer.Discover(iterCtx); err != nil {
log.Warn("Unable to discover new blocks", slog.Any("err", err))
}
return nil
})
}, func(error) {
log.Info("Stopping discovery")
cancel()
})
return discoverer, nil
}
type tsdbDiscoveryOpts struct {
discoveryInterval time.Duration
discoveryConcurrency int
discoveryMinBlockAge time.Duration
externalLabelMatchers matcherSlice
}
func setupTSDBDiscovery(ctx context.Context, g *run.Group, log *slog.Logger, bkt objstore.Bucket, opts tsdbDiscoveryOpts) (*locate.TSDBDiscoverer, error) {
discoverer := locate.NewTSDBDiscoverer(
bkt,
locate.TSDBMetaConcurrency(opts.discoveryConcurrency),
locate.TSDBMinBlockAge(opts.discoveryMinBlockAge),
locate.TSDBMatchExternalLabels(opts.externalLabelMatchers...),
)
log.Info("Running initial tsdb discovery")
iterCtx, iterCancel := context.WithTimeout(ctx, opts.discoveryInterval)
defer iterCancel()
if err := discoverer.Discover(iterCtx); err != nil {
return nil, fmt.Errorf("unable to run initial discovery: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return runutil.Repeat(opts.discoveryInterval, ctx.Done(), func() error {
log.Debug("Running tsdb discovery")
iterCtx, iterCancel := context.WithTimeout(ctx, opts.discoveryInterval)
defer iterCancel()
if err := discoverer.Discover(iterCtx); err != nil {
log.Warn("Unable to discover new tsdb blocks", slog.Any("err", err))
}
return nil
})
}, func(error) {
log.Info("Stopping tsdb discovery")
cancel()
})
return discoverer, nil
}
type syncerOpts struct {
syncerInterval time.Duration
syncerConcurrency int
syncerReadBufferSize units.Base2Bytes
syncerLabelFilesDir string
filterType string
filterThanosBackfillEndpoint string
filterThanosBackfillUpdateInterval time.Duration
filterThanosBackfillOverlap time.Duration
}
func setupMetaFilter(ctx context.Context, g *run.Group, log *slog.Logger, opts syncerOpts) (locate.MetaFilter, error) {
switch opts.filterType {
case "all-metas":
return locate.AllMetasMetaFilter, nil
case "thanos-backfill":
thanosBackfillMetaFilter := locate.NewThanosBackfillMetaFilter(opts.filterThanosBackfillEndpoint, opts.filterThanosBackfillOverlap)
log.Info("Initializing thanos-backfill meta filter")
iterCtx, iterCancel := context.WithTimeout(ctx, opts.filterThanosBackfillUpdateInterval)
defer iterCancel()
if err := thanosBackfillMetaFilter.Update(iterCtx); err != nil {
return nil, fmt.Errorf("unable to initialize thanos-backfill meta filter: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return runutil.Repeat(opts.filterThanosBackfillUpdateInterval, ctx.Done(), func() error {
log.Debug("Updating thanos-backfill meta filter")
iterCtx, iterCancel := context.WithTimeout(ctx, opts.filterThanosBackfillUpdateInterval)
defer iterCancel()
if err := thanosBackfillMetaFilter.Update(iterCtx); err != nil {
log.Warn("Unable to update thanos-backfill meta filter", slog.Any("err", err))
}
return nil
})
}, func(error) {
log.Info("Stopping thanos-backfill meta filter updates")
cancel()
})
return thanosBackfillMetaFilter, nil
default:
return nil, fmt.Errorf("unknown meta filter type: %s", opts.filterType)
}
}
func setupSyncer(ctx context.Context, g *run.Group, log *slog.Logger, bkt objstore.Bucket, discoverer *locate.Discoverer, metaFilter locate.MetaFilter, opts syncerOpts) (*locate.Syncer, error) {
syncer := locate.NewSyncer(
bkt,
locate.FilterMetas(metaFilter),
locate.BlockConcurrency(opts.syncerConcurrency),
locate.BlockOptions(
locate.ReadBufferSize(opts.syncerReadBufferSize),
locate.LabelFilesDir(opts.syncerLabelFilesDir),
),
)
log.Info("Running initial sync")
iterCtx, iterCancel := context.WithTimeout(ctx, opts.syncerInterval)
defer iterCancel()
if err := syncer.Sync(iterCtx, discoverer.Metas()); err != nil {
return nil, fmt.Errorf("unable to run initial sync: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return runutil.Repeat(opts.syncerInterval, ctx.Done(), func() error {
log.Debug("Running sync")
iterCtx, iterCancel := context.WithTimeout(ctx, opts.syncerInterval)
defer iterCancel()
if err := syncer.Sync(iterCtx, discoverer.Metas()); err != nil {
log.Warn("Unable to sync new blocks", slog.Any("err", err))
}
return nil
})
}, func(error) {
log.Info("Stopping syncer")
cancel()
})
return syncer, nil
}