File: test_replication.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (174 lines) | stat: -rw-r--r-- 4,522 bytes parent folder | download | duplicates (2)
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
#include "../lib/legacy.h"
#include "../lib/runner.h"

/******************************************************************************
 *
 * Fixture
 *
 *****************************************************************************/

struct fixture
{
    FIXTURE_CLUSTER;
};

static char *cluster_n[] = {"3", "5", "7", NULL};

static MunitParameterEnum _params[] = {
    {CLUSTER_N_PARAM, cluster_n},
    {NULL, NULL},
};

static void *setup(const MunitParameter params[], MUNIT_UNUSED void *user_data)
{
    struct fixture *f = munit_malloc(sizeof *f);
    SETUP_CLUSTER(0);
    CLUSTER_BOOTSTRAP;
    CLUSTER_RANDOMIZE;
    CLUSTER_START();
    CLUSTER_STEP_UNTIL_HAS_LEADER(10000);
    return f;
}

static void tear_down(void *data)
{
    struct fixture *f = data;
    TEAR_DOWN_CLUSTER;
    free(f);
}

/******************************************************************************
 *
 * Helper macros
 *
 *****************************************************************************/

#define APPLY_ADD_ONE(REQ) CLUSTER_APPLY_ADD_X(CLUSTER_LEADER, REQ, 1, NULL)

/******************************************************************************
 *
 * Tests
 *
 *****************************************************************************/

SUITE(replication)

/* New entries on the leader are eventually replicated to followers. */
TEST(replication, appendEntries, setup, tear_down, 0, _params)
{
    struct fixture *f = data;
    struct raft_apply *req = munit_malloc(sizeof *req);
    (void)params;
    APPLY_ADD_ONE(req);
    CLUSTER_STEP_UNTIL_APPLIED(CLUSTER_N, 2, 2000);
    free(req);
    return MUNIT_OK;
}

/* The cluster remains available even if the current leader dies and a new
 * leader gets elected. */
TEST(replication, availability, setup, tear_down, 0, _params)
{
    struct fixture *f = data;
    struct raft_apply *req1 = munit_malloc(sizeof *req1);
    struct raft_apply *req2 = munit_malloc(sizeof *req2);

    (void)params;

    APPLY_ADD_ONE(req1);
    CLUSTER_STEP_UNTIL_APPLIED(CLUSTER_N, 2, 2000);

    CLUSTER_KILL_LEADER;
    CLUSTER_STEP_UNTIL_HAS_NO_LEADER(10000);
    CLUSTER_STEP_UNTIL_HAS_LEADER(10000);

    APPLY_ADD_ONE(req2);
    CLUSTER_STEP_UNTIL_APPLIED(CLUSTER_LEADER, 3, 2000);

    free(req1);
    free(req2);

    return MUNIT_OK;
}

static void apply_cb(struct raft_apply *req, int status, void *result)
{
    (void)status;
    (void)result;
    free(req);
}

/* If no quorum is available, entries don't get committed. */
TEST(replication, noQuorum, setup, tear_down, 0, _params)
{
    struct fixture *f = data;
    struct raft_apply *req = munit_malloc(sizeof *req);
    unsigned i;

    (void)params;

    CLUSTER_APPLY_ADD_X(CLUSTER_LEADER, req, 1, apply_cb);
    CLUSTER_KILL_MAJORITY;

    CLUSTER_STEP_UNTIL_ELAPSED(10000);

    for (i = 0; i < CLUSTER_N; i++) {
        munit_assert_int(CLUSTER_LAST_APPLIED(i), ==, 1);
    }

    return MUNIT_OK;
}

/* If the cluster is partitioned, entries don't get committed. */
TEST(replication, partitioned, setup, tear_down, 0, _params)
{
    struct fixture *f = data;
    struct raft_apply *req1 = munit_malloc(sizeof *req1);
    struct raft_apply *req2 = munit_malloc(sizeof *req2);
    unsigned leader_id;
    size_t i;
    size_t n;

    (void)params;

    leader_id = CLUSTER_LEADER + 1;

    /* Disconnect the leader from a majority of servers */
    n = 0;
    for (i = 0; n < (CLUSTER_N / 2) + 1; i++) {
        struct raft *raft = CLUSTER_RAFT(i);
        if (raft->id == leader_id) {
            continue;
        }
        raft_fixture_saturate(&f->cluster, leader_id - 1, raft->id - 1);
        raft_fixture_saturate(&f->cluster, raft->id - 1, leader_id - 1);
        n++;
    }

    /* Try to append a new entry using the disconnected leader. */
    CLUSTER_APPLY_ADD_X(CLUSTER_LEADER, req1, 1, apply_cb);

    /* The leader gets deposed. */
    CLUSTER_STEP_UNTIL_HAS_NO_LEADER(10000);

    /* The entry does not get committed. */
    CLUSTER_STEP_UNTIL_ELAPSED(5000);

    /* Reconnect the old leader */
    for (i = 0; i < CLUSTER_N; i++) {
        struct raft *raft = CLUSTER_RAFT(i);
        if (raft->id == leader_id) {
            continue;
        }
        raft_fixture_desaturate(&f->cluster, leader_id - 1, raft->id - 1);
    }

    // TODO this fails with seed 0x3914306f
    CLUSTER_STEP_UNTIL_HAS_LEADER(30000);

    /* Re-try now to append the entry. */
    CLUSTER_APPLY_ADD_X(CLUSTER_LEADER, req2, 1, apply_cb);
    CLUSTER_STEP_UNTIL_APPLIED(CLUSTER_LEADER, 2, 10000);

    return MUNIT_OK;
}