@@ -30,19 +30,26 @@ namespace bustub {
30
30
31
31
/* *
32
32
* Transaction states for 2PL:
33
+ * Running transactions could be aborted during either `GROWING` or `SHRINKING` stage.
33
34
*
34
35
* _________________________
36
+ * | |
35
37
* | v
36
38
* GROWING -> SHRINKING -> COMMITTED ABORTED
37
- * |__________|________________________^
39
+ * | | ^
40
+ * |___________|________________________|
38
41
*
39
42
* Transaction states for Non-2PL:
43
+ * Running transactions could only be aborted during `GROWING` stage, since there is no `SHRINKING` stage.
44
+ *
40
45
* __________
46
+ * | |
41
47
* | v
42
48
* GROWING -> COMMITTED ABORTED
43
- * |_________________________^
49
+ * | ^
50
+ * |_________________________|
44
51
*
45
- ** /
52
+ */
46
53
enum class TransactionState { GROWING, SHRINKING, COMMITTED, ABORTED };
47
54
48
55
/* *
@@ -126,14 +133,17 @@ enum class AbortReason {
126
133
* TransactionAbortException is thrown when state of a transaction is changed to ABORTED
127
134
*/
128
135
class TransactionAbortException : public std ::exception {
129
- txn_id_t txn_id_;
130
- AbortReason abort_reason_;
131
-
132
136
public:
133
137
explicit TransactionAbortException (txn_id_t txn_id, AbortReason abort_reason)
134
138
: txn_id_(txn_id), abort_reason_(abort_reason) {}
139
+
140
+ /* * @return this transaction id */
135
141
auto GetTransactionId () -> txn_id_t { return txn_id_; }
142
+
143
+ /* * @return the abort reason for this transaction */
136
144
auto GetAbortReason () -> AbortReason { return abort_reason_; }
145
+
146
+ /* * @return the detailed information of abort reason */
137
147
auto GetInfo () -> std::string {
138
148
switch (abort_reason_) {
139
149
case AbortReason::LOCK_ON_SHRINKING:
@@ -155,10 +165,17 @@ class TransactionAbortException : public std::exception {
155
165
return " Transaction " + std::to_string (txn_id_) + " aborted because attempted lock upgrade is incompatible\n " ;
156
166
case AbortReason::ATTEMPTED_UNLOCK_BUT_NO_LOCK_HELD:
157
167
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_));
158
171
}
159
- // Todo: Should fail with unreachable.
160
- return " " ;
172
+ // This is impossible
173
+ assert ( false ) ;
161
174
}
175
+
176
+ private:
177
+ txn_id_t txn_id_;
178
+ AbortReason abort_reason_;
162
179
};
163
180
164
181
/* *
@@ -248,22 +265,30 @@ class Transaction {
248
265
return x_row_lock_set_;
249
266
}
250
267
251
- /* * @return the set of resources under a shared lock */
268
+ /* * @return the set of table resources under a shared lock */
252
269
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 */
253
272
inline auto GetExclusiveTableLockSet () -> std::shared_ptr<std::unordered_set<table_oid_t>> {
254
273
return x_table_lock_set_;
255
274
}
275
+
276
+ /* * @return the set of table resources under a intention shared lock */
256
277
inline auto GetIntentionSharedTableLockSet () -> std::shared_ptr<std::unordered_set<table_oid_t>> {
257
278
return is_table_lock_set_;
258
279
}
280
+
281
+ /* * @return the set of table resources under a intention exclusive lock */
259
282
inline auto GetIntentionExclusiveTableLockSet () -> std::shared_ptr<std::unordered_set<table_oid_t>> {
260
283
return ix_table_lock_set_;
261
284
}
285
+
286
+ /* * @return the set of table resources under a shared intention exclusive lock */
262
287
inline auto GetSharedIntentionExclusiveTableLockSet () -> std::shared_ptr<std::unordered_set<table_oid_t>> {
263
288
return six_table_lock_set_;
264
289
}
265
290
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 */
267
292
auto IsRowSharedLocked (const table_oid_t &oid, const RID &rid) -> bool {
268
293
auto row_lock_set = s_row_lock_set_->find (oid);
269
294
if (row_lock_set == s_row_lock_set_->end ()) {
@@ -272,7 +297,7 @@ class Transaction {
272
297
return row_lock_set->second .find (rid) != row_lock_set->second .end ();
273
298
}
274
299
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 */
276
301
auto IsRowExclusiveLocked (const table_oid_t &oid, const RID &rid) -> bool {
277
302
auto row_lock_set = x_row_lock_set_->find (oid);
278
303
if (row_lock_set == x_row_lock_set_->end ()) {
@@ -281,22 +306,27 @@ class Transaction {
281
306
return row_lock_set->second .find (rid) != row_lock_set->second .end ();
282
307
}
283
308
309
+ /* * @return true if the table (specified by oid) is intention shared locked by this transaction */
284
310
auto IsTableIntentionSharedLocked (const table_oid_t &oid) -> bool {
285
311
return is_table_lock_set_->find (oid) != is_table_lock_set_->end ();
286
312
}
287
313
314
+ /* * @return true if the table (specified by oid) is shared locked by this transaction */
288
315
auto IsTableSharedLocked (const table_oid_t &oid) -> bool {
289
316
return s_table_lock_set_->find (oid) != s_table_lock_set_->end ();
290
317
}
291
318
319
+ /* * @return true if the table (specified by oid) is intention exclusive locked by this transaction */
292
320
auto IsTableIntentionExclusiveLocked (const table_oid_t &oid) -> bool {
293
321
return ix_table_lock_set_->find (oid) != ix_table_lock_set_->end ();
294
322
}
295
323
324
+ /* * @return true if the table (specified by oid) is exclusive locked by this transaction */
296
325
auto IsTableExclusiveLocked (const table_oid_t &oid) -> bool {
297
326
return x_table_lock_set_->find (oid) != x_table_lock_set_->end ();
298
327
}
299
328
329
+ /* * @return true if the table (specified by oid) is shared intention exclusive locked by this transaction */
300
330
auto IsTableSharedIntentionExclusiveLocked (const table_oid_t &oid) -> bool {
301
331
return six_table_lock_set_->find (oid) != six_table_lock_set_->end ();
302
332
}
@@ -340,6 +370,7 @@ class Transaction {
340
370
/* * The LSN of the last record written by the transaction. */
341
371
lsn_t prev_lsn_;
342
372
373
+ /* * The latch for this transaction */
343
374
std::mutex latch_;
344
375
345
376
/* * Concurrent index: the pages that were latched during index operation. */
0 commit comments