github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/db/db.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 gc2 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/gc" 19 "io" 20 "runtime" 21 "sync/atomic" 22 23 "github.com/matrixorigin/matrixone/pkg/objectio" 24 25 "github.com/matrixorigin/matrixone/pkg/container/types" 26 27 "github.com/matrixorigin/matrixone/pkg/common/moerr" 28 "github.com/matrixorigin/matrixone/pkg/logutil" 29 "github.com/matrixorigin/matrixone/pkg/txn/client" 30 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/buffer/base" 31 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog" 32 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/checkpoint" 33 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/gc" 34 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif" 35 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logtail" 36 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/model" 37 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options" 38 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tables" 39 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks" 40 wb "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/worker/base" 41 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase" 42 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/wal" 43 ) 44 45 var ( 46 ErrClosed = moerr.NewInternalErrorNoCtx("tae: closed") 47 ) 48 49 type DB struct { 50 Dir string 51 Opts *options.Options 52 53 Catalog *catalog.Catalog 54 55 MTBufMgr base.INodeManager 56 TxnBufMgr base.INodeManager 57 58 TxnMgr *txnbase.TxnManager 59 TransferTable *model.HashPageTable 60 61 LogtailMgr *logtail.Manager 62 Wal wal.Driver 63 64 Scheduler tasks.TaskScheduler 65 66 GCManager *gc.Manager 67 68 BGScanner wb.IHeartbeater 69 BGCheckpointRunner checkpoint.Runner 70 71 DiskCleaner *gc2.DiskCleaner 72 73 Fs *objectio.ObjectFS 74 75 DBLocker io.Closer 76 77 Closed *atomic.Value 78 } 79 80 func (db *DB) FlushTable( 81 tenantID uint32, 82 dbId, tableId uint64, 83 ts types.TS) (err error) { 84 err = db.BGCheckpointRunner.FlushTable(dbId, tableId, ts) 85 return 86 } 87 88 func (db *DB) StartTxn(info []byte) (txnif.AsyncTxn, error) { 89 return db.TxnMgr.StartTxn(info) 90 } 91 92 func (db *DB) CommitTxn(txn txnif.AsyncTxn) (err error) { 93 return txn.Commit() 94 } 95 96 func (db *DB) GetTxnByCtx(txnOperator client.TxnOperator) (txn txnif.AsyncTxn, err error) { 97 txnID := txnOperator.Txn().ID 98 txn = db.TxnMgr.GetTxnByCtx(txnID) 99 if txn == nil { 100 err = moerr.NewNotFoundNoCtx() 101 } 102 return 103 } 104 105 func (db *DB) GetOrCreateTxnWithMeta( 106 info []byte, 107 id []byte, 108 ts types.TS) (txn txnif.AsyncTxn, err error) { 109 return db.TxnMgr.GetOrCreateTxnWithMeta(info, id, ts) 110 } 111 112 func (db *DB) GetTxn(id string) (txn txnif.AsyncTxn, err error) { 113 txn = db.TxnMgr.GetTxn(id) 114 if txn == nil { 115 err = moerr.NewTxnNotFoundNoCtx() 116 } 117 return 118 } 119 120 func (db *DB) RollbackTxn(txn txnif.AsyncTxn) error { 121 return txn.Rollback() 122 } 123 124 func (db *DB) Replay(dataFactory *tables.DataFactory, maxTs types.TS) { 125 // maxTs := db.Catalog.GetCheckpointed().MaxTS 126 replayer := newReplayer(dataFactory, db, maxTs) 127 replayer.OnTimeStamp(maxTs) 128 replayer.Replay() 129 130 err := db.TxnMgr.Init(replayer.GetMaxTS()) 131 if err != nil { 132 panic(err) 133 } 134 } 135 136 func (db *DB) PrintStats() { 137 pc, _, _, _ := runtime.Caller(1) 138 caller := runtime.FuncForPC(pc).Name() 139 stats := db.CollectStats() 140 logutil.Infof("[PrintStats][Caller=%s]:%s", caller, stats.ToString("")) 141 } 142 143 func (db *DB) CollectStats() *Stats { 144 stats := NewStats(db) 145 stats.Collect() 146 return stats 147 } 148 149 func (db *DB) Close() error { 150 if err := db.Closed.Load(); err != nil { 151 panic(err) 152 } 153 db.Closed.Store(ErrClosed) 154 db.GCManager.Stop() 155 db.BGScanner.Stop() 156 db.BGCheckpointRunner.Stop() 157 db.Scheduler.Stop() 158 db.TxnMgr.Stop() 159 db.Wal.Close() 160 db.Opts.Catalog.Close() 161 db.DiskCleaner.Stop() 162 db.TransferTable.Close() 163 return db.DBLocker.Close() 164 }