-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
part_search.go
278 lines (239 loc) · 7.34 KB
/
part_search.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
package storage
import (
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/blockcache"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
// partSearch represents blocks stream for the given search args
// passed to Init.
type partSearch struct {
// BlockRef contains the reference to the found block after NextBlock call.
BlockRef BlockRef
// p is the part to search.
p *part
// tsids contains sorted tsids to search.
tsids []TSID
// tsidIdx points to the currently searched tsid in tsids.
tsidIdx int
// tr is a time range to search.
tr TimeRange
metaindex []metaindexRow
bhs []blockHeader
compressedIndexBuf []byte
indexBuf []byte
err error
}
func (ps *partSearch) reset() {
ps.BlockRef.reset()
ps.p = nil
ps.tsids = nil
ps.tsidIdx = 0
ps.metaindex = nil
ps.bhs = nil
ps.compressedIndexBuf = ps.compressedIndexBuf[:0]
ps.indexBuf = ps.indexBuf[:0]
ps.err = nil
}
var isInTest = func() bool {
return strings.HasSuffix(os.Args[0], ".test")
}()
// Init initializes the ps with the given p, tsids and tr.
//
// tsids must be sorted.
// tsids cannot be modified after the Init call, since it is owned by ps.
func (ps *partSearch) Init(p *part, tsids []TSID, tr TimeRange) {
ps.reset()
ps.p = p
if p.ph.MinTimestamp <= tr.MaxTimestamp && p.ph.MaxTimestamp >= tr.MinTimestamp {
if isInTest && !sort.SliceIsSorted(tsids, func(i, j int) bool { return tsids[i].Less(&tsids[j]) }) {
logger.Panicf("BUG: tsids must be sorted; got %+v", tsids)
}
// take ownership of of tsids.
ps.tsids = tsids
}
ps.tr = tr
ps.metaindex = p.metaindex
// Advance to the first tsid. There is no need in checking
// the returned result, since it will be checked in NextBlock.
ps.nextTSID()
}
// NextBlock advances to the next BlockRef.
//
// Returns true on success.
//
// The blocks are sorted by (TDIS, MinTimestamp). Two subsequent blocks
// for the same TSID may contain overlapped time ranges.
func (ps *partSearch) NextBlock() bool {
for {
if ps.err != nil {
return false
}
if len(ps.bhs) == 0 {
if !ps.nextBHS() {
return false
}
}
if ps.searchBHS() {
return true
}
}
}
// Error returns the last error.
func (ps *partSearch) Error() error {
if ps.err == io.EOF {
return nil
}
return ps.err
}
func (ps *partSearch) nextTSID() bool {
if ps.tsidIdx >= len(ps.tsids) {
ps.err = io.EOF
return false
}
ps.BlockRef.bh.TSID = ps.tsids[ps.tsidIdx]
ps.tsidIdx++
return true
}
func (ps *partSearch) nextBHS() bool {
for len(ps.metaindex) > 0 {
// Optimization: skip tsid values smaller than the minimum value
// from ps.metaindex.
for ps.BlockRef.bh.TSID.Less(&ps.metaindex[0].TSID) {
if !ps.nextTSID() {
return false
}
}
// Invariant: ps.BlockRef.bh.TSID >= ps.metaindex[0].TSID
ps.metaindex = skipSmallMetaindexRows(ps.metaindex, &ps.BlockRef.bh.TSID)
// Invariant: len(ps.metaindex) > 0 && ps.BlockRef.bh.TSID >= ps.metaindex[0].TSID
mr := &ps.metaindex[0]
ps.metaindex = ps.metaindex[1:]
if ps.BlockRef.bh.TSID.Less(&mr.TSID) {
logger.Panicf("BUG: invariant violation: ps.BlockRef.bh.TSID cannot be smaller than mr.TSID; got %+v vs %+v", &ps.BlockRef.bh.TSID, &mr.TSID)
}
if mr.MaxTimestamp < ps.tr.MinTimestamp {
// Skip mr with too small timestamps.
continue
}
if mr.MinTimestamp > ps.tr.MaxTimestamp {
// Skip mr with too big timestamps.
continue
}
// Found the index block which may contain the required data
// for the ps.BlockRef.bh.TSID and the given timestamp range.
indexBlockKey := blockcache.Key{
Part: ps.p,
Offset: mr.IndexBlockOffset,
}
b := ibCache.GetBlock(indexBlockKey)
if b == nil {
// Slow path - actually read and unpack the index block.
ib, err := ps.readIndexBlock(mr)
if err != nil {
ps.err = fmt.Errorf("cannot read index block for part %q at offset %d with size %d: %w",
&ps.p.ph, mr.IndexBlockOffset, mr.IndexBlockSize, err)
return false
}
b = ib
ibCache.PutBlock(indexBlockKey, b)
}
ib := b.(*indexBlock)
ps.bhs = ib.bhs
return true
}
// No more metaindex rows to search.
ps.err = io.EOF
return false
}
func skipSmallMetaindexRows(metaindex []metaindexRow, tsid *TSID) []metaindexRow {
// Invariant: len(metaindex) > 0 && tsid >= metaindex[0].TSID.
if tsid.Less(&metaindex[0].TSID) {
logger.Panicf("BUG: invariant violation: tsid cannot be smaller than metaindex[0]; got %+v vs %+v", tsid, &metaindex[0].TSID)
}
if tsid.MetricID == metaindex[0].TSID.MetricID {
return metaindex
}
// Invariant: tsid > metaindex[0].TSID, so sort.Search cannot return 0.
n := sort.Search(len(metaindex), func(i int) bool {
return !metaindex[i].TSID.Less(tsid)
})
if n == 0 {
logger.Panicf("BUG: invariant violation: sort.Search returned 0 for tsid > metaindex[0].TSID; tsid=%+v; metaindex[0].TSID=%+v",
tsid, &metaindex[0].TSID)
}
// The given tsid may be located in the previous metaindex row,
// so go to the previous row.
// Suppose the following metaindex rows exist [tsid10, tsid20, tsid30].
// The following table contains the corresponding rows to start search for
// for the given tsid values greater than tsid10:
//
// * tsid11 -> tsid10
// * tsid20 -> tsid10, since tsid20 items may present in the index block [tsid10...tsid20]
// * tsid21 -> tsid20
// * tsid30 -> tsid20
// * tsid99 -> tsid30, since tsid99 items may be present in the index block [tsid30...tsidInf]
return metaindex[n-1:]
}
func (ps *partSearch) readIndexBlock(mr *metaindexRow) (*indexBlock, error) {
ps.compressedIndexBuf = bytesutil.ResizeNoCopyMayOverallocate(ps.compressedIndexBuf, int(mr.IndexBlockSize))
ps.p.indexFile.MustReadAt(ps.compressedIndexBuf, int64(mr.IndexBlockOffset))
var err error
ps.indexBuf, err = encoding.DecompressZSTD(ps.indexBuf[:0], ps.compressedIndexBuf)
if err != nil {
return nil, fmt.Errorf("cannot decompress index block: %w", err)
}
ib := &indexBlock{}
ib.bhs, err = unmarshalBlockHeaders(ib.bhs[:0], ps.indexBuf, int(mr.BlockHeadersCount))
if err != nil {
return nil, fmt.Errorf("cannot unmarshal index block: %w", err)
}
return ib, nil
}
func (ps *partSearch) searchBHS() bool {
for i := range ps.bhs {
bh := &ps.bhs[i]
nextTSID:
if bh.TSID.Less(&ps.BlockRef.bh.TSID) {
// Skip blocks with small tsid values.
continue
}
// Invariant: ps.BlockRef.bh.TSID <= bh.TSID
if bh.TSID.MetricID != ps.BlockRef.bh.TSID.MetricID {
// ps.BlockRef.bh.TSID < bh.TSID: no more blocks with the given tsid.
// Proceed to the next (bigger) tsid.
if !ps.nextTSID() {
return false
}
goto nextTSID
}
// Found the block with the given tsid. Verify timestamp range.
// While blocks for the same TSID are sorted by MinTimestamp,
// the may contain overlapped time ranges.
// So use linear search instead of binary search.
if bh.MaxTimestamp < ps.tr.MinTimestamp {
// Skip the block with too small timestamps.
continue
}
if bh.MinTimestamp > ps.tr.MaxTimestamp {
// Proceed to the next tsid, since the remaining blocks
// for the current tsid contain too big timestamps.
if !ps.nextTSID() {
return false
}
continue
}
// Found the tsid block with the matching timestamp range.
// Read it.
ps.BlockRef.init(ps.p, bh)
ps.bhs = ps.bhs[i+1:]
return true
}
ps.bhs = nil
return false
}