github.com/matrixorigin/matrixone@v0.7.0/pkg/txn/util/log.go (about) 1 // Copyright 2022 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 util 16 17 import ( 18 "bytes" 19 "encoding/hex" 20 21 "github.com/matrixorigin/matrixone/pkg/common/log" 22 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 23 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 24 "github.com/matrixorigin/matrixone/pkg/pb/txn" 25 "go.uber.org/zap" 26 ) 27 28 // LogTxnRead log txn read 29 func LogTxnRead( 30 logger *log.MOLogger, 31 txnMeta txn.TxnMeta) { 32 if logger.Enabled(zap.DebugLevel) { 33 logger.Debug("txn read", zap.String("txn", txnMeta.DebugString())) 34 } 35 } 36 37 // LogTxnWrite log txn write 38 func LogTxnWrite( 39 logger *log.MOLogger, 40 txnMeta txn.TxnMeta) { 41 if logger.Enabled(zap.DebugLevel) { 42 logger.Debug("txn write", zap.String("txn", txnMeta.DebugString())) 43 } 44 } 45 46 // LogTxnCommit log txn commit 47 func LogTxnCommit( 48 logger *log.MOLogger, 49 txnMeta txn.TxnMeta) { 50 if logger.Enabled(zap.DebugLevel) { 51 logger.Debug("txn commit", zap.String("txn", txnMeta.DebugString())) 52 } 53 } 54 55 // LogTxnRollback log txn rollback 56 func LogTxnRollback( 57 logger *log.MOLogger, 58 txnMeta txn.TxnMeta) { 59 if logger.Enabled(zap.DebugLevel) { 60 logger.Debug("txn rollback", zap.String("txn", txnMeta.DebugString())) 61 } 62 } 63 64 // LogTxnCreated log txn created 65 func LogTxnCreated( 66 logger *log.MOLogger, 67 txnMeta txn.TxnMeta) { 68 if logger.Enabled(zap.DebugLevel) { 69 logger.Debug("txn created", zap.String("txn", txnMeta.DebugString())) 70 } 71 } 72 73 // LogTxnUpdated log txn updated 74 func LogTxnUpdated( 75 logger *log.MOLogger, 76 txnMeta txn.TxnMeta) { 77 if logger.Enabled(zap.DebugLevel) { 78 logger.Debug("txn updated", zap.String("txn", txnMeta.DebugString())) 79 } 80 } 81 82 // LogTxnWaiterAdded log txn waiter added 83 func LogTxnWaiterAdded( 84 logger *log.MOLogger, 85 txnMeta txn.TxnMeta, 86 waitStatus txn.TxnStatus) { 87 if logger.Enabled(zap.DebugLevel) { 88 logger.Debug("txn waiter added", zap.String("txn", txnMeta.DebugString())) 89 } 90 } 91 92 // LogTxnHandleRequest log txn handle request 93 func LogTxnHandleRequest( 94 logger *log.MOLogger, 95 request *txn.TxnRequest) { 96 if logger.Enabled(zap.DebugLevel) { 97 logger.Debug("txn handle request", 98 TxnIDFieldWithID(request.Txn.ID), 99 zap.String("request", request.DebugString())) 100 } 101 } 102 103 // LogTxnHandleResult log txn handle request 104 func LogTxnHandleResult( 105 logger *log.MOLogger, 106 response *txn.TxnResponse) { 107 if logger.Enabled(zap.DebugLevel) { 108 logger.Debug("txn handle result", 109 zap.String("response", response.DebugString())) 110 } 111 } 112 113 // LogTxnSendRequests log txn send txn requests 114 func LogTxnSendRequests( 115 logger *log.MOLogger, 116 requests []txn.TxnRequest) { 117 if logger.Enabled(zap.DebugLevel) { 118 logger.Debug("txn send requests", 119 zap.String("requests", txn.RequestsDebugString(requests, true))) 120 } 121 } 122 123 // LogTxnSendRequestsFailed log txn send txn requests failed 124 func LogTxnSendRequestsFailed( 125 logger *log.MOLogger, 126 requests []txn.TxnRequest, 127 err error) { 128 // The payload cannot be recorded here because reading the payload field would 129 // cause a DATA RACE, as it is possible that morpc was still processing the send 130 // at the time of the error and would have manipulated the payload field. And logging 131 // the error, the payload field does not need to be logged to the log either, you can 132 // find the previous log to view the paylaod based on the request-id. 133 logger.Error("txn send requests failed", 134 zap.String("requests", txn.RequestsDebugString(requests, false)), 135 zap.Error(err)) 136 } 137 138 // LogTxnReceivedResponses log received txn responses 139 func LogTxnReceivedResponses( 140 logger *log.MOLogger, 141 responses []txn.TxnResponse) { 142 if logger.Enabled(zap.DebugLevel) { 143 logger.Debug("txn received responses", 144 zap.String("responses", txn.ResponsesDebugString(responses))) 145 } 146 } 147 148 // LogTxnCreateOn log Txn create on dn shard. 149 func LogTxnCreateOn( 150 logger *log.MOLogger, 151 txnMeta txn.TxnMeta, 152 dn metadata.DNShard) { 153 if logger.Enabled(zap.DebugLevel) { 154 logger.Debug("txn created on DNShard", 155 TxnField(txnMeta), 156 TxnDNShardField(dn)) 157 } 158 } 159 160 // LogTxnReadBlockedByUncommittedTxns log Txn read blocked by other txns 161 func LogTxnReadBlockedByUncommittedTxns( 162 logger *log.MOLogger, 163 txnMeta txn.TxnMeta, 164 waitTxns [][]byte) { 165 if logger.Enabled(zap.DebugLevel) { 166 logger.Debug("txn read blocked by other uncommitted txns", 167 TxnField(txnMeta), 168 TxnIDsField(waitTxns)) 169 } 170 } 171 172 // LogTxnWaitUncommittedTxnsFailed log Txn wait other uncommitted txns change to committed or abortted 173 // failed. 174 func LogTxnWaitUncommittedTxnsFailed( 175 logger *log.MOLogger, 176 txnMeta txn.TxnMeta, 177 waitTxns [][]byte, 178 err error) { 179 logger.Error("txn wait other uncommitted txns failed", 180 TxnField(txnMeta), 181 TxnIDsField(waitTxns), 182 zap.Error(err)) 183 } 184 185 // LogTxnReadFailed log Txn read failed. 186 func LogTxnNotFoundOn( 187 logger *log.MOLogger, 188 txnMeta txn.TxnMeta, 189 dn metadata.DNShard) { 190 if logger.Enabled(zap.DebugLevel) { 191 logger.Debug("txn not found on DNShard", 192 TxnField(txnMeta), 193 TxnDNShardField(dn)) 194 } 195 } 196 197 // LogTxnWriteOnInvalidStatus log Txn write on invalid txn status. 198 func LogTxnWriteOnInvalidStatus( 199 logger *log.MOLogger, 200 txnMeta txn.TxnMeta) { 201 if logger.Enabled(zap.DebugLevel) { 202 logger.Debug("txn write on invalid status", 203 TxnField(txnMeta)) 204 } 205 } 206 207 // LogTxnCommitOnInvalidStatus log Txn commit on invalid txn status. 208 func LogTxnCommitOnInvalidStatus( 209 logger *log.MOLogger, 210 txnMeta txn.TxnMeta) { 211 if logger.Enabled(zap.DebugLevel) { 212 logger.Debug("txn commit on invalid status", 213 TxnField(txnMeta)) 214 } 215 } 216 217 // LogTxnReadFailed log Txn read failed. 218 func LogTxnReadFailed( 219 logger *log.MOLogger, 220 txnMeta txn.TxnMeta, 221 err error) { 222 logger.Error("txn read failed", 223 TxnField(txnMeta), 224 zap.Error(err)) 225 } 226 227 // LogTxnWriteFailed log Txn write failed. 228 func LogTxnWriteFailed( 229 logger *log.MOLogger, 230 txnMeta txn.TxnMeta, 231 err error) { 232 logger.Error("txn write failed", 233 TxnField(txnMeta), 234 zap.Error(err)) 235 } 236 237 // LogTxnParallelPrepareFailed log Txn parallel prepare failed 238 func LogTxnParallelPrepareFailed( 239 logger *log.MOLogger, 240 txnMeta txn.TxnMeta, 241 err error) { 242 logger.Error("txn parallel prepare failed", 243 TxnField(txnMeta), 244 zap.Error(err)) 245 } 246 247 // LogTxnParallelPrepareCompleted log Txn parallel prepare completed 248 func LogTxnParallelPrepareCompleted( 249 logger *log.MOLogger, 250 txnMeta txn.TxnMeta) { 251 if logger.Enabled(zap.DebugLevel) { 252 logger.Debug("txn parallel prepare completed", 253 TxnField(txnMeta)) 254 } 255 } 256 257 // LogTxnPrepareFailedOn log Tx prepare failed on DNShard 258 func LogTxnPrepareFailedOn( 259 logger *log.MOLogger, 260 txnMeta txn.TxnMeta, 261 dn metadata.DNShard, 262 err *txn.TxnError) { 263 logger.Error("txn prepare failed on DNShard", 264 TxnField(txnMeta), 265 TxnDNShardField(dn), 266 zap.String("error", err.DebugString())) 267 } 268 269 // LogTxnPrepareCompletedOn log Tx prepare completed on DNShard 270 func LogTxnPrepareCompletedOn( 271 logger *log.MOLogger, 272 txnMeta txn.TxnMeta, 273 dn metadata.DNShard, 274 preparedTS timestamp.Timestamp) { 275 if logger.Enabled(zap.DebugLevel) { 276 logger.Debug("txn prepare completed on DNShard", 277 TxnField(txnMeta), 278 TxnDNShardField(dn), 279 zap.String("prepared-ts", preparedTS.DebugString())) 280 } 281 } 282 283 // LogTxnStartAsyncCommit log start async commit distributed txn task 284 func LogTxnStartAsyncCommit( 285 logger *log.MOLogger, 286 txnMeta txn.TxnMeta) { 287 if logger.Enabled(zap.DebugLevel) { 288 logger.Debug("async commit task started", 289 TxnField(txnMeta)) 290 } 291 } 292 293 // LogTxnStartAsyncRollback log start async rollback txn task 294 func LogTxnStartAsyncRollback( 295 logger *log.MOLogger, 296 txnMeta txn.TxnMeta) { 297 if logger.Enabled(zap.DebugLevel) { 298 logger.Debug("async rollback task started", 299 TxnField(txnMeta)) 300 } 301 } 302 303 // LogTxnRollbackCompleted log Txn rollback completed 304 func LogTxnRollbackCompleted( 305 logger *log.MOLogger, 306 txnMeta txn.TxnMeta) { 307 if logger.Enabled(zap.DebugLevel) { 308 logger.Debug("txn rollback completed", 309 TxnField(txnMeta)) 310 } 311 } 312 313 // LogTxnCommittingFailed log Txn Committing failed on coordinator failed 314 func LogTxnCommittingFailed( 315 logger *log.MOLogger, 316 txnMeta txn.TxnMeta, 317 err error) { 318 logger.Error("txn committing failed, retry later", 319 TxnDNShardField(txnMeta.DNShards[0]), 320 TxnField(txnMeta), 321 zap.Error(err)) 322 } 323 324 // LogTxnStart1PCCommit log Txn start 1pc commit 325 func LogTxnStart1PCCommit( 326 logger *log.MOLogger, 327 txnMeta txn.TxnMeta) { 328 if logger.Enabled(zap.DebugLevel) { 329 logger.Debug("txn commit with 1 PC", 330 TxnField(txnMeta)) 331 } 332 } 333 334 // LogTxn1PCCommitCompleted log Txn 1pc commit completed 335 func LogTxn1PCCommitCompleted( 336 logger *log.MOLogger, 337 txnMeta txn.TxnMeta) { 338 if logger.Enabled(zap.DebugLevel) { 339 logger.Debug("txn commit with 1 PC completed", 340 TxnField(txnMeta)) 341 } 342 } 343 344 // LogTxnStart1PCCommitFailed log Txn 1pc commit failed 345 func LogTxnStart1PCCommitFailed( 346 logger *log.MOLogger, 347 txnMeta txn.TxnMeta, 348 err error) { 349 logger.Error("txn commit with 1 PC failed", 350 TxnField(txnMeta), 351 zap.Error(err)) 352 } 353 354 // LogTxnStart2PCCommit log Txn start 2pc commit 355 func LogTxnStart2PCCommit( 356 logger *log.MOLogger, 357 txnMeta txn.TxnMeta) { 358 if logger.Enabled(zap.DebugLevel) { 359 logger.Debug("txn commit with 2 PC", 360 TxnField(txnMeta)) 361 } 362 } 363 364 // LogTxnCommittingCompleted log Txn Committing completed on coordinator failed 365 func LogTxnCommittingCompleted( 366 logger *log.MOLogger, 367 txnMeta txn.TxnMeta) { 368 if logger.Enabled(zap.DebugLevel) { 369 logger.Debug("txn committing completed", 370 TxnField(txnMeta)) 371 } 372 } 373 374 // TxnIDField returns a txn id field 375 func TxnIDField(txnMeta txn.TxnMeta) zap.Field { 376 return TxnIDFieldWithID(txnMeta.ID) 377 } 378 379 // TxnIDsField returns a txn ids field 380 func TxnIDsField(txnIDs [][]byte) zap.Field { 381 var buf bytes.Buffer 382 n := len(txnIDs) - 1 383 buf.WriteString("[") 384 for idx, id := range txnIDs { 385 buf.WriteString(hex.EncodeToString(id)) 386 if idx < n { 387 buf.WriteString(", ") 388 } 389 } 390 buf.WriteString("[") 391 return zap.String("txn-ids", buf.String()) 392 } 393 394 // TxnIDFieldWithID returns a txn id field 395 func TxnIDFieldWithID(id []byte) zap.Field { 396 return zap.String("txn-id", hex.EncodeToString(id)) 397 } 398 399 // TxnDNShardField returns a dn shard zap field 400 func TxnDNShardField(dn metadata.DNShard) zap.Field { 401 return zap.String("dn-shard", dn.DebugString()) 402 } 403 404 // TxnField returns a txn zap field 405 func TxnField(txnMeta txn.TxnMeta) zap.Field { 406 return zap.String("txn", txnMeta.DebugString()) 407 }