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

Commit 94f98c8

Browse files
authored
Merge branch 'master' into ubuntu-cmake
2 parents 0fc7c20 + 37835d6 commit 94f98c8

File tree

17 files changed

+444
-22
lines changed

17 files changed

+444
-22
lines changed

cmake/External/capnproto.cmake

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ if (NOT __CAPNP_INCLUDED) # guard against multiple includes
4949
set(CAPNPC_CXX_EXECUTABLE ${capnp_INSTALL}/bin/capnpc-c++)
5050
set(CAPNP_INCLUDE_DIRS ${capnp_INSTALL}/include)
5151
set(CAPNP_INCLUDE_DIRECTORY ${capnp_INSTALL}/include)
52-
set(CAPNP_LIBRARIES
53-
${capnp_INSTALL}/lib/libcapnp-rpc.a
54-
${capnp_INSTALL}/lib/libcapnp.a
55-
${capnp_INSTALL}/lib/libkj-async.a
56-
${capnp_INSTALL}/lib/libkj.a
57-
${CMAKE_THREAD_LIBS_INIT}
58-
)
59-
set(CAPNP_LIBRARY_DIRS ${capnp_INSTALL}/lib)
6052
set(CAPNP_EXTERNAL TRUE)
6153

6254
list(APPEND external_project_dependencies capnp)

src/brain/query_logger.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// query_logger.cpp
6+
//
7+
// Identification: src/brain/query_logger.cpp
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "brain/query_logger.h"
14+
#include "catalog/query_history_catalog.h"
15+
#include "concurrency/transaction_context.h"
16+
#include "concurrency/transaction_manager_factory.h"
17+
#include "parser/pg_query.h"
18+
19+
namespace peloton {
20+
namespace brain {
21+
22+
void QueryLogger::LogQuery(std::string query_string, uint64_t timestamp) {
23+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
24+
auto txn = txn_manager.BeginTransaction();
25+
std::string fingerprint = pg_query_fingerprint(query_string.c_str()).hexdigest;
26+
27+
catalog::QueryHistoryCatalog::GetInstance()->InsertQueryHistory(
28+
query_string, fingerprint, timestamp, nullptr, txn);
29+
30+
txn_manager.CommitTransaction(txn);
31+
}
32+
33+
} // namespace brain
34+
} // namespace peloton

