| 1 |
#include "../../src/client/protocol.h" |
| 2 |
#include "../../src/server.h" |
| 3 |
#include "../lib/client.h" |
| 4 |
#include "../lib/endpoint.h" |
| 5 |
#include "../lib/fs.h" |
| 6 |
#include "../lib/heap.h" |
| 7 |
#include "../lib/runner.h" |
| 8 |
#include "../lib/server.h" |
| 9 |
#include "../lib/sqlite.h" |
| 10 |
|
| 11 |
#ifndef FLAKY |
| 12 |
|
| 13 |
/****************************************************************************** |
| 14 |
* |
| 15 |
* Fixture |
| 16 |
* |
| 17 |
******************************************************************************/ |
| 18 |
|
| 19 |
#define N_SERVERS 3 |
| 20 |
#define FIXTURE \ |
| 21 |
struct test_server servers[N_SERVERS]; \ |
| 22 |
struct client_proto *client |
| 23 |
|
| 24 |
#define SETUP \ |
| 25 |
unsigned i_; \ |
| 26 |
test_heap_setup(params, user_data); \ |
| 27 |
test_sqlite_setup(params); \ |
| 28 |
for (i_ = 0; i_ < N_SERVERS; i_++) { \ |
| 29 |
struct test_server *server = &f->servers[i_]; \ |
| 30 |
test_server_setup(server, i_ + 1, params); \ |
| 31 |
} \ |
| 32 |
test_server_network(f->servers, N_SERVERS); \ |
| 33 |
for (i_ = 0; i_ < N_SERVERS; i_++) { \ |
| 34 |
struct test_server *server = &f->servers[i_]; \ |
| 35 |
test_server_start(server, params); \ |
| 36 |
} \ |
| 37 |
SELECT(1) |
| 38 |
|
| 39 |
#define TEAR_DOWN \ |
| 40 |
unsigned i_; \ |
| 41 |
for (i_ = 0; i_ < N_SERVERS; i_++) { \ |
| 42 |
test_server_tear_down(&f->servers[i_]); \ |
| 43 |
} \ |
| 44 |
test_sqlite_tear_down(); \ |
| 45 |
test_heap_tear_down(data) |
| 46 |
|
| 47 |
/* Use the client connected to the server with the given ID. */ |
| 48 |
#define SELECT(ID) f->client = test_server_client(&f->servers[ID - 1]) |
| 49 |
|
| 50 |
/****************************************************************************** |
| 51 |
* |
| 52 |
* cluster |
| 53 |
* |
| 54 |
******************************************************************************/ |
| 55 |
|
| 56 |
SUITE(cluster) |
| 57 |
|
| 58 |
struct fixture |
| 59 |
{ |
| 60 |
FIXTURE; |
| 61 |
}; |
| 62 |
|
| 63 |
static void *setUp(const MunitParameter params[], void *user_data) |
| 64 |
{ |
| 65 |
struct fixture *f = munit_malloc(sizeof *f); |
| 66 |
SETUP; |
| 67 |
return f; |
| 68 |
} |
| 69 |
|
| 70 |
static void tearDown(void *data) |
| 71 |
{ |
| 72 |
struct fixture *f = data; |
| 73 |
TEAR_DOWN; |
| 74 |
free(f); |
| 75 |
} |
| 76 |
|
| 77 |
static char *num_records[] = { |
| 78 |
"0", "1", "256", |
| 79 |
/* WAL will just have been checkpointed after 993 writes. */ |
| 80 |
"993", |
| 81 |
/* Non-empty WAL, checkpointed twice, 2 snapshots taken */ |
| 82 |
"2200", NULL}; |
| 83 |
|
| 84 |
static MunitParameterEnum cluster_params[] = { |
| 85 |
{"num_records", num_records}, |
| 86 |
{NULL, NULL}, |
| 87 |
}; |
| 88 |
|
| 89 |
/* Restart a node and check if all data is there */ |
| 90 |
TEST(cluster, restart, setUp, tearDown, 0, cluster_params) |
| 91 |
{ |
| 92 |
struct fixture *f = data; |
| 93 |
uint32_t stmt_id; |
| 94 |
uint64_t last_insert_id; |
| 95 |
uint64_t rows_affected; |
| 96 |
struct rows rows; |
| 97 |
long n_records = |
| 98 |
strtol(munit_parameters_get(params, "num_records"), NULL, 0); |
| 99 |
char sql[128]; |
| 100 |
|
| 101 |
HANDSHAKE; |
| 102 |
OPEN; |
| 103 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 104 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 105 |
|
| 106 |
for (int i = 0; i < n_records; ++i) { |
| 107 |
sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1); |
| 108 |
PREPARE(sql, &stmt_id); |
| 109 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 110 |
} |
| 111 |
|
| 112 |
struct test_server *server = &f->servers[0]; |
| 113 |
test_server_stop(server); |
| 114 |
test_server_start(server, params); |
| 115 |
|
| 116 |
/* The table is visible after restart. */ |
| 117 |
HANDSHAKE; |
| 118 |
OPEN; |
| 119 |
PREPARE("SELECT COUNT(*) from test", &stmt_id); |
| 120 |
QUERY(stmt_id, &rows); |
| 121 |
munit_assert_long(rows.next->values->integer, ==, n_records); |
| 122 |
clientCloseRows(&rows); |
| 123 |
return MUNIT_OK; |
| 124 |
} |
| 125 |
|
| 126 |
/* Add data to a node, add a new node and make sure data is there. */ |
| 127 |
TEST(cluster, dataOnNewNode, setUp, tearDown, 0, cluster_params) |
| 128 |
{ |
| 129 |
struct fixture *f = data; |
| 130 |
uint32_t stmt_id; |
| 131 |
uint64_t last_insert_id; |
| 132 |
uint64_t rows_affected; |
| 133 |
struct rows rows; |
| 134 |
long n_records = |
| 135 |
strtol(munit_parameters_get(params, "num_records"), NULL, 0); |
| 136 |
char sql[128]; |
| 137 |
unsigned id = 2; |
| 138 |
const char *address = "@2"; |
| 139 |
|
| 140 |
HANDSHAKE; |
| 141 |
OPEN; |
| 142 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 143 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 144 |
|
| 145 |
for (int i = 0; i < n_records; ++i) { |
| 146 |
sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1); |
| 147 |
PREPARE(sql, &stmt_id); |
| 148 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 149 |
} |
| 150 |
|
| 151 |
/* Add a second voting server, this one will receive all data from the |
| 152 |
* original leader. */ |
| 153 |
ADD(id, address); |
| 154 |
ASSIGN(id, COWSQL_VOTER); |
| 155 |
|
| 156 |
/* Remove original server so second server becomes leader after election |
| 157 |
* timeout */ |
| 158 |
REMOVE(1); |
| 159 |
sleep(1); |
| 160 |
|
| 161 |
/* The full table is visible from the new node */ |
| 162 |
SELECT(2); |
| 163 |
HANDSHAKE; |
| 164 |
OPEN; |
| 165 |
PREPARE("SELECT COUNT(*) from test", &stmt_id); |
| 166 |
QUERY(stmt_id, &rows); |
| 167 |
munit_assert_long(rows.next->values->integer, ==, n_records); |
| 168 |
clientCloseRows(&rows); |
| 169 |
return MUNIT_OK; |
| 170 |
} |
| 171 |
|
| 172 |
/* Insert a huge row, causing SQLite to allocate overflow pages. Then |
| 173 |
* insert the same row again. (Reproducer for |
| 174 |
* https://github.com/canonical/raft/issues/432.) */ |
| 175 |
TEST(cluster, hugeRow, setUp, tearDown, 0, NULL) |
| 176 |
{ |
| 177 |
struct fixture *f = data; |
| 178 |
uint32_t stmt_id; |
| 179 |
uint64_t last_insert_id; |
| 180 |
uint64_t rows_affected; |
| 181 |
char *sql; |
| 182 |
ssize_t n; |
| 183 |
size_t huge = 20000000; |
| 184 |
(void)params; |
| 185 |
|
| 186 |
HANDSHAKE; |
| 187 |
OPEN; |
| 188 |
PREPARE( |
| 189 |
"CREATE TABLE IF NOT EXISTS model(key TEXT, value TEXT, " |
| 190 |
"UNIQUE(key))", |
| 191 |
&stmt_id); |
| 192 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 193 |
sql = munit_malloc(huge); |
| 194 |
n = snprintf( |
| 195 |
sql, huge, |
| 196 |
"INSERT OR REPLACE INTO model (key, value) VALUES('my-key-1', '"); |
| 197 |
memset(sql + n, 'A', huge - n); |
| 198 |
memcpy(sql + huge - 3, "')", 3); |
| 199 |
PREPARE(sql, &stmt_id); |
| 200 |
free(sql); |
| 201 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 202 |
/* Again */ |
| 203 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 204 |
return MUNIT_OK; |
| 205 |
} |
| 206 |
|
| 207 |
TEST(cluster, modifyingQuery, setUp, tearDown, 0, cluster_params) |
| 208 |
{ |
| 209 |
struct fixture *f = data; |
| 210 |
uint32_t stmt_id; |
| 211 |
uint64_t last_insert_id; |
| 212 |
uint64_t rows_affected; |
| 213 |
struct rows rows; |
| 214 |
long n_records = |
| 215 |
strtol(munit_parameters_get(params, "num_records"), NULL, 0); |
| 216 |
char sql[128]; |
| 217 |
unsigned id = 2; |
| 218 |
const char *address = "@2"; |
| 219 |
|
| 220 |
HANDSHAKE; |
| 221 |
OPEN; |
| 222 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 223 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 224 |
|
| 225 |
for (int i = 0; i < n_records; ++i) { |
| 226 |
sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1); |
| 227 |
PREPARE(sql, &stmt_id); |
| 228 |
QUERY(stmt_id, &rows); |
| 229 |
munit_assert_uint64(rows.column_count, ==, 0); |
| 230 |
munit_assert_ptr(rows.next, ==, NULL); |
| 231 |
clientCloseRows(&rows); |
| 232 |
} |
| 233 |
|
| 234 |
ADD(id, address); |
| 235 |
ASSIGN(id, COWSQL_VOTER); |
| 236 |
|
| 237 |
REMOVE(1); |
| 238 |
sleep(1); |
| 239 |
|
| 240 |
SELECT(2); |
| 241 |
HANDSHAKE; |
| 242 |
OPEN; |
| 243 |
PREPARE("SELECT COUNT(*) from test", &stmt_id); |
| 244 |
QUERY(stmt_id, &rows); |
| 245 |
munit_assert_long(rows.next->values->integer, ==, n_records); |
| 246 |
clientCloseRows(&rows); |
| 247 |
return MUNIT_OK; |
| 248 |
} |
| 249 |
|
| 250 |
TEST(cluster, modifyingQuerySql, setUp, tearDown, 0, cluster_params) |
| 251 |
{ |
| 252 |
struct fixture *f = data; |
| 253 |
uint32_t stmt_id; |
| 254 |
uint64_t last_insert_id; |
| 255 |
uint64_t rows_affected; |
| 256 |
struct rows rows; |
| 257 |
long n_records = |
| 258 |
strtol(munit_parameters_get(params, "num_records"), NULL, 0); |
| 259 |
char sql[128]; |
| 260 |
unsigned id = 2; |
| 261 |
const char *address = "@2"; |
| 262 |
|
| 263 |
HANDSHAKE; |
| 264 |
OPEN; |
| 265 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 266 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 267 |
|
| 268 |
for (int i = 0; i < n_records; ++i) { |
| 269 |
sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1); |
| 270 |
QUERY_SQL(sql, &rows); |
| 271 |
munit_assert_uint64(rows.column_count, ==, 0); |
| 272 |
munit_assert_ptr(rows.next, ==, NULL); |
| 273 |
clientCloseRows(&rows); |
| 274 |
} |
| 275 |
|
| 276 |
ADD(id, address); |
| 277 |
ASSIGN(id, COWSQL_VOTER); |
| 278 |
|
| 279 |
REMOVE(1); |
| 280 |
sleep(1); |
| 281 |
|
| 282 |
SELECT(2); |
| 283 |
HANDSHAKE; |
| 284 |
OPEN; |
| 285 |
PREPARE("SELECT COUNT(*) from test", &stmt_id); |
| 286 |
QUERY(stmt_id, &rows); |
| 287 |
munit_assert_long(rows.next->values->integer, ==, n_records); |
| 288 |
clientCloseRows(&rows); |
| 289 |
return MUNIT_OK; |
| 290 |
} |
| 291 |
|
| 292 |
#endif /* not FLAKY */ |