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

Commit 229fb4a

Browse files
committed
Merge branch 'master' of https://github.com/cmu-db/peloton
2 parents fab78f5 + 26b3215 commit 229fb4a

17 files changed

+801
-246
lines changed

src/common/container/cuckoo_map.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
1413
#include <functional>
1514
#include <iostream>
1615

1716
#include "common/container/cuckoo_map.h"
17+
#include "common/internal_types.h"
1818
#include "common/logger.h"
1919
#include "common/macros.h"
20-
#include "common/internal_types.h"
2120

2221
namespace peloton {
2322

@@ -28,32 +27,31 @@ class TileGroup;
2827
namespace stats {
2928
class BackendStatsContext;
3029
class IndexMetric;
31-
}
30+
} // namespace stats
31+
32+
class StatementCache;
3233

3334
CUCKOO_MAP_TEMPLATE_ARGUMENTS
34-
CUCKOO_MAP_TYPE::CuckooMap(){
35-
}
35+
CUCKOO_MAP_TYPE::CuckooMap() {}
3636

3737
CUCKOO_MAP_TEMPLATE_ARGUMENTS
38-
CUCKOO_MAP_TYPE::~CuckooMap(){
39-
}
38+
CUCKOO_MAP_TYPE::~CuckooMap() {}
4039

