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

Commit daa5970

Browse files
LayoutTunerTest fixed
1 parent 3d8a535 commit daa5970

File tree

8 files changed

+85
-62
lines changed

8 files changed

+85
-62
lines changed

src/catalog/catalog.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "catalog/index_catalog.h"
1919
#include "catalog/index_metrics_catalog.h"
2020
#include "catalog/language_catalog.h"
21+
#include "catalog/layout_catalog.h"
2122
#include "catalog/proc_catalog.h"
2223
#include "catalog/query_history_catalog.h"
2324
#include "catalog/query_metrics_catalog.h"
@@ -65,6 +66,7 @@ Catalog::Catalog() : pool_(new type::EphemeralPool()) {
6566
auto pg_database = DatabaseCatalog::GetInstance(pg_catalog, pool_.get(), txn);
6667
auto pg_table = TableCatalog::GetInstance(pg_catalog, pool_.get(), txn);
6768
IndexCatalog::GetInstance(pg_catalog, pool_.get(), txn);
69+
LayoutCatalog::GetInstance(pg_catalog, pool_.get(), txn);
6870
// ColumnCatalog::GetInstance(); // Called implicitly
6971

7072
// Create indexes on catalog tables, insert them into pg_index
@@ -136,6 +138,8 @@ Catalog::Catalog() : pool_(new type::EphemeralPool()) {
136138
CATALOG_DATABASE_OID, pool_.get(), txn);
137139
pg_table->InsertTable(COLUMN_CATALOG_OID, COLUMN_CATALOG_NAME,
138140
CATALOG_DATABASE_OID, pool_.get(), txn);
141+
pg_table->InsertTable(LAYOUT_CATALOG_OID, LAYOUT_CATALOG_NAME,
142+
CATALOG_DATABASE_OID, pool_.get(), txn);
139143

140144
// Commit transaction
141145
txn_manager.CommitTransaction(txn);
@@ -564,6 +568,7 @@ ResultType Catalog::DropTable(oid_t database_oid, oid_t table_oid,
564568

565569
for (auto it : index_objects) DropIndex(it.second->GetIndexOid(), txn);
566570
ColumnCatalog::GetInstance()->DeleteColumns(table_oid, txn);
571+
LayoutCatalog::GetInstance()->DeleteLayouts(table_oid, txn);
567572
TableCatalog::GetInstance()->DeleteTable(table_oid, txn);
568573

569574
database->GetTableWithOid(table_oid);

src/include/storage/data_table.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ class DataTable : public AbstractTable {
248248

249249
void ClearLayoutSamples();
250250

251-
void SetDefaultLayout(const column_map_type &column_map);
251+
bool SetDefaultLayout(const column_map_type &column_map,
252+
type::AbstractPool *pool,
253+
concurrency::TransactionContext *txn);
252254

253255
const Layout& GetDefaultLayout() const;
254256

src/include/tuning/layout_tuner.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ namespace storage {
2727
class DataTable;
2828
}
2929

30+
namespace type {
31+
class AbstractPool;
32+
}
33+
3034
namespace tuning {
3135

3236
//===--------------------------------------------------------------------===//
@@ -81,14 +85,6 @@ class LayoutTuner {
8185
*/
8286
void ClearTables();
8387

84-
/**
85-
* @brief Gets the column map information.
86-
*
87-
* @param[in] column_map The column map
88-
*
89-
* @return The column map information.
90-
*/
91-
std::string GetColumnMapInfo(const column_map_type &column_map);
9288

9389
protected:
9490
/**
@@ -142,6 +138,9 @@ class LayoutTuner {
142138
/** Desired layout tile count */
143139
oid_t tile_count = 2;
144140

141+
/** Pool to add new layouts in the pg_layout table */
142+
std::unique_ptr<type::AbstractPool> pool_;
143+
145144
};
146145

147146
} // namespace indextuner

src/storage/data_table.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "tuning/clusterer.h"
1717
#include "tuning/sample.h"
1818
#include "catalog/foreign_key.h"
19+
#include "catalog/layout_catalog.h"
1920
#include "catalog/table_catalog.h"
2021
#include "catalog/trigger_catalog.h"
2122
#include "common/container_tuple.h"
@@ -31,7 +32,6 @@
3132
#include "storage/abstract_table.h"
3233
#include "storage/data_table.h"
3334
#include "storage/database.h"
34-
#include "storage/layout.h"
3535
#include "storage/storage_manager.h"
3636
#include "storage/tile.h"
3737
#include "storage/tile_group.h"
@@ -1368,10 +1368,18 @@ void DataTable::ClearIndexSamples() {
13681368
}
13691369
}
13701370

1371-
void DataTable::SetDefaultLayout(const column_map_type &column_map) {
1372-
// TODO Pooja: Generate layout_oid_ and add it to the catalog
1373-
default_layout_ = std::shared_ptr<const Layout>(
1374-
new const Layout(column_map));
1371+
bool DataTable::SetDefaultLayout(const column_map_type &column_map,
1372+
type::AbstractPool *pool,
1373+
concurrency::TransactionContext *txn) {
1374+
oid_t layout_id = GetNextLayoutOid();
1375+
auto new_layout = std::shared_ptr<const Layout>(
1376+
new const Layout(column_map, layout_id));
1377+
auto layout_catalog = catalog::LayoutCatalog::GetInstance();
1378+
bool result = layout_catalog->InsertLayout(table_oid, new_layout, pool, txn);
1379+
if (result) {
1380+
default_layout_ = new_layout;
1381+
}
1382+
return result;
13751383
}
13761384

13771385
const Layout& DataTable::GetDefaultLayout() const {

src/tuning/layout_tuner.cpp

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
#include "catalog/schema.h"
2020
#include "common/logger.h"
2121
#include "common/timer.h"
22+
#include "concurrency/transaction_manager_factory.h"
2223
#include "storage/data_table.h"
24+
#include "type/ephemeral_pool.h"
2325

2426
namespace peloton {
2527
namespace tuning {
@@ -30,7 +32,7 @@ LayoutTuner& LayoutTuner::GetInstance() {
3032
}
3133

3234
LayoutTuner::LayoutTuner() {
33-
// Nothing to do here !
35+
pool_.reset(new type::EphemeralPool());
3436
}
3537

3638
LayoutTuner::~LayoutTuner() {}
@@ -45,42 +47,6 @@ void LayoutTuner::Start() {
4547
LOG_INFO("Started layout tuner");
4648
}
4749

48-
/**
49-
* @brief Print information from column map, used to inspect layout generated
50-
* from clusterer.
51-
*
52-
* @param column_map The column map to be printed.
53-
*/
54-
std::string LayoutTuner::GetColumnMapInfo(const column_map_type& column_map) {
55-
std::stringstream ss;
56-
std::map<oid_t, std::vector<oid_t>> tile_column_map;
57-
58-
// Construct a tile_id => [col_ids] map
59-
for (auto itr = column_map.begin(); itr != column_map.end(); itr++) {
60-
oid_t col_id = itr->first;
61-
oid_t tile_id = itr->second.first;
62-
63-
if (tile_column_map.find(tile_id) == tile_column_map.end()) {
64-
tile_column_map[tile_id] = {};
65-
}
66-
67-
tile_column_map[tile_id].push_back(col_id);
68-
}
69-
70-
// Construct a string from tile_col_map
71-
for (auto itr = tile_column_map.begin(); itr != tile_column_map.end();
72-
itr++) {
73-
oid_t tile_id = itr->first;
74-
ss << tile_id << ": ";
75-
for (oid_t col_id : itr->second) {
76-
ss << col_id << " ";
77-
}
78-
ss << " :: ";
79-
}
80-
81-
return ss.str();
82-
}
83-
8450
Sample GetClustererSample(const Sample& sample, oid_t column_count) {
8551

8652
// Copy over the sample
@@ -142,12 +108,23 @@ void LayoutTuner::UpdateDefaultPartition(storage::DataTable* table) {
142108
table->ClearLayoutSamples();
143109

144110
// Desired number of tiles
145-
auto layout = clusterer.GetPartitioning(tile_count);
146-
147-
LOG_TRACE("%s", GetColumnMapInfo(layout).c_str());
111+
auto column_map = clusterer.GetPartitioning(tile_count);
148112

149113
// Update table layout
150-
table->SetDefaultLayout(layout);
114+
// Since we need the layout to be updated in the catalog,
115+
// Start a transaction before updating the default layout.
116+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
117+
auto *txn = txn_manager.BeginTransaction();
118+
bool result = table->SetDefaultLayout(column_map, pool_.get(), txn);
119+
if (!result) {
120+
txn_manager.AbortTransaction(txn);
121+
LOG_DEBUG("Layout Update to failed.");
122+
return;
123+
}
124+
txn_manager.CommitTransaction(txn);
125+
126+
UNUSED_ATTRIBUTE auto layout = table->GetDefaultLayout();
127+
LOG_TRACE("Updated Layout: %s", layout.GetInfo().c_str());
151128
}
152129

153130
void LayoutTuner::Tune() {

test/executor/testing_executor_util.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,29 @@ storage::DataTable *TestingExecutorUtil::CreateTable(
419419
return table;
420420
}
421421

422+
storage::DataTable *TestingExecutorUtil::CreateTableUpdateCatalog(
423+
int tuples_per_tilegroup_count, std::string &db_name) {
424+
auto table_schema = std::unique_ptr<catalog::Schema>(new catalog::Schema(
425+
{GetColumnInfo(0), GetColumnInfo(1), GetColumnInfo(2), GetColumnInfo(3)}));
426+
std::string table_name("test_table");
427+
428+
bool is_catalog = false;
429+
auto *catalog = catalog::Catalog::GetInstance();
430+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
431+
auto txn = txn_manager.BeginTransaction();
432+
// Insert table in catalog
433+
catalog->CreateTable(db_name, table_name, std::move(table_schema),
434+
txn, is_catalog, tuples_per_tilegroup_count);
435+
txn_manager.EndTransaction(txn);
436+
437+
txn = txn_manager.BeginTransaction();
438+
auto test_db = catalog->GetDatabaseWithName(db_name, txn);
439+
txn_manager.EndTransaction(txn);
440+
441+
auto table = test_db->GetTableWithName(table_name);
442+
return table;
443+
}
444+
422445
/**
423446
* @brief Convenience method to create table for test.
424447
*

test/include/executor/testing_executor_util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class TestingExecutorUtil {
7575
int tuples_per_tilegroup_count = TESTS_TUPLES_PER_TILEGROUP,
7676
bool indexes = true, oid_t table_oid = INVALID_OID);
7777

78+
/** @brief Creates a basic table and adds its entry to the catalog */
79+
static storage::DataTable *CreateTableUpdateCatalog(
80+
int tuples_per_tilegroup_count, std::string &db_name);
81+
7882
/** @brief Creates a basic table with allocated and populated tuples */
7983
static storage::DataTable *CreateAndPopulateTable();
8084

test/tuning/layout_tuner_test.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Identification: test/tuning/layout_tuner_test.cpp
88
//
9-
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-16, Carnegie Mellon University Databa e Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -38,14 +38,17 @@ TEST_F(LayoutTunerTests, BasicTest) {
3838

3939
const int tuple_count = TESTS_TUPLES_PER_TILEGROUP;
4040

41+
std::string db_name = "test_db";
42+
TestingExecutorUtil::InitializeDatabase(db_name);
43+
44+
auto data_table = TestingExecutorUtil::CreateTableUpdateCatalog(tuple_count, db_name);
45+
46+
4147
// Create a table and populate it
4248
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
4349
auto txn = txn_manager.BeginTransaction();
44-
std::unique_ptr<storage::DataTable> data_table(
45-
TestingExecutorUtil::CreateTable(tuple_count, false));
46-
TestingExecutorUtil::PopulateTable(data_table.get(), tuple_count, false,
47-
false,
48-
true, txn);
50+
TestingExecutorUtil::PopulateTable(data_table, tuple_count, false,
51+
false, true, txn);
4952
txn_manager.CommitTransaction(txn);
5053

5154
// Check column count
@@ -56,7 +59,7 @@ TEST_F(LayoutTunerTests, BasicTest) {
5659
tuning::LayoutTuner &layout_tuner = tuning::LayoutTuner::GetInstance();
5760

5861
// Attach table to index tuner
59-
layout_tuner.AddTable(data_table.get());
62+
layout_tuner.AddTable(data_table);
6063

6164
// Check old default tile group layout
6265
auto old_default_layout = data_table->GetDefaultLayout();
@@ -121,6 +124,8 @@ TEST_F(LayoutTunerTests, BasicTest) {
121124
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(1),0);
122125
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(2),0);
123126
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(3),1);
127+
128+
TestingExecutorUtil::DeleteDatabase(db_name);
124129
}
125130

126131
} // namespace test

0 commit comments

Comments
 (0)