-
Notifications
You must be signed in to change notification settings - Fork 806
/
table_manager.go
542 lines (456 loc) · 19.3 KB
/
table_manager.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package chunk
import (
"context"
"errors"
"flag"
"fmt"
"math/rand"
"sort"
"strings"
"time"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
tsdberrors "github.com/prometheus/prometheus/tsdb/errors"
"github.com/weaveworks/common/instrument"
"github.com/weaveworks/common/mtime"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/services"
)
const (
readLabel = "read"
writeLabel = "write"
bucketRetentionEnforcementInterval = 12 * time.Hour
)
type tableManagerMetrics struct {
syncTableDuration *prometheus.HistogramVec
tableCapacity *prometheus.GaugeVec
createFailures prometheus.Gauge
deleteFailures prometheus.Gauge
lastSuccessfulSync prometheus.Gauge
}
func newTableManagerMetrics(r prometheus.Registerer) *tableManagerMetrics {
m := tableManagerMetrics{}
m.syncTableDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "cortex",
Name: "table_manager_sync_duration_seconds",
Help: "Time spent synching tables.",
Buckets: prometheus.DefBuckets,
}, []string{"operation", "status_code"})
m.tableCapacity = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "table_capacity_units",
Help: "Per-table capacity, measured in DynamoDB capacity units.",
}, []string{"op", "table"})
m.createFailures = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "table_manager_create_failures",
Help: "Number of table creation failures during the last table-manager reconciliation",
})
m.deleteFailures = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "table_manager_delete_failures",
Help: "Number of table deletion failures during the last table-manager reconciliation",
})
m.lastSuccessfulSync = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "table_manager_sync_success_timestamp_seconds",
Help: "Timestamp of the last successful table manager sync.",
})
if r != nil {
r.MustRegister(
m.syncTableDuration,
m.tableCapacity,
m.createFailures,
m.deleteFailures,
m.lastSuccessfulSync,
)
}
return &m
}
// TableManagerConfig holds config for a TableManager
type TableManagerConfig struct {
// Master 'off-switch' for table capacity updates, e.g. when troubleshooting
ThroughputUpdatesDisabled bool `yaml:"throughput_updates_disabled"`
// Master 'on-switch' for table retention deletions
RetentionDeletesEnabled bool `yaml:"retention_deletes_enabled"`
// How far back tables will be kept before they are deleted
RetentionPeriod time.Duration `yaml:"-"`
// This is so that we can accept 1w, 1y in the YAML.
RetentionPeriodModel model.Duration `yaml:"retention_period"`
// Period with which the table manager will poll for tables.
PollInterval time.Duration `yaml:"poll_interval"`
// duration a table will be created before it is needed.
CreationGracePeriod time.Duration `yaml:"creation_grace_period"`
IndexTables ProvisionConfig `yaml:"index_tables_provisioning"`
ChunkTables ProvisionConfig `yaml:"chunk_tables_provisioning"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface. To support RetentionPeriod.
func (cfg *TableManagerConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// If we call unmarshal on TableManagerConfig, it will call UnmarshalYAML leading to infinite recursion.
// To make unmarshal fill the plain data struct rather than calling UnmarshalYAML
// again, we have to hide it using a type indirection.
type plain TableManagerConfig
if err := unmarshal((*plain)(cfg)); err != nil {
return err
}
if cfg.RetentionPeriodModel > 0 {
cfg.RetentionPeriod = time.Duration(cfg.RetentionPeriodModel)
}
return nil
}
// MarshalYAML implements the yaml.Marshaler interface. To support RetentionPeriod.
func (cfg *TableManagerConfig) MarshalYAML() (interface{}, error) {
cfg.RetentionPeriodModel = model.Duration(cfg.RetentionPeriod)
return cfg, nil
}
// Validate validates the config.
func (cfg *TableManagerConfig) Validate() error {
// We're setting this field because when using flags, you set the RetentionPeriodModel but not RetentionPeriod.
// TODO(gouthamve): Its a hack, but I can't think of any other way :/
if cfg.RetentionPeriodModel > 0 {
cfg.RetentionPeriod = time.Duration(cfg.RetentionPeriodModel)
}
return nil
}
// ProvisionConfig holds config for provisioning capacity (on DynamoDB for now)
type ProvisionConfig struct {
ProvisionedThroughputOnDemandMode bool `yaml:"enable_ondemand_throughput_mode"`
ProvisionedWriteThroughput int64 `yaml:"provisioned_write_throughput"`
ProvisionedReadThroughput int64 `yaml:"provisioned_read_throughput"`
InactiveThroughputOnDemandMode bool `yaml:"enable_inactive_throughput_on_demand_mode"`
InactiveWriteThroughput int64 `yaml:"inactive_write_throughput"`
InactiveReadThroughput int64 `yaml:"inactive_read_throughput"`
WriteScale AutoScalingConfig `yaml:"write_scale"`
InactiveWriteScale AutoScalingConfig `yaml:"inactive_write_scale"`
InactiveWriteScaleLastN int64 `yaml:"inactive_write_scale_lastn"`
ReadScale AutoScalingConfig `yaml:"read_scale"`
InactiveReadScale AutoScalingConfig `yaml:"inactive_read_scale"`
InactiveReadScaleLastN int64 `yaml:"inactive_read_scale_lastn"`
}
// RegisterFlags adds the flags required to config this to the given FlagSet.
func (cfg *TableManagerConfig) RegisterFlags(f *flag.FlagSet) {
f.BoolVar(&cfg.ThroughputUpdatesDisabled, "table-manager.throughput-updates-disabled", false, "If true, disable all changes to DB capacity")
f.BoolVar(&cfg.RetentionDeletesEnabled, "table-manager.retention-deletes-enabled", false, "If true, enables retention deletes of DB tables")
f.Var(&cfg.RetentionPeriodModel, "table-manager.retention-period", "Tables older than this retention period are deleted. Note: This setting is destructive to data!(default: 0, which disables deletion)")
f.DurationVar(&cfg.PollInterval, "table-manager.poll-interval", 2*time.Minute, "How frequently to poll backend to learn our capacity.")
f.DurationVar(&cfg.CreationGracePeriod, "table-manager.periodic-table.grace-period", 10*time.Minute, "Periodic tables grace period (duration which table will be created/deleted before/after it's needed).")
cfg.IndexTables.RegisterFlags("table-manager.index-table", f)
cfg.ChunkTables.RegisterFlags("table-manager.chunk-table", f)
}
// RegisterFlags adds the flags required to config this to the given FlagSet.
func (cfg *ProvisionConfig) RegisterFlags(argPrefix string, f *flag.FlagSet) {
f.Int64Var(&cfg.ProvisionedWriteThroughput, argPrefix+".write-throughput", 1000, "Table default write throughput. Supported by DynamoDB")
f.Int64Var(&cfg.ProvisionedReadThroughput, argPrefix+".read-throughput", 300, "Table default read throughput. Supported by DynamoDB")
f.BoolVar(&cfg.ProvisionedThroughputOnDemandMode, argPrefix+".enable-ondemand-throughput-mode", false, "Enables on demand throughput provisioning for the storage provider (if supported). Applies only to tables which are not autoscaled. Supported by DynamoDB")
f.Int64Var(&cfg.InactiveWriteThroughput, argPrefix+".inactive-write-throughput", 1, "Table write throughput for inactive tables. Supported by DynamoDB")
f.Int64Var(&cfg.InactiveReadThroughput, argPrefix+".inactive-read-throughput", 300, "Table read throughput for inactive tables. Supported by DynamoDB")
f.BoolVar(&cfg.InactiveThroughputOnDemandMode, argPrefix+".inactive-enable-ondemand-throughput-mode", false, "Enables on demand throughput provisioning for the storage provider (if supported). Applies only to tables which are not autoscaled. Supported by DynamoDB")
cfg.WriteScale.RegisterFlags(argPrefix+".write-throughput.scale", f)
cfg.InactiveWriteScale.RegisterFlags(argPrefix+".inactive-write-throughput.scale", f)
f.Int64Var(&cfg.InactiveWriteScaleLastN, argPrefix+".inactive-write-throughput.scale-last-n", 4, "Number of last inactive tables to enable write autoscale.")
cfg.ReadScale.RegisterFlags(argPrefix+".read-throughput.scale", f)
cfg.InactiveReadScale.RegisterFlags(argPrefix+".inactive-read-throughput.scale", f)
f.Int64Var(&cfg.InactiveReadScaleLastN, argPrefix+".inactive-read-throughput.scale-last-n", 4, "Number of last inactive tables to enable read autoscale.")
}
// TableManager creates and manages the provisioned throughput on DynamoDB tables
type TableManager struct {
services.Service
client TableClient
cfg TableManagerConfig
schemaCfg SchemaConfig
maxChunkAge time.Duration
bucketClient BucketClient
metrics *tableManagerMetrics
bucketRetentionLoop services.Service
}
// NewTableManager makes a new TableManager
func NewTableManager(cfg TableManagerConfig, schemaCfg SchemaConfig, maxChunkAge time.Duration, tableClient TableClient,
objectClient BucketClient, registerer prometheus.Registerer) (*TableManager, error) {
if cfg.RetentionPeriod != 0 {
// Assume the newest config is the one to use for validation of retention
indexTablesPeriod := schemaCfg.Configs[len(schemaCfg.Configs)-1].IndexTables.Period
if indexTablesPeriod != 0 && cfg.RetentionPeriod%indexTablesPeriod != 0 {
return nil, errors.New("retention period should now be a multiple of periodic table duration")
}
}
tm := &TableManager{
cfg: cfg,
schemaCfg: schemaCfg,
maxChunkAge: maxChunkAge,
client: tableClient,
bucketClient: objectClient,
metrics: newTableManagerMetrics(registerer),
}
tm.Service = services.NewBasicService(tm.starting, tm.loop, tm.stopping)
return tm, nil
}
// Start the TableManager
func (m *TableManager) starting(ctx context.Context) error {
if m.bucketClient != nil && m.cfg.RetentionPeriod != 0 && m.cfg.RetentionDeletesEnabled {
m.bucketRetentionLoop = services.NewTimerService(bucketRetentionEnforcementInterval, nil, m.bucketRetentionIteration, nil)
return services.StartAndAwaitRunning(ctx, m.bucketRetentionLoop)
}
return nil
}
// Stop the TableManager
func (m *TableManager) stopping(_ error) error {
if m.bucketRetentionLoop != nil {
return services.StopAndAwaitTerminated(context.Background(), m.bucketRetentionLoop)
}
return nil
}
func (m *TableManager) loop(ctx context.Context) error {
ticker := time.NewTicker(m.cfg.PollInterval)
defer ticker.Stop()
if err := instrument.CollectedRequest(context.Background(), "TableManager.SyncTables", instrument.NewHistogramCollector(m.metrics.syncTableDuration), instrument.ErrorCode, func(ctx context.Context) error {
return m.SyncTables(ctx)
}); err != nil {
level.Error(util.Logger).Log("msg", "error syncing tables", "err", err)
}
// Sleep for a bit to spread the sync load across different times if the tablemanagers are all started at once.
select {
case <-time.After(time.Duration(rand.Int63n(int64(m.cfg.PollInterval)))):
case <-ctx.Done():
return nil
}
for {
select {
case <-ticker.C:
if err := instrument.CollectedRequest(context.Background(), "TableManager.SyncTables", instrument.NewHistogramCollector(m.metrics.syncTableDuration), instrument.ErrorCode, func(ctx context.Context) error {
return m.SyncTables(ctx)
}); err != nil {
level.Error(util.Logger).Log("msg", "error syncing tables", "err", err)
}
case <-ctx.Done():
return nil
}
}
}
// single iteration of bucket retention loop
func (m *TableManager) bucketRetentionIteration(ctx context.Context) error {
err := m.bucketClient.DeleteChunksBefore(ctx, mtime.Now().Add(-m.cfg.RetentionPeriod))
if err != nil {
level.Error(util.Logger).Log("msg", "error enforcing filesystem retention", "err", err)
}
// don't return error, otherwise timer service would stop.
return nil
}
// SyncTables will calculate the tables expected to exist, create those that do
// not and update those that need it. It is exposed for testing.
func (m *TableManager) SyncTables(ctx context.Context) error {
expected := m.calculateExpectedTables()
level.Info(util.Logger).Log("msg", "synching tables", "expected_tables", len(expected))
toCreate, toCheckThroughput, toDelete, err := m.partitionTables(ctx, expected)
if err != nil {
return err
}
if err := m.deleteTables(ctx, toDelete); err != nil {
return err
}
if err := m.createTables(ctx, toCreate); err != nil {
return err
}
if err := m.updateTables(ctx, toCheckThroughput); err != nil {
return err
}
m.metrics.lastSuccessfulSync.SetToCurrentTime()
return nil
}
func (m *TableManager) calculateExpectedTables() []TableDesc {
result := []TableDesc{}
for i, config := range m.schemaCfg.Configs {
// Consider configs which we are about to hit and requires tables to be created due to grace period
if config.From.Time.Time().After(mtime.Now().Add(m.cfg.CreationGracePeriod)) {
continue
}
if config.IndexTables.Period == 0 { // non-periodic table
if len(result) > 0 && result[len(result)-1].Name == config.IndexTables.Prefix {
continue // already got a non-periodic table with this name
}
table := TableDesc{
Name: config.IndexTables.Prefix,
ProvisionedRead: m.cfg.IndexTables.InactiveReadThroughput,
ProvisionedWrite: m.cfg.IndexTables.InactiveWriteThroughput,
UseOnDemandIOMode: m.cfg.IndexTables.InactiveThroughputOnDemandMode,
Tags: config.IndexTables.Tags,
}
isActive := true
if i+1 < len(m.schemaCfg.Configs) {
var (
endTime = m.schemaCfg.Configs[i+1].From.Unix()
gracePeriodSecs = int64(m.cfg.CreationGracePeriod / time.Second)
maxChunkAgeSecs = int64(m.maxChunkAge / time.Second)
now = mtime.Now().Unix()
)
if now >= endTime+gracePeriodSecs+maxChunkAgeSecs {
isActive = false
}
}
if isActive {
table.ProvisionedRead = m.cfg.IndexTables.ProvisionedReadThroughput
table.ProvisionedWrite = m.cfg.IndexTables.ProvisionedWriteThroughput
table.UseOnDemandIOMode = m.cfg.IndexTables.ProvisionedThroughputOnDemandMode
if m.cfg.IndexTables.WriteScale.Enabled {
table.WriteScale = m.cfg.IndexTables.WriteScale
table.UseOnDemandIOMode = false
}
if m.cfg.IndexTables.ReadScale.Enabled {
table.ReadScale = m.cfg.IndexTables.ReadScale
table.UseOnDemandIOMode = false
}
}
result = append(result, table)
} else {
endTime := mtime.Now().Add(m.cfg.CreationGracePeriod)
if i+1 < len(m.schemaCfg.Configs) {
nextFrom := m.schemaCfg.Configs[i+1].From.Time.Time()
if endTime.After(nextFrom) {
endTime = nextFrom
}
}
endModelTime := model.TimeFromUnix(endTime.Unix())
result = append(result, config.IndexTables.periodicTables(
config.From.Time, endModelTime, m.cfg.IndexTables, m.cfg.CreationGracePeriod, m.maxChunkAge, m.cfg.RetentionPeriod,
)...)
if config.ChunkTables.Prefix != "" {
result = append(result, config.ChunkTables.periodicTables(
config.From.Time, endModelTime, m.cfg.ChunkTables, m.cfg.CreationGracePeriod, m.maxChunkAge, m.cfg.RetentionPeriod,
)...)
}
}
}
sort.Sort(byName(result))
return result
}
// partitionTables works out tables that need to be created vs tables that need to be updated
func (m *TableManager) partitionTables(ctx context.Context, descriptions []TableDesc) ([]TableDesc, []TableDesc, []TableDesc, error) {
tables, err := m.client.ListTables(ctx)
if err != nil {
return nil, nil, nil, err
}
existingTables := make(map[string]struct{}, len(tables))
for _, table := range tables {
existingTables[table] = struct{}{}
}
expectedTables := make(map[string]TableDesc, len(descriptions))
for _, desc := range descriptions {
expectedTables[desc.Name] = desc
}
toCreate, toCheck, toDelete := []TableDesc{}, []TableDesc{}, []TableDesc{}
for _, expectedTable := range expectedTables {
if _, ok := existingTables[expectedTable.Name]; ok {
toCheck = append(toCheck, expectedTable)
} else {
toCreate = append(toCreate, expectedTable)
}
}
if m.cfg.RetentionPeriod > 0 {
// Ensure we only delete tables which have a prefix managed by Cortex.
tablePrefixes := map[string]struct{}{}
for _, cfg := range m.schemaCfg.Configs {
if cfg.IndexTables.Prefix != "" {
tablePrefixes[cfg.IndexTables.Prefix] = struct{}{}
}
if cfg.ChunkTables.Prefix != "" {
tablePrefixes[cfg.ChunkTables.Prefix] = struct{}{}
}
}
for existingTable := range existingTables {
if _, ok := expectedTables[existingTable]; !ok {
for tblPrefix := range tablePrefixes {
if strings.HasPrefix(existingTable, tblPrefix) {
toDelete = append(toDelete, TableDesc{Name: existingTable})
break
}
}
}
}
}
return toCreate, toCheck, toDelete, nil
}
func (m *TableManager) createTables(ctx context.Context, descriptions []TableDesc) error {
numFailures := 0
merr := tsdberrors.MultiError{}
for _, desc := range descriptions {
level.Info(util.Logger).Log("msg", "creating table", "table", desc.Name)
err := m.client.CreateTable(ctx, desc)
if err != nil {
numFailures++
merr.Add(err)
}
}
m.metrics.createFailures.Set(float64(numFailures))
return merr.Err()
}
func (m *TableManager) deleteTables(ctx context.Context, descriptions []TableDesc) error {
numFailures := 0
merr := tsdberrors.MultiError{}
for _, desc := range descriptions {
level.Info(util.Logger).Log("msg", "table has exceeded the retention period", "table", desc.Name)
if !m.cfg.RetentionDeletesEnabled {
continue
}
level.Info(util.Logger).Log("msg", "deleting table", "table", desc.Name)
err := m.client.DeleteTable(ctx, desc.Name)
if err != nil {
numFailures++
merr.Add(err)
}
}
m.metrics.deleteFailures.Set(float64(numFailures))
return merr.Err()
}
func (m *TableManager) updateTables(ctx context.Context, descriptions []TableDesc) error {
for _, expected := range descriptions {
level.Debug(util.Logger).Log("msg", "checking provisioned throughput on table", "table", expected.Name)
current, isActive, err := m.client.DescribeTable(ctx, expected.Name)
if err != nil {
return err
}
m.metrics.tableCapacity.WithLabelValues(readLabel, expected.Name).Set(float64(current.ProvisionedRead))
m.metrics.tableCapacity.WithLabelValues(writeLabel, expected.Name).Set(float64(current.ProvisionedWrite))
if m.cfg.ThroughputUpdatesDisabled {
continue
}
if !isActive {
level.Info(util.Logger).Log("msg", "skipping update on table, not yet ACTIVE", "table", expected.Name)
continue
}
if expected.Equals(current) {
level.Info(util.Logger).Log("msg", "provisioned throughput on table, skipping", "table", current.Name, "read", current.ProvisionedRead, "write", current.ProvisionedWrite)
continue
}
err = m.client.UpdateTable(ctx, current, expected)
if err != nil {
return err
}
}
return nil
}
// ExpectTables compares existing tables to an expected set of tables. Exposed
// for testing,
func ExpectTables(ctx context.Context, client TableClient, expected []TableDesc) error {
tables, err := client.ListTables(ctx)
if err != nil {
return err
}
if len(expected) != len(tables) {
return fmt.Errorf("Unexpected number of tables: %v != %v", expected, tables)
}
sort.Strings(tables)
sort.Sort(byName(expected))
for i, expect := range expected {
if tables[i] != expect.Name {
return fmt.Errorf("Expected '%s', found '%s'", expect.Name, tables[i])
}
desc, _, err := client.DescribeTable(ctx, expect.Name)
if err != nil {
return err
}
if !desc.Equals(expect) {
return fmt.Errorf("Expected '%#v', found '%#v' for table '%s'", expect, desc, desc.Name)
}
}
return nil
}