/
flushblk.go
122 lines (111 loc) · 3.24 KB
/
flushblk.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
// 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 jobs
import (
"context"
"math/rand"
"time"
"github.com/matrixorigin/matrixone/pkg/perfcounter"
"github.com/matrixorigin/matrixone/pkg/objectio"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/blockio"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks"
)
type flushBlkTask struct {
*tasks.BaseTask
data *containers.Batch
delta *containers.Batch
meta *catalog.BlockEntry
fs *objectio.ObjectFS
name objectio.ObjectName
blocks []objectio.BlockObject
schemaVer uint32
seqnums []uint16
stat objectio.ObjectStats
isABlk bool
Stats objectio.ObjectStats
}
func NewFlushBlkTask(
ctx *tasks.Context,
schemaVer uint32,
seqnums []uint16,
fs *objectio.ObjectFS,
meta *catalog.BlockEntry,
data *containers.Batch,
delta *containers.Batch,
isABlk bool,
) *flushBlkTask {
task := &flushBlkTask{
schemaVer: schemaVer,
seqnums: seqnums,
data: data,
meta: meta,
fs: fs,
delta: delta,
isABlk: isABlk,
}
task.BaseTask = tasks.NewBaseTask(task, tasks.IOTask, ctx)
return task
}
func (task *flushBlkTask) Scope() *common.ID { return task.meta.AsCommonID() }
func (task *flushBlkTask) Execute(ctx context.Context) (err error) {
if v := ctx.Value(TestFlushBailoutPos1{}); v != nil {
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond)
}
seg := task.meta.ID.Segment()
num, _ := task.meta.ID.Offsets()
name := objectio.BuildObjectName(seg, num)
task.name = name
writer, err := blockio.NewBlockWriterNew(task.fs.Service, name, task.schemaVer, task.seqnums)
if err != nil {
return err
}
if task.isABlk {
writer.SetAppendable()
}
if task.meta.GetSchema().HasPK() {
writer.SetPrimaryKey(uint16(task.meta.GetSchema().GetSingleSortKeyIdx()))
} else if task.meta.GetSchema().HasSortKey() {
writer.SetSortKey(uint16(task.meta.GetSchema().GetSingleSortKeyIdx()))
}
cnBatch := containers.ToCNBatch(task.data)
for _, vec := range cnBatch.Vecs {
if vec == nil {
// this task has been canceled
return nil
}
}
_, err = writer.WriteBatch(cnBatch)
if err != nil {
return err
}
if task.delta != nil {
_, err := writer.WriteTombstoneBatch(containers.ToCNBatch(task.delta))
if err != nil {
return err
}
}
task.blocks, _, err = writer.Sync(ctx)
if err != nil {
return err
}
task.Stats = writer.GetObjectStats()[objectio.SchemaData]
perfcounter.Update(ctx, func(counter *perfcounter.CounterSet) {
counter.TAE.Block.Flush.Add(1)
})
task.stat = writer.Stats()
return err
}