github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/task.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 db 16 17 import ( 18 "context" 19 20 "github.com/matrixorigin/matrixone/pkg/logutil" 21 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks" 23 ) 24 25 type ScheduledTxnTask struct { 26 *tasks.BaseTask 27 db *DB 28 factory tasks.TxnTaskFactory 29 scopes []common.ID 30 } 31 32 func NewScheduledTxnTask(ctx *tasks.Context, db *DB, taskType tasks.TaskType, scopes []common.ID, factory tasks.TxnTaskFactory) (task *ScheduledTxnTask) { 33 task = &ScheduledTxnTask{ 34 db: db, 35 factory: factory, 36 scopes: scopes, 37 } 38 task.BaseTask = tasks.NewBaseTask(task, taskType, ctx) 39 return 40 } 41 42 func (task *ScheduledTxnTask) Scopes() []common.ID { return task.scopes } 43 func (task *ScheduledTxnTask) Scope() *common.ID { 44 if task.scopes == nil || len(task.scopes) == 0 { 45 return nil 46 } 47 return &task.scopes[0] 48 } 49 50 func (task *ScheduledTxnTask) Execute(ctx context.Context) (err error) { 51 txn, err := task.db.TxnMgr.StartTxn(nil) 52 if err != nil { 53 return 54 } 55 txnTask, err := task.factory(nil, txn) 56 if err != nil { 57 err2 := txn.Rollback(ctx) 58 if err2 != nil { 59 panic(err2) 60 } 61 logutil.Warnf("Execute ScheduleTxnTask: %v. Rollbacked", err) 62 return 63 } 64 err = txnTask.OnExec(task.db.Opts.Ctx) 65 if err != nil { 66 logutil.Warnf("Task[%d] exec error: %v", task.ID(), err) 67 err2 := txn.Rollback(ctx) 68 if err2 != nil { 69 panic(err) 70 } 71 } else { 72 err = txn.Commit(ctx) 73 if err != nil { 74 return 75 } 76 err = txn.GetError() 77 } 78 return 79 }