| 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 |
#include "../lib/util.h" |
| 11 |
|
| 12 |
#ifndef FLAKY |
| 13 |
|
| 14 |
/****************************************************************************** |
| 15 |
* |
| 16 |
* Fixture |
| 17 |
* |
| 18 |
******************************************************************************/ |
| 19 |
|
| 20 |
#define N_SERVERS 3 |
| 21 |
#define FIXTURE \ |
| 22 |
struct test_server servers[N_SERVERS]; \ |
| 23 |
struct client_proto *client; \ |
| 24 |
struct rows rows; |
| 25 |
|
| 26 |
#define SETUP \ |
| 27 |
unsigned i_; \ |
| 28 |
test_heap_setup(params, user_data); \ |
| 29 |
test_sqlite_setup(params); \ |
| 30 |
for (i_ = 0; i_ < N_SERVERS; i_++) { \ |
| 31 |
struct test_server *server = &f->servers[i_]; \ |
| 32 |
test_server_setup(server, i_ + 1, params); \ |
| 33 |
} \ |
| 34 |
test_server_network(f->servers, N_SERVERS); \ |
| 35 |
for (i_ = 0; i_ < N_SERVERS; i_++) { \ |
| 36 |
struct test_server *server = &f->servers[i_]; \ |
| 37 |
test_server_start(server, params); \ |
| 38 |
} \ |
| 39 |
SELECT(1) |
| 40 |
|
| 41 |
#define TEAR_DOWN \ |
| 42 |
unsigned i_; \ |
| 43 |
for (i_ = 0; i_ < N_SERVERS; i_++) { \ |
| 44 |
test_server_tear_down(&f->servers[i_]); \ |
| 45 |
} \ |
| 46 |
test_sqlite_tear_down(); \ |
| 47 |
test_heap_tear_down(data) |
| 48 |
|
| 49 |
/****************************************************************************** |
| 50 |
* |
| 51 |
* Helper macros. |
| 52 |
* |
| 53 |
******************************************************************************/ |
| 54 |
|
| 55 |
/* Use the client connected to the server with the given ID. */ |
| 56 |
#define SELECT(ID) f->client = test_server_client(&f->servers[ID - 1]) |
| 57 |
|
| 58 |
/****************************************************************************** |
| 59 |
* |
| 60 |
* join |
| 61 |
* |
| 62 |
******************************************************************************/ |
| 63 |
|
| 64 |
SUITE(membership) |
| 65 |
|
| 66 |
struct fixture |
| 67 |
{ |
| 68 |
FIXTURE; |
| 69 |
}; |
| 70 |
|
| 71 |
static void *setUp(const MunitParameter params[], void *user_data) |
| 72 |
{ |
| 73 |
struct fixture *f = munit_malloc(sizeof *f); |
| 74 |
SETUP; |
| 75 |
return f; |
| 76 |
} |
| 77 |
|
| 78 |
static void tearDown(void *data) |
| 79 |
{ |
| 80 |
struct fixture *f = data; |
| 81 |
TEAR_DOWN; |
| 82 |
free(f); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(membership, join, setUp, tearDown, 0, NULL) |
| 86 |
{ |
| 87 |
struct fixture *f = data; |
| 88 |
unsigned id = 2; |
| 89 |
const char *address = "@2"; |
| 90 |
uint32_t stmt_id; |
| 91 |
uint64_t last_insert_id; |
| 92 |
uint64_t rows_affected; |
| 93 |
|
| 94 |
HANDSHAKE; |
| 95 |
ADD(id, address); |
| 96 |
ASSIGN(id, COWSQL_VOTER); |
| 97 |
OPEN; |
| 98 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 99 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 100 |
PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id); |
| 101 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 102 |
|
| 103 |
/* The table is visible from the new node */ |
| 104 |
TRANSFER(id, f->client); |
| 105 |
SELECT(2); |
| 106 |
HANDSHAKE; |
| 107 |
OPEN; |
| 108 |
PREPARE("SELECT * FROM test", &stmt_id); |
| 109 |
|
| 110 |
/* TODO: fix the standalone test for remove */ |
| 111 |
REMOVE(1); |
| 112 |
return MUNIT_OK; |
| 113 |
} |
| 114 |
|
| 115 |
struct id_last_applied |
| 116 |
{ |
| 117 |
struct fixture *f; |
| 118 |
int id; |
| 119 |
raft_index last_applied; |
| 120 |
}; |
| 121 |
|
| 122 |
static bool last_applied_cond(struct id_last_applied arg) |
| 123 |
{ |
| 124 |
return arg.f->servers[arg.id].cowsql->raft.last_applied >= |
| 125 |
arg.last_applied; |
| 126 |
} |
| 127 |
|
| 128 |
TEST(membership, transfer, setUp, tearDown, 0, NULL) |
| 129 |
{ |
| 130 |
struct fixture *f = data; |
| 131 |
unsigned id = 2; |
| 132 |
const char *address = "@2"; |
| 133 |
uint32_t stmt_id; |
| 134 |
uint64_t last_insert_id; |
| 135 |
uint64_t rows_affected; |
| 136 |
raft_index last_applied; |
| 137 |
struct client_proto c_transfer; /* Client used for transfer requests */ |
| 138 |
struct id_last_applied await_arg; |
| 139 |
|
| 140 |
HANDSHAKE; |
| 141 |
ADD(id, address); |
| 142 |
ASSIGN(id, COWSQL_VOTER); |
| 143 |
OPEN; |
| 144 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 145 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 146 |
PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id); |
| 147 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 148 |
|
| 149 |
/* Transfer leadership and wait until first leader has applied a new |
| 150 |
* entry replicated from the new leader. */ |
| 151 |
test_server_client_connect(&f->servers[0], &c_transfer); |
| 152 |
HANDSHAKE_C(&c_transfer); |
| 153 |
TRANSFER(2, &c_transfer); |
| 154 |
test_server_client_close(&f->servers[0], &c_transfer); |
| 155 |
last_applied = f->servers[0].cowsql->raft.last_applied; |
| 156 |
|
| 157 |
SELECT(2); |
| 158 |
HANDSHAKE; |
| 159 |
OPEN; |
| 160 |
PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id); |
| 161 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 162 |
|
| 163 |
await_arg.f = f; |
| 164 |
await_arg.id = 0; |
| 165 |
await_arg.last_applied = last_applied + 1; |
| 166 |
AWAIT_TRUE(last_applied_cond, await_arg, 2); |
| 167 |
|
| 168 |
return MUNIT_OK; |
| 169 |
} |
| 170 |
|
| 171 |
/* Transfer leadership away from a member that has a pending transaction */ |
| 172 |
TEST(membership, transferPendingTransaction, setUp, tearDown, 0, NULL) |
| 173 |
{ |
| 174 |
struct fixture *f = data; |
| 175 |
unsigned id = 2; |
| 176 |
const char *address = "@2"; |
| 177 |
uint32_t stmt_id; |
| 178 |
uint64_t last_insert_id; |
| 179 |
uint64_t rows_affected; |
| 180 |
raft_index last_applied; |
| 181 |
struct client_proto c_transfer; /* Client used for transfer requests */ |
| 182 |
struct id_last_applied await_arg; |
| 183 |
|
| 184 |
HANDSHAKE; |
| 185 |
ADD(id, address); |
| 186 |
ASSIGN(id, COWSQL_VOTER); |
| 187 |
OPEN; |
| 188 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 189 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 190 |
PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id); |
| 191 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 192 |
|
| 193 |
/* Pending transaction */ |
| 194 |
PREPARE("BEGIN", &stmt_id); |
| 195 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 196 |
PREPARE("SELECT * FROM test", &stmt_id); |
| 197 |
QUERY(stmt_id, &f->rows); |
| 198 |
clientCloseRows(&f->rows); |
| 199 |
|
| 200 |
/* Transfer leadership and wait until first leader has applied a new |
| 201 |
* entry replicated from the new leader. */ |
| 202 |
test_server_client_connect(&f->servers[0], &c_transfer); |
| 203 |
HANDSHAKE_C(&c_transfer); |
| 204 |
TRANSFER(2, &c_transfer); |
| 205 |
test_server_client_close(&f->servers[0], &c_transfer); |
| 206 |
last_applied = f->servers[0].cowsql->raft.last_applied; |
| 207 |
|
| 208 |
SELECT(2); |
| 209 |
HANDSHAKE; |
| 210 |
OPEN; |
| 211 |
PREPARE("INSERT INTO test(n) VALUES(2)", &stmt_id); |
| 212 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 213 |
|
| 214 |
await_arg.f = f; |
| 215 |
await_arg.id = 0; |
| 216 |
await_arg.last_applied = last_applied + 1; |
| 217 |
AWAIT_TRUE(last_applied_cond, await_arg, 2); |
| 218 |
|
| 219 |
return MUNIT_OK; |
| 220 |
} |
| 221 |
|
| 222 |
struct fixture_id |
| 223 |
{ |
| 224 |
struct fixture *f; |
| 225 |
int id; |
| 226 |
}; |
| 227 |
|
| 228 |
static bool transfer_started_cond(struct fixture_id arg) |
| 229 |
{ |
| 230 |
return arg.f->servers[arg.id].cowsql->raft.transfer != NULL; |
| 231 |
} |
| 232 |
|
| 233 |
/* Transfer leadership away from a member and immediately try to EXEC a |
| 234 |
* prepared SQL statement that needs a barrier */ |
| 235 |
TEST(membership, transferAndSqlExecWithBarrier, setUp, tearDown, 0, NULL) |
| 236 |
{ |
| 237 |
int rv; |
| 238 |
struct fixture *f = data; |
| 239 |
unsigned id = 2; |
| 240 |
const char *address = "@2"; |
| 241 |
uint32_t stmt_id; |
| 242 |
uint64_t last_insert_id; |
| 243 |
uint64_t rows_affected; |
| 244 |
struct client_proto c_transfer; /* Client used for transfer requests */ |
| 245 |
struct fixture_id arg; |
| 246 |
|
| 247 |
HANDSHAKE; |
| 248 |
ADD(id, address); |
| 249 |
ASSIGN(id, COWSQL_VOTER); |
| 250 |
OPEN; |
| 251 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 252 |
|
| 253 |
/* Iniate transfer of leadership. This will cause a raft_barrier |
| 254 |
* failure while the node is technically still the leader, so the |
| 255 |
* gateway functionality that checks for leadership still succeeds. */ |
| 256 |
test_server_client_connect(&f->servers[0], &c_transfer); |
| 257 |
HANDSHAKE_C(&c_transfer); |
| 258 |
rv = clientSendTransfer(&c_transfer, 2, NULL); |
| 259 |
munit_assert_int(rv, ==, 0); |
| 260 |
|
| 261 |
/* Wait until transfer is started by raft so the barrier can fail. */ |
| 262 |
arg.f = f; |
| 263 |
arg.id = 0; |
| 264 |
AWAIT_TRUE(transfer_started_cond, arg, 2); |
| 265 |
|
| 266 |
/* Force a barrier. |
| 267 |
* TODO this is hacky, but I can't seem to hit the codepath otherwise */ |
| 268 |
f->servers[0].cowsql->raft.last_applied = 0; |
| 269 |
|
| 270 |
rv = clientSendExec(f->client, stmt_id, NULL, 0, NULL); |
| 271 |
munit_assert_int(rv, ==, 0); |
| 272 |
rv = clientRecvResult(f->client, &last_insert_id, &rows_affected, NULL); |
| 273 |
munit_assert_int(rv, ==, COWSQL_CLIENT_PROTO_ERROR); |
| 274 |
|
| 275 |
test_server_client_close(&f->servers[1], &c_transfer); |
| 276 |
return MUNIT_OK; |
| 277 |
} |
| 278 |
|
| 279 |
/* Transfer leadership back and forth from a member that has a pending |
| 280 |
* transaction */ |
| 281 |
TEST(membership, transferTwicePendingTransaction, setUp, tearDown, 0, NULL) |
| 282 |
{ |
| 283 |
struct fixture *f = data; |
| 284 |
unsigned id = 2; |
| 285 |
const char *address = "@2"; |
| 286 |
uint32_t stmt_id; |
| 287 |
uint64_t last_insert_id; |
| 288 |
uint64_t rows_affected; |
| 289 |
raft_index last_applied; |
| 290 |
struct client_proto c_transfer; /* Client used for transfer requests */ |
| 291 |
struct id_last_applied await_arg; |
| 292 |
|
| 293 |
HANDSHAKE; |
| 294 |
ADD(id, address); |
| 295 |
ASSIGN(id, COWSQL_VOTER); |
| 296 |
OPEN; |
| 297 |
PREPARE("CREATE TABLE test (n INT)", &stmt_id); |
| 298 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 299 |
PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id); |
| 300 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 301 |
|
| 302 |
/* Pending transaction */ |
| 303 |
PREPARE("BEGIN", &stmt_id); |
| 304 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 305 |
PREPARE("SELECT * FROM test", &stmt_id); |
| 306 |
QUERY(stmt_id, &f->rows); |
| 307 |
clientCloseRows(&f->rows); |
| 308 |
|
| 309 |
/* Transfer leadership and wait until first leader has applied a new |
| 310 |
* entry replicated from the new leader. */ |
| 311 |
test_server_client_connect(&f->servers[0], &c_transfer); |
| 312 |
HANDSHAKE_C(&c_transfer); |
| 313 |
TRANSFER(2, &c_transfer); |
| 314 |
test_server_client_close(&f->servers[0], &c_transfer); |
| 315 |
last_applied = f->servers[0].cowsql->raft.last_applied; |
| 316 |
|
| 317 |
SELECT(2); |
| 318 |
HANDSHAKE; |
| 319 |
OPEN; |
| 320 |
PREPARE("INSERT INTO test(n) VALUES(2)", &stmt_id); |
| 321 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 322 |
|
| 323 |
await_arg.f = f; |
| 324 |
await_arg.id = 0; |
| 325 |
await_arg.last_applied = last_applied + 1; |
| 326 |
AWAIT_TRUE(last_applied_cond, await_arg, 2); |
| 327 |
|
| 328 |
/* Transfer leadership back to original node, reconnect the client and |
| 329 |
* ensure queries can be executed. */ |
| 330 |
test_server_client_connect(&f->servers[1], &c_transfer); |
| 331 |
HANDSHAKE_C(&c_transfer); |
| 332 |
TRANSFER(1, &c_transfer); |
| 333 |
test_server_client_close(&f->servers[1], &c_transfer); |
| 334 |
|
| 335 |
last_applied = f->servers[1].cowsql->raft.last_applied; |
| 336 |
test_server_client_reconnect(&f->servers[0], &f->servers[0].client); |
| 337 |
SELECT(1); |
| 338 |
HANDSHAKE; |
| 339 |
OPEN; |
| 340 |
PREPARE("INSERT INTO test(n) VALUES(3)", &stmt_id); |
| 341 |
EXEC(stmt_id, &last_insert_id, &rows_affected); |
| 342 |
|
| 343 |
await_arg.id = 1; |
| 344 |
AWAIT_TRUE(last_applied_cond, await_arg, 2); |
| 345 |
|
| 346 |
return MUNIT_OK; |
| 347 |
} |
| 348 |
|
| 349 |
#endif /* not FLAKY */ |