/
multicolreader.go
386 lines (330 loc) · 12.7 KB
/
multicolreader.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
// Copyright (c) 2021-2024 SigScalr, Inc.
//
// This file is part of SigLens Observability Solution
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package segread
import (
"errors"
"fmt"
"os"
"sort"
"github.com/cespare/xxhash"
"github.com/siglens/siglens/pkg/blob"
"github.com/siglens/siglens/pkg/common/fileutils"
"github.com/siglens/siglens/pkg/config"
"github.com/siglens/siglens/pkg/segment/structs"
"github.com/siglens/siglens/pkg/segment/utils"
"github.com/siglens/siglens/pkg/segment/writer"
toputils "github.com/siglens/siglens/pkg/utils"
log "github.com/sirupsen/logrus"
)
/*
Defines holder struct and functions to construct & manage SegmentFileReaders
across multiple columns
*/
type MultiColSegmentReader struct {
allFileReaders []*SegmentFileReader
allColsReverseIndex map[string]int
timeStampKey string // timestamp key
segKey string // segment key
timeReader *TimeRangeReader
AllColums []*ColumnInfo
allColInfoReverseIndex map[string]*ColumnInfo
}
type ColumnInfo struct {
ColumnName string
count int
}
/*
Defines holder struct and functions to construct & manage SegmentFileReaders
across multiple columns
*/
type SharedMultiColReaders struct {
MultiColReaders []*MultiColSegmentReader
allFDs map[string]*os.File // all fds shared across MultiColSegmentReaders
allInUseFiles []string // all files that need to be released by blob
numReaders int
numOpenFDs int64
}
/*
Initialize a new MultipleColumnSegmentReader. This can be used to load & read any number of columns at once across any blocks
Caller is responsible for calling .CloseAll() to close all the fds.
Can also be used to get the timestamp for any arbitrary record in the Segment
*/
func initNewMultiColumnReader(segKey string, colFDs map[string]*os.File, blockMetadata map[uint16]*structs.BlockMetadataHolder,
blockSummaries []*structs.BlockSummary, qid uint64) (*MultiColSegmentReader, error) {
readCols := make([]*ColumnInfo, 0)
readColsReverseIndex := make(map[string]*ColumnInfo)
colRevserseIndex := make(map[string]int)
allFileReaders := make([]*SegmentFileReader, len(colFDs))
tsKey := config.GetTimeStampKey()
var idx int = 0
retVal := &MultiColSegmentReader{
allFileReaders: allFileReaders,
allColsReverseIndex: colRevserseIndex,
timeStampKey: tsKey,
segKey: segKey,
}
for colName, colFD := range colFDs {
if colName == tsKey {
blkRecCount := make(map[uint16]uint16)
for blkIdx, blkSum := range blockSummaries {
blkRecCount[uint16(blkIdx)] = blkSum.RecCount
}
currTimeReader, err := InitNewTimeReaderWithFD(colFD, tsKey, blockMetadata, blkRecCount, qid)
if err != nil {
log.Errorf("qid=%d, initNewMultiColumnReader: failed initialize timestamp reader for using timestamp key %s and segkey %s. Error: %v",
qid, tsKey, segKey, err)
} else {
retVal.timeReader = currTimeReader
}
continue
}
segReader, err := InitNewSegFileReader(colFD, colName, blockMetadata, qid, blockSummaries)
if err != nil {
log.Errorf("qid=%d, initNewMultiColumnReader: failed initialize segfile reader for column %s Using file %s. Error: %v",
qid, colName, colFD.Name(), err)
continue
}
allFileReaders[idx] = segReader
colRevserseIndex[colName] = idx
currCol := &ColumnInfo{ColumnName: colName, count: 0}
readCols = append(readCols, currCol)
readColsReverseIndex[colName] = currCol
idx++
}
retVal.allFileReaders = retVal.allFileReaders[:idx]
retVal.AllColums = readCols[:idx]
retVal.allColInfoReverseIndex = readColsReverseIndex
return retVal, nil
}
/*
Inializes N MultiColumnSegmentReaders, each of which share the same file descriptor.
Only columns that exist will be loaded, not guaranteed to load all columnns in colNames
It is up to the caller to close the open FDs using .Close()
*/
func InitSharedMultiColumnReaders(segKey string, colNames map[string]bool, blockMetadata map[uint16]*structs.BlockMetadataHolder,
blockSummaries []*structs.BlockSummary, numReaders int, qid uint64) (*SharedMultiColReaders, error) {
allInUseSegSetFiles := make([]string, 0)
maxOpenFds := int64(0)
for cname := range colNames {
if cname != "*" {
maxOpenFds += 1
}
}
maxOpenFds += 2 + 1 // for time rollup files
allFDs := make(map[string]*os.File)
sharedReader := &SharedMultiColReaders{
MultiColReaders: make([]*MultiColSegmentReader, numReaders),
numReaders: numReaders,
numOpenFDs: maxOpenFds,
allFDs: allFDs,
}
err := fileutils.GLOBAL_FD_LIMITER.TryAcquireWithBackoff(maxOpenFds, 10, fmt.Sprintf("InitSharedMultiColumnReaders.qid=%d", qid))
if err != nil {
log.Errorf("qid=%d, Failed to acquire resources to be able to open %+v FDs. Error: %+v", qid, maxOpenFds, err)
return sharedReader, err
}
bulkDownloadFiles := make(map[string]string)
var fName string
for cname := range colNames {
if cname == "" {
return nil, fmt.Errorf("InitSharedMultiColumnReaders: unknown seg set col")
} else if cname == "*" {
continue
} else {
fName = fmt.Sprintf("%v_%v.csg", segKey, xxhash.Sum64String(cname))
}
bulkDownloadFiles[fName] = cname
}
err = blob.BulkDownloadSegmentBlob(bulkDownloadFiles, true)
if err != nil {
log.Errorf("qid=%d, initNewMultiColumnReader failed to bulk download seg files. err=%v", qid, err)
return nil, err
}
for fName, colName := range bulkDownloadFiles {
fName := fName
currFd, err := os.OpenFile(fName, os.O_RDONLY, 0644)
if err != nil {
log.Errorf("qid=%d, initNewMultiColumnReader: failed to open file %s for columns %s. Error: %v.",
qid, fName, colName, err)
continue
}
sharedReader.allFDs[colName] = currFd
allInUseSegSetFiles = append(allInUseSegSetFiles, fName)
}
for i := 0; i < numReaders; i++ {
currReader, err := initNewMultiColumnReader(segKey, sharedReader.allFDs, blockMetadata, blockSummaries, qid)
if err != nil {
sharedReader.Close()
err := blob.SetSegSetFilesAsNotInUse(allInUseSegSetFiles)
if err != nil {
log.Errorf("qid=%d, Failed to release needed segment files from local storage %+v! Err: %+v", qid, allInUseSegSetFiles, err)
}
return sharedReader, err
}
sharedReader.MultiColReaders[i] = currReader
}
sharedReader.allInUseFiles = allInUseSegSetFiles
return sharedReader, nil
}
// Returns all buffers to the pools, closes all FDs shared across multi readers, and updates global semaphore
func (scr *SharedMultiColReaders) Close() {
for _, multiReader := range scr.MultiColReaders {
multiReader.returnBuffers()
}
for _, reader := range scr.allFDs {
if reader != nil {
err := reader.Close()
if err != nil {
log.Errorf("Failed to close fd! err: %+v", err)
}
}
}
err := blob.SetSegSetFilesAsNotInUse(scr.allInUseFiles)
if err != nil {
log.Errorf("Failed to release needed segment files from local storage %+v! Err: %+v", scr.allInUseFiles, err)
}
fileutils.GLOBAL_FD_LIMITER.Release(scr.numOpenFDs)
}
func (mcsr *MultiColSegmentReader) GetTimeStampForRecord(blockNum uint16, recordNum uint16, qid uint64) (uint64, error) {
if mcsr.timeReader == nil {
log.Errorf("qid=%v, Tried to get timestamp using a multi reader wihout an initialized timeReader", qid)
return 0, errors.New("uninitialized timerange reader")
}
return mcsr.timeReader.GetTimeStampForRecord(blockNum, recordNum, qid)
}
func (mcsr *MultiColSegmentReader) GetAllTimeStampsForBlock(blockNum uint16) ([]uint64, error) {
if mcsr.timeReader == nil {
log.Errorf("Tried to get all block timestamps using a multi reader wihout an initialized timeReader")
return nil, errors.New("uninitialized timerange reader")
}
return mcsr.timeReader.GetAllTimeStampsForBlock(blockNum)
}
// Reads the raw value and returns the []byte in TLV format (type-[length]-value encoding)
func (mcsr *MultiColSegmentReader) ReadRawRecordFromColumnFile(col string, blockNum uint16, recordNum uint16, qid uint64) ([]byte, error) {
if col == mcsr.timeStampKey {
ts, err := mcsr.GetTimeStampForRecord(blockNum, recordNum, qid)
if err != nil {
return nil, err
}
retVal := make([]byte, 9)
copy(retVal[0:], utils.VALTYPE_ENC_UINT64[:])
copy(retVal[1:], toputils.Uint64ToBytesLittleEndian(ts))
return retVal, nil
}
keyIndex, ok := mcsr.allColsReverseIndex[col]
if !ok {
// Debug to avoid log flood for when the column does not exist
log.Debugf("ReadRawRecordFromColumnFile: failed to find column %s in muli col reader. All cols %+v", col, mcsr.allColsReverseIndex)
return nil, errors.New("column not found in MultipleColumnSegmentReader")
}
return mcsr.allFileReaders[keyIndex].ReadRecordFromBlock(blockNum, recordNum)
}
// Reads the request value and converts it to a *utils.CValueEnclosure
func (mcsr *MultiColSegmentReader) ExtractValueFromColumnFile(col string, blockNum uint16, recordNum uint16,
qid uint64) (*utils.CValueEnclosure, error) {
if col == mcsr.timeStampKey {
ts, err := mcsr.GetTimeStampForRecord(blockNum, recordNum, qid)
if err != nil {
return &utils.CValueEnclosure{}, err
}
return &utils.CValueEnclosure{
Dtype: utils.SS_DT_UNSIGNED_NUM,
CVal: ts,
}, nil
}
rawVal, err := mcsr.ReadRawRecordFromColumnFile(col, blockNum, recordNum, qid)
if err != nil {
return &utils.CValueEnclosure{
Dtype: utils.SS_DT_BACKFILL,
CVal: nil,
}, err
}
cval, _, err := writer.GetCvalFromRec(rawVal, qid)
return &cval, err
}
func (mcsr *MultiColSegmentReader) returnBuffers() {
if mcsr.allFileReaders != nil {
for _, reader := range mcsr.allFileReaders {
if reader != nil {
reader.returnBuffers()
}
}
}
if mcsr.timeReader != nil {
mcsr.timeReader.returnBuffers()
}
}
func (mcsr *MultiColSegmentReader) IncrementColumnUsage(colName string) {
mcsr.allColInfoReverseIndex[colName].count++
}
// reorders mcsr.AllColumns to be ordered on usage
func (mcsr *MultiColSegmentReader) ReorderColumnUsage() {
sort.Slice(mcsr.AllColums, func(i, j int) bool {
return mcsr.AllColums[i].count > mcsr.AllColums[j].count
})
}
func (mcsr *MultiColSegmentReader) IsBlkDictEncoded(cname string,
blkNum uint16) (bool, error) {
// reads the csg file and decides whether this particular block is encoded via dictionary encoding
// or raw csg encoding, and returns if it is dict-enc, along with the map of each dict-key => recNums pairing
keyIndex, ok := mcsr.allColsReverseIndex[cname]
if !ok {
// Debug to avoid log flood for when the column does not exist
log.Debugf("IsBlkDictEncoded: failed to find column %s in muli col reader. All cols %+v", cname, mcsr.allColsReverseIndex)
return false, errors.New("column not found in MultipleColumnSegmentReader")
}
return mcsr.allFileReaders[keyIndex].IsBlkDictEncoded(blkNum)
}
/*
parameters:
results: map of recNum -> colName -> colValue to be filled in.
col: columnName
blockNum: blocknum to search for
rnMap: map of recordNumbers to for which to find the colValue for the given colname
returns:
bool: if we are able to find the requested column in dict encoding
*/
func (mcsr *MultiColSegmentReader) GetDictEncCvalsFromColFile(results map[uint16]map[string]interface{},
col string, blockNum uint16, orderedRecNums []uint16, qid uint64) bool {
keyIndex, ok := mcsr.allColsReverseIndex[col]
if !ok {
return false
}
return mcsr.allFileReaders[keyIndex].GetDictEncCvalsFromColFile(results, blockNum, orderedRecNums)
}
func (mcsr *MultiColSegmentReader) ApplySearchToMatchFilterDictCsg(match *structs.MatchFilter,
bsh *structs.BlockSearchHelper, cname string) (bool, error) {
keyIndex, ok := mcsr.allColsReverseIndex[cname]
if !ok {
return false, errors.New("could not find sfr for cname")
}
return mcsr.allFileReaders[keyIndex].ApplySearchToMatchFilterDictCsg(match, bsh)
}
func (mcsr *MultiColSegmentReader) ApplySearchToExpressionFilterDictCsg(qValDte *utils.DtypeEnclosure,
fop utils.FilterOperator, isRegexSearch bool, bsh *structs.BlockSearchHelper,
cname string) (bool, error) {
keyIndex, ok := mcsr.allColsReverseIndex[cname]
if !ok {
return false, errors.New("could not find sfr for cname")
}
return mcsr.allFileReaders[keyIndex].ApplySearchToExpressionFilterDictCsg(qValDte,
fop, isRegexSearch, bsh)
}
func (mcsr *MultiColSegmentReader) IsColPresent(cname string) bool {
_, ok := mcsr.allColsReverseIndex[cname]
return ok
}