Skip to content

Commit 7214d66

Browse files
committed
Cherry-pick GC fixes from cmu-db#1349, third part. Compiles. Brought in tests, need to verify.
1 parent 2830687 commit 7214d66

File tree

4 files changed

+1055
-99
lines changed

4 files changed

+1055
-99
lines changed

test/concurrency/testing_transaction_util.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ storage::DataTable *TestingTransactionUtil::CreateTable(
212212
return table;
213213
}
214214

215+
void TestingTransactionUtil::AddSecondaryIndex(storage::DataTable *table) {
216+
// Create unique index on the value column
217+
std::vector<oid_t> key_attrs = {1};
218+
auto tuple_schema = table->GetSchema();
219+
bool unique = false;
220+
auto key_schema = catalog::Schema::CopySchema(tuple_schema, key_attrs);
221+
key_schema->SetIndexedColumns(key_attrs);
222+
auto index_metadata2 = new index::IndexMetadata(
223+
"unique_btree_index", 1235, TEST_TABLE_OID, CATALOG_DATABASE_OID,
224+
IndexType::BWTREE, IndexConstraintType::UNIQUE, tuple_schema, key_schema,
225+
key_attrs, unique);
226+
227+
std::shared_ptr<index::Index> secondary_key_index(
228+
index::IndexFactory::GetIndex(index_metadata2));
229+
230+
table->AddIndex(secondary_key_index);
231+
}
232+
215233
std::unique_ptr<const planner::ProjectInfo>
216234
TestingTransactionUtil::MakeProjectInfoFromTuple(const storage::Tuple *tuple) {
217235
TargetList target_list;
@@ -456,5 +474,83 @@ bool TestingTransactionUtil::ExecuteScan(
456474
}
457475
return true;
458476
}
477+
478+
ResultType TestingTransactionUtil::UpdateTuple(storage::DataTable *table,
479+
const int key) {
480+
srand(15721);
481+
482+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
483+
TransactionScheduler scheduler(1, table, &txn_manager);
484+
scheduler.Txn(0).Update(key, rand() % 15721);
485+
scheduler.Txn(0).Commit();
486+
scheduler.Run();
487+
488+
return scheduler.schedules[0].txn_result;
489+
}
490+
491+
ResultType TestingTransactionUtil::InsertTuple(storage::DataTable *table,
492+
const int key) {
493+
srand(15721);
494+
495+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
496+
TransactionScheduler scheduler(1, table, &txn_manager);
497+
scheduler.Txn(0).Insert(key, rand() % 15721);
498+
scheduler.Txn(0).Commit();
499+
scheduler.Run();
500+
501+
return scheduler.schedules[0].txn_result;
502+
}
503+
504+
ResultType TestingTransactionUtil::BulkInsertTuples(storage::DataTable *table,
505+
const size_t num_tuples) {
506+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
507+
TransactionScheduler scheduler(1, table, &txn_manager);
508+
for (size_t i = 0; i < num_tuples; i++) {
509+
scheduler.Txn(0).Insert(i, i);
510+
}
511+
scheduler.Txn(0).Commit();
512+
scheduler.Run();
513+
514+
return scheduler.schedules[0].txn_result;
515+
}
516+
517+
ResultType TestingTransactionUtil::BulkDeleteTuples(storage::DataTable *table,
518+
const size_t num_tuples) {
519+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
520+
TransactionScheduler scheduler(1, table, &txn_manager);
521+
for (size_t i = 0; i < num_tuples; i++) {
522+
scheduler.Txn(0).Delete(i, false);
523+
}
524+
scheduler.Txn(0).Commit();
525+
scheduler.Run();
526+
527+
return scheduler.schedules[0].txn_result;
528+
}
529+
530+
ResultType TestingTransactionUtil::DeleteTuple(storage::DataTable *table,
531+
const int key) {
532+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
533+
TransactionScheduler scheduler(1, table, &txn_manager);
534+
scheduler.Txn(0).Delete(key);
535+
scheduler.Txn(0).Commit();
536+
scheduler.Run();
537+
538+
return scheduler.schedules[0].txn_result;
459539
}
540+
541+
ResultType TestingTransactionUtil::SelectTuple(storage::DataTable *table,
542+
const int key,
543+
std::vector<int> &results) {
544+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
545+
TransactionScheduler scheduler(1, table, &txn_manager);
546+
scheduler.Txn(0).Read(key);
547+
scheduler.Txn(0).Commit();
548+
scheduler.Run();
549+
550+
results = scheduler.schedules[0].results;
551+
552+
return scheduler.schedules[0].txn_result;
460553
}
554+
555+
} // namespace test
556+
} // namespace peloton

test/gc/garbage_collection_test.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ int GarbageNum(storage::DataTable *table) {
106106
// get tuple recycled by GC
107107
int RecycledNum(storage::DataTable *table) {
108108
int count = 0;
109-
auto table_id = table->GetOid();
110-
while (!gc::GCManagerFactory::GetInstance().ReturnFreeSlot(table_id).IsNull())
109+
while (
110+
!gc::GCManagerFactory::GetInstance().GetRecycledTupleSlot(table).IsNull())
111111
count++;
112112

113113
LOG_INFO("recycled version num = %d", count);
@@ -134,7 +134,7 @@ TEST_F(GarbageCollectionTests, UpdateTest) {
134134
std::unique_ptr<storage::DataTable> table(TestingTransactionUtil::CreateTable(
135135
num_key, "UPDATE_TABLE", db_id, INVALID_OID, 1234, true));
136136

137-
EXPECT_TRUE(gc_manager.GetTableCount() == 1);
137+
EXPECT_EQ(1, gc_manager.GetTableCount());
138138

139139
gc_manager.StartGC(gc_threads);
140140

@@ -284,11 +284,7 @@ TEST_F(GarbageCollectionTests, DeleteTest) {
284284

285285
// there should be two versions to be recycled by the GC:
286286
// the deleted version and the empty version.
287-
// however, the txn will explicitly pass one version (the deleted
288-
// version) to the GC manager.
289-
// The GC itself should be responsible for recycling the
290-
// empty version.
291-
EXPECT_EQ(1, recycle_num);
287+
EXPECT_EQ(2, recycle_num);
292288

293289
gc_manager.StopGC();
294290
gc::GCManagerFactory::Configure(0);

0 commit comments

Comments
 (0)