Skip to content

Commit 50726c2

Browse files
authored
chore(docs): improve overall format & resolve todo related to transaction part (#588)
docs: improve overall format & resolve todo related to transaction part
1 parent 942f7c3 commit 50726c2

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

src/concurrency/transaction_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "catalog/catalog.h"
2121
#include "common/macros.h"
2222
#include "storage/table/table_heap.h"
23+
2324
namespace bustub {
2425

2526
void TransactionManager::Commit(Transaction *txn) {

src/include/concurrency/transaction.h

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,26 @@ namespace bustub {
3030

3131
/**
3232
* Transaction states for 2PL:
33+
* Running transactions could be aborted during either `GROWING` or `SHRINKING` stage.
3334
*
3435
* _________________________
36+
* | |
3537
* | v
3638
* GROWING -> SHRINKING -> COMMITTED ABORTED
37-
* |__________|________________________^
39+
* | | ^
40+
* |___________|________________________|
3841
*
3942
* Transaction states for Non-2PL:
43+
* Running transactions could only be aborted during `GROWING` stage, since there is no `SHRINKING` stage.
44+
*
4045
* __________
46+
* | |
4147
* | v
4248
* GROWING -> COMMITTED ABORTED
43-
* |_________________________^
49+
* | ^
50+
* |_________________________|
4451
*
45-
**/
52+
*/
4653
enum class TransactionState { GROWING, SHRINKING, COMMITTED, ABORTED };
4754

4855
/**
@@ -126,14 +133,17 @@ enum class AbortReason {
126133
* TransactionAbortException is thrown when state of a transaction is changed to ABORTED
127134
*/
128135
class TransactionAbortException : public std::exception {
129-
txn_id_t txn_id_;
130-
AbortReason abort_reason_;
131-
132136
public:
133137
explicit TransactionAbortException(txn_id_t txn_id, AbortReason abort_reason)
134138
: txn_id_(txn_id), abort_reason_(abort_reason) {}
139+
140+
/** @return this transaction id */
135141
auto GetTransactionId() -> txn_id_t { return txn_id_; }
142+
143+
/** @return the abort reason for this transaction */
136144
auto GetAbortReason() -> AbortReason { return abort_reason_; }
145+
146+
/** @return the detailed information of abort reason */
137147
auto GetInfo() -> std::string {
138148
switch (abort_reason_) {
139149
case AbortReason::LOCK_ON_SHRINKING:
@@ -155,10 +165,17 @@ class TransactionAbortException : public std::exception {
155165
return "Transaction " + std::to_string(txn_id_) + " aborted because attempted lock upgrade is incompatible\n";
156166
case AbortReason::ATTEMPTED_UNLOCK_BUT_NO_LOCK_HELD:
157167
return "Transaction " + std::to_string(txn_id_) + " aborted because attempted to unlock but no lock held \n";
168+
default:
169+
// Unknown AbortReason
170+
throw bustub::Exception("Unknown abort reason for transaction " + std::to_string(txn_id_));
158171
}
159-
// Todo: Should fail with unreachable.
160-
return "";
172+
// This is impossible
173+
assert(false);
161174
}
175+
176+
private:
177+
txn_id_t txn_id_;
178+
AbortReason abort_reason_;
162179
};
163180

164181
/**
@@ -248,22 +265,30 @@ class Transaction {
248265
return x_row_lock_set_;
249266
}
250267

251-
/** @return the set of resources under a shared lock */
268+
/** @return the set of table resources under a shared lock */
252269
inline auto GetSharedTableLockSet() -> std::shared_ptr<std::unordered_set<table_oid_t>> { return s_table_lock_set_; }
270+
271+
/** @return the set of table resources under a exclusive lock */
253272
inline auto GetExclusiveTableLockSet() -> std::shared_ptr<std::unordered_set<table_oid_t>> {
254273
return x_table_lock_set_;
255274
}
275+
276+
/** @return the set of table resources under a intention shared lock */
256277
inline auto GetIntentionSharedTableLockSet() -> std::shared_ptr<std::unordered_set<table_oid_t>> {
257278
return is_table_lock_set_;
258279
}
280+
281+
/** @return the set of table resources under a intention exclusive lock */
259282
inline auto GetIntentionExclusiveTableLockSet() -> std::shared_ptr<std::unordered_set<table_oid_t>> {
260283
return ix_table_lock_set_;
261284
}
285+
286+
/** @return the set of table resources under a shared intention exclusive lock */
262287
inline auto GetSharedIntentionExclusiveTableLockSet() -> std::shared_ptr<std::unordered_set<table_oid_t>> {
263288
return six_table_lock_set_;
264289
}
265290

266-
/** @return true if rid (belong to table oid) is shared locked by this transaction */
291+
/** @return true if the specified row (belong to table oid) is shared locked by this transaction */
267292
auto IsRowSharedLocked(const table_oid_t &oid, const RID &rid) -> bool {
268293
auto row_lock_set = s_row_lock_set_->find(oid);
269294
if (row_lock_set == s_row_lock_set_->end()) {
@@ -272,7 +297,7 @@ class Transaction {
272297
return row_lock_set->second.find(rid) != row_lock_set->second.end();
273298
}
274299

275-
/** @return true if rid (belong to table oid) is exclusive locked by this transaction */
300+
/** @return true if the specified row (belong to table oid) is exclusive locked by this transaction */
276301
auto IsRowExclusiveLocked(const table_oid_t &oid, const RID &rid) -> bool {
277302
auto row_lock_set = x_row_lock_set_->find(oid);
278303
if (row_lock_set == x_row_lock_set_->end()) {
@@ -281,22 +306,27 @@ class Transaction {
281306
return row_lock_set->second.find(rid) != row_lock_set->second.end();
282307
}
283308

309+
/** @return true if the table (specified by oid) is intention shared locked by this transaction */
284310
auto IsTableIntentionSharedLocked(const table_oid_t &oid) -> bool {
285311
return is_table_lock_set_->find(oid) != is_table_lock_set_->end();
286312
}
287313

314+
/** @return true if the table (specified by oid) is shared locked by this transaction */
288315
auto IsTableSharedLocked(const table_oid_t &oid) -> bool {
289316
return s_table_lock_set_->find(oid) != s_table_lock_set_->end();
290317
}
291318

319+
/** @return true if the table (specified by oid) is intention exclusive locked by this transaction */
292320
auto IsTableIntentionExclusiveLocked(const table_oid_t &oid) -> bool {
293321
return ix_table_lock_set_->find(oid) != ix_table_lock_set_->end();
294322
}
295323

324+
/** @return true if the table (specified by oid) is exclusive locked by this transaction */
296325
auto IsTableExclusiveLocked(const table_oid_t &oid) -> bool {
297326
return x_table_lock_set_->find(oid) != x_table_lock_set_->end();
298327
}
299328

329+
/** @return true if the table (specified by oid) is shared intention exclusive locked by this transaction */
300330
auto IsTableSharedIntentionExclusiveLocked(const table_oid_t &oid) -> bool {
301331
return six_table_lock_set_->find(oid) != six_table_lock_set_->end();
302332
}
@@ -340,6 +370,7 @@ class Transaction {
340370
/** The LSN of the last record written by the transaction. */
341371
lsn_t prev_lsn_;
342372

373+
/** The latch for this transaction */
343374
std::mutex latch_;
344375

345376
/** Concurrent index: the pages that were latched during index operation. */

src/include/concurrency/transaction_manager.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ class TransactionManager {
7171
void Abort(Transaction *txn);
7272

7373
/**
74-
* Global list of running transactions
74+
* The transaction map is a global list of all the running transactions in the system.
7575
*/
76-
77-
/** The transaction map is a global list of all the running transactions in the system. */
7876
std::unordered_map<txn_id_t, Transaction *> txn_map_;
77+
/** Coordination for the transaction map */
7978
std::shared_mutex txn_map_mutex_;
8079

8180
/**

0 commit comments

Comments
 (0)