4140
CUCKOO_MAP_TEMPLATE_ARGUMENTS
42-
bool CUCKOO_MAP_TYPE::Insert(const KeyType &key, ValueType value){
41+
bool CUCKOO_MAP_TYPE::Insert(const KeyType &key, ValueType value) {
4342
auto status = cuckoo_map.insert(key, value);
4443
LOG_TRACE("insert status : %d", status);
4544
return status;
4645
}
4746

4847
CUCKOO_MAP_TEMPLATE_ARGUMENTS
49-
bool CUCKOO_MAP_TYPE::Update(const KeyType &key, ValueType value){
50-
auto status = cuckoo_map.update(key, value);
48+
bool CUCKOO_MAP_TYPE::Update(const KeyType &key, ValueType value) {
49+
auto status = cuckoo_map.update(key, value);
5150
return status;
5251
}
5352

5453
CUCKOO_MAP_TEMPLATE_ARGUMENTS
55-
bool CUCKOO_MAP_TYPE::Erase(const KeyType &key){
56-
54+
bool CUCKOO_MAP_TYPE::Erase(const KeyType &key) {
5755
auto status = cuckoo_map.erase(key);
5856
LOG_TRACE("erase status : %d", status);
5957

@@ -62,42 +60,46 @@ bool CUCKOO_MAP_TYPE::Erase(const KeyType &key){
6260

6361
CUCKOO_MAP_TEMPLATE_ARGUMENTS
6462
bool CUCKOO_MAP_TYPE::Find(const KeyType &key, ValueType &value) const {
65-
6663
auto status = cuckoo_map.find(key, value);
6764
LOG_TRACE("find status : %d", status);
6865
return status;
6966
}
7067

7168
CUCKOO_MAP_TEMPLATE_ARGUMENTS
72-
bool CUCKOO_MAP_TYPE::Contains(const KeyType &key){
69+
bool CUCKOO_MAP_TYPE::Contains(const KeyType &key) {
7370
return cuckoo_map.contains(key);
7471
}
7572

7673
CUCKOO_MAP_TEMPLATE_ARGUMENTS
77-
void CUCKOO_MAP_TYPE::Clear(){
78-
cuckoo_map.clear();
79-
}
74+
void CUCKOO_MAP_TYPE::Clear() { cuckoo_map.clear(); }
8075

8176
CUCKOO_MAP_TEMPLATE_ARGUMENTS
82-
size_t CUCKOO_MAP_TYPE::GetSize() const{
83-
return cuckoo_map.size();
84-
}
77+
size_t CUCKOO_MAP_TYPE::GetSize() const { return cuckoo_map.size(); }
8578

8679
CUCKOO_MAP_TEMPLATE_ARGUMENTS
87-
bool CUCKOO_MAP_TYPE::IsEmpty() const{
88-
return cuckoo_map.empty();
89-
}
80+
bool CUCKOO_MAP_TYPE::IsEmpty() const { return cuckoo_map.empty(); }
81+
82+
CUCKOO_MAP_TEMPLATE_ARGUMENTS
83+
CUCKOO_MAP_ITERATOR_TYPE
84+
CUCKOO_MAP_TYPE::GetIterator() { return cuckoo_map.lock_table(); }
9085

9186
// Explicit template instantiation
9287
template class CuckooMap<uint32_t, uint32_t>;
9388

9489
template class CuckooMap<oid_t, std::shared_ptr<storage::TileGroup>>;
9590

91+
// Used in Shared Pointer test and iterator test
9692
template class CuckooMap<oid_t, std::shared_ptr<oid_t>>;
9793

9894
template class CuckooMap<std::thread::id,
9995
std::shared_ptr<stats::BackendStatsContext>>;
10096

10197
template class CuckooMap<oid_t, std::shared_ptr<stats::IndexMetric>>;
10298

99+
// Used in SharedPointerKeyTest
100+
template class CuckooMap<std::shared_ptr<oid_t>, std::shared_ptr<oid_t>>;
101+
102+
// Used in StatementCacheManager
103+
template class CuckooMap<StatementCache *, StatementCache *>;
104+
103105
} // namespace peloton

src/common/init.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "brain/layout_tuner.h"
2020
#include "catalog/catalog.h"
2121
#include "common/thread_pool.h"
22+
#include "common/statement_cache_manager.h"
2223
#include "concurrency/transaction_manager_factory.h"
2324
#include "gc/gc_manager_factory.h"
2425
#include "settings/settings_manager.h"
@@ -79,6 +80,9 @@ void PelotonInit::Initialize() {
7980
pg_catalog->CreateDatabase(DEFAULT_DB_NAME, txn);
8081

8182
txn_manager.CommitTransaction(txn);
83+
84+
// Initialize the Statement Cache Manager
85+
StatementCacheManager::Init();
8286
}
8387

8488
void PelotonInit::Shutdown() {

src/common/statement_cache.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// statement_cache.cpp
6+
//
7+
// Identification: src/include/common/statement_cache.cpp
8+
//
9+
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <unordered_set>
14+
15+
#include "common/statement_cache.h"
16+
17+
namespace peloton {
18+
// Add a statement to the cache
19+
void StatementCache::AddStatement(std::shared_ptr<Statement> stmt) {
20+
UpdateFromInvalidTableQueue();
21+
statement_map_[stmt->GetStatementName()] = stmt;
22+
for (auto table_id : stmt->GetReferencedTables()) {
23+
table_ref_[table_id].insert(stmt);
24+
}
25+
}
26+
27+
// Get the statement by its name;
28+
std::shared_ptr<Statement> StatementCache::GetStatement(std::string name) {
29+
UpdateFromInvalidTableQueue();
30+
return statement_map_[name];
31+
}
32+
33+
// Delete the statement
34+
void StatementCache::DeleteStatement(std::string name) {
35+
if (!statement_map_.count(name)) {
36+
return;
37+
}
38+
auto to_delete = statement_map_[name];
39+
for (auto table_id : to_delete->GetReferencedTables()) {
40+
table_ref_[table_id].erase(to_delete);
41+
}
42+
statement_map_.erase(name);
43+
}
44+
45+
// Notify the Statement Cache a table id that is invalidated
46+
void StatementCache::NotifyInvalidTable(oid_t table_id) {
47+
invalid_table_queue_.Enqueue(table_id);
48+
}
49+
50+
void StatementCache::UpdateFromInvalidTableQueue() {
51+
std::unordered_set<oid_t> invalid_set;
52+
oid_t invalid_oid;
53+
while (invalid_table_queue_.Dequeue(invalid_oid)) {
54+
if (table_ref_.count(invalid_oid)) invalid_set.insert(invalid_oid);
55+
}
56+
57+
if (invalid_set.size()) {
58+
for (oid_t id : invalid_set)
59+
for (auto &statement : table_ref_[id]) statement->SetNeedsReplan(true);
60+
}
61+
}
62+
63+
void StatementCache::Clear() {
64+
statement_map_.clear();
65+
table_ref_.clear();
66+
while (!invalid_table_queue_.IsEmpty()) {
67+
oid_t dummy;
68+
invalid_table_queue_.Dequeue(dummy);
69+
}
70+
}
71+
72+
} // namespace peloton
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// statement_cache_manager.cpp
6+
//
7+
// Identification: src/include/common/statement_cache_manager.cpp
8+
//
9+
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "common/statement_cache_manager.h"
14+
15+
namespace peloton {
16+
17+
void StatementCacheManager::RegisterStatementCache(StatementCache *stmt_cache) {
18+
statement_caches_.Insert(stmt_cache, stmt_cache);
19+
}
20+
21+
void StatementCacheManager::UnRegisterStatementCache(StatementCache *stmt_cache) {
22+
statement_caches_.Erase(stmt_cache);
23+
}
24+
25+
void StatementCacheManager::InvalidateTableOid(oid_t table_id) {
26+
if (statement_caches_.IsEmpty())
27+
return;
28+
29+
// Lock the table by grabbing the iterator
30+
LOG_TRACE("Locking the table to get the iterator");
31+
auto iterator = statement_caches_.GetIterator();
32+
33+
// Iterate each plan cache
34+
for (auto &iter : iterator) {
35+
iter.first->NotifyInvalidTable(table_id);
36+
}
37+
// Automatically release the table;
38+
}
39+
40+
void StatementCacheManager::InvalidateTableOids(std::set<oid_t> &table_ids) {
41+
if (table_ids.empty() || statement_caches_.IsEmpty())
42+
return;
43+
44+
// Lock the table by grabbing the iterator
45+
LOG_TRACE("Locking the table to get the iterator");
46+
auto iterator = statement_caches_.GetIterator();
47+
48+
// Iterate each plan cache and notify every table id
49+
for (auto &iter : iterator) {
50+
for (auto &table_id : table_ids)
51+
iter.first->NotifyInvalidTable(table_id);
52+
}
53+
// Automatically release the table;
54+
}
55+
}
56+

src/executor/drop_executor.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212

1313
#include "executor/drop_executor.h"
1414

15-
#include "catalog/index_catalog.h"
1615
#include "catalog/catalog.h"
16+
#include "catalog/database_catalog.h"
17+
#include "catalog/index_catalog.h"
18+
#include "catalog/table_catalog.h"
1719
#include "catalog/trigger_catalog.h"
1820
#include "common/logger.h"
21+
#include "common/statement_cache_manager.h"
1922
#include "executor/executor_context.h"
2023

2124
namespace peloton {
@@ -72,6 +75,7 @@ bool DropExecutor::DExecute() {
7275
bool DropExecutor::DropDatabase(const planner::DropPlan &node,
7376
concurrency::TransactionContext *txn) {
7477
auto database_name = node.GetDatabaseName();
78+
7579
if (node.IsMissing()) {
7680
try {
7781
auto database_object = catalog::Catalog::GetInstance()->GetDatabaseObject(
@@ -82,12 +86,25 @@ bool DropExecutor::DropDatabase(const planner::DropPlan &node,
8286
}
8387
}
8488

89+
auto database_object =
90+
catalog::Catalog::GetInstance()->GetDatabaseObject(database_name, txn);
91+
8592
ResultType result =
8693
catalog::Catalog::GetInstance()->DropDatabaseWithName(database_name, txn);
8794
txn->SetResult(result);
8895

8996
if (txn->GetResult() == ResultType::SUCCESS) {
9097
LOG_TRACE("Dropping database succeeded!");
98+
99+
if (StatementCacheManager::GetStmtCacheManager().get()) {
100+
std::set<oid_t> table_ids;
101+
auto table_objects = database_object->GetTableObjects(false);
102+
for (auto it : table_objects) {
103+
table_ids.insert(it.second->GetTableOid());
104+
}
105+
StatementCacheManager::GetStmtCacheManager()->InvalidateTableOids(
106+
table_ids);
107+
}
91108
} else {
92109
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());
93110
}
@@ -115,6 +132,14 @@ bool DropExecutor::DropTable(const planner::DropPlan &node,
115132

116133
if (txn->GetResult() == ResultType::SUCCESS) {
117134
LOG_TRACE("Dropping table succeeded!");
135+
136+
if (StatementCacheManager::GetStmtCacheManager().get()) {
137+
oid_t table_id = catalog::Catalog::GetInstance()
138+
->GetTableObject(database_name, table_name, txn)
139+
->GetTableOid();
140+
StatementCacheManager::GetStmtCacheManager()->InvalidateTableOid(
141+
table_id);
142+
}
118143
} else {
119144
LOG_TRACE("Result is: %s", ResultTypeToString(txn->GetResult()).c_str());
120145
}
@@ -125,14 +150,23 @@ bool DropExecutor::DropTrigger(const planner::DropPlan &node,
125150
concurrency::TransactionContext *txn) {
126151
auto database_name = node.GetDatabaseName();
127152
std::string table_name = node.GetTableName();
153+
LOG_DEBUG("database name: %s", database_name.c_str());
154+
LOG_DEBUG("table name: %s", table_name.c_str());
128155
std::string trigger_name = node.GetTriggerName();
129156

130157
ResultType result = catalog::TriggerCatalog::GetInstance().DropTrigger(
131158
database_name, table_name, trigger_name, txn);
132159
txn->SetResult(result);
133-
134160
if (txn->GetResult() == ResultType::SUCCESS) {
135-
LOG_TRACE("Dropping trigger succeeded!");
161+
LOG_DEBUG("Dropping trigger succeeded!");
162+
163+
if (StatementCacheManager::GetStmtCacheManager().get()) {
164+
oid_t table_id = catalog::Catalog::GetInstance()
165+
->GetTableObject(database_name, table_name, txn)
166+
->GetTableOid();
167+
StatementCacheManager::GetStmtCacheManager()->InvalidateTableOid(
168+
table_id);
169+
}
136170
} else if (txn->GetResult() == ResultType::FAILURE && node.IsMissing()) {
137171
txn->SetResult(ResultType::SUCCESS);
138172
LOG_TRACE("Dropping trigger Succeeded!");
@@ -149,10 +183,17 @@ bool DropExecutor::DropIndex(const planner::DropPlan &node,
149183
std::string index_name = node.GetIndexName();
150184
auto index_object =
151185
catalog::IndexCatalog::GetInstance()->GetIndexObject(index_name, txn);
186+
152187
ResultType result = catalog::Catalog::GetInstance()->DropIndex(
153188
index_object->GetIndexOid(), txn);
154189
txn->SetResult(result);
190+
155191
if (txn->GetResult() == ResultType::SUCCESS) {
192+
if (StatementCacheManager::GetStmtCacheManager().get()) {
193+
oid_t table_id = index_object->GetTableOid();
194+
StatementCacheManager::GetStmtCacheManager()->InvalidateTableOid(
195+
table_id);
196+
}
156197
LOG_TRACE("Dropping Index Succeeded! Index oid: %d",
157198
index_object->GetIndexOid());
158199
} else {

src/include/common/container/cuckoo_map.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ namespace peloton {
2828
// CUCKOO_MAP_TYPE
2929
#define CUCKOO_MAP_TYPE CuckooMap<KeyType, ValueType>
3030

31+
// Iterator type
32+
#define CUCKOO_MAP_ITERATOR_TYPE \
33+
typename cuckoohash_map<KeyType, ValueType>::locked_table
34+
3135
CUCKOO_MAP_TEMPLATE_ARGUMENTS
3236
class CuckooMap {
3337
public:
@@ -59,6 +63,12 @@ class CuckooMap {
5963
// Checks if the cuckoo_map is empty
6064
bool IsEmpty() const;
6165

66+
// Lock the table and get iterator
67+
// The table would be unlock when the iterator
68+
// is out of scope
69+
CUCKOO_MAP_ITERATOR_TYPE
70+
GetIterator();
71+
6272
private:
6373

6474
// cuckoo map

0 commit comments

Comments
 (0)