Skip to content

Commit 12b1e3a

Browse files
committed
feat(txn): harden transactional integrity and error handling
1 parent e76ca62 commit 12b1e3a

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

include/transaction/transaction.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define CLOUDSQL_TRANSACTION_TRANSACTION_HPP
88

99
#include <atomic>
10+
#include <cassert>
1011
#include <mutex>
1112
#include <optional>
1213
#include <unordered_set>
@@ -123,13 +124,15 @@ class Transaction {
123124
void add_undo_log(UndoLog::Type type, const std::string& table_name,
124125
const storage::HeapTable::TupleId& rid) {
125126
/* Enforce invariant: non-UPDATE types should not provide old_rid through this overload */
127+
assert(type != UndoLog::Type::UPDATE);
126128
undo_logs_.push_back({type, table_name, rid, std::nullopt});
127129
}
128130

129131
void add_undo_log(UndoLog::Type type, const std::string& table_name,
130132
const storage::HeapTable::TupleId& rid,
131133
const storage::HeapTable::TupleId& old_rid) {
132134
/* Enforce invariant: this overload is primarily for UPDATE types providing old_rid */
135+
assert(type == UndoLog::Type::UPDATE);
133136
undo_logs_.push_back({type, table_name, rid, old_rid});
134137
}
135138

src/executor/query_executor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,10 @@ QueryResult QueryExecutor::execute_delete(const parser::DeleteStatement& stmt,
533533

534534
/* Retrieve old tuple for logging and index maintenance (unconditional) */
535535
Tuple old_tuple;
536-
static_cast<void>(table.get(rid, old_tuple));
536+
if (!table.get(rid, old_tuple)) {
537+
result.set_error("Failed to retrieve tuple for deletion maintenance: " + rid.to_string());
538+
return result;
539+
}
537540

538541
if (table.remove(rid, xmax)) {
539542
/* Update Indexes */

src/transaction/transaction_manager.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ bool TransactionManager::undo_transaction(Transaction* txn) {
165165
const auto& log = *it;
166166
auto table_meta_opt = catalog_.get_table_by_name(log.table_name);
167167
if (!table_meta_opt) {
168+
std::cerr << "Rollback ERROR: Table metadata not found for '" << log.table_name
169+
<< "' during undo. Transaction: " << txn->get_id() << "\n";
170+
success = false;
168171
continue;
169172
}
170173
const auto* table_meta = table_meta_opt.value();
@@ -187,7 +190,12 @@ bool TransactionManager::undo_transaction(Transaction* txn) {
187190
uint16_t pos = idx_info.column_positions[0];
188191
common::ValueType ktype = table_meta->columns[pos].type;
189192
storage::BTreeIndex index(idx_info.name, bpm_, ktype);
190-
static_cast<void>(index.remove(tuple.get(pos), log.rid));
193+
if (!index.remove(tuple.get(pos), log.rid)) {
194+
std::cerr << "Rollback ERROR: Index remove failed for table '"
195+
<< log.table_name << "', index '" << idx_info.name
196+
<< "'\n";
197+
success = false;
198+
}
191199
}
192200
}
193201
}
@@ -210,7 +218,12 @@ bool TransactionManager::undo_transaction(Transaction* txn) {
210218
uint16_t pos = idx_info.column_positions[0];
211219
common::ValueType ktype = table_meta->columns[pos].type;
212220
storage::BTreeIndex index(idx_info.name, bpm_, ktype);
213-
static_cast<void>(index.insert(tuple.get(pos), log.rid));
221+
if (!index.insert(tuple.get(pos), log.rid)) {
222+
std::cerr << "Rollback ERROR: Index insert failed for table '"
223+
<< log.table_name << "', index '" << idx_info.name
224+
<< "'\n";
225+
success = false;
226+
}
214227
}
215228
}
216229
}
@@ -227,7 +240,12 @@ bool TransactionManager::undo_transaction(Transaction* txn) {
227240
uint16_t pos = idx_info.column_positions[0];
228241
common::ValueType ktype = table_meta->columns[pos].type;
229242
storage::BTreeIndex index(idx_info.name, bpm_, ktype);
230-
static_cast<void>(index.remove(new_tuple.get(pos), log.rid));
243+
if (!index.remove(new_tuple.get(pos), log.rid)) {
244+
std::cerr << "Rollback ERROR: Index remove failed for table '"
245+
<< log.table_name << "', index '" << idx_info.name
246+
<< "'\n";
247+
success = false;
248+
}
231249
}
232250
}
233251
}
@@ -250,8 +268,12 @@ bool TransactionManager::undo_transaction(Transaction* txn) {
250268
uint16_t pos = idx_info.column_positions[0];
251269
common::ValueType ktype = table_meta->columns[pos].type;
252270
storage::BTreeIndex index(idx_info.name, bpm_, ktype);
253-
static_cast<void>(
254-
index.insert(old_tuple.get(pos), log.old_rid.value()));
271+
if (!index.insert(old_tuple.get(pos), log.old_rid.value())) {
272+
std::cerr << "Rollback ERROR: Index insert failed for table '"
273+
<< log.table_name << "', index '" << idx_info.name
274+
<< "'\n";
275+
success = false;
276+
}
255277
}
256278
}
257279
}

0 commit comments

Comments
 (0)