-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice.go
433 lines (355 loc) · 12.2 KB
/
service.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
426
427
428
429
430
431
432
433
package ingestor
import (
"context"
"errors"
"fmt"
"io/fs"
"net/http"
"regexp"
"strconv"
"time"
"github.com/Azure/adx-mon/ingestor/adx"
"github.com/Azure/adx-mon/ingestor/cluster"
ingestorstorage "github.com/Azure/adx-mon/ingestor/storage"
"github.com/Azure/adx-mon/metrics"
adxhttp "github.com/Azure/adx-mon/pkg/http"
"github.com/Azure/adx-mon/pkg/logger"
"github.com/Azure/adx-mon/pkg/scheduler"
"github.com/Azure/adx-mon/pkg/wal"
"github.com/Azure/adx-mon/storage"
"github.com/Azure/adx-mon/transform"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// invalidEntityCharacters is a regex that matches invalid characters for Kusto entities and segment files.
// This is a subset of the invalid characters for Kusto entities and segment files naming patterns. This should
// match tranform.Normalize.
var invalidEntityCharacters = regexp.MustCompile(`[^a-zA-Z0-9]`)
type Service struct {
walOpts wal.WALOpts
opts ServiceOpts
// database is a map of known DB names used for validating requests.
databases map[string]struct{}
uploader adx.Uploader
replicator cluster.Replicator
coordinator cluster.Coordinator
batcher cluster.Batcher
closeFn context.CancelFunc
store storage.Store
metrics metrics.Service
scheduler *scheduler.Periodic
requestFilter *transform.RequestTransformer
health interface{ IsHealthy() bool }
}
type ServiceOpts struct {
StorageDir string
Uploader adx.Uploader
MaxSegmentSize int64
MaxSegmentAge time.Duration
// StaticColumns is a slice of column=value elements where each element will be added all rows.
StaticColumns []string
// LiftedColumns is a slice of label names where each element will be added as a column if the label exists.
LiftedColumns []string
// DropLabels is a map of metric names regexes to label name regexes. When both match, the label will be dropped.
DropLabels map[*regexp.Regexp]*regexp.Regexp
// DropMetrics is a slice of regexes that drops metrics when the metric name matches. The metric name format
// should match the Prometheus naming style before the metric is translated to a Kusto table name.
DropMetrics []*regexp.Regexp
K8sCli kubernetes.Interface
K8sCtrlCli client.Client
// MetricsKustoCli is the Kusto client connected to the metrics kusto cluster.
MetricsKustoCli []metrics.StatementExecutor
// LogsKustoCli is the Kusto client connected to the logs kusto cluster.
LogsKustoCli []metrics.StatementExecutor
// InsecureSkipVerify disables TLS certificate verification.
InsecureSkipVerify bool
// Namespace is the namespace used for peer discovery.
Namespace string
// Hostname is the hostname of the current node.
Hostname string
// Region is a location identifier
Region string
// DisablePeerTransfer disables peer discovery and prevents transfers of small segments to an owner.
// Each instance of ingestor will upload received segments directly.
DisablePeerTransfer bool
// MaxTransferSize is the minimum size of a segment that will be transferred to another node. If a segment
// exceeds this size, it will be uploaded directly by the current node.
MaxTransferSize int64
// MaxTransferAge is the maximum age of a segment that will be transferred to another node. If a segment
// exceeds this age, it will be uploaded directly by the current node.
MaxTransferAge time.Duration
// MaxSegmentCount is the maximum number of segments files allowed on disk before signaling back-pressure.
MaxSegmentCount int64
// MaxDiskUsage is the maximum disk usage allowed before signaling back-pressure.
MaxDiskUsage int64
// AllowedDatabases is the distinct set of database names that are allowed to be written to.
AllowedDatabase []string
// MetricsDatabase is the name of the metrics database.
MetricsDatabases []string
// LogsDatabases is a slice of log database names.
LogsDatabases []string
// PartitionSize is the max size of the group of nodes forming a partition. A partition is a set of nodes where
// keys are distributed.
PartitionSize int
}
func NewService(opts ServiceOpts) (*Service, error) {
store := storage.NewLocalStore(storage.StoreOpts{
StorageDir: opts.StorageDir,
SegmentMaxSize: opts.MaxSegmentSize,
SegmentMaxAge: opts.MaxSegmentAge,
LiftedLabels: opts.LiftedColumns,
})
coord, err := cluster.NewCoordinator(&cluster.CoordinatorOpts{
WriteTimeSeriesFn: store.WriteTimeSeries,
K8sCli: opts.K8sCli,
Hostname: opts.Hostname,
Namespace: opts.Namespace,
PartitionSize: opts.PartitionSize,
})
if err != nil {
return nil, err
}
health := cluster.NewHealth(cluster.HealthOpts{
UnhealthyTimeout: time.Minute,
MaxSegmentCount: opts.MaxSegmentCount,
MaxDiskUsage: opts.MaxDiskUsage,
})
repl, err := cluster.NewReplicator(cluster.ReplicatorOpts{
Hostname: opts.Hostname,
Partitioner: coord,
InsecureSkipVerify: opts.InsecureSkipVerify,
Health: health,
SegmentRemover: store,
})
if err != nil {
return nil, err
}
batcher := cluster.NewBatcher(cluster.BatcherOpts{
StorageDir: opts.StorageDir,
MaxSegmentAge: opts.MaxSegmentAge,
MaxTransferSize: opts.MaxTransferSize,
MaxTransferAge: opts.MaxTransferAge,
Partitioner: coord,
Segmenter: store.Index(),
UploadQueue: opts.Uploader.UploadQueue(),
TransferQueue: repl.TransferQueue(),
PeerHealthReporter: health,
TransfersDisabled: opts.DisablePeerTransfer,
})
health.QueueSizer = batcher
metricsSvc := metrics.NewService(metrics.ServiceOpts{
Hostname: opts.Hostname,
Elector: coord,
KustoCli: opts.MetricsKustoCli,
PeerHealthReport: health,
})
dbs := make(map[string]struct{}, len(opts.AllowedDatabase))
for _, db := range opts.AllowedDatabase {
dbs[db] = struct{}{}
}
databases := make(map[string]struct{})
for _, db := range opts.LogsDatabases {
databases[db] = struct{}{}
}
for _, db := range opts.MetricsDatabases {
databases[db] = struct{}{}
}
sched := scheduler.NewScheduler(coord)
return &Service{
opts: opts,
databases: databases,
uploader: opts.Uploader,
replicator: repl,
store: store,
coordinator: coord,
batcher: batcher,
metrics: metricsSvc,
health: health,
scheduler: sched,
requestFilter: &transform.RequestTransformer{
DropMetrics: opts.DropMetrics,
DropLabels: opts.DropLabels,
},
}, nil
}
func (s *Service) Open(ctx context.Context) error {
var svcCtx context.Context
svcCtx, s.closeFn = context.WithCancel(ctx)
if err := s.store.Open(svcCtx); err != nil {
return err
}
if err := s.coordinator.Open(svcCtx); err != nil {
return err
}
if err := s.batcher.Open(svcCtx); err != nil {
return err
}
if err := s.replicator.Open(svcCtx); err != nil {
return err
}
if err := s.metrics.Open(svcCtx); err != nil {
return err
}
if err := s.scheduler.Open(svcCtx); err != nil {
return err
}
s.scheduler.ScheduleEvery(time.Minute, "ingestor-health-check", func(ctx context.Context) error {
metrics.IngestorHealthCheck.WithLabelValues(s.opts.Region).Set(1)
return nil
})
fnStore := ingestorstorage.NewFunctions(s.opts.K8sCtrlCli, s.coordinator)
for _, v := range s.opts.MetricsKustoCli {
t := adx.NewDropUnusedTablesTask(v)
s.scheduler.ScheduleEvery(12*time.Hour, "delete-unused-tables", func(ctx context.Context) error {
return t.Run(ctx)
})
f := adx.NewSyncFunctionsTask(fnStore, v)
s.scheduler.ScheduleEvery(5*time.Minute, "sync-metrics-functions", func(ctx context.Context) error {
return f.Run(ctx)
})
}
for _, v := range s.opts.LogsKustoCli {
f := adx.NewSyncFunctionsTask(fnStore, v)
s.scheduler.ScheduleEvery(5*time.Minute, "sync-logs-functions", func(ctx context.Context) error {
return f.Run(ctx)
})
}
return nil
}
func (s *Service) Close() error {
s.closeFn()
if err := s.scheduler.Close(); err != nil {
return err
}
if err := s.metrics.Close(); err != nil {
return err
}
if err := s.replicator.Close(); err != nil {
return err
}
if err := s.batcher.Close(); err != nil {
return err
}
if err := s.coordinator.Close(); err != nil {
return err
}
return s.store.Close()
}
// HandleReady handles the readiness probe.
func (s *Service) HandleReady(w http.ResponseWriter, r *http.Request) {
if s.health.IsHealthy() {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
return
}
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("not ready"))
}
// HandleTransfer handles the transfer WAL segments from other nodes in the cluster.
func (s *Service) HandleTransfer(w http.ResponseWriter, r *http.Request) {
adxhttp.MaybeCloseConnection(w, r)
start := time.Now()
m := metrics.RequestsReceived.MustCurryWith(prometheus.Labels{"path": "/transfer"})
filename := r.URL.Query().Get("filename")
if filename == "" {
m.WithLabelValues(strconv.Itoa(http.StatusBadRequest)).Inc()
http.Error(w, "missing filename", http.StatusBadRequest)
return
}
defer func() {
dur := time.Since(start)
if dur.Seconds() > 10 {
logger.Warnf("slow request: path=/transfer duration=%s from=%s size=%d file=%s", dur.String(), r.RemoteAddr, r.ContentLength, filename)
}
if err := r.Body.Close(); err != nil {
logger.Errorf("close http body: %s, path=/transfer duration=%s", err.Error(), dur.String())
}
}()
if !s.health.IsHealthy() {
m.WithLabelValues(strconv.Itoa(http.StatusTooManyRequests)).Inc()
http.Error(w, "Overloaded. Retry later", http.StatusTooManyRequests)
return
}
// https://pkg.go.dev/io/fs#ValidPath
// Check for possible traversal attacks.
f := s.validateFileName(filename)
if f == "" {
logger.Errorf("Transfer requested with an invalid filename %q", filename)
m.WithLabelValues(strconv.Itoa(http.StatusBadRequest)).Inc()
http.Error(w, "filename is invalid", http.StatusBadRequest)
return
}
n, err := s.store.Import(f, r.Body)
if errors.Is(err, wal.ErrMaxSegmentsExceeded) || errors.Is(err, wal.ErrMaxDiskUsageExceeded) {
m.WithLabelValues(strconv.Itoa(http.StatusTooManyRequests)).Inc()
http.Error(w, "Overloaded. Retry later", http.StatusTooManyRequests)
return
} else if errors.Is(err, wal.ErrSegmentLocked) {
http.Error(w, err.Error(), http.StatusLocked)
return
} else if err != nil {
logger.Errorf("Failed to import %s: %s", filename, err.Error())
m.WithLabelValues(strconv.Itoa(http.StatusInternalServerError)).Inc()
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
if logger.IsDebug() {
logger.Debugf("Imported %d bytes to %s", n, filename)
}
}
m.WithLabelValues(strconv.Itoa(http.StatusAccepted)).Inc()
w.WriteHeader(http.StatusAccepted)
}
func (s *Service) UploadSegments() error {
if err := s.batcher.BatchSegments(); err != nil {
return err
}
logger.Infof("Waiting for upload queue to drain, %d batches remaining", len(s.uploader.UploadQueue()))
logger.Infof("Waiting for transfer queue to drain, %d batches remaining", len(s.replicator.TransferQueue()))
t := time.NewTicker(time.Second)
defer t.Stop()
timeout := time.NewTimer(30 * time.Second)
defer timeout.Stop()
for {
select {
case <-t.C:
if len(s.uploader.UploadQueue()) == 0 && len(s.replicator.TransferQueue()) == 0 {
return nil
}
if len(s.uploader.UploadQueue()) != 0 {
logger.Infof("Waiting for upload queue to drain, %d batches remaining", len(s.uploader.UploadQueue()))
}
if len(s.replicator.TransferQueue()) != 0 {
logger.Infof("Waiting for transfer queue to drain, %d batches remaining", len(s.replicator.TransferQueue()))
}
case <-timeout.C:
return fmt.Errorf("failed to upload segments")
}
}
}
func (s *Service) DisableWrites() error {
if err := s.metrics.Close(); err != nil {
return err
}
if err := s.store.Close(); err != nil {
return err
}
return nil
}
func (s *Service) validateFileName(filename string) string {
if !fs.ValidPath(filename) {
return ""
}
db, table, schema, epoch, err := wal.ParseFilename(filename)
if err != nil {
return ""
}
if invalidEntityCharacters.MatchString(db) || invalidEntityCharacters.MatchString(table) || invalidEntityCharacters.MatchString(epoch) || invalidEntityCharacters.MatchString(schema) {
return ""
}
if _, ok := s.databases[db]; !ok {
return ""
}
return wal.Filename(db, table, schema, epoch)
}