-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
581 lines (505 loc) · 13.9 KB
/
main.c
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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <uv.h>
#include "sm.h"
// ============================================================================
// State machines and protocol.
// ============================================================================
#define assert_disk_io_never_fails(p) assert((p))
#define assert_timer_never_fails_also(p) assert((p))
enum msg_type {
MSG_CHUNK_SIZE = 4096,
MSG_RESTARTED = 0x42,
MSG_RESTARTED_REPLY,
MSG_BLOB,
MSG_BLOB_REPLY,
};
struct msg_base {
int type;
uint64_t sm_id;
uint32_t sm_pid;
};
struct msg_restarted {
struct msg_base b;
uint32_t chunks_total;
};
struct msg_restarted_reply {
struct msg_base b;
uint32_t restarted; /* boolean flag */
};
struct msg_blob {
struct msg_base b;
uint32_t chunk_index;
uint32_t chunk_size;
char blob[MSG_CHUNK_SIZE];
};
struct msg_blob_reply {
struct msg_base b;
uint32_t known_index;
int result;
};
enum role {
R_LEADER,
R_FOLLOWER,
R_NONE,
};
struct party {
uv_udp_send_t sender;
uv_timer_t timeout;
uv_udp_t server;
enum role role;
struct sm rpc_sm;
struct sm sm;
uint32_t chunk_index;
uint32_t chunks_total;
uint32_t file_size;
int fd;
const char *fd_name;
const char *dst_ipaddr;
int dst_port;
};
enum trigger {
T_TIMEOUT,
T_MSGSENT,
T_MSGRECV,
T_AGAIN,
T_NR,
};
enum leader_sm_states {
L_RESTARTED_LOOP,
L_RESTARTED_SENT,
L_RESTARTED_REPLIED,
L_BLOB_LOOP,
L_BLOB_SENT,
L_BLOB_REPLIED,
L_NR,
};
static const struct sm_conf leader_sm_conf[L_NR] = {
[L_RESTARTED_LOOP] = {
.flags = SM_INITIAL | SM_FINAL,
.name = "rest-loop",
.allowed = BITS(L_RESTARTED_LOOP)
| BITS(L_RESTARTED_SENT),
},
[L_RESTARTED_SENT] = {
.name = "rest-sent",
.allowed = BITS(L_RESTARTED_LOOP)
| BITS(L_RESTARTED_REPLIED),
},
[L_RESTARTED_REPLIED] = {
.name = "rest-replied",
.allowed = BITS(L_BLOB_LOOP),
},
[L_BLOB_LOOP] = {
.flags = SM_INITIAL | SM_FINAL,
.name = "blob-loop",
.allowed = BITS(L_BLOB_LOOP)
| BITS(L_BLOB_SENT),
},
[L_BLOB_SENT] = {
.name = "blob-sent",
.allowed = BITS(L_BLOB_LOOP)
| BITS(L_BLOB_REPLIED),
},
[L_BLOB_REPLIED] = {
.name = "blob-replied",
.allowed = BITS(L_RESTARTED_LOOP)
| BITS(L_BLOB_LOOP),
},
};
enum rpc_sm_states {
R_INIT,
R_SENT,
R_REPLIED,
R_TIMEOUT,
R_RECV,
R_RECV_SENT,
R_FAILED,
R_NR,
};
static const struct sm_conf rpc_sm_conf[R_NR] = {
[R_INIT] = {
.name = "init",
.flags = SM_INITIAL | SM_FINAL,
.allowed = BITS(R_FAILED)
| BITS(R_SENT)
| BITS(R_RECV),
},
[R_FAILED] = {
.name = "failed",
.flags = SM_FINAL,
},
[R_SENT] = {
.name = "sent",
.allowed = BITS(R_REPLIED)
| BITS(R_TIMEOUT)
| BITS(R_FAILED),
},
[R_REPLIED] = {
.name = "replied",
.flags = SM_FINAL,
},
[R_TIMEOUT] = {
.name = "timeout",
.flags = SM_FINAL,
},
[R_RECV] = {
.name = "recv",
.allowed = BITS(R_RECV_SENT)
| BITS(R_FAILED),
},
[R_RECV_SENT] = {
.name = "recv-sent",
.flags = SM_FINAL,
},
};
static void sent_cb(uv_udp_send_t* req, int status);
static void on_timeout(uv_timer_t *timeout);
static void on_send(uv_udp_send_t *req, int status);
static void *alloc(size_t size)
{
void *ret = malloc(size);
/* Assume allocation never fails */
assert(ret != NULL);
return ret;
}
static bool rpc_sm_invariant(const struct sm *sm, int prev_state)
{
return true;
}
static bool leader_sm_invariant(const struct sm *sm, int prev_state)
{
return true;
}
static void leader_file_open(struct party *leader)
{
off_t size;
/* NOTE: For our experiments, assume disk IO never fails. */
leader->fd = open(leader->fd_name, O_RDONLY);
assert_disk_io_never_fails(leader->fd > 0);
size = lseek(leader->fd, 0, SEEK_END);
assert_disk_io_never_fails(size > 0);
leader->file_size = size;
leader->chunks_total = size == 0 ? 0 : 1 + size / MSG_CHUNK_SIZE;
fprintf(stderr, "size=%ld total=%d\n", size, leader->chunks_total);
size = lseek(leader->fd, 0, SEEK_SET);
assert_disk_io_never_fails(size == 0);
}
static void leader_file_read_chunk(struct party *leader, struct msg_blob *blob)
{
ssize_t rc;
uint32_t remaining;
blob->chunk_index = leader->chunk_index;
remaining = leader->file_size - blob->chunk_index * MSG_CHUNK_SIZE;
blob->chunk_size = MIN(MSG_CHUNK_SIZE, remaining);
/* NOTE: For our experiments, assume disk IO never fails. */
rc = read(leader->fd, blob->blob, blob->chunk_size);
assert_disk_io_never_fails(rc >= 0);
}
static int rpc_tick(struct party *leader)
{
struct sockaddr_in saddr;
struct sm *rpc_sm = &leader->rpc_sm;
struct sm *sm = &leader->sm;
uv_buf_t buf;
int rc;
switch(sm_state(sm)) {
case L_RESTARTED_LOOP: {
struct msg_restarted *message;
sm_init(rpc_sm, rpc_sm_invariant, NULL, rpc_sm_conf, R_INIT);
sm_to_sm_obs(&leader->sm, rpc_sm);
message = alloc(sizeof(*message));
buf = uv_buf_init((char *) message, sizeof *message);
message->b.type = MSG_RESTARTED;
message->b.sm_id = rpc_sm->id;
message->b.sm_pid = sm_pid();
message->chunks_total = leader->chunks_total;
uv_req_set_data((uv_req_t *) &leader->sender, message);
sm_move(rpc_sm, R_SENT);
break;
}
case L_BLOB_LOOP: {
struct msg_blob *message;
sm_init(rpc_sm, rpc_sm_invariant, NULL, rpc_sm_conf, R_INIT);
sm_to_sm_obs(&leader->sm, rpc_sm);
message = alloc(sizeof(*message));
buf = uv_buf_init((char *) message, sizeof *message);
message->b.type = MSG_BLOB;
message->b.sm_id = rpc_sm->id;
message->b.sm_pid = sm_pid();
leader_file_read_chunk(leader, message);
uv_req_set_data((uv_req_t *) &leader->sender, message);
sm_move(rpc_sm, R_SENT);
sm_attr_obs_d(rpc_sm, "chunk_index", leader->chunk_index);
sm_attr_obs_d(rpc_sm, "chunks_total", leader->chunks_total);
sm_attr_obs_d(rpc_sm, "file_size", leader->file_size);
sm_attr_obs_d(rpc_sm, "chunk_size", message->chunk_size);
break;
}
case L_RESTARTED_REPLIED:
case L_BLOB_REPLIED:
rc = uv_timer_stop(&leader->timeout);
assert(rc == 0);
sm_move(rpc_sm, R_REPLIED);
sm_fini(rpc_sm);
return 0;
case L_RESTARTED_SENT:
case L_BLOB_SENT:
default:
IMPOSSIBLE("Unexpected request");
}
rc = uv_ip4_addr(leader->dst_ipaddr, leader->dst_port, &saddr);
assert(rc == 0);
rc = uv_timer_start(&leader->timeout, on_timeout, 1000, 0);
assert_timer_never_fails_also(rc == 0);
return uv_udp_send(&leader->sender, &leader->server, &buf, 1,
(const struct sockaddr*) &saddr, sent_cb);
}
static void leader_tick(struct party *leader, enum trigger trigger,
const uv_buf_t *message, int status)
{
struct sm *sm = &leader->sm;
int rc;
again:
switch(sm_state(sm)) {
case L_RESTARTED_LOOP:
if (trigger != T_TIMEOUT)
break;
leader_file_open(leader);
rc = rpc_tick(leader);
if (rc == 0) {
sm_move(sm, L_RESTARTED_SENT);
return;
}
break;
case L_RESTARTED_SENT:
if (trigger == T_TIMEOUT) {
sm_move(sm, L_RESTARTED_LOOP);
sm_move(&leader->rpc_sm, R_TIMEOUT);
goto again;
} else if (trigger == T_MSGSENT && status != 0) {
break;
} else if (trigger == T_MSGRECV) {
sm_move(sm, L_RESTARTED_REPLIED);
goto again;
}
break;
case L_RESTARTED_REPLIED: {
rc = rpc_tick(leader);
/* TODO: sender may fail, this needs to be handled. */
assert(rc == 0);
struct msg_restarted_reply *msg;
msg = (struct msg_restarted_reply *) message->base;
/* TODO: we may receive duplicates of unknown type,
* therefore the code around assert needs to be
* reworked out. */
assert(msg->b.type == MSG_RESTARTED_REPLY);
if (msg->restarted) {
sm_move(sm, L_BLOB_LOOP);
trigger = T_AGAIN;
goto again;
}
sm_move(sm, L_RESTARTED_LOOP);
break;
}
case L_BLOB_LOOP:
rc = rpc_tick(leader);
if (rc == 0) {
sm_move(sm, L_BLOB_SENT);
return;
} else {
/* TODO: retry sending message after certain timeout */
}
break;
case L_BLOB_SENT:
if (trigger == T_TIMEOUT) {
sm_move(sm, L_BLOB_LOOP);
goto again;
} else if (trigger == T_MSGSENT && status != 0) {
break;
} else if (trigger == T_MSGRECV) {
sm_move(sm, L_BLOB_REPLIED);
goto again;
}
break;
case L_BLOB_REPLIED: {
rc = rpc_tick(leader);
assert(rc == 0);
struct msg_blob_reply *msg;
msg = (struct msg_blob_reply *) message->base;
assert(msg->b.type == MSG_BLOB_REPLY);
leader->chunk_index++; /* TODO: assume all is good */
if (leader->chunk_index < leader->chunks_total) {
sm_move(sm, L_BLOB_LOOP);
goto again;
}
break;
}
default:
IMPOSSIBLE("Unexpected request");
}
}
static void follower_tick(struct party *follower, const struct sockaddr *addr,
ssize_t nread, const uv_buf_t *message)
{
int rc;
uv_buf_t buf;
struct msg_base *req;
req = (struct msg_base *) message->base;
fprintf(stderr, "recv type=%x\n", req->type);
sm_init(&follower->rpc_sm, rpc_sm_invariant, NULL, rpc_sm_conf, R_INIT);
sm_move(&follower->rpc_sm, R_RECV);
sm_from_to_obs("rpc", req->sm_pid, req->sm_id,
"rpc", sm_pid(), follower->rpc_sm.id);
switch(req->type) {
case MSG_RESTARTED: {
struct msg_restarted_reply *reply;
reply = alloc(sizeof(*reply));
uv_req_set_data((uv_req_t *) &follower->sender, reply);
reply->b.type = MSG_RESTARTED_REPLY;
reply->restarted = 1;
buf = uv_buf_init((char *)reply, sizeof(*reply));
break;
}
case MSG_BLOB: {
struct msg_blob_reply *reply;
struct msg_blob *blob;
off_t size;
int fd;
/* TODO: check incoming message */
blob = (struct msg_blob *) message->base;
fd = open(follower->fd_name, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
assert(fd > 0);
size = lseek(fd, blob->chunk_index * MSG_CHUNK_SIZE, SEEK_SET);
assert(size >= 0);
rc = write(fd, blob->blob, blob->chunk_size);
assert(rc > 0);
close(fd);
/* Prepare message */
reply = alloc(sizeof(*reply));
uv_req_set_data((uv_req_t *) &follower->sender, reply);
reply->b.type = MSG_BLOB_REPLY;
reply->result = 0;
reply->known_index = blob->chunk_index;
buf = uv_buf_init((char *)reply, sizeof(*reply));
break;
}
default:
IMPOSSIBLE("Unexpected request");
}
rc = uv_udp_send(&follower->sender, &follower->server,
&buf, 1, addr, on_send);
/* TODO: In case of the link layer failure, wait 100ms and retry. */
assert(rc == 0);
sm_move(&follower->rpc_sm, R_RECV_SENT);
sm_fini(&follower->rpc_sm);
}
// ============================================================================
// libuv-related hooks, callbacks and main.
// ============================================================================
static void pool_alloc(uv_handle_t *handle, size_t _suggested_sz, uv_buf_t *buf)
{
static char slab[16 * 64 * 1024];
buf->base = slab;
buf->len = sizeof(slab);
}
static void on_send(uv_udp_send_t *req, int status)
{
assert(req != NULL);
assert(status == 0);
free(uv_req_get_data((uv_req_t *) req));
}
static void on_recv(uv_udp_t *handle, ssize_t nread, const uv_buf_t *rcvbuf,
const struct sockaddr *addr, unsigned flags)
{
struct party *p = container_of(handle, struct party, server);
if (nread == 0)
return;
if (p->role == R_LEADER) {
leader_tick(p, T_MSGRECV, rcvbuf, 0);
return;
} else if (p->role == R_FOLLOWER) {
assert(nread > 0);
assert(addr->sa_family == AF_INET);
follower_tick(p, addr, nread, rcvbuf);
}
}
static int udp4_start(uv_udp_t *server, const char *src_addr, int port)
{
struct sockaddr_in addr;
int rc;
rc = uv_ip4_addr(src_addr, port, &addr);
assert(rc == 0);
rc = uv_udp_init(uv_default_loop(), server);
if (rc != 0) {
fprintf(stderr, "uv_udp_init: %s\n", uv_strerror(rc));
return 1;
}
rc = uv_udp_bind(server, (const struct sockaddr *) &addr, 0);
if (rc != 0) {
fprintf(stderr, "uv_udp_bind: %s\n", uv_strerror(rc));
return 1;
}
rc = uv_udp_recv_start(server, pool_alloc, on_recv);
if (rc != 0) {
fprintf(stderr, "uv_udp_recv_start: %s\n", uv_strerror(rc));
return 1;
}
return 0;
}
static void sent_cb(uv_udp_send_t* req, int status)
{
assert(req != 0);
struct party *p = container_of(req, struct party, sender);
leader_tick(p, T_MSGSENT, NULL, status);
free(uv_req_get_data((uv_req_t *) req));
}
static void on_timeout(uv_timer_t *timeout)
{
struct party *p = container_of(timeout, struct party, timeout);
leader_tick(p, T_TIMEOUT, NULL, 0);
}
int main(int argc, char **argv)
{
int rc;
int port;
const char *src_addr;
struct party party = {0};
if (argc < 5)
return EINVAL;
if (strcmp("leader", argv[1]) == 0) {
party.role = R_LEADER;
party.fd_name = argv[4];
src_addr = argv[2];
party.dst_ipaddr = argv[3];
party.dst_port = 8080;
port = 8081;
} else if (strcmp("follower", argv[1]) == 0) {
party.role = R_FOLLOWER;
party.fd_name = argv[4];
src_addr = argv[2];
port = 8080;
} else
return EINVAL;
rc = uv_timer_init(uv_default_loop(), &party.timeout);
if (rc != 0)
return rc;
if (party.role == R_LEADER) {
rc = uv_timer_start(&party.timeout, on_timeout, 0, 0);
assert(rc == 0);
party.sm = (struct sm) { .name = "leader" };
sm_init(&party.sm, leader_sm_invariant, NULL, leader_sm_conf,
L_RESTARTED_LOOP);
sm_attr_obs_d(&party.sm, "port", port);
}
party.rpc_sm = (struct sm) { .name = "rpc" };
rc = udp4_start(&party.server, src_addr, port);
if (rc != 0)
return rc;
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
return 0;
}