-
Notifications
You must be signed in to change notification settings - Fork 276
/
read.go
661 lines (595 loc) · 16.7 KB
/
read.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// Copyright 2021 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package blockio
import (
"context"
"math"
"time"
"github.com/matrixorigin/matrixone/pkg/common/mpool"
"github.com/matrixorigin/matrixone/pkg/container/batch"
"github.com/matrixorigin/matrixone/pkg/container/nulls"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/container/vector"
"github.com/matrixorigin/matrixone/pkg/fileservice"
"github.com/matrixorigin/matrixone/pkg/logutil"
"github.com/matrixorigin/matrixone/pkg/objectio"
"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
v2 "github.com/matrixorigin/matrixone/pkg/util/metric/v2"
"github.com/matrixorigin/matrixone/pkg/vm/engine"
"go.uber.org/zap"
)
type ReadFilter = func([]*vector.Vector) []int32
func ReadByFilter(
ctx context.Context,
info *objectio.BlockInfo,
inputDeletes []int64,
columns []uint16,
colTypes []types.Type,
ts types.TS,
filter ReadFilter,
fs fileservice.FileService,
mp *mpool.MPool,
) (sels []int32, err error) {
bat, release, err := LoadColumns(ctx, columns, colTypes, fs, info.MetaLocation(), mp, fileservice.Policy(0))
if err != nil {
return
}
defer release()
var deleteMask *nulls.Nulls
// merge persisted deletes
if !info.DeltaLocation().IsEmpty() {
now := time.Now()
var persistedDeletes *batch.Batch
var persistedByCN bool
var release func()
// load from storage
if persistedDeletes, persistedByCN, release, err = ReadBlockDelete(ctx, info.DeltaLocation(), fs); err != nil {
return
}
defer release()
readcost := time.Since(now)
var rows *nulls.Nulls
var bisect time.Duration
if persistedByCN {
rows = EvalDeleteRowsByTimestampForDeletesPersistedByCN(persistedDeletes, ts, info.CommitTs)
} else {
nowx := time.Now()
rows = EvalDeleteRowsByTimestamp(persistedDeletes, ts, &info.BlockID)
bisect = time.Since(nowx)
}
if rows != nil {
deleteMask = rows
}
readtotal := time.Since(now)
RecordReadDel(readtotal, readcost, bisect)
}
if deleteMask == nil {
deleteMask = nulls.NewWithSize(len(inputDeletes))
}
// merge input deletes
for _, row := range inputDeletes {
deleteMask.Add(uint64(row))
}
sels = filter(bat.Vecs)
// deslect deleted rows from sels
if !deleteMask.IsEmpty() {
var rows []int32
for _, row := range sels {
if !deleteMask.Contains(uint64(row)) {
rows = append(rows, row)
}
}
sels = rows
}
return
}
// BlockRead read block data from storage and apply deletes according given timestamp. Caller make sure metaloc is not empty
func BlockRead(
ctx context.Context,
info *objectio.BlockInfo,
inputDeletes []int64,
columns []uint16,
colTypes []types.Type,
ts timestamp.Timestamp,
filterSeqnums []uint16,
filterColTypes []types.Type,
filter ReadFilter,
fs fileservice.FileService,
mp *mpool.MPool,
vp engine.VectorPool,
policy fileservice.Policy,
) (*batch.Batch, error) {
if logutil.GetSkip1Logger().Core().Enabled(zap.DebugLevel) {
logutil.Debugf("read block %s, columns %v, types %v", info.BlockID.String(), columns, colTypes)
}
var (
sels []int32
err error
)
if filter != nil && info.Sorted {
if sels, err = ReadByFilter(
ctx, info, inputDeletes, filterSeqnums, filterColTypes,
types.TimestampToTS(ts), filter, fs, mp,
); err != nil {
return nil, err
}
v2.TaskSelReadFilterTotal.Inc()
if len(sels) == 0 {
RecordReadFilterSelectivity(1, 1)
v2.TaskSelReadFilterHit.Inc()
} else {
RecordReadFilterSelectivity(0, 1)
}
if len(sels) == 0 {
result := batch.NewWithSize(len(colTypes))
for i, typ := range colTypes {
if vp == nil {
result.Vecs[i] = vector.NewVec(typ)
} else {
result.Vecs[i] = vp.GetVector(typ)
}
}
return result, nil
}
}
columnBatch, err := BlockReadInner(
ctx, info, inputDeletes, columns, colTypes,
types.TimestampToTS(ts), sels, fs, mp, vp, policy,
)
if err != nil {
return nil, err
}
columnBatch.SetRowCount(columnBatch.Vecs[0].Length())
return columnBatch, nil
}
func BlockCompactionRead(
ctx context.Context,
location objectio.Location,
deletes []int64,
seqnums []uint16,
colTypes []types.Type,
fs fileservice.FileService,
mp *mpool.MPool,
) (*batch.Batch, error) {
loaded, release, err := LoadColumns(ctx, seqnums, colTypes, fs, location, mp, fileservice.Policy(0))
if err != nil {
return nil, err
}
defer release()
if len(deletes) == 0 {
return loaded, nil
}
result := batch.NewWithSize(len(loaded.Vecs))
for i, col := range loaded.Vecs {
typ := *col.GetType()
result.Vecs[i] = vector.NewVec(typ)
if err = vector.GetUnionAllFunction(typ, mp)(result.Vecs[i], col); err != nil {
break
}
result.Vecs[i].Shrink(deletes, true)
}
if err != nil {
for _, col := range result.Vecs {
if col != nil {
col.Free(mp)
}
}
return nil, err
}
result.SetRowCount(result.Vecs[0].Length())
return result, nil
}
func BlockReadInner(
ctx context.Context,
info *objectio.BlockInfo,
inputDeleteRows []int64,
columns []uint16,
colTypes []types.Type,
ts types.TS,
selectRows []int32, // if selectRows is not empty, it was already filtered by filter
fs fileservice.FileService,
mp *mpool.MPool,
vp engine.VectorPool,
policy fileservice.Policy,
) (result *batch.Batch, err error) {
var (
rowidPos int
deletedRows []int64
deleteMask nulls.Bitmap
loaded *batch.Batch
release func()
)
// read block data from storage specified by meta location
if loaded, rowidPos, deleteMask, release, err = readBlockData(
ctx, columns, colTypes, info, ts, fs, mp, vp, policy,
); err != nil {
return
}
defer release()
// assemble result batch for return
result = batch.NewWithSize(len(loaded.Vecs))
if len(selectRows) > 0 {
// NOTE: it always goes here if there is a filter and the block is sorted
// and there are selected rows after applying the filter and delete mask
// build rowid column if needed
if rowidPos >= 0 {
if loaded.Vecs[rowidPos], err = buildRowidColumn(
info, selectRows, mp, vp,
); err != nil {
return
}
}
// assemble result batch only with selected rows
for i, col := range loaded.Vecs {
typ := *col.GetType()
if typ.Oid == types.T_Rowid {
result.Vecs[i] = col
continue
}
if vp == nil {
result.Vecs[i] = vector.NewVec(typ)
} else {
result.Vecs[i] = vp.GetVector(typ)
}
if err = result.Vecs[i].Union(col, selectRows, mp); err != nil {
break
}
}
if err != nil {
for _, col := range result.Vecs {
if col != nil {
col.Free(mp)
}
}
}
return
}
// read deletes from storage specified by delta location
if !info.DeltaLocation().IsEmpty() {
var deletes *batch.Batch
var persistedByCN bool
var release func()
now := time.Now()
// load from storage
if deletes, persistedByCN, release, err = ReadBlockDelete(ctx, info.DeltaLocation(), fs); err != nil {
return
}
defer release()
readcost := time.Since(now)
// eval delete rows by timestamp
var rows *nulls.Nulls
var bisect time.Duration
if persistedByCN {
rows = EvalDeleteRowsByTimestampForDeletesPersistedByCN(deletes, ts, info.CommitTs)
} else {
nowx := time.Now()
rows = EvalDeleteRowsByTimestamp(deletes, ts, &info.BlockID)
bisect = time.Since(nowx)
}
// merge delete rows
deleteMask.Merge(rows)
readtotal := time.Since(now)
RecordReadDel(readtotal, readcost, bisect)
if logutil.GetSkip1Logger().Core().Enabled(zap.DebugLevel) {
logutil.Debugf(
"blockread %s read delete %d: base %s filter out %v\n",
info.BlockID.String(), deletes.RowCount(), ts.ToString(), deleteMask.Count())
}
}
// merge deletes from input
// deletes from storage + deletes from input
for _, row := range inputDeleteRows {
deleteMask.Add(uint64(row))
}
// Note: it always goes here if no filter or the block is not sorted
// transform delete mask to deleted rows
// TODO: avoid this transformation
if !deleteMask.IsEmpty() {
deletedRows = deleteMask.ToI64Arrary()
// logutil.Debugf("deleted/length: %d/%d=%f",
// len(deletedRows),
// loaded.Vecs[0].Length(),
// float64(len(deletedRows))/float64(loaded.Vecs[0].Length()))
}
// build rowid column if needed
if rowidPos >= 0 {
if loaded.Vecs[rowidPos], err = buildRowidColumn(
info, nil, mp, vp,
); err != nil {
return
}
}
// assemble result batch
for i, col := range loaded.Vecs {
typ := *col.GetType()
if typ.Oid == types.T_Rowid {
// rowid is already allocted by the mpool, no need to create a new vector
result.Vecs[i] = col
} else {
// for other types, we need to create a new vector
if vp == nil {
result.Vecs[i] = vector.NewVec(typ)
} else {
result.Vecs[i] = vp.GetVector(typ)
}
// copy the data from loaded vector to result vector
// TODO: avoid this allocation and copy
if err = vector.GetUnionAllFunction(typ, mp)(result.Vecs[i], col); err != nil {
break
}
}
// shrink the vector by deleted rows
if len(deletedRows) > 0 {
result.Vecs[i].Shrink(deletedRows, true)
}
}
// if any error happens, free the result batch allocated
if err != nil {
for _, col := range result.Vecs {
if col != nil {
col.Free(mp)
}
}
}
return
}
func getRowsIdIndex(colIndexes []uint16, colTypes []types.Type) (int, []uint16, []types.Type) {
idx := -1
for i, typ := range colTypes {
if typ.Oid == types.T_Rowid {
idx = i
break
}
}
if idx < 0 {
return idx, colIndexes, colTypes
}
idxes := make([]uint16, 0, len(colTypes)-1)
typs := make([]types.Type, 0, len(colTypes)-1)
idxes = append(idxes, colIndexes[:idx]...)
idxes = append(idxes, colIndexes[idx+1:]...)
typs = append(typs, colTypes[:idx]...)
typs = append(typs, colTypes[idx+1:]...)
return idx, idxes, typs
}
func buildRowidColumn(
info *objectio.BlockInfo,
sels []int32,
m *mpool.MPool,
vp engine.VectorPool,
) (col *vector.Vector, err error) {
if vp == nil {
col = vector.NewVec(objectio.RowidType)
} else {
col = vp.GetVector(objectio.RowidType)
}
if len(sels) == 0 {
err = objectio.ConstructRowidColumnTo(
col,
&info.BlockID,
0,
info.MetaLocation().Rows(),
m,
)
} else {
err = objectio.ConstructRowidColumnToWithSels(
col,
&info.BlockID,
sels,
m,
)
}
if err != nil {
col.Free(m)
col = nil
}
return
}
func readBlockData(
ctx context.Context,
colIndexes []uint16,
colTypes []types.Type,
info *objectio.BlockInfo,
ts types.TS,
fs fileservice.FileService,
m *mpool.MPool,
vp engine.VectorPool,
policy fileservice.Policy,
) (bat *batch.Batch, rowidPos int, deleteMask nulls.Bitmap, release func(), err error) {
rowidPos, idxes, typs := getRowsIdIndex(colIndexes, colTypes)
readColumns := func(cols []uint16) (result *batch.Batch, loaded *batch.Batch, err error) {
if len(cols) == 0 && rowidPos >= 0 {
// only read rowid column on non appendable block, return early
result = batch.NewWithSize(1)
// result.Vecs[0] = rowid
release = func() {}
return
}
if loaded, release, err = LoadColumns(ctx, cols, typs, fs, info.MetaLocation(), m, policy); err != nil {
return
}
colPos := 0
result = batch.NewWithSize(len(colTypes))
for i, typ := range colTypes {
if typ.Oid != types.T_Rowid {
result.Vecs[i] = loaded.Vecs[colPos]
colPos++
}
}
return
}
readABlkColumns := func(cols []uint16) (result *batch.Batch, deletes nulls.Bitmap, err error) {
var loaded *batch.Batch
// appendable block should be filtered by committs
cols = append(cols, objectio.SEQNUM_COMMITTS, objectio.SEQNUM_ABORT) // committs, aborted
// no need to add typs, the two columns won't be generated
if result, loaded, err = readColumns(cols); err != nil {
return
}
t0 := time.Now()
aborts := vector.MustFixedCol[bool](loaded.Vecs[len(loaded.Vecs)-1])
commits := vector.MustFixedCol[types.TS](loaded.Vecs[len(loaded.Vecs)-2])
for i := 0; i < len(commits); i++ {
if aborts[i] || commits[i].Greater(&ts) {
deletes.Add(uint64(i))
}
}
logutil.Debugf(
"blockread %s scan filter cost %v: base %s filter out %v\n ",
info.BlockID.String(), time.Since(t0), ts.ToString(), deletes.Count())
return
}
if info.EntryState {
bat, deleteMask, err = readABlkColumns(idxes)
} else {
bat, _, err = readColumns(idxes)
}
return
}
func ReadBlockDelete(ctx context.Context, deltaloc objectio.Location, fs fileservice.FileService) (bat *batch.Batch, isPersistedByCN bool, release func(), err error) {
isPersistedByCN, err = IsPersistedByCN(ctx, deltaloc, fs)
if err != nil {
return
}
bat, release, err = ReadBlockDeleteBySchema(ctx, deltaloc, fs, isPersistedByCN)
return
}
func ReadBlockDeleteBySchema(ctx context.Context, deltaloc objectio.Location, fs fileservice.FileService, isPersistedByCN bool) (bat *batch.Batch, release func(), err error) {
var cols []uint16
if isPersistedByCN {
cols = []uint16{0, 1}
} else {
cols = []uint16{0, 1, 2, 3}
}
bat, release, err = LoadTombstoneColumns(ctx, cols, nil, fs, deltaloc, nil)
return
}
func IsPersistedByCN(ctx context.Context, deltaloc objectio.Location, fs fileservice.FileService) (bool, error) {
objectMeta, err := objectio.FastLoadObjectMeta(ctx, &deltaloc, false, fs)
if err != nil {
return false, err
}
meta, ok := objectMeta.TombstoneMeta()
if !ok {
meta = objectMeta.MustDataMeta()
}
blkmeta := meta.GetBlockMeta(uint32(deltaloc.ID()))
columnCount := blkmeta.GetColumnCount()
return columnCount == 2, nil
}
func EvalDeleteRowsByTimestamp(deletes *batch.Batch, ts types.TS, blockid *types.Blockid) (rows *nulls.Bitmap) {
if deletes == nil {
return
}
// record visible delete rows
rows = nulls.NewWithSize(64)
rowids := vector.MustFixedCol[types.Rowid](deletes.Vecs[0])
tss := vector.MustFixedCol[types.TS](deletes.Vecs[1])
aborts := deletes.Vecs[3]
start, end := FindIntervalForBlock(rowids, blockid)
for i := start; i < end; i++ {
abort := vector.GetFixedAt[bool](aborts, i)
if abort || tss[i].Greater(&ts) {
continue
}
row := rowids[i].GetRowOffset()
rows.Add(uint64(row))
}
return
}
func EvalDeleteRowsByTimestampForDeletesPersistedByCN(deletes *batch.Batch, ts types.TS, committs types.TS) (rows *nulls.Bitmap) {
if deletes == nil || ts.Less(&committs) {
return
}
// record visible delete rows
rows = nulls.NewWithSize(0)
rowids := vector.MustFixedCol[types.Rowid](deletes.Vecs[0])
for _, rowid := range rowids {
row := rowid.GetRowOffset()
rows.Add(uint64(row))
}
return
}
// BlockPrefetch is the interface for cn to call read ahead
// columns Which columns should be taken for columns
// service fileservice
// infos [s3object name][block]
func BlockPrefetch(idxes []uint16, service fileservice.FileService, infos [][]*objectio.BlockInfo, prefetchFile bool) error {
// Generate prefetch task
for i := range infos {
// build reader
pref, err := BuildPrefetchParams(service, infos[i][0].MetaLocation())
if err != nil {
return err
}
for _, info := range infos[i] {
pref.AddBlock(idxes, []uint16{info.MetaLocation().ID()})
if !info.DeltaLocation().IsEmpty() {
// Need to read all delete
err = PrefetchTombstone([]uint16{0, 1, 2}, []uint16{info.DeltaLocation().ID()}, service, info.DeltaLocation())
if err != nil {
return err
}
}
}
pref.prefetchFile = prefetchFile
err = pipeline.Prefetch(pref)
if err != nil {
return err
}
}
return nil
}
func RecordReadDel(total, read, bisect time.Duration) {
pipeline.stats.selectivityStats.RecordReadDel(total, read, bisect)
}
func RecordReadFilterSelectivity(hit, total int) {
pipeline.stats.selectivityStats.RecordReadFilterSelectivity(hit, total)
}
func RecordBlockSelectivity(hit, total int) {
pipeline.stats.selectivityStats.RecordBlockSelectivity(hit, total)
}
func RecordColumnSelectivity(hit, total int) {
pipeline.stats.selectivityStats.RecordColumnSelectivity(hit, total)
}
func ExportSelectivityString() string {
return pipeline.stats.selectivityStats.ExportString()
}
func FindIntervalForBlock(rowids []types.Rowid, id *types.Blockid) (start int, end int) {
lowRowid := objectio.NewRowid(id, 0)
highRowid := objectio.NewRowid(id, math.MaxUint32)
i, j := 0, len(rowids)
for i < j {
m := (i + j) / 2
// first value >= lowRowid
if !rowids[m].Less(*lowRowid) {
j = m
} else {
i = m + 1
}
}
start = i
i, j = 0, len(rowids)
for i < j {
m := (i + j) / 2
// first value > highRowid
if highRowid.Less(rowids[m]) {
j = m
} else {
i = m + 1
}
}
end = i
return
}