-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathConflictSpamTest.cpp
More file actions
296 lines (244 loc) · 10 KB
/
ConflictSpamTest.cpp
File metadata and controls
296 lines (244 loc) · 10 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <libstuff/SData.h>
#include <test/clustertest/BedrockClusterTester.h>
struct ConflictSpamTest : tpunit::TestFixture {
ConflictSpamTest()
: tpunit::TestFixture("ConflictSpam",
BEFORE_CLASS(ConflictSpamTest::setup),
AFTER_CLASS(ConflictSpamTest::teardown),
TEST(ConflictSpamTest::slow),
TEST(ConflictSpamTest::spam)) { }
/* What's a conflict spam test? The main point of this test is to make sure we have lots of conflicting commits
* coming in to the whole cluster, so that we can make sure they all eventually get committed and replicated in a
* sane way. This is supposed to be a "worst case scenario" test where we can verify that the database isn't
* corrupted or anything else horrible happens even in less-than-ideal circumstances.
*/
BedrockClusterTester* tester;
atomic<int> cmdID;
void setup() {
cmdID.store(0);
tester = new BedrockClusterTester();
}
void teardown() {
delete tester;
}
void slow()
{
// Send some write commands to each node in the cluster.
for (int h = 0; h <= 4; h++) {
for (int i : {0, 1, 2}) {
BedrockTester& brtester = tester->getTester(i);
SData query("idcollision b");
// What if we throw in a few sync commands?
query["writeConsistency"] = "ASYNC";
int cmdNum = cmdID.fetch_add(1);
query["value"] = "sent-" + to_string(cmdNum);
// Ok, send.
brtester.executeWaitVerifyContent(query);
}
}
// Now see if they all match. If they don't, give them a few seconds to sync.
int tries = 0;
bool success = false;
while (tries < 10) {
vector<string> results(3);
for (int i : {0, 1, 2}) {
BedrockTester& brtester = tester->getTester(i);
SData query("Query");
query["writeConsistency"] = "ASYNC";
query["query"] = "SELECT id, value FROM test ORDER BY id;";
string result = brtester.executeWaitVerifyContent(query);
results[i] = result;
}
if (results[0] == results[1] && results[1] == results[2] && results[0].size()) {
success = true;
break;
}
sleep(1);
}
ASSERT_TRUE(success);
}
void spam()
{
recursive_mutex m;
atomic<int> totalRequestFailures(0);
// Let's spin up three threads, each spamming commands at one of our nodes.
list<thread> threads;
for (int i : {0, 1, 2}) {
threads.emplace_back([this, i, &totalRequestFailures](){
BedrockTester& brtester = tester->getTester(i);
// Let's make ourselves 20 commands to spam at each node.
vector<SData> requests;
int numCommands = 200;
for (int j = 0; j < numCommands; j++) {
SData query("idcollision b2");
query["writeConsistency"] = "ASYNC";
int cmdNum = cmdID.fetch_add(1);
query["value"] = "sent-" + to_string(cmdNum);
requests.push_back(query);
}
// Ok, send them all!
auto results = brtester.executeWaitMultipleData(requests);
int failures = 0;
for (auto row : results) {
if (SToInt(row.methodLine) != 200) {
cout << "[ConflictSpamTest] Node " << i << " Expected 200, got: " << SToInt(row.methodLine) << endl;
cout << "[ConflictSpamTest] " << row.content << endl;
failures++;
}
}
totalRequestFailures.fetch_add(failures);
});
}
// Done.
for (thread& t : threads) {
t.join();
}
threads.clear();
// Let's collect the names of the journal tables on each node.
vector <string> allResults(3);
for (int i : {0, 1, 2}) {
threads.emplace_back([this, i, &allResults, &m](){
BedrockTester& brtester = tester->getTester(i);
SData query("Query");
query["query"] = "SELECT name FROM sqlite_master WHERE type='table';";
// Ok, send them all!
auto result = brtester.executeWaitVerifyContent(query);
SAUTOLOCK(m);
allResults[i] = result;
});
}
// Done.
for (thread& t : threads) {
t.join();
}
threads.clear();
// Build a list of journal tables on each node.
vector<list<string>> tables(3);
int i = 0;
for (auto result : allResults) {
list<string> lines = SParseList(result, '\n');
list<string> output;
for (auto line : lines) {
if (SStartsWith(line, "journal")) {
output.push_back(line);
}
}
tables[i] = output;
i++;
}
// We'll let this go a couple of times. It's feasible that these won't match if the whole journal hasn't
// replicated yet.
int tries = 0;
while(tries++ < 60) {
// Now lets compose a query for the journal of each node.
allResults.clear();
allResults.resize(3);
for (int i : {0, 1, 2}) {
threads.emplace_back([this, i, &allResults, &tables, &m](){
BedrockTester& brtester = tester->getTester(i);
auto journals = tables[i];
list <string> queries;
for (auto journal : journals) {
queries.push_back("SELECT MAX(id) as maxIDs FROM " + journal);
}
string query = "SELECT MAX(maxIDs) FROM (" + SComposeList(queries, " UNION ");
query += ");";
SData cmd("Query");
cmd["query"] = query;
// Ok, send them all!
auto result = brtester.executeWaitVerifyContent(cmd);
SAUTOLOCK(m);
allResults[i] = result;
});
}
// Done.
for (thread& t : threads) {
t.join();
}
threads.clear();
if (allResults[0] == allResults[1] && allResults[1] == allResults[2]) {
break;
}
cout << "[ConflictSpamTest] Results didn't match, waiting for journals to equalize." << endl;
sleep(1);
}
// Verify the journals all match.
ASSERT_TRUE(allResults[0].size() > 0);
ASSERT_EQUAL(allResults[0], allResults[1]);
ASSERT_EQUAL(allResults[1], allResults[2]);
// Let's query the leader DB's journals, and see how many rows each had.
{
BedrockTester& brtester = tester->getTester(0);
auto journals = tables[0];
vector <SData> commands;
for (auto journal : journals) {
string query = "SELECT COUNT(id) FROM " + journal + ";";
SData cmd("Query");
cmd["query"] = query;
commands.push_back(cmd);
}
// Ok, send them all!
auto results = brtester.executeWaitMultipleData(commands);
for (size_t i = 0; i < results.size(); i++) {
// Make sure they all succeeded.
ASSERT_TRUE(SToInt(results[i].methodLine) == 200);
list<string> lines = SParseList(results[i].content, '\n');
lines.pop_front();
}
// We can't verify the size of the journal, because we can insert any number of 'upgrade database' rows as
// each node comes online as leader during startup.
// ASSERT_EQUAL(totalRows, 69);
}
// Spit out the actual table contents, for debugging.
allResults.clear();
allResults.resize(3);
for (int i : {0, 1, 2}) {
threads.emplace_back([this, i, &allResults, &m](){
BedrockTester& brtester = tester->getTester(i);
SData cmd("Query");
cmd["query"] = "SELECT * FROM test;";
// Ok, send them all!
auto result = brtester.executeWaitVerifyContent(cmd);
SAUTOLOCK(m);
allResults[i] = result;
});
}
// Done.
for (thread& t : threads) {
t.join();
}
threads.clear();
// Verify the actual table contains the right number of rows.
allResults.clear();
allResults.resize(3);
for (int i : {0, 1, 2}) {
threads.emplace_back([this, i, &allResults, &m](){
BedrockTester& brtester = tester->getTester(i);
SData cmd("Query");
cmd["query"] = "SELECT COUNT(id) FROM test;";
// Ok, send them all!
auto result = brtester.executeWaitVerifyContent(cmd);
SAUTOLOCK(m);
allResults[i] = result;
});
}
// Done.
for (thread& t : threads) {
t.join();
}
threads.clear();
// Verify these came out the same.
ASSERT_TRUE(allResults[0].size() > 0);
ASSERT_EQUAL(allResults[0], allResults[1]);
ASSERT_EQUAL(allResults[1], allResults[2]);
// And that they're all 66.
list<string> resultCount = SParseList(allResults[0], '\n');
resultCount.pop_front();
ASSERT_EQUAL(cmdID.load(), SToInt(resultCount.front()));
int fail = totalRequestFailures.load();
if (fail > 0) {
cout << "[ConflictSpamTest] Total failures: " << fail << endl;
}
ASSERT_EQUAL(fail, 0);
}
} __ConflictSpamTest;