github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tasks/scheduler.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 "github.com/matrixorigin/matrixone/pkg/common/moerr" 19 "github.com/matrixorigin/matrixone/pkg/container/types" 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/model" 24 iops "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/ops/base" 25 ops "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/worker" 26 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/wal" 27 ) 28 29 var ( 30 ErrDispatcherNotFound = moerr.NewInternalErrorNoCtx("tae sched: dispatcher not found") 31 ErrSchedule = moerr.NewInternalErrorNoCtx("tae sched: cannot schedule") 32 ) 33 34 type Scheduler interface { 35 Start() 36 Stop() 37 Schedule(Task) error 38 } 39 40 type TaskScheduler interface { 41 Scheduler 42 ScheduleTxnTask(ctx *Context, taskType TaskType, factory TxnTaskFactory) (Task, error) 43 ScheduleMultiScopedTxnTask(ctx *Context, taskType TaskType, scopes []common.ID, factory TxnTaskFactory) (Task, error) 44 ScheduleMultiScopedFn(ctx *Context, taskType TaskType, scopes []common.ID, fn FuncT) (Task, error) 45 ScheduleFn(ctx *Context, taskType TaskType, fn func() error) (Task, error) 46 ScheduleScopedFn(ctx *Context, taskType TaskType, scope *common.ID, fn func() error) (Task, error) 47 Checkpoint(indexes []*wal.Index) error 48 49 AddTransferPage(*model.TransferHashPage) error 50 DeleteTransferPage(id *common.ID) error 51 52 GetCheckpointedLSN() uint64 53 GetPenddingLSNCnt() uint64 54 GetGCTS() types.TS 55 GetCheckpointTS() types.TS 56 } 57 58 type BaseScheduler struct { 59 ops.OpWorker 60 idAlloc *common.IdAlloctor 61 Dispatchers map[TaskType]Dispatcher 62 } 63 64 func NewBaseScheduler(name string) *BaseScheduler { 65 scheduler := &BaseScheduler{ 66 OpWorker: *ops.NewOpWorker(name), 67 idAlloc: common.NewIdAlloctor(1), 68 Dispatchers: make(map[TaskType]Dispatcher), 69 } 70 scheduler.ExecFunc = scheduler.doDispatch 71 return scheduler 72 } 73 74 func (s *BaseScheduler) RegisterDispatcher(t TaskType, dispatcher Dispatcher) { 75 s.Dispatchers[t] = dispatcher 76 } 77 78 func (s *BaseScheduler) Schedule(task Task) error { 79 // task.AttachID(s.idAlloc()) 80 if !s.SendOp(task) { 81 return ErrSchedule 82 } 83 return nil 84 } 85 86 func (s *BaseScheduler) doDispatch(op iops.IOp) { 87 task := op.(Task) 88 dispatcher := s.Dispatchers[task.Type()] 89 if dispatcher == nil { 90 logutil.Errorf("No dispatcher found for %d[T] Task", task.Type()) 91 panic(ErrDispatcherNotFound) 92 } 93 dispatcher.Dispatch(task) 94 } 95 96 func (s *BaseScheduler) Stop() { 97 s.OpWorker.Stop() 98 for _, d := range s.Dispatchers { 99 d.Close() 100 } 101 }