forked from rescrv/HyperDex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplication-stress-test.cc
663 lines (561 loc) · 17.5 KB
/
replication-stress-test.cc
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// Copyright (c) 2011-2012, Cornell University
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of HyperDex nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// The purpose of this test is to expose errors in the replication logic.
//
// The test is designed to be run on a space with dimensions A, B, C.
// Dimension A is the key while dimensions B and C are meant to be separate
// subspaces. The code intentionally creates certain scenarios which will
// fail with high probability if the code is not correct.
//
// The prefix provided as a commandline argument is used to efficiently prune
// out duplicate results. It should match P in "auto P R" used for each
// subspace for basic tests. It is important to test this both on a single
// daemon instance, and on several daemon instances. The former stresses code
// with lightening quick responses while the latter introduces delay and
// non-determinism.
// C
#include <cstdio>
// Popt
#include <popt.h>
// STL
#include <iomanip>
#include <map>
#include <memory>
#include <string>
#include <tr1/functional>
// po6
#include <po6/net/ipaddr.h>
#include <po6/net/location.h>
// e
#include <e/bitfield.h>
#include <e/convert.h>
// HyperspaceHashing
#include "hyperspacehashing/hyperspacehashing/hashes.h"
#include "hyperspacehashing/hyperspacehashing/prefix.h"
// HyperDex
#include "hyperclient/hyperclient.h"
// These 32-bit values all hash (using hyperspacehashing) to be have the same
// high-order byte as their index. E.g. index 0 has a hash of
// 0x00??????????????, while index 255 has a hash of 0xff??????????????.
static uint32_t nums[256];
// These are all the incomplete operations
static std::map<int64_t, hyperclient_returncode*> incompleteops;
static const char* space = "replication";
static const char* host = "127.0.0.1";
static po6::net::ipaddr coord(host);
static long port = 1234;
static long prefix = 3;
static void
find_hashes();
static void
test0(hyperclient* cl);
static void
test1(hyperclient* cl);
static void
test2(hyperclient* cl);
static void
test3(hyperclient* cl);
static void
test4(hyperclient* cl);
class generator
{
public:
static uint32_t end()
{
bool looped = false;
uint32_t ret = 0;
for (generator i; i.has_next(); i.next())
{
looped = true;
ret = i;
}
assert(looped);
return ret;
}
public:
generator() : m_i(0) {}
public:
bool has_next() const { return m_i < 256; }
public:
void next()
{
uint32_t current = (m_i >> (8 - prefix));
while (m_i < 256 && current == (m_i >> (8 - prefix)))
{
++ m_i;
}
}
public:
operator uint32_t () const { return nums[m_i]; }
private:
uint32_t m_i;
};
extern "C"
{
static struct poptOption popts[] = {
POPT_AUTOHELP
{"prefix", 'P', POPT_ARG_LONG, &prefix, 'P',
"the size of the prefix used when creating the space",
"number"},
{"space", 's', POPT_ARG_STRING, &space, 's',
"the HyperDex space to use",
"space"},
{"host", 'h', POPT_ARG_STRING, &host, 'h',
"the IP address of the coordinator",
"IP"},
{"port", 'p', POPT_ARG_LONG, &port, 'p',
"the port number of the coordinator",
"port"},
POPT_TABLEEND
};
} // extern "C"
int
main(int argc, const char* argv[])
{
poptContext poptcon;
poptcon = poptGetContext(NULL, argc, argv, popts, POPT_CONTEXT_POSIXMEHARDER);
e::guard g = e::makeguard(poptFreeContext, poptcon);
g.use_variable();
int rc;
while ((rc = poptGetNextOpt(poptcon)) != -1)
{
switch (rc)
{
case 'P':
if (prefix < 0)
{
std::cerr << "prefix must be >= 0" << std::endl;
return EXIT_FAILURE;
}
if (prefix > 64)
{
std::cerr << "prefix must be <= 64" << std::endl;
return EXIT_FAILURE;
}
break;
case 's':
break;
case 'h':
try
{
coord = po6::net::ipaddr(host);
}
catch (po6::error& e)
{
std::cerr << "cannot parse coordinator address" << std::endl;
return EXIT_FAILURE;
}
catch (std::invalid_argument& e)
{
std::cerr << "cannot parse coordinator address" << std::endl;
return EXIT_FAILURE;
}
break;
case 'p':
if (port < 0 || port >= (1 << 16))
{
std::cerr << "port number out of range for TCP" << std::endl;
return EXIT_FAILURE;
}
break;
case POPT_ERROR_NOARG:
case POPT_ERROR_BADOPT:
case POPT_ERROR_BADNUMBER:
case POPT_ERROR_OVERFLOW:
std::cerr << poptStrerror(rc) << " " << poptBadOption(poptcon, 0) << std::endl;
return EXIT_FAILURE;
case POPT_ERROR_OPTSTOODEEP:
case POPT_ERROR_BADQUOTE:
case POPT_ERROR_ERRNO:
default:
std::cerr << "logic error in argument parsing" << std::endl;
return EXIT_FAILURE;
}
}
find_hashes();
try
{
hyperclient cl(host, port);
test0(&cl);
test1(&cl);
test2(&cl);
test3(&cl);
test4(&cl);
}
catch (po6::error& e)
{
std::cerr << "There was a system error: " << e.what();
return EXIT_FAILURE;
}
catch (std::runtime_error& e)
{
std::cerr << "There was a runtime error: " << e.what();
return EXIT_FAILURE;
}
catch (std::bad_alloc& e)
{
std::cerr << "There was a memory allocation error: " << e.what();
return EXIT_FAILURE;
}
catch (std::exception& e)
{
std::cerr << "There was a generic error: " << e.what();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static void
find_hashes()
{
bool found[256];
size_t complete = 0;
for (size_t i = 0; i < 256; ++i)
{
found[i] = false;
}
std::vector<hyperspacehashing::hash_t> hashes(1, hyperspacehashing::EQUALITY);
hyperspacehashing::prefix::hasher hasher(hashes);
for (uint32_t value = 0; complete < 256; ++value)
{
e::slice key(&value, sizeof(uint32_t));
uint64_t hash = hasher.hash(key).point;
unsigned high_byte = (hash >> 56) & 0xff;
if (!found[high_byte])
{
found[high_byte] = true;
nums[high_byte] = value;
++complete;
}
}
const char* terminator = "";
const char* sep;
std::cout << "Hashes turn out to be:\n";
for (size_t i = 0; i < 256; ++i)
{
if (i % 16 == 0)
{
std::cout << terminator << std::setw(4) << i;
sep = " [";
terminator = "]\n";
}
std::cout << sep << std::setw(4) << nums[i];
sep = ", ";
}
std::cout << "]\n" << std::endl;
}
static void
success(int testno)
{
std::cout << "Test " << testno << ": [\x1b[32mOK\x1b[0m]\n";
}
#define FAIL(TESTNO, REASON) \
do { \
std::cout << "Test " << TESTNO << ": [\x1b[31mFAIL\x1b[0m]\n" \
<< "location: " << __FILE__ << ":" << __LINE__ << "\n" \
<< "reason: " << REASON << "\n"; \
abort(); \
} while (0)
static void
put(int testno,
hyperclient* cl,
uint32_t A, uint32_t B, uint32_t C)
{
hyperclient_attribute attrs[2];
attrs[0].attr = "B";
attrs[0].value = reinterpret_cast<const char*>(&B);
attrs[0].value_sz = sizeof(uint32_t);
attrs[0].datatype = HYPERDATATYPE_STRING;
attrs[1].attr = "C";
attrs[1].value = reinterpret_cast<const char*>(&C);
attrs[1].value_sz = sizeof(uint32_t);
attrs[1].datatype = HYPERDATATYPE_STRING;
hyperclient_returncode* status = new hyperclient_returncode();
int64_t id = cl->put(space, reinterpret_cast<const char*>(&A), sizeof(uint32_t), attrs, 2, status);
if (id < 0)
{
FAIL(testno, "put encountered error " << status);
}
std::pair<std::map<int64_t, hyperclient_returncode*>::iterator, bool> res;
res = incompleteops.insert(std::make_pair(id, status));
if (res.second != true)
{
FAIL(testno, "put could not insert into incompleteops");
}
}
static void
del(int testno,
hyperclient* cl,
uint32_t A)
{
hyperclient_returncode* status = new hyperclient_returncode();
int64_t id = cl->del(space, reinterpret_cast<const char*>(&A), sizeof(uint32_t), status);
if (id < 0)
{
FAIL(testno, "del encountered error " << status);
}
std::pair<std::map<int64_t, hyperclient_returncode*>::iterator, bool> res;
res = incompleteops.insert(std::make_pair(id, status));
if (res.second != true)
{
FAIL(testno, "del could not insert into incompleteops");
}
}
static void
flush(int testno,
hyperclient* cl)
{
while (!incompleteops.empty())
{
hyperclient_returncode status;
int64_t id = cl->loop(10000, &status);
if (id < 0)
{
FAIL(testno, "loop returned error " << status);
}
else
{
std::map<int64_t, hyperclient_returncode*>::iterator incomplete;
incomplete = incompleteops.find(id);
if (incomplete == incompleteops.end())
{
FAIL(testno, "loop returned unknown id " << id);
}
if (*(incomplete->second) != HYPERCLIENT_SUCCESS)
{
FAIL(testno, "operation " << id << " returned " << *(incomplete->second));
}
delete incomplete->second;
incompleteops.erase(incomplete);
}
}
}
static void
present(int testno,
hyperclient* cl,
uint32_t A, uint32_t B, uint32_t C)
{
flush(testno, cl);
hyperclient_returncode gstatus;
hyperclient_returncode lstatus;
hyperclient_attribute* attrs;
size_t attrs_sz;
int64_t gid = cl->get(space, reinterpret_cast<char*>(&A), sizeof(uint32_t), &gstatus, &attrs, &attrs_sz);
if (gid < 0)
{
FAIL(testno, "get encountered error " << gstatus);
}
int64_t lid = cl->loop(10000, &lstatus);
if (lid < 0)
{
FAIL(testno, "loop encountered error " << lstatus);
}
if (gid != lid)
{
FAIL(testno, "loop id (" << lid << ") does not match get id (" << gid << ")");
}
if (gstatus != HYPERCLIENT_SUCCESS)
{
FAIL(testno, "operation " << gid << " (a presence check) returned " << gstatus);
}
if (attrs_sz != 2)
{
FAIL(testno, "presence check: " << attrs_sz << " attributes instead of 2 attributes");
}
if (strcmp(attrs[0].attr, "B") != 0)
{
FAIL(testno, "presence check: first attribute is \"" << attrs[0].attr << "\" instead of \"B\"");
}
if (attrs[0].datatype != HYPERDATATYPE_STRING)
{
FAIL(testno, "presence check: first attribute is not of datatype \"string\"");
}
if (e::slice(attrs[0].value, attrs[0].value_sz) != e::slice(&B, sizeof(uint32_t)))
{
FAIL(testno, "presence check: first attribute does not match " << B);
}
if (strcmp(attrs[1].attr, "C") != 0)
{
FAIL(testno, "presence check: second attribute is \"" << attrs[1].attr << "\" instead of \"C\"");
}
if (attrs[1].datatype != HYPERDATATYPE_STRING)
{
FAIL(testno, "presence check: second attribute is not of datatype \"string\"");
}
if (e::slice(attrs[1].value, attrs[1].value_sz) != e::slice(&C, sizeof(uint32_t)))
{
FAIL(testno, "presence check: second attribute does not match " << C);
}
free(attrs);
}
static void
absent(int testno,
hyperclient* cl,
uint32_t A)
{
flush(testno, cl);
hyperclient_returncode gstatus;
hyperclient_returncode lstatus;
hyperclient_attribute* attrs = NULL;
size_t attrs_sz = 0;
int64_t gid = cl->get(space, reinterpret_cast<char*>(&A), sizeof(uint32_t), &gstatus, &attrs, &attrs_sz);
if (gid < 0)
{
FAIL(testno, "get encountered error " << gstatus);
}
int64_t lid = cl->loop(10000, &lstatus);
if (lid < 0)
{
FAIL(testno, "loop returned error " << lstatus);
}
if (gid != lid)
{
FAIL(testno, "loop id (" << lid << ") does not match get id (" << gid << ")");
}
if (gstatus != HYPERCLIENT_NOTFOUND)
{
FAIL(testno, "operation " << gid << " (an absence check) returned " << gstatus);
}
assert(!attrs && !attrs_sz);
}
// This test continually puts keys, ensuring that every way in which one could
// select regions from the subspaces is chosen.
void
test0(hyperclient* cl)
{
for (generator A; A.has_next(); A.next())
{
absent(0, cl, A);
for (generator B; B.has_next(); B.next())
{
for (generator C; C.has_next(); C.next())
{
put(0, cl, A, B, C);
}
}
present(0, cl, A, generator::end(), generator::end());
del(0, cl, A);
absent(0, cl, A);
}
success(0);
}
// This test continually puts/deletes keys, ensuring that a [PUT, DEL] sequence
// of operations is run through every way in which one could select regions from
// the subspaces. 6 * 2**prefix requests will be performed. This tests that
// delete plays nicely with the fresh bit.
void
test1(hyperclient* cl)
{
for (generator A; A.has_next(); A.next())
{
absent(1, cl, A);
for (generator B; B.has_next(); B.next())
{
for (generator C; C.has_next(); C.next())
{
put(1, cl, A, B, C);
del(1, cl, A);
}
}
absent(1, cl, A);
}
success(1);
}
// This test puts keys such that A and B are fixed, but every choice of C for
// a fixed A, B is exercised. Keys are deleted when changing A or B. This
// tests that CHAIN_SUBSPACE messages work. This tests that CHAIN_SUBSPACE
// messages work correctly.
void
test2(hyperclient* cl)
{
for (generator A; A.has_next(); A.next())
{
for (generator B; B.has_next(); B.next())
{
absent(2, cl, A);
for (generator C; C.has_next(); C.next())
{
put(2, cl, A, B, C);
}
present(2, cl, A, B, generator::end());
del(2, cl, A);
absent(2, cl, A);
}
}
success(2);
}
// This is isomorphic to test2 except that columns A and C are fixed.
void
test3(hyperclient* cl)
{
for (generator A; A.has_next(); A.next())
{
for (generator C; C.has_next(); C.next())
{
absent(3, cl, A);
for (generator B; B.has_next(); B.next())
{
put(3, cl, A, B, C);
}
present(3, cl, A, generator::end(), C);
del(3, cl, A);
absent(3, cl, A);
}
}
success(3);
}
// This test stresses the interaction of CHAIN_SUBSPACE with DELETE messages.
// For each A, B, C pair, it creates a point. It then issues a PUT which causes
// B and C to jump to another subspace. It then issues a DEL to try to create a
// deferred update.
void
test4(hyperclient* cl)
{
for (generator A; A.has_next(); A.next())
{
for (generator B; B.has_next(); B.next())
{
for (generator C; C.has_next(); C.next())
{
absent(4, cl, A);
for (generator BP; BP.has_next(); BP.next())
{
for (generator CP; CP.has_next(); CP.next())
{
if (B != BP && C != CP)
{
put(4, cl, A, B, C);
put(4, cl, A, BP, CP);
del(4, cl, A);
}
}
}
absent(4, cl, A);
}
}
}
success(4);
}