-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathplan.go
More file actions
187 lines (162 loc) · 5.16 KB
/
plan.go
File metadata and controls
187 lines (162 loc) · 5.16 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
// 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 convert
import (
"cmp"
"slices"
"time"
"github.com/thanos-io/thanos-parquet-gateway/internal/util"
"github.com/thanos-io/thanos-parquet-gateway/schema"
"github.com/thanos-io/thanos/pkg/block/metadata"
)
type Step struct {
Date util.Date // Date for which we are building a parquet block.
Sources []metadata.Meta // Source TSDB blocks we will use to generate a parquet block.
}
// isFullyCovered returns true if blocks for this date cover the whole day.
// We return true even if there are gaps in coverage, we only care that there
// are blocks that cover both min and max timestamps.
func (s Step) isFullyCovered() bool {
mint := s.Date.MinT()
maxt := s.Date.MaxT()
var gotMin, gotMax bool
for _, s := range s.Sources {
if s.MinTime <= mint {
gotMin = true
}
if s.MaxTime >= maxt {
gotMax = true
}
}
return gotMin && gotMax
}
type Plan struct {
Steps []Step
}
type Planner struct {
// Do not create parquet blocks that are younger then this.
notAfter time.Time
// Do not create parquet blocks that are older then this.
notBefore time.Time
// Maximum number of days to plan conversions for. Planner might still produce a plan with more days
// if it would be converting a TSDB block that spans over more days, to avoid re-downloading that block
// for the next plan.
maxDays int
}
func NewPlanner(notAfter, notBefore time.Time, maxDays int) Planner {
return Planner{notAfter: notAfter, notBefore: notBefore, maxDays: maxDays}
}
func (p Planner) Plan(tsdbMetas map[string]metadata.Meta, parquetMetas map[string]schema.Meta) Plan {
// Make a list of days covered by TSDB blocks.
tsdbDates := map[util.Date][]metadata.Meta{}
for _, tsdb := range tsdbMetas {
for _, partialDate := range util.SplitIntoDates(tsdb.MinTime, tsdb.MaxTime) {
tsdbDates[partialDate] = append(tsdbDates[partialDate], tsdb)
}
}
// Make a list of days covered by parquet blocks.
pqDates := map[util.Date]struct{}{}
for _, pq := range parquetMetas {
for _, partialDate := range util.SplitIntoDates(pq.Mint, pq.Maxt) {
pqDates[partialDate] = struct{}{}
}
}
// Find TSDB dates not covered by parquet dates.
steps := make([]Step, 0, len(tsdbDates))
for date, metas := range tsdbDates {
if !date.ToTime().Before(p.notAfter) {
// Ignore TSDB blocks that are for dates excluded from conversions.
continue
}
if date.ToTime().Before(p.notBefore) {
// Ignore TSDB blocks that are too old.
continue
}
if _, ok := pqDates[date]; ok {
// This date is already covered by a parquet block.
continue
}
// Sort our tsdb block metas from oldest to the newest.
slices.SortFunc(metas, func(a, b metadata.Meta) int {
return cmp.Compare(a.MinTime, b.MinTime)
})
steps = append(steps, Step{
Date: date,
Sources: metas,
})
}
// Sort our days, most recent first.
slices.SortFunc(steps, func(a, b Step) int {
return cmp.Compare(b.Date.MinT(), a.Date.MinT())
})
// Remove the most recent day if it's not fully covered by TSDB blocks.
// We do this because we might get some delayed blocks for it.
// Any gaps in days older than the most recent one are ignored.
steps = truncateLastPartialDay(steps)
// Restrict our plan to have only up to maxDays number of steps.
// But allow more steps if they come from TSDB blocks that are only on the plan.
steps = limitSteps(steps, p.maxDays)
return Plan{Steps: steps}
}
// Get rid of the most recent day if we don't have TSDB blocks for the whole day.
func truncateLastPartialDay(steps []Step) []Step {
// Empty source, return it as is.
if len(steps) < 1 {
return steps
}
// Most recent day is NOT fully covered, return all but most recent day.
if !steps[0].isFullyCovered() {
return steps[1:]
}
// Return as is.
return steps
}
// Limit the plan to specified max number of days.
// This is a soft limit, final plan might have more days if it makes sense.
func limitSteps(steps []Step, limit int) []Step {
if len(steps) <= limit {
// Plan is <= the limit, return it as is.
return steps
}
metas := MergeMetas(steps[:limit])
for i := limit; i < len(steps); i++ {
// Loop over all excess days and check if they would require a new block.
// If yes then exclude them from the plan.
// If no then keep them on the plan.
var newBlock bool
for _, meta := range steps[i].Sources {
if !slices.ContainsFunc(metas, func(m metadata.Meta) bool {
return meta.ULID == m.ULID
}) {
newBlock = true
}
}
if newBlock {
break
}
limit = i + 1
}
return steps[:limit]
}
// Merge dates from all steps into a single slice.
func MergeDates(steps []Step) []util.Date {
dates := make([]util.Date, 0, len(steps))
for _, step := range steps {
dates = append(dates, step.Date)
}
return dates
}
// Merge TSDB block metas from all steps into a single slice.
func MergeMetas(steps []Step) (metas []metadata.Meta) {
for _, step := range steps {
for _, m := range step.Sources {
if !slices.ContainsFunc(metas, func(meta metadata.Meta) bool {
return meta.ULID == m.ULID
}) {
metas = append(metas, m)
}
}
}
return metas
}