github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/moengine/engine.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 moengine 16 17 import ( 18 "context" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/defines" 24 "github.com/matrixorigin/matrixone/pkg/logutil" 25 "github.com/matrixorigin/matrixone/pkg/pb/plan" 26 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 27 28 "github.com/matrixorigin/matrixone/pkg/txn/client" 29 "github.com/matrixorigin/matrixone/pkg/vm/engine" 30 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db" 31 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif" 32 ) 33 34 var ( 35 _ engine.Engine = (*txnEngine)(nil) 36 _ Engine = (*txnEngine)(nil) 37 ) 38 39 func NewEngine(impl *db.DB) *txnEngine { 40 return &txnEngine{ 41 impl: impl, 42 } 43 } 44 45 func (e *txnEngine) New(_ context.Context, _ client.TxnOperator) error { 46 return nil 47 } 48 49 func (e *txnEngine) Commit(_ context.Context, _ client.TxnOperator) error { 50 return nil 51 } 52 53 func (e *txnEngine) Rollback(_ context.Context, _ client.TxnOperator) error { 54 return nil 55 } 56 57 func (e *txnEngine) NewBlockReader(_ context.Context, _ int, _ timestamp.Timestamp, 58 _ *plan.Expr, _ [][]byte, _ *plan.TableDef) ([]engine.Reader, error) { 59 return nil, nil 60 } 61 62 func (e *txnEngine) Delete(ctx context.Context, name string, txnOp client.TxnOperator) (err error) { 63 var txn txnif.AsyncTxn 64 if txn, err = e.impl.GetTxnByCtx(txnOp); err != nil { 65 panic(err) 66 } 67 txnBindAccessInfoFromCtx(txn, ctx) 68 _, err = txn.DropDatabase(name) 69 return 70 } 71 72 func (e *txnEngine) DropDatabase(ctx context.Context, name string, txnHandle Txn) (err error) { 73 var txn txnif.AsyncTxn 74 if txn, err = e.impl.GetTxn(txnHandle.GetID()); err != nil { 75 panic(err) 76 } 77 txnBindAccessInfoFromCtx(txn, ctx) 78 _, err = txn.DropDatabase(name) 79 return 80 } 81 82 func (e *txnEngine) DropDatabaseByID(ctx context.Context, id uint64, txnHandle Txn) (err error) { 83 var txn txnif.AsyncTxn 84 if txn, err = e.impl.GetTxn(txnHandle.GetID()); err != nil { 85 panic(err) 86 } 87 _, err = txn.DropDatabaseByID(id) 88 return 89 } 90 91 func (e *txnEngine) Create(ctx context.Context, name string, txnOp client.TxnOperator) (err error) { 92 var txn txnif.AsyncTxn 93 if txn, err = e.impl.GetTxnByCtx(txnOp); err != nil { 94 panic(err) 95 } 96 txnBindAccessInfoFromCtx(txn, ctx) 97 createSql := "todosql" 98 if ctx != nil { 99 createSql, _ = ctx.Value(defines.SqlKey{}).(string) 100 } 101 _, err = txn.CreateDatabase(name, createSql) 102 return 103 } 104 105 func (e *txnEngine) CreateDatabase(ctx context.Context, name string, txnHandle Txn) (err error) { 106 var txn txnif.AsyncTxn 107 if txn, err = e.impl.GetTxn(txnHandle.GetID()); err != nil { 108 panic(err) 109 } 110 txnBindAccessInfoFromCtx(txn, ctx) 111 _, err = txn.CreateDatabase(name, "todosql") 112 return 113 } 114 115 func (e *txnEngine) CreateDatabaseWithID(ctx context.Context, 116 name, createSql string, id uint64, txnHandle Txn) (err error) { 117 var txn txnif.AsyncTxn 118 if txn, err = e.impl.GetTxn(txnHandle.GetID()); err != nil { 119 panic(err) 120 } 121 txnBindAccessInfoFromCtx(txn, ctx) 122 _, err = txn.CreateDatabaseWithID(name, createSql, id) 123 return 124 } 125 126 func (e *txnEngine) Databases(ctx context.Context, txnOp client.TxnOperator) ([]string, error) { 127 var err error 128 var txn txnif.AsyncTxn 129 130 if txn, err = e.impl.GetTxnByCtx(txnOp); err != nil { 131 panic(err) 132 } 133 txnBindAccessInfoFromCtx(txn, ctx) 134 return txn.DatabaseNames(), nil 135 } 136 137 func (e *txnEngine) DatabaseNames(ctx context.Context, txnHandle Txn) ([]string, error) { 138 var err error 139 var txn txnif.AsyncTxn 140 141 if txn, err = e.impl.GetTxn(txnHandle.GetID()); err != nil { 142 panic(err) 143 } 144 txnBindAccessInfoFromCtx(txn, ctx) 145 return txn.DatabaseNames(), nil 146 } 147 148 func (e *txnEngine) GetNameById(ctx context.Context, op client.TxnOperator, tableId uint64) (dbName string, tblName string, err error) { 149 return "", "", moerr.NewNYI(ctx, "interface GetNameById is not implemented") 150 } 151 152 func (e *txnEngine) GetRelationById(ctx context.Context, op client.TxnOperator, tableId uint64) (dbName string, tblName string, rel engine.Relation, err error) { 153 return "", "", nil, moerr.NewNYI(ctx, "interface GetRelationById is not implemented") 154 } 155 156 func (e *txnEngine) Database(ctx context.Context, name string, txnOp client.TxnOperator) (engine.Database, error) { 157 var err error 158 var txn txnif.AsyncTxn 159 160 if txn, err = e.impl.GetTxnByCtx(txnOp); err != nil { 161 panic(err) 162 } 163 txnBindAccessInfoFromCtx(txn, ctx) 164 h, err := txn.GetDatabase(name) 165 if err != nil { 166 return nil, err 167 } 168 db := newDatabase(h) 169 return db, nil 170 } 171 172 func (e *txnEngine) GetDatabase(ctx context.Context, name string, txnHandle Txn) (Database, error) { 173 var err error 174 var txn txnif.AsyncTxn 175 176 if txn, err = e.impl.GetTxn(txnHandle.GetID()); err != nil { 177 panic(err) 178 } 179 txnBindAccessInfoFromCtx(txn, ctx) 180 h, err := txn.GetDatabase(name) 181 if err != nil { 182 return nil, err 183 } 184 db := newDatabase(h) 185 return db, nil 186 } 187 188 func (e *txnEngine) GetDatabaseByID(_ context.Context, id uint64, txnHandle Txn) (Database, error) { 189 var err error 190 var txn txnif.AsyncTxn 191 192 if txn, err = e.impl.GetTxn(txnHandle.GetID()); err != nil { 193 panic(err) 194 } 195 h, err := txn.GetDatabaseByID(id) 196 if err != nil { 197 return nil, err 198 } 199 db := newDatabase(h) 200 return db, nil 201 } 202 203 func (e *txnEngine) GetTAE(_ context.Context) *db.DB { 204 return e.impl 205 } 206 func (e *txnEngine) FlushTable(ctx context.Context, tenantID uint32, databaseId, tableId uint64, ts types.TS) error { 207 return e.impl.FlushTable(tenantID, databaseId, tableId, ts) 208 } 209 210 func (e *txnEngine) Nodes() (engine.Nodes, error) { 211 return nil, nil 212 } 213 214 func (e *txnEngine) StartTxn(info []byte) (txn Txn, err error) { 215 return e.impl.StartTxn(info) 216 } 217 218 func (e *txnEngine) GetTxnByID(id []byte) (txn Txn, err error) { 219 return e.impl.GetTxn(string(id)) 220 } 221 222 func (e *txnEngine) GetOrCreateTxnWithMeta(info []byte, id []byte, ts types.TS) (txn Txn, err error) { 223 return e.impl.GetOrCreateTxnWithMeta(info, id, ts) 224 } 225 226 func (e *txnEngine) Hints() (h engine.Hints) { 227 h.CommitOrRollbackTimeout = time.Minute 228 return 229 } 230 231 func (e *txnEngine) Close() (err error) { 232 return e.impl.Close() 233 } 234 235 func (e *txnEngine) Destroy() (err error) { 236 panic(moerr.NewNYINoCtx("Pls implement me!")) 237 } 238 239 func (e *txnEngine) ForceCheckpoint(ctx context.Context, ts types.TS, flushDuration time.Duration) error { 240 e.impl.BGCheckpointRunner.DisableCheckpoint() 241 defer e.impl.BGCheckpointRunner.EnableCheckpoint() 242 e.impl.BGCheckpointRunner.CleanPenddingCheckpoint() 243 t0 := time.Now() 244 err := e.impl.BGCheckpointRunner.ForceFlush(ts, ctx, flushDuration) 245 logutil.Infof("[Force Checkpoint] flush takes %v", time.Since(t0)) 246 if err != nil { 247 return err 248 } 249 err = e.impl.BGCheckpointRunner.ForceIncrementalCheckpoint(ts) 250 if err != nil { 251 return err 252 } 253 lsn := e.impl.BGCheckpointRunner.MaxLSNInRange(ts) 254 _, err = e.impl.Wal.RangeCheckpoint(1, lsn) 255 logutil.Debugf("[Force Checkpoint] takes %v", time.Since(t0)) 256 return err 257 }