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