src/catalog/catalog.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "catalog/index_metrics_catalog.h"
2020
#include "catalog/language_catalog.h"
2121
#include "catalog/proc_catalog.h"
22+
#include "catalog/query_history_catalog.h"
2223
#include "catalog/query_metrics_catalog.h"
2324
#include "catalog/settings_catalog.h"
2425
#include "catalog/table_catalog.h"
@@ -30,6 +31,7 @@
3031
#include "function/old_engine_string_functions.h"
3132
#include "function/timestamp_functions.h"
3233
#include "index/index_factory.h"
34+
#include "settings/settings_manager.h"
3335
#include "storage/storage_manager.h"
3436
#include "storage/table_factory.h"
3537
#include "type/ephemeral_pool.h"
@@ -146,11 +148,15 @@ void Catalog::Bootstrap() {
146148
DatabaseMetricsCatalog::GetInstance(txn);
147149
TableMetricsCatalog::GetInstance(txn);
148150
IndexMetricsCatalog::GetInstance(txn);
149-
QueryMetricsCatalog::GetInstance(txn);
151+
QueryMetricsCatalog::GetInstance(txn);
150152
SettingsCatalog::GetInstance(txn);
151153
TriggerCatalog::GetInstance(txn);
152154
LanguageCatalog::GetInstance(txn);
153155
ProcCatalog::GetInstance(txn);
156+
157+
if (settings::SettingsManager::GetBool(settings::SettingId::brain)) {
158+
QueryHistoryCatalog::GetInstance(txn);
159+
}
154160

155161
txn_manager.CommitTransaction(txn);
156162

src/catalog/query_history_catalog.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// query_history_catalog.cpp
6+
//
7+
// Identification: src/catalog/query_history_catalog.cpp
8+
//
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "catalog/query_history_catalog.h"
14+
15+
#include "catalog/catalog.h"
16+
#include "executor/logical_tile.h"
17+
#include "parser/pg_query.h"
18+
#include "storage/data_table.h"
19+
#include "type/value_factory.h"
20+
21+
namespace peloton {
22+
namespace catalog {
23+
24+
QueryHistoryCatalog *QueryHistoryCatalog::GetInstance(
25+
concurrency::TransactionContext *txn) {
26+
static QueryHistoryCatalog query_history_catalog{txn};
27+
return &query_history_catalog;
28+
}
29+
30+
QueryHistoryCatalog::QueryHistoryCatalog(concurrency::TransactionContext *txn)
31+
: AbstractCatalog("CREATE TABLE " CATALOG_DATABASE_NAME
32+
"." QUERY_HISTORY_CATALOG_NAME
33+
" ("
34+
"query_string VARCHAR NOT NULL, "
35+
"fingerprint VARCHAR NOT NULL, "
36+
"timestamp TIMESTAMP NOT NULL);",
37+
txn) {}
38+
39+
QueryHistoryCatalog::~QueryHistoryCatalog() {}
40+
41+
bool QueryHistoryCatalog::InsertQueryHistory(const std::string &query_string,
42+
std::string &fingerprint, uint64_t timestamp,
43+
type::AbstractPool *pool,
44+
concurrency::TransactionContext *txn) {
45+
std::unique_ptr<storage::Tuple> tuple(
46+
new storage::Tuple(catalog_table_->GetSchema(), true));
47+
48+
auto val0 = type::ValueFactory::GetVarcharValue(query_string, pool);
49+
auto val1 = type::ValueFactory::GetVarcharValue(fingerprint, pool);
50+
auto val2 = type::ValueFactory::GetTimestampValue(timestamp);
51+
52+
tuple->SetValue(ColumnId::QUERY_STRING, val0, pool);
53+
tuple->SetValue(ColumnId::FINGERPRINT, val1, pool);
54+
tuple->SetValue(ColumnId::TIMESTAMP, val2, pool);
55+
56+
// Insert the tuple
57+
return InsertTuple(std::move(tuple), txn);
58+
}
59+
60+
} // namespace catalog
61+
} // namespace peloton

src/common/init.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void PelotonInit::Initialize() {
4242
// start worker pool
4343
threadpool::MonoQueuePool::GetInstance().Startup();
4444

45+
// start brain thread pool
46+
if (settings::SettingsManager::GetBool(settings::SettingId::brain)) {
47+
threadpool::MonoQueuePool::GetBrainInstance().Startup();
48+
}
49+
4550
int parallelism = (std::thread::hardware_concurrency() + 3) / 4;
4651
storage::DataTable::SetActiveTileGroupCount(parallelism);
4752
storage::DataTable::SetActiveIndirectionArrayCount(parallelism);
@@ -107,6 +112,11 @@ void PelotonInit::Shutdown() {
107112
// stop worker pool
108113
threadpool::MonoQueuePool::GetInstance().Shutdown();
109114

115+
// stop brain thread pool
116+
if (settings::SettingsManager::GetBool(settings::SettingId::brain)) {
117+
threadpool::MonoQueuePool::GetBrainInstance().Shutdown();
118+
}
119+
110120
thread_pool.Shutdown();
111121

112122
// shutdown protocol buf library

src/concurrency/transaction_manager.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "catalog/manager.h"
1616
#include "concurrency/transaction_context.h"
17+
#include "function/date_functions.h"
1718
#include "gc/gc_manager_factory.h"
1819
#include "logging/log_manager.h"
1920
#include "settings/settings_manager.h"
@@ -72,6 +73,8 @@ TransactionContext *TransactionManager::BeginTransaction(
7273
.StartTimer();
7374
}
7475

76+
txn->SetTimestamp(function::DateFunctions::Now());
77+
7578
return txn;
7679
}
7780

src/gc/transaction_level_gc_manager.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212

1313
#include "gc/transaction_level_gc_manager.h"
1414

15+
#include "brain/query_logger.h"
1516
#include "catalog/manager.h"
1617
#include "common/container_tuple.h"
1718
#include "concurrency/epoch_manager_factory.h"
1819
#include "concurrency/transaction_manager_factory.h"
20+
#include "settings/settings_manager.h"
1921
#include "storage/database.h"
2022
#include "storage/tile_group.h"
2123
#include "storage/tuple.h"
2224
#include "storage/storage_manager.h"
25+
#include "threadpool/mono_queue_pool.h"
26+
2327

2428
namespace peloton {
2529
namespace gc {
@@ -127,6 +131,20 @@ int TransactionLevelGCManager::Unlink(const int &thread_id,
127131
break;
128132
}
129133

134+
// Log the query into query_history_catalog
135+
if (settings::SettingsManager::GetBool(settings::SettingId::brain)) {
136+
std::vector<std::string> query_strings = txn_ctx->GetQueryStrings();
137+
if(query_strings.size() != 0) {
138+
uint64_t timestamp = txn_ctx->GetTimestamp();
139+
auto &pool = threadpool::MonoQueuePool::GetBrainInstance();
140+
for(auto query_string: query_strings) {
141+
pool.SubmitTask([this, query_string, timestamp] {
142+
brain::QueryLogger::LogQuery(query_string, timestamp);
143+
});
144+
}
145+
}
146+
}
147+
130148
// Deallocate the Transaction Context of transactions that don't involve
131149
// any garbage collection
132150
if (txn_ctx->GetIsolationLevel() == IsolationLevelType::READ_ONLY || \

src/include/brain/query_logger.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// query_logger.h
6+
//
7+
// Identification: src/include/brain/query_logger.h
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#pragma once
14+
15+
#include <string>
16+
17+
namespace peloton {
18+
namespace brain {
19+
20+
//===--------------------------------------------------------------------===//
21+
// QueryLogger
22+
//===--------------------------------------------------------------------===//
23+
24+
class QueryLogger {
25+
public:
26+
QueryLogger() = default;
27+
28+
/*
29+
* @brief This function logs the query into query_history_catalog
30+
* @param the sql string corresponding to the query
31+
* @param timestamp of the transaction that executed the query
32+
*/
33+
static void LogQuery(std::string query_string, uint64_t timestamp);
34+
};
35+
36+
} // namespace brain
37+
} // namespace peloton
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// query_history_catalog.h
6+
//
7+
// Identification: src/include/catalog/query_history_catalog.h
8+
//
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
//===----------------------------------------------------------------------===//
14+
// pg_query
15+
//
16+
// Schema: (column offset: column_name)
17+
// 0: query_string
18+
// 1: fingerprint
19+
// 2: timestamp
20+
//
21+
//===----------------------------------------------------------------------===//
22+
23+
#pragma once
24+
25+
#include "catalog/abstract_catalog.h"
26+
27+
#define QUERY_HISTORY_CATALOG_NAME "pg_query_history"
28+
29+
namespace peloton {
30+
namespace catalog {
31+
32+
class QueryHistoryCatalog : public AbstractCatalog {
33+
public:
34+
~QueryHistoryCatalog();
35+
36+
// Global Singleton
37+
static QueryHistoryCatalog *GetInstance(
38+
concurrency::TransactionContext *txn = nullptr);
39+
40+
//===--------------------------------------------------------------------===//
41+
// write Related API
42+
//===--------------------------------------------------------------------===//
43+
bool InsertQueryHistory(const std::string &query_string,
44+
std::string &fingerprint, uint64_t timestamp,
45+
type::AbstractPool *pool,
46+
concurrency::TransactionContext *txn);
47+
48+
enum ColumnId {
49+
QUERY_STRING = 0,
50+
FINGERPRINT = 1,
51+
TIMESTAMP = 2,
52+
};
53+
54+
private:
55+
QueryHistoryCatalog(concurrency::TransactionContext *txn);
56+
57+
};
58+
59+
} // namespace catalog
60+
} // namespace peloton

src/include/concurrency/transaction_context.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,20 @@ class TransactionContext : public Printable {
7373

7474
inline eid_t GetEpochId() const { return epoch_id_; }
7575

76+
inline uint64_t GetTimestamp() const { return timestamp_; }
77+
78+
inline const std::vector<std::string>& GetQueryStrings() const {
79+
return query_strings_; }
80+
7681
inline void SetCommitId(const cid_t commit_id) { commit_id_ = commit_id; }
7782

7883
inline void SetEpochId(const eid_t epoch_id) { epoch_id_ = epoch_id; }
84+
85+
inline void SetTimestamp(const uint64_t timestamp) { timestamp_ = timestamp; }
86+
87+
inline void AddQueryString(const char* query_string) {
88+
query_strings_.push_back(std::string(query_string));
89+
}
7990

8091
void RecordCreate(oid_t database_oid, oid_t table_oid, oid_t index_oid) {
8192
rw_object_set_.emplace_back(database_oid, table_oid, index_oid,
@@ -174,6 +185,13 @@ class TransactionContext : public Printable {
174185
// GC manager uses this id to check whether a version is still visible.
175186
eid_t epoch_id_;
176187

188+
// vector of strings to log at the end of the transaction
189+
// populated only if the brain is running
190+
std::vector<std::string> query_strings_;
191+
192+
// timestamp when the transaction began
193+
uint64_t timestamp_;
194+
177195
ReadWriteSet rw_set_;
178196
CreateDropSet rw_object_set_;
179197

0 commit comments

Comments
 (0)