TEST_F(LayoutTunerTests, BasicTest) { const int tuple_count = TESTS_TUPLES_PER_TILEGROUP; std::string db_name = "test_db"; TestingExecutorUtil::InitializeDatabase(db_name); auto data_table = TestingExecutorUtil::CreateTableUpdateCatalog(tuple_count, db_name); // Create a table and populate it auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); auto txn = txn_manager.BeginTransaction(); TestingExecutorUtil::PopulateTable(data_table, tuple_count, false, false, true, txn); txn_manager.CommitTransaction(txn); // Check column count oid_t column_count = data_table->GetSchema()->GetColumnCount(); EXPECT_EQ(column_count, 4); // Layout tuner tuning::LayoutTuner &layout_tuner = tuning::LayoutTuner::GetInstance(); // Attach table to index tuner layout_tuner.AddTable(data_table); // Check old default tile group layout auto old_default_layout = data_table->GetDefaultLayout(); LOG_INFO("Layout: %s", old_default_layout->GetColumnMapInfo().c_str()); // Start layout tuner layout_tuner.Start(); std::vector<double> columns_accessed(column_count, 0); double sample_weight; // initialize a uniform distribution between 0 and 1 UniformGenerator generator; for (int sample_itr = 0; sample_itr < 10000; sample_itr++) { auto rng_val = generator.GetSample(); if (rng_val < 0.9) { columns_accessed = {0, 1, 2}; sample_weight = 100; } else { columns_accessed = {3}; sample_weight = 10; } // Create a table access sample // Indicates the columns accessed (bitmap), and query weight tuning::Sample sample(columns_accessed, sample_weight); // Collect layout sample in table data_table->RecordLayoutSample(sample); // Periodically sleep a bit // Layout tuner thread will process the layout samples periodically, // derive the new table layout, and // transform the layout of existing tile groups in the table if(sample_itr % 100 == 0 ){ std::this_thread::sleep_for(std::chrono::microseconds(1000)); } } // Stop layout tuner layout_tuner.Stop(); // Detach all tables from layout tuner layout_tuner.ClearTables(); // Check new default tile group layout auto new_default_layout = data_table->GetDefaultLayout(); LOG_INFO("Layout: %s", new_default_layout->GetColumnMapInfo().c_str()); // Ensure that the layout has been changed EXPECT_NE(*new_default_layout, *old_default_layout); // Check the new default table layout column_count = new_default_layout->GetColumnCount(); EXPECT_EQ(column_count, 4); // Check the tile corresponding to each column. EXPECT_EQ(new_default_layout->GetTileIdFromColumnId(0), 0); EXPECT_EQ(new_default_layout->GetTileIdFromColumnId(1), 0); EXPECT_EQ(new_default_layout->GetTileIdFromColumnId(2), 0); EXPECT_EQ(new_default_layout->GetTileIdFromColumnId(3), 1); // Check the per tile stats of the new layout // The layout must contain 2 tiles with the following stats // 0 -> 3 // 1 -> 1 auto layout_stats = new_default_layout->GetLayoutStats(); EXPECT_EQ(layout_stats[0], 3); EXPECT_EQ(layout_stats[1], 1); TestingExecutorUtil::DeleteDatabase(db_name); }
TEST_F(CatalogTests, LayoutCatalogTest) { // This test creates a table, changes its layout. // Create another additional layout. // Ensure that default is not changed. // Drops layout and verifies that the default_layout is reset. // It also queries pg_layout to ensure that the entry is removed. auto db_name = "temp_db"; auto table_name = "temp_table"; auto catalog = catalog::Catalog::GetInstance(); // Create database. auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); auto txn = txn_manager.BeginTransaction(); EXPECT_EQ(ResultType::SUCCESS, catalog->CreateDatabase(db_name, txn)); // Create table. auto val0 = catalog::Column(type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), "val0", true); auto val1 = catalog::Column(type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), "val1", true); auto val2 = catalog::Column(type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), "val2", true); auto val3 = catalog::Column(type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER), "val3", true); std::unique_ptr<catalog::Schema> table_schema( new catalog::Schema({val0, val1, val2, val3})); EXPECT_EQ(ResultType::SUCCESS, catalog->CreateTable(db_name, DEFAULT_SCHEMA_NAME, table_name, std::move(table_schema), txn)); txn_manager.CommitTransaction(txn); txn = txn_manager.BeginTransaction(); auto database_oid = catalog->GetDatabaseObject(db_name, txn)->GetDatabaseOid(); auto table_object = catalog->GetTableObject(db_name, DEFAULT_SCHEMA_NAME, table_name, txn); auto table_oid = table_object->GetTableOid(); auto table = catalog->GetTableWithName(db_name, DEFAULT_SCHEMA_NAME, table_name, txn); txn_manager.CommitTransaction(txn); // Change default layout. std::map<oid_t, std::pair<oid_t, oid_t>> default_map; default_map[0] = std::make_pair(0, 0); default_map[1] = std::make_pair(0, 1); default_map[2] = std::make_pair(1, 0); default_map[3] = std::make_pair(1, 1); txn = txn_manager.BeginTransaction(); auto default_layout = catalog->CreateDefaultLayout(database_oid, table_oid, default_map, txn); txn_manager.CommitTransaction(txn); EXPECT_NE(nullptr, default_layout); // Create additional layout. std::map<oid_t, std::pair<oid_t, oid_t>> non_default_map; non_default_map[0] = std::make_pair(0, 0); non_default_map[1] = std::make_pair(0, 1); non_default_map[2] = std::make_pair(1, 0); non_default_map[3] = std::make_pair(1, 1); txn = txn_manager.BeginTransaction(); auto other_layout = catalog->CreateLayout(database_oid, table_oid, non_default_map, txn); txn_manager.CommitTransaction(txn); // Check that the default layout is still the same. EXPECT_NE(*other_layout.get(), table->GetDefaultLayout()); // Drop the default layout. auto default_layout_oid = default_layout->GetOid(); txn = txn_manager.BeginTransaction(); EXPECT_EQ(ResultType::SUCCESS, catalog->DropLayout(database_oid, table_oid, default_layout_oid, txn)); txn_manager.CommitTransaction(txn); // Check that default layout is reset and set to row_store. EXPECT_NE(*default_layout.get(), table->GetDefaultLayout()); EXPECT_TRUE(table->GetDefaultLayout().IsRowStore()); // Query pg_layout to ensure that the entry is dropped txn = txn_manager.BeginTransaction(); auto pg_layout = catalog->GetSystemCatalogs(database_oid)->GetLayoutCatalog(); EXPECT_EQ(nullptr, pg_layout->GetLayoutWithOid(table_oid, default_layout_oid, txn)); // The additional layout must be present in pg_layout auto other_layout_oid = other_layout->GetOid(); EXPECT_EQ( *(other_layout.get()), *(pg_layout->GetLayoutWithOid(table_oid, other_layout_oid, txn).get())); txn_manager.CommitTransaction(txn); // Drop database txn = txn_manager.BeginTransaction(); catalog->DropDatabaseWithName(db_name, txn); txn_manager.CommitTransaction(txn); }