github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tasks/ops/ops.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 ops 16 17 import ( 18 "context" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 iops "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/ops/base" 23 iworker "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/worker/base" 24 ) 25 26 func NewOp(impl iops.IOpInternal, w iworker.IOpWorker) *Op { 27 op := &Op{ 28 Impl: impl, 29 Worker: w, 30 CreateTime: time.Now(), 31 } 32 return op 33 } 34 35 func (op *Op) Push() error { 36 r := op.Worker.SendOp(op) 37 if !r { 38 return moerr.NewInternalErrorNoCtx("send op error") 39 } 40 return nil 41 } 42 43 func (op *Op) GetError() error { 44 return op.Err 45 } 46 47 func (op *Op) SetError(err error) { 48 op.EndTime = time.Now() 49 op.Err = err 50 if op.ErrorC != nil { 51 op.ErrorC <- err 52 } else if op.DoneCB != nil { 53 op.DoneCB(op) 54 } else { 55 panic("logic error") 56 } 57 if op.Observers != nil { 58 for _, observer := range op.Observers { 59 observer.OnExecDone(op.Impl) 60 } 61 } 62 } 63 64 func (op *Op) Waitable() bool { 65 return op.DoneCB == nil 66 } 67 68 func (op *Op) WaitDone(ctx context.Context) error { 69 if op.WaitedOnce.Load() { 70 return moerr.NewTAEErrorNoCtx("wait done twice") 71 } 72 defer op.WaitedOnce.Store(true) 73 74 if op.ErrorC == nil { 75 return moerr.NewTAEErrorNoCtx("wait done without error channel") 76 } 77 select { 78 case err := <-op.ErrorC: 79 return err 80 case <-ctx.Done(): 81 return ctx.Err() 82 } 83 } 84 85 func (op *Op) PreExecute() error { 86 return nil 87 } 88 89 func (op *Op) PostExecute() error { 90 return nil 91 } 92 93 func (op *Op) Execute() error { 94 return nil 95 } 96 97 func (op *Op) OnExec(ctx context.Context) error { 98 op.StartTime = time.Now() 99 err := op.Impl.PreExecute() 100 if err != nil { 101 return err 102 } 103 err = op.Impl.Execute(ctx) 104 if err != nil { 105 return err 106 } 107 err = op.Impl.PostExecute() 108 return err 109 } 110 111 func (op *Op) GetCreateTime() time.Time { 112 return op.CreateTime 113 } 114 115 func (op *Op) GetStartTime() time.Time { 116 return op.StartTime 117 } 118 119 func (op *Op) GetEndTime() time.Time { 120 return op.EndTime 121 } 122 123 func (op *Op) GetExecutTime() int64 { 124 return op.EndTime.Sub(op.StartTime).Microseconds() 125 } 126 127 func (op *Op) AddObserver(o iops.Observer) { 128 if op.Observers == nil { 129 op.Observers = make([]iops.Observer, 0) 130 } 131 op.Observers = append(op.Observers, o) 132 }