| 356 | } |
| 357 | |
| 358 | TxnTimestamp CnchServerTransaction::commit() |
| 359 | { |
| 360 | LOG_DEBUG(log, "Transaction {} starts commit", txn_record.txnID().toUInt64()); |
| 361 | Stopwatch watch(CLOCK_MONOTONIC_COARSE); |
| 362 | auto lock = getLock(); |
| 363 | if (isReadOnly() || !txn_record.isPrepared()) |
| 364 | throw Exception("Invalid commit operation", ErrorCodes::LOGICAL_ERROR); |
| 365 | |
| 366 | TxnTimestamp commit_ts = global_context->getTimestamp(); |
| 367 | int retry = MAX_RETRY; |
| 368 | do |
| 369 | { |
| 370 | try |
| 371 | { |
| 372 | if (TransactionCommitMode::SEQUENTIAL == getCommitMode()) |
| 373 | { |
| 374 | commit_time = commit_ts; |
| 375 | bool success = getContext()->getGlobalTxnCommitter()->commit(shared_from_this()); |
| 376 | if (success) |
| 377 | { |
| 378 | TransactionRecord updated_record = getTransactionRecord(); |
| 379 | updated_record.setStatus(CnchTransactionStatus::Finished) |
| 380 | .setCommitTs(commit_ts) |
| 381 | .setMainTableUUID(getMainTableUUID()); |
| 382 | txn_record = std::move(updated_record); |
| 383 | ProfileEvents::increment(ProfileEvents::CnchTxnCommitted); |
| 384 | ProfileEvents::increment(ProfileEvents::CnchTxnFinishedTransactionRecord); |
| 385 | |
| 386 | return commit_ts; |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | setStatus(CnchTransactionStatus::Aborted); |
| 391 | throw Exception("Fail to commit txn " + txn_record.txnID().toString() + " by using GlobalTxnCommitter.", ErrorCodes::CNCH_TRANSACTION_COMMIT_ERROR); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | if (isPrimary() && !consumer_group.empty()) /// Kafka transaction is always primary |
| 396 | { |
| 397 | if (tpl.empty()) |
| 398 | throw Exception("No tpl found for committing Kafka transaction", ErrorCodes::LOGICAL_ERROR); |
| 399 | |
| 400 | // CAS operation |
| 401 | TransactionRecord target_record = getTransactionRecord(); |
| 402 | target_record.setStatus(CnchTransactionStatus::Finished) |
| 403 | .setCommitTs(commit_ts) |
| 404 | .setMainTableUUID(getMainTableUUID()); |
| 405 | Stopwatch stop_watch; |
| 406 | auto success = global_context->getCnchCatalog()->setTransactionRecordStatusWithOffsets(txn_record, target_record, consumer_group, tpl); |
| 407 | |
| 408 | txn_record = std::move(target_record); |
| 409 | if (success) |
| 410 | { |
| 411 | ProfileEvents::increment(ProfileEvents::CnchTxnCommitted); |
| 412 | ProfileEvents::increment(ProfileEvents::CnchTxnFinishedTransactionRecord); |
| 413 | LOG_DEBUG(log, "Successfully committed Kafka transaction {} at {} with {} offsets number, elapsed {} ms", |
| 414 | txn_record.txnID().toUInt64(), commit_ts, tpl.size(), stop_watch.elapsedMilliseconds()); |
| 415 | commitModifiedCount(this->modified_counter); |
nothing calls this directly
no test coverage detected