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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
|
#include "../../src/configuration.h"
#include "../../src/log.h"
#include "../lib/heap.h"
#include "../lib/runner.h"
/******************************************************************************
*
* Fixture
*
*****************************************************************************/
struct fixture
{
FIXTURE_HEAP;
struct raft_log *log;
};
/******************************************************************************
*
* Helper macros
*
*****************************************************************************/
/* Accessors */
#define NUM_ENTRIES logNumEntries(f->log)
#define LAST_INDEX logLastIndex(f->log)
#define TERM_OF(INDEX) logTermOf(f->log, INDEX)
#define LAST_TERM logLastTerm(f->log)
#define GET(INDEX) logGet(f->log, INDEX)
/* Append one command entry with the given term and a hard-coded payload. */
#define APPEND(TERM) \
{ \
struct raft_buffer buf_; \
int rv_; \
buf_.base = raft_malloc(8); \
buf_.len = 8; \
strcpy(buf_.base, "hello"); \
rv_ = logAppend(f->log, TERM, RAFT_COMMAND, &buf_, NULL); \
munit_assert_int(rv_, ==, 0); \
}
/* Same as APPEND, but repeated N times. */
#define APPEND_MANY(TERM, N) \
{ \
int i_; \
for (i_ = 0; i_ < N; i_++) { \
APPEND(TERM); \
} \
}
/* Invoke append and assert that it returns the given error. */
#define APPEND_ERROR(TERM, RV) \
{ \
struct raft_buffer buf_; \
int rv_; \
buf_.base = raft_malloc(8); \
buf_.len = 8; \
rv_ = logAppend(f->log, TERM, RAFT_COMMAND, &buf_, NULL); \
munit_assert_int(rv_, ==, RV); \
raft_free(buf_.base); \
}
/* Append N entries all belonging to the same batch. Each entry will have 64-bit
* payload set to i * 1000, where i is the index of the entry in the batch. */
#define APPEND_BATCH(N) \
{ \
void *batch; \
size_t offset; \
int i; \
batch = raft_malloc(8 * N); \
munit_assert_ptr_not_null(batch); \
offset = 0; \
for (i = 0; i < N; i++) { \
struct raft_buffer buf; \
int rv; \
buf.base = (uint8_t *)batch + offset; \
buf.len = 8; \
*(uint64_t *)buf.base = i * 1000; \
rv = logAppend(f->log, 1, RAFT_COMMAND, &buf, batch); \
munit_assert_int(rv, ==, 0); \
offset += 8; \
} \
}
#define ACQUIRE(INDEX) \
{ \
int rv2; \
rv2 = logAcquire(f->log, INDEX, &entries, &n); \
munit_assert_int(rv2, ==, 0); \
}
#define RELEASE(INDEX) logRelease(f->log, INDEX, entries, n);
#define TRUNCATE(N) logTruncate(f->log, N)
#define SNAPSHOT(INDEX, TRAILING) logSnapshot(f->log, INDEX, TRAILING)
#define RESTORE(INDEX, TERM) logRestore(f->log, INDEX, TERM)
/******************************************************************************
*
* Set up an empty configuration.
*
*****************************************************************************/
static void *setUp(const MunitParameter params[], MUNIT_UNUSED void *user_data)
{
struct fixture *f = munit_malloc(sizeof *f);
SET_UP_HEAP;
f->log = logInit();
if (f->log == NULL) {
munit_assert_true(false);
}
return f;
}
static void tearDown(void *data)
{
struct fixture *f = data;
logClose(f->log);
TEAR_DOWN_HEAP;
free(f);
}
/******************************************************************************
*
* Assertions
*
*****************************************************************************/
/* Assert the state of the fixture's log in terms of size, front/back indexes,
* offset and number of entries. */
#define ASSERT(SIZE, FRONT, BACK, OFFSET, N) \
munit_assert_int(f->log->size, ==, SIZE); \
munit_assert_int(f->log->front, ==, FRONT); \
munit_assert_int(f->log->back, ==, BACK); \
munit_assert_int(f->log->offset, ==, OFFSET); \
munit_assert_int(logNumEntries(f->log), ==, N)
/* Assert the last index and term of the most recent snapshot. */
#define ASSERT_SNAPSHOT(INDEX, TERM) \
munit_assert_int(f->log->snapshot.last_index, ==, INDEX); \
munit_assert_int(f->log->snapshot.last_term, ==, TERM)
/* Assert that the term of entry at INDEX equals TERM. */
#define ASSERT_TERM_OF(INDEX, TERM) \
{ \
const struct raft_entry *entry; \
entry = logGet(f->log, INDEX); \
munit_assert_ptr_not_null(entry); \
munit_assert_int(entry->term, ==, TERM); \
}
/* Assert that the number of outstanding references for the entry at INDEX
* equals COUNT. */
#define ASSERT_REFCOUNT(INDEX, COUNT) \
{ \
size_t i; \
munit_assert_ptr_not_null(f->log->refs); \
for (i = 0; i < f->log->refs_size; i++) { \
if (f->log->refs[i].index == INDEX) { \
munit_assert_int(f->log->refs[i].count, ==, COUNT); \
break; \
} \
} \
if (i == f->log->refs_size) { \
munit_errorf("no refcount found for entry with index %d", \
(int)INDEX); \
} \
}
/******************************************************************************
*
* logNumEntries
*
*****************************************************************************/
SUITE(logNumEntries)
/* If the log is empty, the return value is zero. */
TEST(logNumEntries, empty, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
munit_assert_int(NUM_ENTRIES, ==, 0);
return MUNIT_OK;
}
/* The log is not wrapped. */
TEST(logNumEntries, not_wrapped, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1 /* term */);
munit_assert_int(NUM_ENTRIES, ==, 1);
return MUNIT_OK;
}
/* The log is wrapped. */
TEST(logNumEntries, wrapped, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
SNAPSHOT(4 /* last_index */, 1 /* trailing */);
APPEND_MANY(1 /* term */, 2 /* n entries */);
munit_assert_int(NUM_ENTRIES, ==, 4);
return MUNIT_OK;
}
/* The log has an offset and is empty. */
TEST(logNumEntries, offset, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
SNAPSHOT(5 /* last index */, 0 /* trailing */);
munit_assert_int(NUM_ENTRIES, ==, 0);
return MUNIT_OK;
}
/* The log has an offset and is not empty. */
TEST(logNumEntries, offsetNotEmpty, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
SNAPSHOT(4 /* last index */, 2 /* trailing */);
munit_assert_int(NUM_ENTRIES, ==, 3);
return MUNIT_OK;
}
/******************************************************************************
*
* logLastIndex
*
*****************************************************************************/
SUITE(logLastIndex)
/* If the log is empty, last index is 0. */
TEST(logLastIndex, empty, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
munit_assert_int(LAST_INDEX, ==, 0);
return MUNIT_OK;
}
/* If the log is empty and has an offset, last index is calculated
accordingly. */
TEST(logLastIndex, emptyWithOffset, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1);
SNAPSHOT(1, 0);
munit_assert_int(LAST_INDEX, ==, 1);
return MUNIT_OK;
}
/* The log has one entry. */
TEST(logLastIndex, one, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1 /* term */);
munit_assert_int(LAST_INDEX, ==, 1);
return MUNIT_OK;
}
/* The log has two entries. */
TEST(logLastIndex, two, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 2 /* n */);
munit_assert_int(LAST_INDEX, ==, 2);
return MUNIT_OK;
}
/* If the log starts at a certain offset, the last index is bumped
* accordingly. */
TEST(logLastIndex, twoWithOffset, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n */);
SNAPSHOT(5 /* last index */, 2 /* trailing */);
munit_assert_int(LAST_INDEX, ==, 5);
return MUNIT_OK;
}
/******************************************************************************
*
* logLastTerm
*
*****************************************************************************/
SUITE(logLastTerm)
/* If the log is empty, return zero. */
TEST(logLastTerm, empty, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
munit_assert_int(LAST_TERM, ==, 0);
return MUNIT_OK;
}
/* If the log has a snapshot and no outstanding entries, return the last term of
* the snapshot. */
TEST(logLastTerm, snapshot, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1 /* term */);
SNAPSHOT(1 /* last index */, 0 /* trailing */);
munit_assert_int(LAST_TERM, ==, 1);
return MUNIT_OK;
}
/******************************************************************************
*
* logTermOf
*
*****************************************************************************/
SUITE(logTermOf)
/* If the given index is beyond the last index, return 0. */
TEST(logTermOf, beyondLast, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
munit_assert_int(TERM_OF(2), ==, 0);
munit_assert_int(TERM_OF(10), ==, 0);
return MUNIT_OK;
}
/* If the log is empty but has a snapshot, and the given index matches the last
* index of the snapshot, return the snapshot last term. */
TEST(logTermOf, snapshotLastIndex, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
SNAPSHOT(5 /* last entry */, 0 /* trailing */);
munit_assert_int(TERM_OF(5), ==, 1);
return MUNIT_OK;
}
/* The log has one entry. */
TEST(logTermOf, one, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(3 /* term */);
munit_assert_int(TERM_OF(1), ==, 3);
return MUNIT_OK;
}
/* The log has two entries. */
TEST(logTermOf, two, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(4 /* term */, 2 /* n */);
munit_assert_int(TERM_OF(1), ==, 4);
munit_assert_int(TERM_OF(2), ==, 4);
return MUNIT_OK;
}
/* The log has a snapshot and hence has an offset. */
TEST(logTermOf, withSnapshot, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
SNAPSHOT(3 /* last index */, 0 /* trailing */);
munit_assert_int(TERM_OF(1), ==, 0);
munit_assert_int(TERM_OF(2), ==, 0);
munit_assert_int(TERM_OF(3), ==, 1);
munit_assert_int(TERM_OF(4), ==, 1);
munit_assert_int(TERM_OF(5), ==, 1);
return MUNIT_OK;
}
/* The log has a snapshot with trailing entries. */
TEST(logTermOf, snapshotTrailing, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
SNAPSHOT(3 /* last index */, 2 /* trailing */);
munit_assert_int(TERM_OF(1), ==, 0);
munit_assert_int(TERM_OF(2), ==, 1);
munit_assert_int(TERM_OF(3), ==, 1);
munit_assert_int(TERM_OF(4), ==, 1);
munit_assert_int(TERM_OF(5), ==, 1);
return MUNIT_OK;
}
/******************************************************************************
*
* logGet
*
*****************************************************************************/
SUITE(logGet)
/* The log is empty. */
TEST(logGet, empty_log, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
munit_assert_ptr_null(GET(1));
return MUNIT_OK;
}
/* The log is empty but has an offset. */
TEST(logGet, emptyWithOffset, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(4 /* term */, 10 /* n */);
SNAPSHOT(10 /* last index */, 0 /* trailing */);
munit_assert_ptr_null(GET(1));
munit_assert_ptr_null(GET(10));
munit_assert_ptr_null(GET(11));
return MUNIT_OK;
}
/* The log has one entry. */
TEST(logGet, one, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(3 /* term */);
munit_assert_int(GET(1)->term, ==, 3);
munit_assert_ptr_null(GET(2));
return MUNIT_OK;
}
/* The log has two entries. */
TEST(logGet, two, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(4 /* term */, 2 /* n */);
munit_assert_int(GET(1)->term, ==, 4);
munit_assert_int(GET(2)->term, ==, 4);
munit_assert_ptr_null(GET(3));
return MUNIT_OK;
}
/* The log starts at a certain offset. */
TEST(logGet, twoWithOffset, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 3 /* n */);
APPEND(2 /* term */);
APPEND(3 /* term */);
SNAPSHOT(4 /* las index */, 1 /* trailing */);
munit_assert_ptr_null(GET(1));
munit_assert_ptr_null(GET(2));
munit_assert_ptr_null(GET(3));
munit_assert_int(GET(4)->term, ==, 2);
munit_assert_int(GET(5)->term, ==, 3);
return MUNIT_OK;
}
/******************************************************************************
*
* logAppend
*
*****************************************************************************/
SUITE(logAppend)
/* Append one entry to an empty log. */
TEST(logAppend, one, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1 /* term */);
ASSERT(2 /* size */,
0 /* front */,
1 /* back */,
0 /* offset */,
1 /* n */);
ASSERT_TERM_OF(1 /* entry index */, 1 /* term */);
ASSERT_REFCOUNT(1 /* entry index */, 1 /* count */);
return MUNIT_OK;
}
/* Append two entries to to an empty log. */
TEST(logAppend, two, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1 /* term */);
APPEND(1 /* term */);
ASSERT(6 /* size */,
0 /* front */,
2 /* back */,
0 /* offset */,
2 /* n */);
ASSERT_TERM_OF(1 /* entry index */, 1 /* term */);
ASSERT_TERM_OF(2 /* entry index */, 1 /* term */);
ASSERT_REFCOUNT(1 /* entry index */, 1 /* count */);
ASSERT_REFCOUNT(2 /* entry index */, 1 /* count */);
return MUNIT_OK;
}
/* Append three entries in sequence. */
TEST(logAppend, three, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
/* One -> [e1, NULL] */
APPEND(1 /* term */);
/* Two -> [e1, e2, NULL, NULL, NULL, NULL] */
APPEND(1 /* term */);
/* Three -> [e1, e2, e3, NULL, NULL, NULL] */
APPEND(1 /* term */);
ASSERT(6 /* size */,
0 /* front */,
3 /* back */,
0 /* offset */,
3 /* n */);
ASSERT_TERM_OF(1 /* entry index */, 1 /* term */);
ASSERT_TERM_OF(2 /* entry index */, 1 /* term */);
ASSERT_TERM_OF(3 /* entry index */, 1 /* term */);
ASSERT_REFCOUNT(1 /* entry index */, 1 /* count */);
ASSERT_REFCOUNT(2 /* entry index */, 1 /* count */);
ASSERT_REFCOUNT(3 /* entry index */, 1 /* count */);
return MUNIT_OK;
}
/* Append enough entries to force the reference count hash table to be
* resized. */
TEST(logAppend, many, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
int i;
for (i = 0; i < 3000; i++) {
APPEND(1 /* term */);
}
munit_assert_int(f->log->refs_size, ==, 4096);
return MUNIT_OK;
}
/* Append to wrapped log that needs to be grown. */
TEST(logAppend, wrap, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n */);
/* Now the log is [e1, e2, e3, e4, e5, NULL] */
ASSERT(6 /* size */,
0 /* front */,
5 /* back */,
0 /* offset */,
5 /* n */);
/* Delete the first 4 entries. */
SNAPSHOT(4 /* last entry */, 0 /* trailing */);
/* Now the log is [NULL, NULL, NULL, NULL, e5, NULL] */
ASSERT(6 /* size */,
4 /* front */,
5 /* back */,
4 /* offset */,
1 /* n */);
/* Append another 3 entries. */
APPEND_MANY(1 /* term */, 3 /* n */);
/* Now the log is [e7, e8, NULL, NULL, e5, e6] */
ASSERT(6 /* size */,
4 /* front */,
2 /* back */,
4 /* offset */,
4 /* n */);
/* Append another 3 entries. */
APPEND_MANY(1 /* term */, 3 /* n */);
/* Now the log is [e5, ..., e11, NULL, ..., NULL] */
ASSERT(14 /* size */,
0 /* front */,
7 /* back */,
4 /* offset */,
7 /* n */);
return MUNIT_OK;
}
/* Append a batch of entries to an empty log. */
TEST(logAppend, batch, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_BATCH(3);
ASSERT(6 /* size */,
0 /* front */,
3 /* back */,
0 /* offset */,
3 /* n */);
return MUNIT_OK;
}
static char *logAppendOomHeapFaultDelay[] = {"0", "1", NULL};
static char *logAppendOomHeapFaultRepeat[] = {"1", NULL};
static MunitParameterEnum logAppendOom[] = {
{TEST_HEAP_FAULT_DELAY, logAppendOomHeapFaultDelay},
{TEST_HEAP_FAULT_REPEAT, logAppendOomHeapFaultRepeat},
{NULL, NULL},
};
/* Out of memory. */
TEST(logAppend, oom, setUp, tearDown, 0, logAppendOom)
{
struct fixture *f = data;
struct raft_buffer buf;
int rv;
buf.base = NULL;
buf.len = 0;
HeapFaultEnable(&f->heap);
rv = logAppend(f->log, 1, RAFT_COMMAND, &buf, NULL);
munit_assert_int(rv, ==, RAFT_NOMEM);
return MUNIT_OK;
}
/* Out of memory when trying to grow the refs count table. */
TEST(logAppend, oomRefs, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1, LOG__REFS_INITIAL_SIZE);
HeapFaultConfig(&f->heap, 1, 1);
HeapFaultEnable(&f->heap);
APPEND_ERROR(1, RAFT_NOMEM);
return MUNIT_OK;
}
/* Return an error when trying to append an entry with the same index and term
* of an older entry that got trucated but its still referenced. */
TEST(logAppend, busy, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries1;
struct raft_entry *entries2;
struct raft_entry *entries;
unsigned n;
APPEND(1 /* term */);
ACQUIRE(1);
entries1 = entries;
ACQUIRE(1);
entries2 = entries;
TRUNCATE(1);
entries = entries1;
RELEASE(1);
APPEND_ERROR(1 /* term */, RAFT_BUSY);
entries = entries2;
RELEASE(1);
return MUNIT_OK;
}
/******************************************************************************
*
* logAppendConfiguration
*
*****************************************************************************/
SUITE(logAppendConfiguration)
static char *logAppendConfigurationOomHeapFaultDelay[] = {"0", "1", NULL};
static char *logAppendConfigurationOomHeapFaultRepeat[] = {"1", NULL};
static MunitParameterEnum logAppendConfigurationOom[] = {
{TEST_HEAP_FAULT_DELAY, logAppendConfigurationOomHeapFaultDelay},
{TEST_HEAP_FAULT_REPEAT, logAppendConfigurationOomHeapFaultRepeat},
{NULL, NULL},
};
/* Out of memory. */
TEST(logAppendConfiguration, oom, setUp, tearDown, 0, logAppendConfigurationOom)
{
struct fixture *f = data;
struct raft_configuration configuration;
int rv;
configurationInit(&configuration);
rv = configurationAdd(&configuration, 1, "1", RAFT_VOTER);
munit_assert_int(rv, ==, 0);
HeapFaultEnable(&f->heap);
rv = logAppendConfiguration(f->log, 1, &configuration);
munit_assert_int(rv, ==, RAFT_NOMEM);
configurationClose(&configuration);
return MUNIT_OK;
}
/******************************************************************************
*
* logAcquire
*
*****************************************************************************/
SUITE(logAcquire)
/* Acquire a single log entry. */
TEST(logAcquire, one, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
APPEND(1 /* term */);
ACQUIRE(1 /* index */);
munit_assert_ptr_not_null(entries);
munit_assert_int(n, ==, 1);
munit_assert_int(entries[0].type, ==, RAFT_COMMAND);
ASSERT_REFCOUNT(1 /* index */, 2 /* count */);
RELEASE(1 /* index */);
ASSERT_REFCOUNT(1 /* index */, 1 /* count */);
return MUNIT_OK;
}
/* Acquire two log entries. */
TEST(logAcquire, two, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
APPEND(1 /* term */);
APPEND(1 /* term */);
ACQUIRE(1 /* index */);
munit_assert_ptr_not_null(entries);
munit_assert_int(n, ==, 2);
munit_assert_int(entries[0].type, ==, RAFT_COMMAND);
munit_assert_int(entries[1].type, ==, RAFT_COMMAND);
ASSERT_REFCOUNT(1 /* index */, 2 /* count */);
ASSERT_REFCOUNT(2 /* index */, 2 /* count */);
RELEASE(1 /* index */);
ASSERT_REFCOUNT(1 /* index */, 1 /* count */);
ASSERT_REFCOUNT(2 /* index */, 1 /* count */);
return MUNIT_OK;
}
/* Acquire two log entries in a wrapped log. */
TEST(logAcquire, wrap, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
APPEND_MANY(1 /* term */, 5 /* n */);
/* Now the log is [e1, e2, e3, e4, e5, NULL] */
ASSERT(6 /* size */,
0 /* front */,
5 /* back */,
0 /* offset */,
5 /* n */);
/* Delete the first 4 entries. */
SNAPSHOT(4 /* last index */, 0 /* trailing */);
/* Now the log is [NULL, NULL, NULL, NULL, e5, NULL] */
ASSERT(6 /* size */,
4 /* front */,
5 /* back */,
4 /* offset */,
1 /* n */);
/* Append another 3 entries. */
APPEND_MANY(1 /* term */, 3 /* n */);
/* Now the log is [e7, e8, NULL, NULL, e5, e6] */
ASSERT(6 /* size */,
4 /* front */,
2 /* back */,
4 /* offset */,
4 /* n */);
ACQUIRE(6 /* index */);
munit_assert_int(n, ==, 3);
RELEASE(6 /* index */);
return MUNIT_OK;
}
/* Acquire several entries some of which belong to batches. */
TEST(logAcquire, batch, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
APPEND(1 /* term */);
APPEND_BATCH(2 /* n entries */);
APPEND(1 /* term */);
APPEND_BATCH(3 /* n entries */);
ACQUIRE(2 /* index */);
munit_assert_ptr_not_null(entries);
munit_assert_int(n, ==, 6);
ASSERT_REFCOUNT(2 /* index */, 2 /* count */);
/* Truncate the last 5 entries, so the only references left for the second
* batch are the ones in the acquired entries. */
TRUNCATE(3 /* index */);
RELEASE(2 /* index */);
ASSERT_REFCOUNT(2 /* index */, 1 /* count */);
return MUNIT_OK;
}
/* Trying to acquire entries out of range results in a NULL pointer. */
TEST(logAcquire, outOfRange, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
APPEND(1 /* term */);
APPEND(1 /* term */);
SNAPSHOT(1 /* index */, 0 /* trailing */);
ACQUIRE(1 /* index */);
munit_assert_ptr_null(entries);
ACQUIRE(3 /* index */);
munit_assert_ptr_null(entries);
return MUNIT_OK;
}
/* Out of memory. */
TEST(logAcquire, oom, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
int rv;
APPEND(1 /* term */);
HeapFaultConfig(&f->heap, 0, 1);
HeapFaultEnable(&f->heap);
rv = logAcquire(f->log, 1, &entries, &n);
munit_assert_int(rv, ==, RAFT_NOMEM);
return MUNIT_OK;
}
/******************************************************************************
*
* logTruncate
*
*****************************************************************************/
SUITE(logTruncate)
/* Truncate the last entry of a log with a single entry. */
TEST(logTruncate, lastOfOne, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1 /* term */);
TRUNCATE(1 /* index */);
ASSERT(0 /* size */,
0 /* front */,
0 /* back */,
0 /* offset */,
0 /* n */);
return MUNIT_OK;
}
/* Truncate the last entry of a log with a two entries. */
TEST(logTruncate, lastOfTwo, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1 /* term */);
APPEND(1 /* term */);
TRUNCATE(2 /* index */);
ASSERT(6 /* size */,
0 /* front */,
1 /* back */,
0 /* offset */,
1 /* n */);
ASSERT_TERM_OF(1 /* entry index */, 1 /* term */);
return MUNIT_OK;
}
/* Truncate from an entry which makes the log wrap. */
TEST(logTruncate, wrap, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
/* Now the log is [e1, e2, e3, e4, e5, NULL] */
ASSERT(6 /* size */,
0 /* front */,
5 /* back */,
0 /* offset */,
5 /* n */);
/* Delete the first 4 entries. */
SNAPSHOT(4 /* last index */, 0 /* trailing */);
/* Now the log is [NULL, NULL, NULL, NULL, e5, NULL] */
ASSERT(6 /* size */,
4 /* front */,
5 /* back */,
4 /* offset */,
1 /* n */);
/* Append another 3 entries. */
APPEND_MANY(1 /* term */, 3 /* n entries */);
/* Now the log is [e7, e8, NULL, NULL, e5, e6] */
ASSERT(6 /* size */,
4 /* front */,
2 /* back */,
4 /* offset */,
4 /* n */);
/* Truncate from e6 onward (wrapping) */
TRUNCATE(6 /* index */);
/* Now the log is [NULL, NULL, NULL, NULL, e5, NULL] */
ASSERT(6 /* size */,
4 /* front */,
5 /* back */,
4 /* offset */,
1 /* n */);
return MUNIT_OK;
}
/* Truncate the last entry of a log with a single entry, which still has an
* outstanding reference created by a call to logAcquire(). */
TEST(logTruncate, referenced, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
APPEND(1 /* term */);
ACQUIRE(1 /* index */);
TRUNCATE(1 /* index */);
ASSERT(0 /* size */,
0 /* front */,
0 /* back */,
0 /* offset */,
0 /* n */);
/* The entry has still an outstanding reference. */
ASSERT_REFCOUNT(1 /* index */, 1 /* count */);
munit_assert_string_equal((const char *)entries[0].buf.base, "hello");
RELEASE(1 /* index */);
ASSERT_REFCOUNT(1 /* index */, 0 /* count */);
return MUNIT_OK;
}
/* Truncate all entries belonging to a batch. */
TEST(logTruncate, batch, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_BATCH(3 /* n entries */);
TRUNCATE(1 /* index */);
munit_assert_int(f->log->size, ==, 0);
return MUNIT_OK;
}
/* Acquire entries at a certain index. Truncate the log at that index. The
* truncated entries are still referenced. Then append a new entry, which will
* have the same index but different term. */
TEST(logTruncate, acquired, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
APPEND(1 /* term */);
APPEND(1 /* term */);
ACQUIRE(2 /* index */);
munit_assert_int(n, ==, 1);
TRUNCATE(2 /* index */);
APPEND(2 /* term */);
RELEASE(2 /*index */);
return MUNIT_OK;
}
/* Acquire some entries, truncate the log and then append new ones forcing the
log to be grown and the reference count hash table to be re-built. */
TEST(logTruncate, acquireAppend, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
size_t i;
APPEND(1 /* term */);
APPEND(1 /* term */);
ACQUIRE(2);
munit_assert_int(n, ==, 1);
TRUNCATE(2);
for (i = 0; i < LOG__REFS_INITIAL_SIZE; i++) {
APPEND(2 /* term */);
}
RELEASE(2);
return MUNIT_OK;
}
static char *logTruncateAcquiredHeapFaultDelay[] = {"0", NULL};
static char *logTruncateAcquiredFaultRepeat[] = {"1", NULL};
static MunitParameterEnum logTruncateAcquiredOom[] = {
{TEST_HEAP_FAULT_DELAY, logTruncateAcquiredHeapFaultDelay},
{TEST_HEAP_FAULT_REPEAT, logTruncateAcquiredFaultRepeat},
{NULL, NULL},
};
/* Acquire entries at a certain index. Truncate the log at that index. The
* truncated entries are still referenced. Then append a new entry, which fails
* to be appended due to OOM. */
TEST(logTruncate, acquiredOom, setUp, tearDown, 0, logTruncateAcquiredOom)
{
struct fixture *f = data;
struct raft_entry *entries;
unsigned n;
struct raft_buffer buf;
int rv;
APPEND(1 /* term */);
APPEND(1 /* term */);
ACQUIRE(2);
munit_assert_int(n, ==, 1);
TRUNCATE(2);
buf.base = NULL;
buf.len = 0;
HeapFaultEnable(&f->heap);
rv = logAppend(f->log, 2, RAFT_COMMAND, &buf, NULL);
munit_assert_int(rv, ==, RAFT_NOMEM);
RELEASE(2);
return MUNIT_OK;
}
/******************************************************************************
*
* logSnapshot
*
*****************************************************************************/
SUITE(logSnapshot)
/* Take a snapshot at entry 3, keeping 2 trailing entries. */
TEST(logSnapshot, trailing, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND(1 /* term */);
APPEND(2 /* term */);
APPEND(2 /* term */);
SNAPSHOT(3 /* last index */, 2 /* trailing */);
ASSERT(6 /* size */,
1 /* front */,
3 /* back */,
1 /* offset */,
2 /* n */);
ASSERT_SNAPSHOT(3 /* index */, 2 /* term */);
munit_assert_int(NUM_ENTRIES, ==, 2);
munit_assert_int(LAST_INDEX, ==, 3);
return MUNIT_OK;
}
/* Take a snapshot when the number of outstanding entries is lower than the
* desired trail (so no entry will be deleted). */
TEST(logSnapshot, trailingHigherThanNumEntries, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
/* Take a snapshot leaving just one entry in the log. */
APPEND_MANY(1 /* term */, 3 /* n entries */);
SNAPSHOT(3 /* last index */, 1 /* trailing */);
/* Take another snapshot, trying to leave 3 entries, but only 2 are
* available at all. */
APPEND(2 /* term */);
SNAPSHOT(4 /* last index */, 3 /* trailing */);
ASSERT(6 /* size */,
2 /* front */,
4 /* back */,
2 /* offset */,
2 /* n */);
ASSERT_SNAPSHOT(4 /* index */, 2 /* term */);
munit_assert_int(NUM_ENTRIES, ==, 2);
munit_assert_int(LAST_INDEX, ==, 4);
return MUNIT_OK;
}
/* Take a snapshot when the number of outstanding entries is exactly equal to
* the desired trail (so no entry will be deleted). */
TEST(logSnapshot, trailingMatchesOutstanding, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
/* Take a snapshot leaving just one entry in the log. */
APPEND_MANY(1 /* term */, 3 /* n entries */);
SNAPSHOT(3 /* last index */, 1 /* trailing */);
/* Take another snapshot, leaving 2 entries, which are the ones we have. */
APPEND(2 /* term */);
SNAPSHOT(4 /* last index */, 2 /* trailing */);
ASSERT(6 /* size */,
2 /* front */,
4 /* back */,
2 /* offset */,
2 /* n */);
ASSERT_SNAPSHOT(4 /* index */, 2 /* term */);
munit_assert_int(NUM_ENTRIES, ==, 2);
munit_assert_int(LAST_INDEX, ==, 4);
return MUNIT_OK;
}
/* Take a snapshot at an index which is not the last one. */
TEST(logSnapshot, lessThanHighestIndex, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
/* Take a snapshot leaving three entries in the log. */
APPEND_MANY(1 /* term */, 5 /* n entries */);
SNAPSHOT(4 /* last index */, 2 /* trailing */);
ASSERT(6 /* size */,
2 /* front */,
5 /* back */,
2 /* offset */,
3 /* n */);
ASSERT_SNAPSHOT(4 /* index */, 1 /* term */);
munit_assert_int(NUM_ENTRIES, ==, 3);
munit_assert_int(LAST_INDEX, ==, 5);
return MUNIT_OK;
}
/* Take a snapshot at a point where the log needs to wrap. */
TEST(logSnapshot, wrap, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
/* Now the log is [e1, e2, e3, e4, e5, NULL] */
ASSERT(6 /* size */,
0 /* front */,
5 /* back */,
0 /* offset */,
5 /* n */);
/* Take a snapshot at e5, keeping just e5 itself. */
SNAPSHOT(5 /* last index */, 1 /* trailing */);
/* Now the log is [NULL, NULL, NULL, NULL, e5, NULL] */
ASSERT(6 /* size */,
4 /* front */,
5 /* back */,
4 /* offset */,
1 /* n */);
ASSERT_SNAPSHOT(5 /* index */, 1 /* term */);
/* Append another 4 entries. */
APPEND_MANY(1 /* term */, 4 /* n */);
/* Now the log is [e7, e8, e9, NULL, e5, e6] */
ASSERT(6 /* size */,
4 /* front */,
3 /* back */,
4 /* offset */,
5 /* n */);
/* Take a snapshot at e8 keeping only e8 itself (wrapping) */
SNAPSHOT(8 /* last index */, 1 /* trailing */);
/* Now the log is [NULL, e8, e9, NULL, NULL, NULL] */
ASSERT(6 /* size */,
1 /* front */,
3 /* back */,
7 /* offset */,
2 /* n */);
ASSERT_SNAPSHOT(8 /* index */, 1 /* term */);
return MUNIT_OK;
}
/******************************************************************************
*
* logRestore
*
*****************************************************************************/
SUITE(logRestore)
/* Mimic the initial restore of a snapshot after loading state from disk, when
* there are no outstanding entries. */
TEST(logRestore, initial, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
RESTORE(2 /* last index */, 3 /* last term */);
ASSERT_SNAPSHOT(2 /* index */, 3 /* term */);
munit_assert_int(LAST_INDEX, ==, 2);
return MUNIT_OK;
}
/* If there are existing entries they are wiped out. */
TEST(logRestore, wipe, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
APPEND_MANY(1 /* term */, 5 /* n entries */);
RESTORE(2 /* last index */, 3 /* last term */);
ASSERT_SNAPSHOT(2 /* index */, 3 /* term */);
munit_assert_int(LAST_INDEX, ==, 2);
return MUNIT_OK;
}
|