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
2426namespace peloton {
2527namespace tuning {
@@ -30,7 +32,7 @@ LayoutTuner& LayoutTuner::GetInstance() {
3032}
3133
3234LayoutTuner::LayoutTuner () {
33- // Nothing to do here !
35+ pool_. reset ( new type::EphemeralPool ());
3436}
3537
3638LayoutTuner::~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-
8450Sample 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
153130void LayoutTuner::Tune () {
0 commit comments