-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
165 lines (140 loc) · 7.61 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <chrono>
#include <thread>
#include "BuzzDB.h"
#include "QueryParser.h"
#include "QueryExecutor.h"
#include "ConcurrencyControl.h"
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <concurrency_control_mode>\n";
return 1;
}
std::string mode = argv[1];
ConcurrencyControl cc_mode;
if (mode == "2PL") {
cc_mode = ConcurrencyControl::MV2PL;
} else if (mode == "OCC") {
cc_mode = ConcurrencyControl::MVOCC;
} else {
throw std::invalid_argument("Invalid concurrency control mode");
}
// Start the transaction
auto start = std::chrono::high_resolution_clock::now();
{
/// Testing Concurrency
/// Test 1: Lost Updates
{
BuzzDB db(cc_mode);
// Insert two tuples
int64_t currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
auto t1 = std::make_unique<Transaction>(currentTime, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
db.insert(6, 93, t1);
db.insert(7, 104, t1);
t1->commit();
/// Create 2 threads and let them update the same tuple, resultant state should the sum of both operations.
std::vector<std::thread> threads;
for(int i = 0; i < 2; i++) {
threads.push_back(std::thread([&db, i](){
int64_t currentTime_new = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
auto t2 = std::make_unique<Transaction>(currentTime_new, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
db.updateTuples(7, i+1, t2);
std::this_thread::sleep_for(std::chrono::seconds(2));
int result = t2->commit();
while(result == -1) {
int64_t time_new = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
auto t2_new = std::make_unique<Transaction>(time_new, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
db.updateTuples(7, i+1, t2_new);
result = t2_new->commit();
}
}));
}
for(auto& thread : threads) {
thread.join();
}
db.printTuples();
}
{
// Test 2
// Lets update different tuples.
// BuzzDB db(cc_mode);
// int64_t currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// auto t1 = std::make_unique<Transaction>(currentTime, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
// db.insert(1, 93, t1);
// db.insert(2, 104, t1);
// db.insert(3, 113, t1);
// db.insert(4, 156, t1);
// db.insert(5, 178, t1);
// db.insert(6, 187, t1);
// t1->commit();
// /// Create 2 threads and let them update different set of tuples.
// std::vector<std::thread> threads;
// for(int i = 0; i < 2; i++) {
// threads.push_back(std::thread([&db, i](){
// int64_t currentTime_new = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// auto t2 = std::make_unique<Transaction>(currentTime_new, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
// db.updateTuples((i*3)+1, i+1, t2);
// db.updateTuples((i*3)+2, i+1, t2);
// db.updateTuples((i*3)+3, i+1, t2);
// std::this_thread::sleep_for(std::chrono::seconds(2));
// int result = t2->commit();
// while(result == -1) {
// int64_t time_new = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// auto t2_new = std::make_unique<Transaction>(time_new, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
// db.updateTuples((i*3)+1, i+1, t2);
// db.updateTuples((i*3)+2, i+1, t2);
// db.updateTuples((i*3)+3, i+1, t2);
// result = t2_new->commit();
// }
// }));
// }
// for(auto& thread : threads) {
// thread.join();
// }
// db.printTuples();
}
{
// BuzzDB db(cc_mode);
// /// Test 3: Update same and different tuples by different transactions
// int64_t currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// auto t1 = std::make_unique<Transaction>(currentTime, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
// db.insert(1, 93, t1);
// db.insert(2, 104, t1);
// db.insert(3, 113, t1);
// db.insert(4, 156, t1);
// db.insert(5, 178, t1);
// db.insert(6, 187, t1);
// t1->commit();
// /// Create 2 threads and let them update a mix of different and same set of tuples.
// std::vector<std::thread> threads;
// for(int i = 0; i < 2; i++) {
// threads.push_back(std::thread([&db, i](){
// int64_t currentTime_new = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// auto t2 = std::make_unique<Transaction>(currentTime_new, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
// db.updateTuples(2 + i, i + 1, t2);
// db.updateTuples(3 + i, i + 1, t2);
// db.updateTuples(4 + i, i + 1, t2);
// std::this_thread::sleep_for(std::chrono::seconds(2));
// int result = t2->commit();
// while(result == -1) {
// int64_t time_new = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// auto t2_new = std::make_unique<Transaction>(time_new, db.buffer_manager, db.version_manager, db.transaction_manager, db.lock_manager, db.cc_mode);
// db.updateTuples(2 + i, i + 1, t2);
// db.updateTuples(3 + i, i + 1, t2);
// db.updateTuples(4 + i, i + 1, t2);
// result = t2_new->commit();
// }
// }));
// }
// for(auto& thread : threads) {
// thread.join();
// }
// db.printTuples();
}
}
// Calculate and print the elapsed time
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::micro> elapsed = end - start;
std::cout << "Elapsed time: " << elapsed.count() << " microseconds" << std::endl;
return 0;
}