| 102 | } |
| 103 | |
| 104 | void TransactionManager::commit(main::ClientContext& clientContext, Transaction* transaction) { |
| 105 | bool shouldForceCheckpoint = false; |
| 106 | bool shouldAutoCheckpoint = false; |
| 107 | bool markedAsCommitting = false; |
| 108 | uint64_t walCommitSequence = 0; |
| 109 | try { |
| 110 | { |
| 111 | std::unique_lock lck{mtxForSerializingPublicFunctionCalls}; |
| 112 | clientContext.cleanUp(); |
| 113 | switch (transaction->getType()) { |
| 114 | case TransactionType::READ_ONLY: { |
| 115 | clearTransactionNoLock(transaction->getID()); |
| 116 | } break; |
| 117 | case TransactionType::RECOVERY: |
| 118 | case TransactionType::WRITE: { |
| 119 | committingWriteTransactionCount.fetch_add(1, std::memory_order_release); |
| 120 | markedAsCommitting = true; |
| 121 | lck.unlock(); |
| 122 | transaction->writeCommitToWAL(&wal, walCommitSequence); |
| 123 | lck.lock(); |
| 124 | if (walCommitSequence != 0) { |
| 125 | cvForPublishingCommit.wait(lck, |
| 126 | [&]() { return walCommitSequence == nextWALCommitSequenceToPublish; }); |
| 127 | } |
| 128 | lastTimestamp.fetch_add(1, std::memory_order_acq_rel); |
| 129 | transaction->commitTS = lastTimestamp.load(std::memory_order_acquire); |
| 130 | transaction->publishCommit(); |
| 131 | if (walCommitSequence != 0) { |
| 132 | nextWALCommitSequenceToPublish++; |
| 133 | cvForPublishingCommit.notify_all(); |
| 134 | } |
| 135 | shouldForceCheckpoint = transaction->shouldForceCheckpoint(); |
| 136 | shouldAutoCheckpoint = Checkpointer::canAutoCheckpoint(clientContext, *transaction); |
| 137 | clearTransactionNoLock(transaction->getID()); |
| 138 | activeWriteTransactionCount.fetch_sub(1, std::memory_order_release); |
| 139 | committingWriteTransactionCount.fetch_sub(1, std::memory_order_release); |
| 140 | cvForCommittingWriteTransaction.notify_all(); |
| 141 | markedAsCommitting = false; |
| 142 | } break; |
| 143 | // LCOV_EXCL_START |
| 144 | default: { |
| 145 | throw TransactionManagerException("Invalid transaction type to commit."); |
| 146 | } |
| 147 | // LCOV_EXCL_STOP |
| 148 | } |
| 149 | } |
| 150 | } catch (...) { |
| 151 | if (walCommitSequence != 0) { |
| 152 | std::unique_lock lck{mtxForSerializingPublicFunctionCalls}; |
| 153 | cvForPublishingCommit.wait(lck, |
| 154 | [&]() { return walCommitSequence == nextWALCommitSequenceToPublish; }); |
| 155 | nextWALCommitSequenceToPublish++; |
| 156 | cvForPublishingCommit.notify_all(); |
| 157 | } |
| 158 | if (markedAsCommitting) { |
| 159 | std::unique_lock lck{mtxForSerializingPublicFunctionCalls}; |
| 160 | // Keep the transaction active so the caller's rollback path can undo any partial |
| 161 | // in-memory publish work. The rollback path clears activeWriteTransactionCount. |
nothing calls this directly
no test coverage detected