forked from m3db/m3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoded_block_builder.go
217 lines (188 loc) · 6.07 KB
/
encoded_block_builder.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package m3db
import (
"bytes"
"sort"
"time"
"github.com/m3db/m3/src/dbnode/encoding"
"github.com/m3db/m3/src/query/block"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/ts/m3db/consolidators"
"github.com/m3db/m3x/ident"
)
const initBlockLength = 10
type blockAtTime struct {
time time.Time
block encodedBlock
}
type blocksAtTime []blockAtTime
type encodedBlockBuilder struct {
lookback time.Duration
blocksAtTime blocksAtTime
tagOptions models.TagOptions
consolidationFn consolidators.ConsolidationFunc
}
func newEncodedBlockBuilder(
opts Options,
) *encodedBlockBuilder {
lookback := opts.LookbackDuration()
tagOptions := opts.TagOptions()
consolidationFn := opts.ConsolidationFunc()
return &encodedBlockBuilder{
lookback: lookback,
blocksAtTime: make(blocksAtTime, 0, initBlockLength),
tagOptions: tagOptions,
consolidationFn: consolidationFn,
}
}
func (b *encodedBlockBuilder) add(
bounds models.Bounds,
iter encoding.SeriesIterator,
lastBlock bool,
) {
start := bounds.Start
consolidation := consolidationSettings{
consolidationFn: consolidators.TakeLast,
currentTime: start,
bounds: bounds,
}
for idx, bl := range b.blocksAtTime {
if bl.time.Equal(start) {
block := bl.block
block.seriesBlockIterators = append(block.seriesBlockIterators, iter)
b.blocksAtTime[idx].block = block
return
}
}
block := newEncodedBlock(
[]encoding.SeriesIterator{},
b.tagOptions,
consolidation,
b.lookback,
lastBlock,
)
block.seriesBlockIterators = append(block.seriesBlockIterators, iter)
b.blocksAtTime = append(b.blocksAtTime, blockAtTime{
time: start,
block: block,
})
}
func (b *encodedBlockBuilder) build() ([]block.Block, error) {
if err := b.backfillMissing(); err != nil {
return nil, err
}
// Sort blocks by ID.
for _, bl := range b.blocksAtTime {
sort.Sort(seriesIteratorByID(bl.block.seriesBlockIterators))
}
blocks := make([]block.Block, 0, len(b.blocksAtTime))
for _, bl := range b.blocksAtTime {
block := bl.block
if err := block.generateMetas(); err != nil {
return nil, err
}
blocks = append(blocks, &block)
}
return blocks, nil
}
// Since some series can be missing if there are no values in composing blocks,
// need to backfill these, since downstream functions rely on series order.
//
// TODO: this will be removed after https://github.com/m3db/m3/issues/1281 is
// completed, which will allow temporal functions, and final read accumulator
// to empty series, and do its own matching rather than relying on matching
// series order. This is functionally throwaway code and should be regarded
// as such.
func (b *encodedBlockBuilder) backfillMissing() error {
// map series to indeces of b.blocksAtTime at which they are seen
seenMap := make(map[string]seriesIteratorDetails, initBlockReplicaLength)
for idx, bl := range b.blocksAtTime {
block := bl.block
for _, iter := range block.seriesBlockIterators {
id := iter.ID().String()
if seen, found := seenMap[id]; !found {
seenMap[id] = seriesIteratorDetails{
start: iter.Start(),
end: iter.End(),
id: iter.ID(),
ns: iter.Namespace(),
tagIter: iter.Tags(),
present: []int{idx},
}
} else {
seen.present = append(seen.present, idx)
seenMap[id] = seen
}
}
}
// make sure that each seen series exists in every block.
blockLen := len(b.blocksAtTime)
for _, iterDetails := range seenMap {
present := iterDetails.present
if len(present) == blockLen {
// This series exists in every block already, thus no backfilling necessary.
continue
}
for blockIdx, bl := range b.blocksAtTime {
found := false
for _, presentVal := range present {
if presentVal == blockIdx {
found = true
break
}
}
// this series exists in the current block.
if found {
continue
}
// blockIdx does not contain the present value; need to populate it with
// an empty series iterator.
tags, err := cloneTagIterator(iterDetails.tagIter)
if err != nil {
return err
}
iter := encoding.NewSeriesIterator(encoding.SeriesIteratorOptions{
ID: ident.StringID(iterDetails.id.String()),
Namespace: ident.StringID(iterDetails.ns.String()),
Tags: tags,
StartInclusive: iterDetails.start,
EndExclusive: iterDetails.end,
}, nil)
block := bl.block
block.seriesBlockIterators = append(block.seriesBlockIterators, iter)
b.blocksAtTime[blockIdx].block = block
}
}
return nil
}
type seriesIteratorDetails struct {
start, end time.Time
id, ns ident.ID
tagIter ident.TagIterator
// NB: the indices that this series iterator exists in already.
present []int
}
type seriesIteratorByID []encoding.SeriesIterator
func (s seriesIteratorByID) Len() int { return len(s) }
func (s seriesIteratorByID) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s seriesIteratorByID) Less(i, j int) bool {
return bytes.Compare(s[i].ID().Bytes(), s[j].ID().Bytes()) == -1
}