github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/trace/types.go (about) 1 // Copyright 2024 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 trace 16 17 import ( 18 "fmt" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/common/runtime" 22 "github.com/matrixorigin/matrixone/pkg/container/batch" 23 "github.com/matrixorigin/matrixone/pkg/container/vector" 24 "github.com/matrixorigin/matrixone/pkg/pb/api" 25 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 26 "github.com/matrixorigin/matrixone/pkg/txn/client" 27 ) 28 29 const ( 30 DebugDB = "mo_debug" 31 FeaturesTables = "trace_features" 32 TraceTableFilterTable = "trace_table_filters" 33 TraceTxnFilterTable = "trace_txn_filters" 34 TraceStatementFilterTable = "trace_statement_filters" 35 TraceStatementTable = "trace_statement" 36 EventTxnTable = "trace_event_txn" 37 EventDataTable = "trace_event_data" 38 EventErrorTable = "trace_event_error" 39 EventTxnActionTable = "trace_event_txn_action" 40 41 FeatureTraceStatement = "statement" 42 FeatureTraceTxn = "txn" 43 FeatureTraceTxnWorkspace = "txn-workspace" 44 FeatureTraceTxnAction = "txn-action" 45 FeatureTraceData = "data" 46 StateEnable = "enable" 47 StateDisable = "disable" 48 ) 49 50 var ( 51 EventTxnTableSQL = fmt.Sprintf(`create table %s.%s( 52 ts bigint not null, 53 txn_id varchar(50) not null, 54 cn varchar(100) not null, 55 event_type varchar(50) not null, 56 txn_status varchar(10), 57 snapshot_ts varchar(50), 58 commit_ts varchar(50), 59 info varchar(1000) 60 )`, DebugDB, EventTxnTable) 61 62 EventDataTableSQL = fmt.Sprintf(`create table %s.%s( 63 ts bigint not null, 64 cn varchar(100) not null, 65 event_type varchar(50) not null, 66 entry_type varchar(50) not null, 67 table_id bigint UNSIGNED not null, 68 txn_id varchar(50), 69 row_data varchar(500) not null, 70 committed_ts varchar(50), 71 snapshot_ts varchar(50) 72 )`, DebugDB, EventDataTable) 73 74 TraceTableFilterTableSQL = fmt.Sprintf(`create table %s.%s( 75 id bigint UNSIGNED primary key auto_increment, 76 table_id bigint UNSIGNED not null, 77 table_name varchar(50) not null, 78 columns varchar(200) 79 )`, DebugDB, TraceTableFilterTable) 80 81 TraceTxnFilterTableSQL = fmt.Sprintf(`create table %s.%s( 82 id bigint UNSIGNED primary key auto_increment, 83 method varchar(50) not null, 84 value varchar(500) not null 85 )`, DebugDB, TraceTxnFilterTable) 86 87 TraceStatementFilterTableSQL = fmt.Sprintf(`create table %s.%s( 88 id bigint UNSIGNED primary key auto_increment, 89 method varchar(50) not null, 90 value varchar(500) not null 91 )`, DebugDB, TraceStatementFilterTable) 92 93 EventErrorTableSQL = fmt.Sprintf(`create table %s.%s( 94 ts bigint not null, 95 txn_id varchar(50) not null, 96 error_info varchar(1000) not null 97 )`, DebugDB, EventErrorTable) 98 99 TraceStatementTableSQL = fmt.Sprintf(`create table %s.%s( 100 ts bigint not null, 101 txn_id varchar(50) not null, 102 sql varchar(1000) not null, 103 cost_us bigint not null 104 )`, DebugDB, TraceStatementTable) 105 106 EventTxnActionTableSQL = fmt.Sprintf(`create table %s.%s( 107 ts bigint not null, 108 txn_id varchar(50) not null, 109 cn varchar(50) not null, 110 table_id bigint UNSIGNED, 111 action varchar(100) not null, 112 action_sequence bigint UNSIGNED not null, 113 value bigint, 114 unit varchar(10), 115 err varchar(100) 116 )`, DebugDB, EventTxnActionTable) 117 118 FeaturesTablesSQL = fmt.Sprintf(`create table %s.%s( 119 name varchar(50) not null primary key, 120 state varchar(20) not null 121 )`, DebugDB, FeaturesTables) 122 123 InitFeatureTraceTxnSQL = fmt.Sprintf(`insert into %s.%s (name, state) values ('%s', '%s')`, 124 DebugDB, 125 FeaturesTables, 126 FeatureTraceTxn, 127 StateDisable) 128 129 InitFeatureTraceTxnActionSQL = fmt.Sprintf(`insert into %s.%s (name, state) values ('%s', '%s')`, 130 DebugDB, 131 FeaturesTables, 132 FeatureTraceTxnAction, 133 StateDisable) 134 135 InitFeatureTraceDataSQL = fmt.Sprintf(`insert into %s.%s (name, state) values ('%s', '%s')`, 136 DebugDB, 137 FeaturesTables, 138 FeatureTraceData, 139 StateDisable) 140 141 InitFeatureTraceStatementSQL = fmt.Sprintf(`insert into %s.%s (name, state) values ('%s', '%s')`, 142 DebugDB, 143 FeaturesTables, 144 FeatureTraceStatement, 145 StateDisable) 146 147 InitFeatureTraceTxnWorkspaceSQL = fmt.Sprintf(`insert into %s.%s (name, state) values ('%s', '%s')`, 148 DebugDB, 149 FeaturesTables, 150 FeatureTraceTxnWorkspace, 151 StateDisable) 152 ) 153 154 var ( 155 InitSQLs = []string{ 156 fmt.Sprintf("create database %s", DebugDB), 157 EventTxnTableSQL, 158 EventDataTableSQL, 159 TraceTableFilterTableSQL, 160 TraceTxnFilterTableSQL, 161 TraceStatementFilterTableSQL, 162 EventErrorTableSQL, 163 TraceStatementTableSQL, 164 EventTxnActionTableSQL, 165 FeaturesTablesSQL, 166 InitFeatureTraceTxnSQL, 167 InitFeatureTraceTxnActionSQL, 168 InitFeatureTraceDataSQL, 169 InitFeatureTraceStatementSQL, 170 InitFeatureTraceTxnWorkspaceSQL, 171 } 172 ) 173 174 func GetService() Service { 175 v, ok := runtime.ProcessLevelRuntime().GetGlobalVariables(runtime.TxnTraceService) 176 if !ok { 177 return &service{} 178 } 179 return v.(Service) 180 } 181 182 type txnEventService interface { 183 TxnCreated(op client.TxnOperator) 184 TxnNoConflictChanged(op client.TxnOperator, tableID uint64, lockedAt, newSnapshotTS timestamp.Timestamp) 185 TxnConflictChanged(op client.TxnOperator, tableID uint64, lastCommitAt timestamp.Timestamp) 186 TxnUpdateSnapshot(op client.TxnOperator, tableID uint64, why string) 187 TxnCommit(op client.TxnOperator, entries []*api.Entry) 188 TxnRead(op client.TxnOperator, snapshotTS timestamp.Timestamp, tableID uint64, columns []string, bat *batch.Batch) 189 TxnReadBlock(op client.TxnOperator, tableID uint64, block []byte) 190 TxnWrite(op client.TxnOperator, tableID uint64, typ string, bat *batch.Batch) 191 TxnAdjustWorkspace(op client.TxnOperator, index int, writes func() (tableID uint64, typ string, bat *batch.Batch, more bool)) 192 TxnError(op client.TxnOperator, err error) 193 194 TxnStatementStart(op client.TxnOperator, sql string, seq uint64) 195 TxnStatementCompleted(op client.TxnOperator, sql string, cost time.Duration, seq uint64, affectRows int, err error) 196 197 AddTxnDurationAction(op client.TxnOperator, eventType client.EventType, seq uint64, tableID uint64, value time.Duration, err error) 198 AddTxnAction(op client.TxnOperator, eventType client.EventType, seq uint64, tableID uint64, value int64, unit string, err error) 199 AddTxnActionInfo(op client.TxnOperator, eventType client.EventType, seq uint64, tableID uint64, value func(Writer)) 200 201 AddTxnFilter(method, value string) error 202 ClearTxnFilters() error 203 RefreshTxnFilters() error 204 } 205 206 type dataEventService interface { 207 ApplyLogtail(logtail *api.Entry, commitTSIndex int) 208 ApplyFlush(txnID []byte, tableID uint64, from, to timestamp.Timestamp, count int) 209 ApplyTransferRowID(txnID []byte, tableID uint64, fromRowID, toRowID, fromBlockID, toBlockID []byte, vec *vector.Vector, row int) 210 ApplyDeleteObject(tableID uint64, ts timestamp.Timestamp, objName string, tag string) 211 212 AddTableFilter(name string, columns []string) error 213 ClearTableFilters() error 214 RefreshTableFilters() error 215 } 216 217 type statementService interface { 218 AddStatement(op client.TxnOperator, statement string, cost time.Duration) 219 AddStatementFilter(method, value string) error 220 ClearStatementFilters() error 221 RefreshStatementFilters() error 222 } 223 224 type Service interface { 225 txnEventService 226 dataEventService 227 statementService 228 229 EnableFlush() 230 Enable(feature string) error 231 Disable(feature string) error 232 Enabled(feature string) bool 233 Sync() 234 235 DecodeHexComplexPK(hex string) (string, error) 236 237 Close() 238 } 239 240 // Option options to create trace service 241 type Option func(*service) 242 243 // EntryFilter entry filter to hold the entries we care about, to reduce the 244 // amount size of trace data. 245 type EntryFilter interface { 246 // Filter returns true means the entry should be skipped. 247 Filter(entry *EntryData) bool 248 Name() string 249 } 250 251 type TxnFilter interface { 252 // Filter returns true means the txn should be skipped. 253 Filter(op client.TxnOperator) bool 254 } 255 256 type StatementFilter interface { 257 // Filter returns true means the txn should be skipped. 258 Filter(op client.TxnOperator, sql string, cost time.Duration) bool 259 } 260 261 type csvEvent interface { 262 toCSVRecord(cn string, buf *buffer, records []string) 263 } 264 265 type event struct { 266 csv csvEvent 267 buffer *buffer 268 } 269 270 type Writer interface { 271 WriteUint(uint64) 272 WriteInt(int64) 273 WriteString(string) 274 WriteHex([]byte) 275 }