Skip to content
This repository has been archived by the owner on Sep 27, 2019. It is now read-only.

Commit

Permalink
Modify CreateTable for tests + modify LayoutTuner
Browse files Browse the repository at this point in the history
  • Loading branch information
poojanilangekar committed May 12, 2018
1 parent 0ff874d commit ecb95bb
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/include/storage/layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ class Layout : public Printable {
};

} // namespace storage
} // namespace peloton
} // namespace peloton
3 changes: 2 additions & 1 deletion src/include/tuning/layout_tuner.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ class LayoutTuner {
* Update layout of table
*
* @param table The table
* @return true if the update succeeds, false otherwise
*/
void UpdateDefaultPartition(storage::DataTable *table);
bool UpdateDefaultPartition(storage::DataTable *table);

private:
/**
Expand Down
12 changes: 8 additions & 4 deletions src/tuning/layout_tuner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Sample GetClustererSample(const Sample& sample, oid_t column_count) {
return clusterer_sample;
}

void LayoutTuner::UpdateDefaultPartition(storage::DataTable* table) {
bool LayoutTuner::UpdateDefaultPartition(storage::DataTable *table) {
oid_t column_count = table->GetSchema()->GetColumnCount();

// Set up clusterer
Expand All @@ -87,7 +87,9 @@ void LayoutTuner::UpdateDefaultPartition(storage::DataTable* table) {

// Check if we have any samples
if (samples.empty()) {
return;
LOG_DEBUG("Table[%u] contains no LayoutSamples. Layout not tuned.",
table->GetOid());
return false;
}

for (auto sample : samples) {
Expand Down Expand Up @@ -120,12 +122,13 @@ void LayoutTuner::UpdateDefaultPartition(storage::DataTable* table) {
nullptr) {
txn_manager.AbortTransaction(txn);
LOG_DEBUG("Layout Update to failed.");
return;
return false;
}
txn_manager.CommitTransaction(txn);

UNUSED_ATTRIBUTE auto layout = table->GetDefaultLayout();
LOG_TRACE("Updated Layout: %s", layout.GetInfo().c_str());
return true;
}

void LayoutTuner::Tune() {
Expand All @@ -142,7 +145,8 @@ void LayoutTuner::Tune() {
table->TransformTileGroup(tile_group_offset, theta);

// Update partitioning periodically
UpdateDefaultPartition(table);
// TODO Lin/Tianyu - Add Failure Handling/Retry logic.
UNUSED_ATTRIBUTE bool update_result = UpdateDefaultPartition(table);

// Sleep a bit
std::this_thread::sleep_for(std::chrono::microseconds(sleep_duration));
Expand Down
100 changes: 20 additions & 80 deletions test/codegen/table_scan_translator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,90 +36,18 @@ class TableScanTranslatorTest : public PelotonCodeGenTest {
CreateAndLoadAllColsTable();
}

void ExecuteTileGroupTest(peloton::LayoutType layout_type) {
const int tuples_per_tilegroup = 100;
const int tile_group_count = 5;
const int tuple_count = tuples_per_tilegroup * tile_group_count;
const oid_t col_count = 100;
const bool is_inlined = true;

/////////////////////////////////////////////////////////
// Define the schema.
/////////////////////////////////////////////////////////

std::vector<catalog::Column> columns;

for (oid_t col_itr = 0; col_itr <= col_count; col_itr++) {
auto column = catalog::Column(
type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
"FIELD" + std::to_string(col_itr), is_inlined);

columns.push_back(column);
}

std::unique_ptr<catalog::Schema> table_schema =
std::unique_ptr<catalog::Schema>(new catalog::Schema(columns));
std::string table_name("TEST_TABLE");

/////////////////////////////////////////////////////////
// Create table.
/////////////////////////////////////////////////////////

bool is_catalog = false;
auto *catalog = catalog::Catalog::GetInstance();
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
const bool allocate = true;
auto txn = txn_manager.BeginTransaction();

// Insert table in catalog
catalog->CreateTable(test_db_name, DEFAULT_SCHEMA_NAME, table_name,
std::move(table_schema), txn, is_catalog,
tuples_per_tilegroup, layout_type);
// Get table reference
auto table = catalog->GetTableWithName(test_db_name, DEFAULT_SCHEMA_NAME,
table_name, txn);
txn_manager.EndTransaction(txn);

/////////////////////////////////////////////////////////
// Load in the data
/////////////////////////////////////////////////////////

// Insert tuples into tile_group.

txn = txn_manager.BeginTransaction();
auto table_schema_ptr = table->GetSchema();
auto testing_pool = TestingHarness::GetInstance().GetTestingPool();

for (oid_t row_id = 0; row_id < tuple_count; row_id++) {
int populate_value = row_id;

storage::Tuple tuple(table_schema_ptr, allocate);

for (oid_t col_id = 0; col_id <= col_count; col_id++) {
auto value =
type::ValueFactory::GetIntegerValue(populate_value + col_id);
tuple.SetValue(col_id, value, testing_pool);
}

ItemPointer *index_entry_ptr = nullptr;
ItemPointer tuple_slot_id =
table->InsertTuple(&tuple, txn, &index_entry_ptr);

EXPECT_TRUE(tuple_slot_id.block != INVALID_OID);
EXPECT_TRUE(tuple_slot_id.offset != INVALID_OID);

txn_manager.PerformInsert(txn, tuple_slot_id, index_entry_ptr);
}

txn_manager.CommitTransaction(txn);
void ScanLayoutTable(oid_t tuples_per_tilegroup, oid_t tilegroup_count,
oid_t column_count) {
auto table = GetLayoutTable();
oid_t tuple_count = tuples_per_tilegroup * tilegroup_count;

/////////////////////////////////////////////////////////
// Do a seq scan on the table with the given layout
/////////////////////////////////////////////////////////

// Column ids to be scanned.
std::vector<oid_t> column_ids;
for (oid_t col_id = 0; col_id < col_count; col_id++) {
for (oid_t col_id = 0; col_id < column_count; col_id++) {
column_ids.push_back(col_id);
}

Expand All @@ -143,7 +71,7 @@ class TableScanTranslatorTest : public PelotonCodeGenTest {
for (oid_t tuple_id = 0; tuple_id < tuple_count; tuple_id++) {
auto &tuple = results[tuple_id];
int tuple_id_value = tuple_id;
for (oid_t col_id = 0; col_id < col_count; col_id++) {
for (oid_t col_id = 0; col_id < column_count; col_id++) {
auto value =
type::ValueFactory::GetIntegerValue(tuple_id_value + col_id);
EXPECT_EQ(CmpBool::CmpTrue,
Expand Down Expand Up @@ -706,15 +634,27 @@ TEST_F(TableScanTranslatorTest, ScanRowLayout) {
// Creates a table with LayoutType::ROW and
// invokes the TableScanTranslator
//
ExecuteTileGroupTest(LayoutType::ROW);
oid_t tuples_per_tilegroup = 100;
oid_t tilegroup_count = 5;
oid_t column_count = 100;
bool is_inlined = true;
CreateAndLoadTableWithLayout(LayoutType::ROW, tuples_per_tilegroup,
tilegroup_count, column_count, is_inlined);
ScanLayoutTable(tuples_per_tilegroup, tilegroup_count, column_count);
}

TEST_F(TableScanTranslatorTest, ScanColumnLayout) {
//
// Creates a table with LayoutType::COLUMN and
// invokes the TableScanTranslator
//
ExecuteTileGroupTest(LayoutType::COLUMN);
oid_t tuples_per_tilegroup = 100;
oid_t tilegroup_count = 5;
oid_t column_count = 100;
bool is_inlined = true;
CreateAndLoadTableWithLayout(LayoutType::COLUMN, tuples_per_tilegroup,
tilegroup_count, column_count, is_inlined);
ScanLayoutTable(tuples_per_tilegroup, tilegroup_count, column_count);
}

TEST_F(TableScanTranslatorTest, MultiLayoutScan) {
Expand Down
87 changes: 82 additions & 5 deletions test/codegen/testing_codegen_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ namespace test {
// PELOTON CODEGEN TEST
//===----------------------------------------------------------------------===//

PelotonCodeGenTest::PelotonCodeGenTest(oid_t tuples_per_tilegroup) {
PelotonCodeGenTest::PelotonCodeGenTest(oid_t tuples_per_tilegroup,
peloton::LayoutType layout_type) {
auto *catalog = catalog::Catalog::GetInstance();
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto txn = txn_manager.BeginTransaction();
Expand All @@ -41,9 +42,10 @@ PelotonCodeGenTest::PelotonCodeGenTest(oid_t tuples_per_tilegroup) {
catalog->CreateDatabase(test_db_name, txn);
test_db = catalog->GetDatabaseWithName(test_db_name, txn);
// Create test table
CreateTestTables(txn, tuples_per_tilegroup);
CreateTestTables(txn, tuples_per_tilegroup, layout_type);

txn_manager.CommitTransaction(txn);
layout_table = nullptr;
}

PelotonCodeGenTest::~PelotonCodeGenTest() {
Expand Down Expand Up @@ -103,13 +105,14 @@ std::unique_ptr<catalog::Schema> PelotonCodeGenTest::CreateTestSchema(

// Create all the test tables, but don't load any data
void PelotonCodeGenTest::CreateTestTables(concurrency::TransactionContext *txn,
oid_t tuples_per_tilegroup) {
oid_t tuples_per_tilegroup,
peloton::LayoutType layout_type) {
auto *catalog = catalog::Catalog::GetInstance();
for (int i = 0; i < 4; i++) {
auto table_schema = CreateTestSchema();
catalog->CreateTable(test_db_name, DEFAULT_SCHEMA_NAME, test_table_names[i],
std::move(table_schema), txn, false,
tuples_per_tilegroup);
tuples_per_tilegroup, layout_type);
test_table_oids.push_back(catalog
->GetTableObject(test_db_name,
DEFAULT_SCHEMA_NAME,
Expand All @@ -120,7 +123,7 @@ void PelotonCodeGenTest::CreateTestTables(concurrency::TransactionContext *txn,
auto table_schema = CreateTestSchema(true);
catalog->CreateTable(test_db_name, DEFAULT_SCHEMA_NAME, test_table_names[i],
std::move(table_schema), txn, false,
tuples_per_tilegroup);
tuples_per_tilegroup, layout_type);
test_table_oids.push_back(catalog
->GetTableObject(test_db_name,
DEFAULT_SCHEMA_NAME,
Expand Down Expand Up @@ -175,6 +178,80 @@ void PelotonCodeGenTest::LoadTestTable(oid_t table_id, uint32_t num_rows,
txn_manager.CommitTransaction(txn);
}

void PelotonCodeGenTest::CreateAndLoadTableWithLayout(
peloton::LayoutType layout_type, oid_t tuples_per_tilegroup,
oid_t tile_group_count, oid_t column_count, bool is_inlined) {
oid_t tuple_count = tuples_per_tilegroup * tile_group_count;
/////////////////////////////////////////////////////////
// Define the schema.
/////////////////////////////////////////////////////////

std::vector<catalog::Column> columns;

for (oid_t col_itr = 0; col_itr <= column_count; col_itr++) {
auto column = catalog::Column(
type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
"FIELD" + std::to_string(col_itr), is_inlined);

columns.push_back(column);
}

std::unique_ptr<catalog::Schema> table_schema =
std::unique_ptr<catalog::Schema>(new catalog::Schema(columns));
std::string table_name("LAYOUT_TABLE");

/////////////////////////////////////////////////////////
// Create table.
/////////////////////////////////////////////////////////

bool is_catalog = false;
auto *catalog = catalog::Catalog::GetInstance();
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
const bool allocate = true;
auto txn = txn_manager.BeginTransaction();

// Insert table in catalog
catalog->CreateTable(test_db_name, DEFAULT_SCHEMA_NAME, table_name,
std::move(table_schema), txn, is_catalog,
tuples_per_tilegroup, layout_type);
// Get table reference
layout_table = catalog->GetTableWithName(test_db_name, DEFAULT_SCHEMA_NAME,
table_name, txn);
txn_manager.EndTransaction(txn);

/////////////////////////////////////////////////////////
// Load in the data
/////////////////////////////////////////////////////////

// Insert tuples into tile_group.

txn = txn_manager.BeginTransaction();
auto table_schema_ptr = layout_table->GetSchema();
auto testing_pool = TestingHarness::GetInstance().GetTestingPool();

for (oid_t row_id = 0; row_id < tuple_count; row_id++) {
int populate_value = row_id;

storage::Tuple tuple(table_schema_ptr, allocate);

for (oid_t col_id = 0; col_id <= column_count; col_id++) {
auto value = type::ValueFactory::GetIntegerValue(populate_value + col_id);
tuple.SetValue(col_id, value, testing_pool);
}

ItemPointer *index_entry_ptr = nullptr;
ItemPointer tuple_slot_id =
layout_table->InsertTuple(&tuple, txn, &index_entry_ptr);

EXPECT_TRUE(tuple_slot_id.block != INVALID_OID);
EXPECT_TRUE(tuple_slot_id.offset != INVALID_OID);

txn_manager.PerformInsert(txn, tuple_slot_id, index_entry_ptr);
}

txn_manager.CommitTransaction(txn);
}

void PelotonCodeGenTest::ExecuteSync(
codegen::Query &query,
std::unique_ptr<executor::ExecutorContext> executor_context,
Expand Down
19 changes: 15 additions & 4 deletions test/include/codegen/testing_codegen_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class PelotonCodeGenTest : public PelotonTest {
"table4", "table5"};
std::vector<oid_t> test_table_oids;

PelotonCodeGenTest(oid_t tuples_per_tilegroup = DEFAULT_TUPLES_PER_TILEGROUP);
PelotonCodeGenTest(oid_t tuples_per_tilegroup = DEFAULT_TUPLES_PER_TILEGROUP,
peloton::LayoutType layout_type = LayoutType::ROW);

virtual ~PelotonCodeGenTest();

Expand All @@ -64,21 +65,30 @@ class PelotonCodeGenTest : public PelotonTest {
return *GetDatabase().GetTableWithOid(static_cast<uint32_t>(table_id));
}

// Get the layout table
storage::DataTable *GetLayoutTable() const { return layout_table; }

// Create the schema (common among all tables)
catalog::Column GetTestColumn(uint32_t col_id) const;

std::unique_ptr<catalog::Schema> CreateTestSchema(
bool add_primary = false) const;

// Create the test tables
void CreateTestTables(
concurrency::TransactionContext *txn,
oid_t tuples_per_tilegroup = DEFAULT_TUPLES_PER_TILEGROUP);
void CreateTestTables(concurrency::TransactionContext *txn,
oid_t tuples_per_tilegroup,
peloton::LayoutType layout_type);

// Load the given table with the given number of rows
void LoadTestTable(oid_t table_id, uint32_t num_rows,
bool insert_nulls = false);

// Load tables with the specified layout
void CreateAndLoadTableWithLayout(peloton::LayoutType layout_type,
oid_t tuples_per_tilegroup,
oid_t tile_group_count, oid_t column_count,
bool is_inlined);

static void ExecuteSync(
codegen::Query &query,
std::unique_ptr<executor::ExecutorContext> executor_context,
Expand Down Expand Up @@ -117,6 +127,7 @@ class PelotonCodeGenTest : public PelotonTest {

private:
storage::Database *test_db;
storage::DataTable *layout_table;
};

//===----------------------------------------------------------------------===//
Expand Down

0 comments on commit ecb95bb

Please sign in to comment.