/
worker_thread.cpp
266 lines (219 loc) · 7.99 KB
/
worker_thread.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
This file is part of barrageDB
Copyright (C) 2015 Spyridon Papageorgiou
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <iostream>
#include <random>
#include <algorithm>
#include <iterator>
#include <cmath>
#include "worker_thread.hpp"
#include "db_ops.hpp"
#include "crypto/sha256.hpp"
WorkerThread::WorkerThread(int thread_id, WorkerType work_type)
{
std::cout << "Worker thread constructor | tid: " << thread_id << std::endl;
tid = thread_id;
type = work_type;
}
void WorkerThread::start()
{
this_thread = boost::thread(&WorkerThread::do_work, this);
}
void WorkerThread::join()
{
this_thread.join();
}
void WorkerThread::set_connection_string(std::string str)
{
connection_string = str;
}
int WorkerThread::get_tid(void)
{
return tid;
}
int WorkerThread::do_work(void)
{
// At this point we should randomly select among a specific
// set of SQL queries
// 1) Insert a new entry to the database
// 2) Read (and validate) an entry
// 3) Modify an existing entry (need locking or can we use transactions?)
// 4) Delete an existing entry (same as above)
//Could implement this by using a query_generator class, storing 4 function
//pointers and rand()
switch(type) {
case VALIDATOR:
{
//Read sequentialy
int ret = get_row_count_slow(connection_string);
if (ret == CONNECTION_ERROR) {
std::cout << "Unable to connect to database!" << std::endl;
break;
}
int db_rows = ret;
std::cout << ">Table rows: " << db_rows << std::endl;
std::cout << ">Validating.." << std::endl;
int validation_errors = 0;
for (int i = 1; i < db_rows; ++i){
if(!read_query_sequential(i)){
++validation_errors;
}
}
std::cout << "============================" << std::endl;
std::cout << ">Validated " << db_rows << " tuples." << std::endl;
std::cout << ">>Total # of errors: " << validation_errors << std::endl;
}
break;
case GENERATOR:
{
std::random_device rd;
std::default_random_engine rng(rd());
std::uniform_int_distribution<int> dist(0, QUERY_MAX - 1);
while(1) {
try {
int op = dist(rng);
switch(op) {
case INSERT_QUERY:
insert_query();
break;
case READ_QUERY:
read_query_random();
break;
default:
break;
}
boost::this_thread::interruption_point();
}
catch (boost::thread_interrupted const&)
{
break;
}
}
}
break;
case POPULATOR:
{
for (int i = 0; i < 100; ++i) {
insert_query();
}
break;
}
default:
break;
}
}
uint64_t __64bit_generator()
{
std::random_device rd;
std::mt19937_64 e2(rd());
std::uniform_int_distribution<long long int> dist(std::llround(std::pow(2,61)), std::llround(std::pow(2,62)));
return dist(e2);
}
char __string_generator() {
static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
std::random_device rd;
std::default_random_engine rng(rd());
std::uniform_int_distribution<> dist(0, sizeof(alphabet)/sizeof(*alphabet)-2);
return alphabet[dist(rng)];
}
std::string __generate_rand_string(int length)
{
std::string rand_string;
std::generate_n(std::back_inserter(rand_string), length, __string_generator);
return rand_string;
}
int WorkerThread::insert_query(void)
{
struct row_data data;
data.num = __64bit_generator();
data.string_a = __generate_rand_string(256);
data.string_b = __generate_rand_string(256);
data.sha_digest = sha256(data.string_a + data.string_b + std::to_string(data.num));
// uint64_t num = __64bit_generator();
// std::string str1 = __generate_rand_string(256);
// std::string str2 = __generate_rand_string(256);
// std::string sha_digest = sha256(str1+str2+std::to_string(num));
//insert_entry(connection_string, num, str1, str2, sha_digest);
insert_entry(connection_string, data);
return 0;
}
int WorkerThread::read_query_random(void)
{
struct row_data data;
read_entry_random(connection_string, &data);
std::string validation_sha = sha256(data.string_a + data.string_b + std::to_string(data.num));
if (validation_sha.compare(data.sha_digest)) {
std::cout << "VALIDATION ERROR: sha256 checksum mismatch (expected: '" << validation_sha << "', found: '" << data.sha_digest << "'" << std::endl;
}
return 0;
}
int WorkerThread::read_query_sequential(int row_number)
{
struct row_data data;
read_entry_sequential(connection_string, row_number, &data);
std::string validation_sha = sha256(data.string_a + data.string_b + std::to_string(data.num));
if (validation_sha.compare(data.sha_digest)) {
std::cout << "VALIDATION ERROR @ row " << row_number <<" : sha256 checksum mismatch (expected: '" << validation_sha << "', found: '" << data.sha_digest << "'" << std::endl;
return 0;
}
return 1;
}
boost::thread *WorkerThread::get_thread_descriptor(void)
{
return &this_thread;
}
/*********************** WorkGroup **************************/
WorkGroup::WorkGroup(std::string db_string, int number_of_threads, int duration, WorkerType work_type)
{
run_time = duration;
thread_count = number_of_threads;
type = work_type;
for (int i = 0; i < number_of_threads; ++i) {
threads.push_back(new WorkerThread(i, work_type));
threads[i]->set_connection_string(db_string);
}
}
void WorkGroup::start(void)
{
for (int i = 0; i < thread_count; ++i) {
threads[i]->start();
}
}
void WorkGroup::wait_all(void)
{
switch(type) {
case GENERATOR:
sleep(run_time);
// Raise interrupts
for (int i = 0; i < thread_count; ++i) {
(threads[i]->get_thread_descriptor())->interrupt();
}
// Join all
for (int i = 0; i < thread_count; ++i) {
threads[i]->join();
}
break;
case VALIDATOR:
case POPULATOR:
for (int i = 0; i < thread_count; ++i) {
threads[i]->join();
}
break;
default:
break;
}
}