-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathplan.go
More file actions
252 lines (218 loc) · 6.91 KB
/
plan.go
File metadata and controls
252 lines (218 loc) · 6.91 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
// 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.
ExternalLabels schema.ExternalLabels
}
// 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
// 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 time.Time, maxDays int) Planner {
return Planner{notAfter: notAfter, maxDays: maxDays}
}
func (p Planner) planStream(tsdb schema.TSDBBlocksStream, parquet schema.ParquetBlocksStream, minTimeDate string, maxTimeDate string) Plan {
var (
windowActive bool
winStartDay time.Time
winEndDay time.Time
)
if minTimeDate != "" || maxTimeDate != "" {
windowActive = true
// Parse minDate and round to start of day (00:00:00 UTC, inclusive)
if minTimeDate != "" {
t, err := time.Parse("2006-01-02", minTimeDate)
if err != nil {
// Invalid date format, disable window filtering
windowActive = false
} else {
winStartDay = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)
}
}
// Parse maxDate and round to start of next day (00:00:00 UTC, exclusive)
if maxTimeDate != "" {
t, err := time.Parse("2006-01-02", maxTimeDate)
if err != nil {
// Invalid date format, disable window filtering
windowActive = false
} else {
// Add 1 day to make the boundary exclusive
winEndDay = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, time.UTC)
}
}
}
// Make a list of days covered by TSDB blocks.
tsdbDates := map[util.Date][]metadata.Meta{}
for _, tsdb := range tsdb.Metas {
for _, partialDate := range util.SplitIntoDates(tsdb.MinTime, tsdb.MaxTime) {
if windowActive {
pt := partialDate.ToTime()
if minTimeDate != "" && pt.Before(winStartDay) {
continue
}
if maxTimeDate != "" && !pt.Before(winEndDay) {
continue
}
}
tsdbDates[partialDate] = append(tsdbDates[partialDate], tsdb)
}
}
pqDates := map[util.Date]struct{}{}
for _, pq := range parquet.Metas {
for _, partialDate := range util.SplitIntoDates(pq.Mint, pq.Maxt) {
if windowActive {
pt := partialDate.ToTime()
if minTimeDate != "" && pt.Before(winStartDay) {
continue
}
if maxTimeDate != "" && !pt.Before(winEndDay) {
continue
}
}
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 _, 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}
}
func (p Planner) Plan(tsdbStreams map[schema.ExternalLabelsHash]schema.TSDBBlocksStream, parquetStreams map[schema.ExternalLabelsHash]schema.ParquetBlocksStream, minTimeDate string, maxTimeDate string) Plan {
outPlan := Plan{Steps: []Step{}}
for tsdbEH := range tsdbStreams {
parquet, ok := parquetStreams[tsdbEH]
if !ok {
parquet = schema.ParquetBlocksStream{}
}
streamPlan := p.planStream(tsdbStreams[tsdbEH], parquet, minTimeDate, maxTimeDate)
for i := range streamPlan.Steps {
streamPlan.Steps[i].ExternalLabels = tsdbStreams[tsdbEH].ExternalLabels
}
outPlan.Steps = append(outPlan.Steps, streamPlan.Steps...)
}
return outPlan
}
// 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
}