github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tasks/base.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tasks 16 17 import ( 18 "fmt" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/logutil" 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 23 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/ops" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/ops/base" 25 ) 26 27 var WaitableCtx = &Context{Waitable: true} 28 29 type Context struct { 30 DoneCB ops.OpDoneCB 31 Waitable bool 32 } 33 34 // func NewWaitableCtx() *Context { 35 // return &Context{Waitable: true} 36 // } 37 38 type BaseTask struct { 39 ops.Op 40 impl Task 41 id uint64 42 taskType TaskType 43 exec func(Task) error 44 } 45 46 func NewBaseTask(impl Task, taskType TaskType, ctx *Context) *BaseTask { 47 task := &BaseTask{ 48 id: NextTaskId(), 49 taskType: taskType, 50 impl: impl, 51 } 52 var doneCB ops.OpDoneCB 53 if ctx != nil { 54 if ctx.DoneCB == nil && !ctx.Waitable { 55 doneCB = task.onDone 56 } 57 } else { 58 doneCB = task.onDone 59 } 60 if impl == nil { 61 impl = task 62 } 63 task.Op = ops.Op{ 64 Impl: impl.(base.IOpInternal), 65 DoneCB: doneCB, 66 } 67 if doneCB == nil { 68 task.Op.ErrorC = make(chan error, 1) 69 } 70 return task 71 } 72 73 func (task *BaseTask) onDone(_ base.IOp) { 74 logutil.Debug("[Done]", common.OperationField(task.impl.Name()), 75 common.DurationField(time.Duration(task.GetExecutTime())), 76 common.ErrorField(task.Err)) 77 } 78 func (task *BaseTask) Type() TaskType { return task.taskType } 79 func (task *BaseTask) Cancel() (err error) { panic("todo") } 80 func (task *BaseTask) ID() uint64 { return task.id } 81 func (task *BaseTask) Execute() (err error) { 82 if task.exec != nil { 83 return task.exec(task) 84 } 85 logutil.Debugf("Execute Task Type=%d, ID=%d", task.taskType, task.id) 86 return nil 87 } 88 func (task *BaseTask) Name() string { 89 return fmt.Sprintf("Task[ID=%d][T=%s]", task.id, TaskName(task.taskType)) 90 }