Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Commit a84af21

Browse files
authored
Pilot Preparation (#1309)
1 parent 7182657 commit a84af21

22 files changed

+966
-1
lines changed

src/include/common/error/exception.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace noisepage {
2929
#define EXECUTION_EXCEPTION(msg, code) ExecutionException(msg, __FILE__, __LINE__, (code))
3030
#define BINDER_EXCEPTION(msg, code) BinderException(msg, __FILE__, __LINE__, (code))
3131
#define SETTINGS_EXCEPTION(msg, code) SettingsException(msg, __FILE__, __LINE__, (code))
32+
#define PILOT_EXCEPTION(msg, code) PilotException(msg, __FILE__, __LINE__, (code))
3233

3334
/**
3435
* Exception types
@@ -40,6 +41,7 @@ enum class ExceptionType : uint8_t {
4041
CATALOG,
4142
CONVERSION,
4243
MESSENGER,
44+
PILOT,
4345
NETWORK,
4446
PARSER,
4547
SETTINGS,
@@ -98,6 +100,8 @@ class Exception : public std::runtime_error {
98100
return "Optimizer";
99101
case ExceptionType::EXECUTION:
100102
return "Execution";
103+
case ExceptionType::PILOT:
104+
return "Pilot";
101105
default:
102106
return "Unknown exception type";
103107
}
@@ -166,6 +170,7 @@ DEFINE_EXCEPTION(AbortException, ExceptionType::EXECUTION);
166170
DEFINE_EXCEPTION_WITH_ERRCODE(ExecutionException, ExceptionType::EXECUTION);
167171
DEFINE_EXCEPTION_WITH_ERRCODE(BinderException, ExceptionType::BINDER);
168172
DEFINE_EXCEPTION_WITH_ERRCODE(SettingsException, ExceptionType::SETTINGS);
173+
DEFINE_EXCEPTION_WITH_ERRCODE(PilotException, ExceptionType::PILOT);
169174

170175
/**
171176
* Specialized Parser exception since we want a cursor position to get more verbose output

src/include/execution/compiler/executable_query.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace noisepage {
1515
namespace selfdriving {
1616
class PipelineOperatingUnits;
17+
class PilotUtil;
1718
} // namespace selfdriving
1819

1920
namespace execution {
@@ -198,6 +199,7 @@ class ExecutableQuery {
198199
// MiniRunners needs to set query_identifier and pipeline_operating_units_.
199200
friend class noisepage::runner::MiniRunners;
200201
friend class noisepage::runner::MiniRunners_SEQ0_OutputRunners_Benchmark;
202+
friend class noisepage::selfdriving::PilotUtil;
201203
friend class noisepage::runner::MiniRunners_SEQ10_0_IndexInsertRunners_Benchmark;
202204
};
203205

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "loggers/loggers_util.h"
4+
5+
#ifdef NOISEPAGE_USE_LOGGING
6+
7+
namespace noisepage::selfdriving {
8+
extern common::SanctionedSharedPtr<spdlog::logger>::Ptr selfdriving_logger;
9+
10+
void InitSelfDrivingLogger();
11+
} // namespace noisepage::selfdriving
12+
13+
#define SELFDRIVING_LOG_TRACE(...) ::noisepage::selfdriving::selfdriving_logger->trace(__VA_ARGS__);
14+
#define SELFDRIVING_LOG_DEBUG(...) ::noisepage::selfdriving::selfdriving_logger->debug(__VA_ARGS__);
15+
#define SELFDRIVING_LOG_INFO(...) ::noisepage::selfdriving::selfdriving_logger->info(__VA_ARGS__);
16+
#define SELFDRIVING_LOG_WARN(...) ::noisepage::selfdriving::selfdriving_logger->warn(__VA_ARGS__);
17+
#define SELFDRIVING_LOG_ERROR(...) ::noisepage::selfdriving::selfdriving_logger->error(__VA_ARGS__);
18+
19+
#else
20+
21+
#define SELFDRIVING_LOG_TRACE(...) ((void)0)
22+
#define SELFDRIVING_LOG_DEBUG(...) ((void)0)
23+
#define SELFDRIVING_LOG_INFO(...) ((void)0)
24+
#define SELFDRIVING_LOG_WARN(...) ((void)0)
25+
#define SELFDRIVING_LOG_ERROR(...) ((void)0)
26+
27+
#endif

src/include/main/db_main.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "network/postgres/postgres_protocol_interpreter.h"
1818
#include "optimizer/statistics/stats_storage.h"
1919
#include "self_driving/model_server/model_server_manager.h"
20+
#include "self_driving/pilot/pilot.h"
21+
#include "self_driving/pilot/pilot_thread.h"
2022
#include "settings/settings_manager.h"
2123
#include "settings/settings_param.h"
2224
#include "storage/garbage_collector_thread.h"
@@ -432,6 +434,19 @@ class DBMain {
432434
std::make_unique<modelserver::ModelServerManager>(model_server_path_, messenger_layer->GetMessenger());
433435
}
434436

437+
std::unique_ptr<selfdriving::PilotThread> pilot_thread = DISABLED;
438+
std::unique_ptr<selfdriving::Pilot> pilot = DISABLED;
439+
if (use_pilot_thread_) {
440+
NOISEPAGE_ASSERT(model_server_enable_, "Pilot requires model server manager.");
441+
pilot = std::make_unique<selfdriving::Pilot>(
442+
model_save_path_, common::ManagedPointer(catalog_layer->GetCatalog()),
443+
common::ManagedPointer(metrics_thread), common::ManagedPointer(model_server_manager),
444+
common::ManagedPointer(settings_manager), common::ManagedPointer(stats_storage),
445+
common::ManagedPointer(txn_layer->GetTransactionManager()), workload_forecast_interval_);
446+
pilot_thread = std::make_unique<selfdriving::PilotThread>(
447+
common::ManagedPointer(pilot), std::chrono::microseconds{pilot_interval_}, pilot_planning_);
448+
}
449+
435450
db_main->settings_manager_ = std::move(settings_manager);
436451
db_main->metrics_manager_ = std::move(metrics_manager);
437452
db_main->metrics_thread_ = std::move(metrics_thread);
@@ -446,6 +461,8 @@ class DBMain {
446461
db_main->execution_layer_ = std::move(execution_layer);
447462
db_main->traffic_cop_ = std::move(traffic_cop);
448463
db_main->network_layer_ = std::move(network_layer);
464+
db_main->pilot_thread_ = std::move(pilot_thread);
465+
db_main->pilot_ = std::move(pilot);
449466
db_main->model_server_manager_ = std::move(model_server_manager);
450467
db_main->messenger_layer_ = std::move(messenger_layer);
451468

@@ -768,6 +785,11 @@ class DBMain {
768785
uint64_t wal_persist_threshold_ = static_cast<uint64_t>(1 << 20);
769786
bool use_logging_ = false;
770787
bool use_gc_ = false;
788+
bool use_pilot_thread_ = false;
789+
bool pilot_planning_ = false;
790+
uint64_t pilot_interval_ = 1e7;
791+
uint64_t workload_forecast_interval_ = 1e7;
792+
std::string model_save_path_;
771793
bool use_catalog_ = false;
772794
bool create_default_database_ = true;
773795
uint64_t block_store_size_ = 1e5;
@@ -823,8 +845,13 @@ class DBMain {
823845

824846
use_metrics_ = settings_manager->GetBool(settings::Param::metrics);
825847
use_metrics_thread_ = settings_manager->GetBool(settings::Param::use_metrics_thread);
848+
use_pilot_thread_ = settings_manager->GetBool(settings::Param::use_pilot_thread);
849+
pilot_planning_ = settings_manager->GetBool(settings::Param::pilot_planning);
826850

827851
gc_interval_ = settings_manager->GetInt(settings::Param::gc_interval);
852+
pilot_interval_ = settings_manager->GetInt64(settings::Param::pilot_interval);
853+
workload_forecast_interval_ = settings_manager->GetInt64(settings::Param::workload_forecast_interval);
854+
model_save_path_ = settings_manager->GetString(settings::Param::model_save_path);
828855

829856
uds_file_directory_ = settings_manager->GetString(settings::Param::uds_file_directory);
830857
// TODO(WAN): open an issue for handling settings.
@@ -939,6 +966,18 @@ class DBMain {
939966
return common::ManagedPointer(gc_thread_);
940967
}
941968

969+
/**
970+
* @return ManagedPointer to the component, can be nullptr if disabled
971+
*/
972+
common::ManagedPointer<selfdriving::Pilot> GetPilot() const { return common::ManagedPointer(pilot_); }
973+
974+
/**
975+
* @return ManagedPointer to the component, can be nullptr if disabled
976+
*/
977+
common::ManagedPointer<selfdriving::PilotThread> GetPilotThread() const {
978+
return common::ManagedPointer(pilot_thread_);
979+
}
980+
942981
/**
943982
* @return ManagedPointer to the component, can be nullptr if disabled
944983
*/
@@ -986,6 +1025,8 @@ class DBMain {
9861025
std::unique_ptr<ExecutionLayer> execution_layer_;
9871026
std::unique_ptr<trafficcop::TrafficCop> traffic_cop_;
9881027
std::unique_ptr<NetworkLayer> network_layer_;
1028+
std::unique_ptr<selfdriving::PilotThread> pilot_thread_;
1029+
std::unique_ptr<selfdriving::Pilot> pilot_;
9891030
std::unique_ptr<modelserver::ModelServerManager> model_server_manager_;
9901031
std::unique_ptr<MessengerLayer> messenger_layer_;
9911032
};

src/include/metrics/pipeline_metric.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include "self_driving/modeling/operating_unit_util.h"
1818
#include "transaction/transaction_defs.h"
1919

20+
namespace noisepage::selfdriving {
21+
class PilotUtil;
22+
}
2023
namespace noisepage::metrics {
2124

2225
/**
@@ -83,6 +86,7 @@ class PipelineMetricRawData : public AbstractRawData {
8386

8487
private:
8588
friend class PipelineMetric;
89+
friend class selfdriving::PilotUtil;
8690
FRIEND_TEST(MetricsTests, PipelineCSVTest);
8791
struct PipelineData;
8892

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <map>
4+
#include <memory>
5+
#include <string>
6+
#include <unordered_map>
7+
#include <utility>
8+
#include <vector>
9+
10+
#include "parser/expression/constant_value_expression.h"
11+
#include "self_driving/forecast/workload_forecast_segment.h"
12+
13+
namespace noisepage::selfdriving {
14+
15+
/**
16+
* Breaking predicted queries passed in by the Pilot into segments by their associated timestamps
17+
* Executing each query while extracting pipeline features
18+
*/
19+
class WorkloadForecast {
20+
public:
21+
/**
22+
* Constructor for WorkloadForecast
23+
* @param forecast_interval Interval used to partition the queries into segments
24+
*
25+
*/
26+
explicit WorkloadForecast(uint64_t forecast_interval);
27+
28+
private:
29+
friend class PilotUtil;
30+
31+
void LoadQueryTrace();
32+
void LoadQueryText();
33+
void CreateSegments();
34+
35+
std::multimap<uint64_t, execution::query_id_t> query_timestamp_to_id_;
36+
std::unordered_map<execution::query_id_t, std::vector<std::vector<parser::ConstantValueExpression>>>
37+
query_id_to_params_;
38+
std::unordered_map<execution::query_id_t, std::vector<type::TypeId>> query_id_to_param_types_;
39+
std::unordered_map<execution::query_id_t, std::string> query_id_to_text_;
40+
std::unordered_map<std::string, execution::query_id_t> query_text_to_id_;
41+
std::unordered_map<execution::query_id_t, uint64_t> query_id_to_dboid_;
42+
uint64_t num_sample_{5};
43+
44+
std::vector<WorkloadForecastSegment> forecast_segments_;
45+
uint64_t num_forecast_segment_{0};
46+
uint64_t forecast_interval_;
47+
uint64_t optimizer_timeout_{10000000};
48+
};
49+
50+
} // namespace noisepage::selfdriving
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <queue>
4+
#include <tuple>
5+
#include <unordered_map>
6+
#include <utility>
7+
8+
#include "execution/exec_defs.h"
9+
10+
namespace noisepage::selfdriving {
11+
/**
12+
* Contains query ids and number of executions for each query for queries predicted to be in this time interval
13+
*/
14+
class WorkloadForecastSegment {
15+
public:
16+
/**
17+
* Constructor for WorkloadForecastSegment
18+
* @param id_to_num_exec Map from qids to number of execution of this query in this interval
19+
*/
20+
explicit WorkloadForecastSegment(std::unordered_map<execution::query_id_t, uint64_t> id_to_num_exec);
21+
22+
private:
23+
std::unordered_map<execution::query_id_t, uint64_t> id_to_num_exec_;
24+
};
25+
26+
} // namespace noisepage::selfdriving

src/include/self_driving/modeling/operating_unit.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ class ExecutionOperatingUnitFeature {
133133
num_loops_(other.num_loops_),
134134
num_concurrent_(other.num_concurrent_) {}
135135

136+
/**
137+
* Returns a vector of doubles consisting of 7 features starting with num_rows
138+
*/
139+
std::vector<double> GetAllAttributes() const {
140+
std::vector<double> all_attributes;
141+
all_attributes.push_back(num_rows_);
142+
all_attributes.push_back(key_size_);
143+
all_attributes.push_back(num_keys_);
144+
all_attributes.push_back(cardinality_);
145+
all_attributes.push_back(GetMemFactor());
146+
all_attributes.push_back(num_loops_);
147+
all_attributes.push_back(num_concurrent_);
148+
return all_attributes;
149+
}
150+
136151
/** @return The ID of the translator for this ExecutionOperatingUnitFeature. */
137152
execution::translator_id_t GetTranslatorId() const { return translator_id_; }
138153

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#pragma once
2+
3+
#include <fstream>
4+
#include <iostream>
5+
#include <map>
6+
#include <memory>
7+
#include <string>
8+
#include <tuple>
9+
#include <unordered_map>
10+
#include <utility>
11+
#include <vector>
12+
13+
#include "catalog/catalog.h"
14+
#include "common/action_context.h"
15+
#include "common/macros.h"
16+
#include "common/managed_pointer.h"
17+
#include "execution/exec_defs.h"
18+
#include "self_driving/forecast/workload_forecast.h"
19+
20+
namespace noisepage {
21+
namespace messenger {
22+
class Messenger;
23+
}
24+
25+
namespace metrics {
26+
class MetricsThread;
27+
}
28+
29+
namespace modelserver {
30+
class ModelServerManager;
31+
}
32+
33+
namespace optimizer {
34+
class StatsStorage;
35+
}
36+
37+
namespace settings {
38+
class SettingsManager;
39+
}
40+
41+
namespace transaction {
42+
class TransactionManager;
43+
}
44+
45+
} // namespace noisepage
46+
47+
namespace noisepage::selfdriving {
48+
class PilotUtil;
49+
50+
/**
51+
* The pilot processes the query trace predictions by executing them and extracting pipeline features
52+
*/
53+
class Pilot {
54+
protected:
55+
/** @return Name of the environment variable to be set as the absolute path of build directory */
56+
static constexpr const char *BUILD_ABS_PATH = "BUILD_ABS_PATH";
57+
58+
public:
59+
/**
60+
* Constructor for Pilot
61+
* @param model_save_path model save path
62+
* @param catalog catalog
63+
* @param metrics_thread metrics thread for metrics manager
64+
* @param model_server_manager model server manager
65+
* @param settings_manager settings manager
66+
* @param stats_storage stats_storage
67+
* @param txn_manager transaction manager
68+
* @param workload_forecast_interval Interval used in the forecastor
69+
*/
70+
Pilot(std::string model_save_path, common::ManagedPointer<catalog::Catalog> catalog,
71+
common::ManagedPointer<metrics::MetricsThread> metrics_thread,
72+
common::ManagedPointer<modelserver::ModelServerManager> model_server_manager,
73+
common::ManagedPointer<settings::SettingsManager> settings_manager,
74+
common::ManagedPointer<optimizer::StatsStorage> stats_storage,
75+
common::ManagedPointer<transaction::TransactionManager> txn_manager, uint64_t workload_forecast_interval);
76+
77+
/**
78+
* Performs Pilot Logic, load and execute the predict queries while extracting pipeline features
79+
*/
80+
void PerformPlanning();
81+
82+
private:
83+
/**
84+
* WorkloadForecast object performing the query execution and feature gathering
85+
*/
86+
std::unique_ptr<selfdriving::WorkloadForecast> forecast_;
87+
88+
/**
89+
* Empty Setter Callback for setting bool value for flags
90+
*/
91+
static void EmptySetterCallback(common::ManagedPointer<common::ActionContext> action_context UNUSED_ATTRIBUTE) {}
92+
93+
void ExecuteForecast();
94+
95+
std::string model_save_path_;
96+
common::ManagedPointer<catalog::Catalog> catalog_;
97+
common::ManagedPointer<metrics::MetricsThread> metrics_thread_;
98+
common::ManagedPointer<modelserver::ModelServerManager> model_server_manager_;
99+
common::ManagedPointer<settings::SettingsManager> settings_manager_;
100+
common::ManagedPointer<optimizer::StatsStorage> stats_storage_;
101+
common::ManagedPointer<transaction::TransactionManager> txn_manager_;
102+
uint64_t workload_forecast_interval_{10000000};
103+
friend class noisepage::selfdriving::PilotUtil;
104+
};
105+
106+
} // namespace noisepage::selfdriving

0 commit comments

Comments
 (0)