|
| 1 | +/** |
| 2 | + * @file transaction_coverage_tests.cpp |
| 3 | + * @brief Targeted unit tests to increase coverage of Transaction and Lock Manager |
| 4 | + */ |
| 5 | + |
| 6 | +#include <gtest/gtest.h> |
| 7 | + |
| 8 | +#include <atomic> |
| 9 | +#include <chrono> |
| 10 | +#include <cstdio> |
| 11 | +#include <thread> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include "catalog/catalog.hpp" |
| 15 | +#include "common/config.hpp" |
| 16 | +#include "storage/buffer_pool_manager.hpp" |
| 17 | +#include "storage/heap_table.hpp" |
| 18 | +#include "storage/storage_manager.hpp" |
| 19 | +#include "transaction/lock_manager.hpp" |
| 20 | +#include "transaction/transaction.hpp" |
| 21 | +#include "transaction/transaction_manager.hpp" |
| 22 | + |
| 23 | +using namespace cloudsql; |
| 24 | +using namespace cloudsql::transaction; |
| 25 | +using namespace cloudsql::storage; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +/** |
| 30 | + * @class TransactionCoverageTests |
| 31 | + * @brief Fixture for transaction-related coverage tests to ensure proper resource management. |
| 32 | + */ |
| 33 | +class TransactionCoverageTests : public ::testing::Test { |
| 34 | + protected: |
| 35 | + void SetUp() override { |
| 36 | + catalog_ptr = Catalog::create(); |
| 37 | + disk_manager_ptr = std::make_unique<StorageManager>("./test_data"); |
| 38 | + bpm_ptr = std::make_unique<BufferPoolManager>(config::Config::DEFAULT_BUFFER_POOL_SIZE, |
| 39 | + *disk_manager_ptr); |
| 40 | + lm_ptr = std::make_unique<LockManager>(); |
| 41 | + tm_ptr = std::make_unique<TransactionManager>(*lm_ptr, *catalog_ptr, *bpm_ptr, nullptr); |
| 42 | + |
| 43 | + std::vector<ColumnInfo> cols = {{"id", common::ValueType::TYPE_INT64, 0}, |
| 44 | + {"val", common::ValueType::TYPE_TEXT, 1}}; |
| 45 | + catalog_ptr->create_table("rollback_stress", cols); |
| 46 | + |
| 47 | + executor::Schema schema; |
| 48 | + schema.add_column("id", common::ValueType::TYPE_INT64); |
| 49 | + schema.add_column("val", common::ValueType::TYPE_TEXT); |
| 50 | + |
| 51 | + table_ptr = std::make_unique<HeapTable>("rollback_stress", *bpm_ptr, schema); |
| 52 | + table_ptr->create(); |
| 53 | + |
| 54 | + txn = nullptr; |
| 55 | + } |
| 56 | + |
| 57 | + void TearDown() override { |
| 58 | + if (txn != nullptr) { |
| 59 | + tm_ptr->abort(txn); |
| 60 | + } |
| 61 | + table_ptr.reset(); |
| 62 | + tm_ptr.reset(); |
| 63 | + lm_ptr.reset(); |
| 64 | + bpm_ptr.reset(); |
| 65 | + disk_manager_ptr.reset(); |
| 66 | + catalog_ptr.reset(); |
| 67 | + |
| 68 | + static_cast<void>(std::remove("./test_data/rollback_stress.heap")); |
| 69 | + } |
| 70 | + |
| 71 | + // Pointers managed by the fixture |
| 72 | + std::unique_ptr<Catalog> catalog_ptr; |
| 73 | + std::unique_ptr<StorageManager> disk_manager_ptr; |
| 74 | + std::unique_ptr<BufferPoolManager> bpm_ptr; |
| 75 | + std::unique_ptr<LockManager> lm_ptr; |
| 76 | + std::unique_ptr<TransactionManager> tm_ptr; |
| 77 | + std::unique_ptr<HeapTable> table_ptr; |
| 78 | + |
| 79 | + // Live transaction pointer for cleanup |
| 80 | + Transaction* txn; |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * @brief Stress tests the LockManager with concurrent shared and exclusive requests. |
| 85 | + */ |
| 86 | +TEST(TransactionCoverageTestsStandalone, LockManagerConcurrency) { |
| 87 | + LockManager lm; |
| 88 | + const int num_readers = 5; |
| 89 | + std::vector<std::thread> readers; |
| 90 | + std::atomic<int> shared_granted{0}; |
| 91 | + std::atomic<bool> stop{false}; |
| 92 | + |
| 93 | + Transaction writer_txn(100); |
| 94 | + |
| 95 | + // Writers holds exclusive lock initially |
| 96 | + ASSERT_TRUE(lm.acquire_exclusive(&writer_txn, "RESOURCE")); |
| 97 | + |
| 98 | + for (int i = 0; i < num_readers; ++i) { |
| 99 | + readers.emplace_back([&, i]() { |
| 100 | + Transaction reader_txn(i); |
| 101 | + if (lm.acquire_shared(&reader_txn, "RESOURCE")) { |
| 102 | + shared_granted++; |
| 103 | + while (!stop) { |
| 104 | + std::this_thread::yield(); |
| 105 | + } |
| 106 | + lm.unlock(&reader_txn, "RESOURCE"); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + // Readers should be blocked by the writer |
| 112 | + std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 113 | + EXPECT_EQ(shared_granted.load(), 0); |
| 114 | + |
| 115 | + // Release writer lock, readers should proceed |
| 116 | + lm.unlock(&writer_txn, "RESOURCE"); |
| 117 | + |
| 118 | + // Wait for all readers to get the lock |
| 119 | + for (int i = 0; i < 50 && shared_granted.load() < num_readers; ++i) { |
| 120 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 121 | + } |
| 122 | + EXPECT_EQ(shared_granted.load(), num_readers); |
| 123 | + |
| 124 | + stop = true; |
| 125 | + for (auto& t : readers) { |
| 126 | + t.join(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * @brief Tests deep rollback functionality via the Undo Log. |
| 132 | + * Uses the TransactionCoverageTests fixture for automated cleanup. |
| 133 | + */ |
| 134 | +TEST_F(TransactionCoverageTests, DeepRollback) { |
| 135 | + // Expose symbols to reuse existing test body logic |
| 136 | + TransactionManager& tm = *tm_ptr; |
| 137 | + HeapTable& table = *table_ptr; |
| 138 | + |
| 139 | + txn = tm.begin(); |
| 140 | + |
| 141 | + // 1. Insert some data |
| 142 | + auto rid1 = |
| 143 | + table.insert(executor::Tuple({common::Value::make_int64(1), common::Value::make_text("A")}), |
| 144 | + txn->get_id()); |
| 145 | + txn->add_undo_log(UndoLog::Type::INSERT, "rollback_stress", rid1); |
| 146 | + |
| 147 | + auto rid2 = |
| 148 | + table.insert(executor::Tuple({common::Value::make_int64(2), common::Value::make_text("B")}), |
| 149 | + txn->get_id()); |
| 150 | + txn->add_undo_log(UndoLog::Type::INSERT, "rollback_stress", rid2); |
| 151 | + |
| 152 | + // 2. Update data |
| 153 | + table.remove(rid1, txn->get_id()); // Mark old version deleted |
| 154 | + auto rid1_new = table.insert( |
| 155 | + executor::Tuple({common::Value::make_int64(1), common::Value::make_text("A_NEW")}), |
| 156 | + txn->get_id()); |
| 157 | + txn->add_undo_log(UndoLog::Type::UPDATE, "rollback_stress", rid1_new, rid1); |
| 158 | + |
| 159 | + // 3. Delete data |
| 160 | + table.remove(rid2, txn->get_id()); |
| 161 | + txn->add_undo_log(UndoLog::Type::DELETE, "rollback_stress", rid2); |
| 162 | + |
| 163 | + EXPECT_EQ(table.tuple_count(), 1U); // rid1_new is active, rid1 and rid2 are logically deleted |
| 164 | + |
| 165 | + // 4. Abort |
| 166 | + tm.abort(txn); |
| 167 | + txn = nullptr; // Marked as aborted and handled by TearDown if still set |
| 168 | + |
| 169 | + // 5. Verify restoration |
| 170 | + EXPECT_EQ(table.tuple_count(), |
| 171 | + 0U); // Inserted rows should be physically removed or logically invisible |
| 172 | + |
| 173 | + // The table should be empty because we aborted the inserts |
| 174 | + auto iter = table.scan(); |
| 175 | + executor::Tuple t; |
| 176 | + EXPECT_FALSE(iter.next(t)); |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace |
0 commit comments