-
Notifications
You must be signed in to change notification settings - Fork 0
/
mserver.c
861 lines (705 loc) · 23.8 KB
/
mserver.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
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
// Copyright (C) 2016, 2017 Alexey Khrabrov, Bogdan Simion
//
// Distributed under the terms of the GNU General Public License.
//
// This file is part of Assignment 3, CSC469, Fall 2017.
//
// This is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this file. If not, see <http://www.gnu.org/licenses/>.
// The metadata server implementation
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "defs.h"
#include "util.h"
// Program arguments
// Ports for listening to incoming connections from clients and servers
static uint16_t clients_port = 0;
static uint16_t servers_port = 0;
// Server configuration file name
static char cfg_file_name[PATH_MAX] = "";
// Timeout for detecting server failures; you might want to adjust this default value
static const int default_server_timeout = 2;
static int server_timeout = 0;
// Log file name
static char log_file_name[PATH_MAX] = "";
static void usage(char **argv)
{
printf("usage: %s -c <client port> -s <servers port> -C <config file> "
"[-t <timeout (seconds)> -l <log file>]\n", argv[0]);
printf("Default timeout is %d seconds\n", default_server_timeout);
printf("If the log file (-l) is not specified, log output is written to stdout\n");
}
// Returns false if the arguments are invalid
static bool parse_args(int argc, char **argv)
{
char option;
while ((option = getopt(argc, argv, "c:s:C:l:t:")) != -1) {
switch(option) {
case 'c': clients_port = atoi(optarg); break;
case 's': servers_port = atoi(optarg); break;
case 'l': strncpy(log_file_name, optarg, PATH_MAX); break;
case 'C': strncpy(cfg_file_name, optarg, PATH_MAX); break;
case 't': server_timeout = atoi(optarg); break;
default:
fprintf(stderr, "Invalid option: -%c\n", option);
return false;
}
}
server_timeout = (server_timeout != 0) ? server_timeout : default_server_timeout;
return (clients_port != 0) && (servers_port != 0) && (cfg_file_name[0] != '\0');
}
// Current machine host name
static char mserver_host_name[HOST_NAME_MAX] = "";
// Sockets for incoming connections from clients and servers
static int clients_fd = -1;
static int servers_fd = -1;
// Store socket fds for all connected clients, up to MAX_CLIENT_SESSIONS
#define MAX_CLIENT_SESSIONS 1000
static int client_fd_table[MAX_CLIENT_SESSIONS];
typedef enum {
A,
} server_status;
// Structure describing a key-value server state
typedef struct _server_node {
// Server host name, possibly prefixed by "user@" (for starting servers remotely via ssh)
char host_name[HOST_NAME_MAX];
// Servers/client/mserver port numbers
uint16_t sport;
uint16_t cport;
uint16_t mport;
// Server ID
int sid;
// Socket for receiving requests from the server
int socket_fd_in;
// Socket for sending requests to the server
int socket_fd_out;
// Server process PID (it is a child process of mserver)
pid_t pid;
time_t latest_alive;
// TODO: add fields for necessary additional server state information
// ...
} server_node;
// Total number of servers
static int num_servers = 0;
// Server state information
static server_node *server_nodes = NULL;
static struct {
// whether there are recovery going on right now
bool is_in;
// failed server id
int failed_sid;
// who is the primary of the failed server; will send replica again
int primary_sid;
// who is the secondary of the failed server; will send primary and act as temp primary
int secondary_sid;
bool updated_primary;
bool updated_secondary;
bool stop_write;
} recovery;
// Read the configuration file, fill in the server_nodes array
// Returns false if the configuration is invalid
static bool read_config_file()
{
FILE *cfg_file = fopen(cfg_file_name, "r");
if (cfg_file == NULL) {
perror(cfg_file_name);
return false;
}
bool result = false;
// The first line contains the number of servers
if (fscanf(cfg_file, "%d\n", &num_servers) < 1) {
goto end;
}
// Need at least 3 servers to avoid cross-replication
if (num_servers < 3) {
log_error("Invalid number of servers: %d\n", num_servers);
goto end;
}
if ((server_nodes = calloc(num_servers, sizeof(server_node))) == NULL) {
log_perror("calloc");
goto end;
}
for (int i = 0; i < num_servers; i++) {
server_node *node = &(server_nodes[i]);
// Format: <host_name> <clients port> <servers port> <mservers_port>
if ((fscanf(cfg_file, "%s %hu %hu %hu\n", node->host_name,
&(node->cport), &(node->sport), &(node->mport)) < 4) ||
((strcmp(node->host_name, "localhost") != 0) && (strchr(node->host_name, '@') == NULL)) ||
(node->cport == 0) || (node->sport == 0) || (node->mport == 0))
{
free(server_nodes);
server_nodes = NULL;
goto end;
}
node->sid = i;
node->socket_fd_in = -1;
node->socket_fd_out = -1;
node->pid = 0;
}
// Print server configuration
printf("Key-value servers configuration:\n");
for (int i = 0; i < num_servers; i++) {
server_node *node = &(server_nodes[i]);
printf("\thost: %s, client port: %d, server port: %d\n", node->host_name, node->cport, node->sport);
}
result = true;
end:
fclose(cfg_file);
return result;
}
static void cleanup();
static bool init_servers();
// Initialize and start the metadata server
static bool init_mserver()
{
for (int i = 0; i < MAX_CLIENT_SESSIONS; i++) {
client_fd_table[i] = -1;
}
recovery.is_in = false;
recovery.failed_sid = -1;
recovery.primary_sid = -1;
recovery.secondary_sid = -1;
recovery.updated_primary = false;
recovery.updated_secondary = false;
recovery.stop_write = false;
// Get the host name that server is running on
if (get_local_host_name(mserver_host_name, sizeof(mserver_host_name)) < 0) {
return false;
}
log_write("%s Metadata server starts on host: %s\n", current_time_str(), mserver_host_name);
// Create sockets for incoming connections from servers
if ((servers_fd = create_server(servers_port, num_servers + 1, NULL)) < 0) {
goto cleanup;
}
// Start key-value servers
if (!init_servers()) {
goto cleanup;
}
// Create sockets for incoming connections from clients
if ((clients_fd = create_server(clients_port, MAX_CLIENT_SESSIONS, NULL)) < 0) {
goto cleanup;
}
log_write("Metadata server initialized\n");
return true;
cleanup:
cleanup();
return false;
}
// Cleanup and release all the resources
static void cleanup()
{
close_safe(&clients_fd);
close_safe(&servers_fd);
// Close all client connections
for (int i = 0; i < MAX_CLIENT_SESSIONS; i++) {
close_safe(&(client_fd_table[i]));
}
if (server_nodes != NULL) {
for (int i = 0; i < num_servers; i++) {
server_node *node = &(server_nodes[i]);
if (node->socket_fd_out != -1 && recovery.failed_sid != i) {
// Request server shutdown
server_ctrl_request request = {0};
request.hdr.type = MSG_SERVER_CTRL_REQ;
request.type = SHUTDOWN;
send_msg(node->socket_fd_out, &request, sizeof(request));
}
// Close the connections
close_safe(&(server_nodes[i].socket_fd_out));
close_safe(&(server_nodes[i].socket_fd_in));
// Wait with timeout (or kill if timeout expires) for the server process
if (server_nodes[i].pid > 0) {
kill_safe(&(server_nodes[i].pid), 5);
}
}
free(server_nodes);
server_nodes = NULL;
}
}
static const int max_cmd_length = 32;
static const char *remote_path = "csc469_a3/";
// Generate a command to start a key-value server (see server.c for arguments description)
static char **get_spawn_cmd(int sid)
{
char **cmd = calloc(max_cmd_length, sizeof(char*));
assert(cmd != NULL);
server_node *node = &(server_nodes[sid]);
int i = -1;
if (strcmp(node->host_name, "localhost") != 0) {
// Remote server, host_name format is "user@host"
assert(strchr(node->host_name, '@') != NULL);
// Use ssh to run the command on a remote machine
cmd[++i] = strdup("ssh");
cmd[++i] = strdup("-o");
cmd[++i] = strdup("StrictHostKeyChecking=no");
cmd[++i] = strdup(node->host_name);
cmd[++i] = strdup("cd");
cmd[++i] = strdup(remote_path);
cmd[++i] = strdup("&&");
}
cmd[++i] = strdup("./server\0");
cmd[++i] = strdup("-h");
cmd[++i] = strdup(mserver_host_name);
cmd[++i] = strdup("-m");
cmd[++i] = malloc(8); sprintf(cmd[i], "%hu", servers_port);
cmd[++i] = strdup("-c");
cmd[++i] = malloc(8); sprintf(cmd[i], "%hu", node->cport);
cmd[++i] = strdup("-s");
cmd[++i] = malloc(8); sprintf(cmd[i], "%hu", node->sport);
cmd[++i] = strdup("-M");
cmd[++i] = malloc(8); sprintf(cmd[i], "%hu", node->mport);
cmd[++i] = strdup("-S");
cmd[++i] = malloc(8); sprintf(cmd[i], "%d", sid);
cmd[++i] = strdup("-n");
cmd[++i] = malloc(8); sprintf(cmd[i], "%d", num_servers);
cmd[++i] = strdup("-l");
cmd[++i] = malloc(20); sprintf(cmd[i], "server_%d.log", sid);
cmd[++i] = NULL;
assert(i < max_cmd_length);
return cmd;
}
static void free_cmd(char **cmd)
{
assert(cmd != NULL);
for (int i = 0; i < max_cmd_length; i++) {
if (cmd[i] != NULL) {
free(cmd[i]);
}
}
free(cmd);
}
// Start a key-value server with given id
static int spawn_server(int sid)
{
server_node *node = &(server_nodes[sid]);
close_safe(&(node->socket_fd_in));
close_safe(&(node->socket_fd_out));
kill_safe(&(node->pid), 0);
// Spawn the server as a process on either the local machine or a remote machine (using ssh)
pid_t pid = fork();
switch (pid) {
case -1:
log_perror("fork");
return -1;
case 0: {
char **cmd = get_spawn_cmd(sid);
execvp(cmd[0], cmd);
// If exec returns, some error happened
perror(cmd[0]);
free_cmd(cmd);
_exit(1);
}
default:
node->pid = pid;
break;
}
// Wait for the server to connect
int fd_idx = accept_connection(servers_fd, &(node->socket_fd_in), 1);
if (fd_idx < 0) {
// Something went wrong, kill the server process
kill_safe(&(node->pid), 1);
return -1;
}
assert(fd_idx == 0);
// Extract the host name from "user@host"
char *at = strchr(node->host_name, '@');
char *host = (at == NULL) ? node->host_name : (at + 1);
// Connect to the server
if ((node->socket_fd_out = connect_to_server(host, node->mport)) < 0) {
// Something went wrong, kill the server process
close_safe(&(node->socket_fd_in));
kill_safe(&(node->pid), 1);
return -1;
}
return 0;
}
// Send the initial SET-SECONDARY message to a newly created server; returns true on success
static bool send_set_secondary(int sid)
{
char buffer[MAX_MSG_LEN] = {0};
server_ctrl_request *request = (server_ctrl_request*)buffer;
// Fill in the request parameters
request->hdr.type = MSG_SERVER_CTRL_REQ;
request->type = SET_SECONDARY;
server_node *secondary_node = &(server_nodes[secondary_server_id(sid, num_servers)]);
request->port = secondary_node->sport;
// Extract the host name from "user@host"
char *at = strchr(secondary_node->host_name, '@');
char *host = (at == NULL) ? secondary_node->host_name : (at + 1);
int host_name_len = strlen(host) + 1;
strncpy(request->host_name, host, host_name_len);
// Send the request and receive the response
server_ctrl_response response = {0};
if (!send_msg(server_nodes[sid].socket_fd_out, request, sizeof(*request) + host_name_len) ||
!recv_msg(server_nodes[sid].socket_fd_out, &response, sizeof(response), MSG_SERVER_CTRL_RESP))
{
return false;
}
if (response.status != CTRLREQ_SUCCESS) {
log_error("Server %d failed SET-SECONDARY\n", sid);
return false;
}
return true;
}
// Send the initial UPDATE_PRIMARY message to the secondary of the failed server; returns true on success
static bool send_update_primary(int sid)
{
char buffer[MAX_MSG_LEN] = {0};
server_ctrl_request *request = (server_ctrl_request*)buffer;
// Fill in the request parameters
request->hdr.type = MSG_SERVER_CTRL_REQ;
request->type = UPDATE_PRIMARY;
server_node *failed_node = &(server_nodes[recovery.failed_sid]);
request->port = failed_node->sport;
// Extract the host name from "user@host"
char *at = strchr(failed_node->host_name, '@');
char *host = (at == NULL) ? failed_node->host_name : (at + 1);
int host_name_len = strlen(host) + 1;
strncpy(request->host_name, host, host_name_len);
// Send the request and receive the response
server_ctrl_response response = {0};
if (!send_msg(server_nodes[sid].socket_fd_out, request, sizeof(*request) + host_name_len) ||
!recv_msg(server_nodes[sid].socket_fd_out, &response, sizeof(response), MSG_SERVER_CTRL_RESP))
{
return false;
}
if (response.status != CTRLREQ_SUCCESS) {
log_error("Server %d failed UPDATE_PRIMARY\n", sid);
return false;
}
return true;
}
// Send the initial UPDATE_SECONDARY message to the primary of the failed server; returns true on success
static bool send_update_secondary(int sid)
{
char buffer[MAX_MSG_LEN] = {0};
server_ctrl_request *request = (server_ctrl_request*)buffer;
// Fill in the request parameters
request->hdr.type = MSG_SERVER_CTRL_REQ;
request->type = UPDATE_SECONDARY;
server_node *failed_node = &(server_nodes[recovery.failed_sid]);
request->port = failed_node->sport;
// Extract the host name from "user@host"
char *at = strchr(failed_node->host_name, '@');
char *host = (at == NULL) ? failed_node->host_name : (at + 1);
int host_name_len = strlen(host) + 1;
strncpy(request->host_name, host, host_name_len);
// Send the request and receive the response
server_ctrl_response response = {0};
if (!send_msg(server_nodes[sid].socket_fd_out, request, sizeof(*request) + host_name_len) ||
!recv_msg(server_nodes[sid].socket_fd_out, &response, sizeof(response), MSG_SERVER_CTRL_RESP))
{
return false;
}
if (response.status != CTRLREQ_SUCCESS) {
log_error("Server %d failed UPDATE_PRIMARY\n", sid);
return false;
}
return true;
}
// Send the initial SWITCH_PRIMARY message to the primary of the failed server; returns true on success
static bool send_switch_primary(int sid)
{
char buffer[MAX_MSG_LEN] = {0};
server_ctrl_request *request = (server_ctrl_request*)buffer;
// Fill in the request parameters
request->hdr.type = MSG_SERVER_CTRL_REQ;
request->type = SWITCH_PRIMARY;
// Send the request and receive the response
server_ctrl_response response = {0};
if (!send_msg(server_nodes[sid].socket_fd_out, request, sizeof(*request)) ||
!recv_msg(server_nodes[sid].socket_fd_out, &response, sizeof(response), MSG_SERVER_CTRL_RESP))
{
return false;
}
if (response.status != CTRLREQ_SUCCESS) {
log_error("Server %d failed SWITCH_PRIMARY\n", sid);
return false;
}
// TODO switch back the primary
recovery.is_in = false;
recovery.failed_sid = -1;
recovery.primary_sid = -1;
recovery.secondary_sid = -1;
recovery.updated_primary = false;
recovery.updated_secondary = false;
recovery.stop_write = false;
log_write("Primary switched, failure handle over\n");
log_write("----------------------------------------------------------------------------\n");
return true;
}
// Start all key-value servers
static bool init_servers()
{
// Spawn all the servers
for (int i = 0; i < num_servers; i++) {
if (spawn_server(i) < 0) {
return false;
}
}
// Let each server know the location of its secondary replica
for (int i = 0; i < num_servers; i++) {
if (!send_set_secondary(i)) {
return false;
}
}
return true;
}
// Connection will be closed after calling this function regardless of result
static void process_client_message(int fd)
{
log_write("%s Receiving a client message\n", current_time_str());
// Read and parse the message
locate_request request = {0};
if (!recv_msg(fd, &request, sizeof(request), MSG_LOCATE_REQ)) {
return;
}
// Determine which server is responsible for the requested key
int server_id = key_server_id(request.key, num_servers);
// TODO: redirect client requests to the secondary replica while the primary is being recovered
if (recovery.is_in && recovery.failed_sid == server_id) {
if (recovery.stop_write) {
// write stopped, dropped connection (TODO)
log_write("write stopped, dropped connection (TODO), for %d to %d\n", server_id, recovery.secondary_sid);
return;
} else {
log_write("relayed request for %d to %d\n", server_id, recovery.secondary_sid);
server_id = recovery.secondary_sid;
}
} else {
log_write("it's it self, handle \n");
}
// Fill in the response with the key-value server location information
char buffer[MAX_MSG_LEN] = {0};
locate_response *response = (locate_response*)buffer;
response->hdr.type = MSG_LOCATE_RESP;
response->port = server_nodes[server_id].cport;
// Extract the host name from "user@host"
char *at = strchr(server_nodes[server_id].host_name, '@');
char *host = (at == NULL) ? server_nodes[server_id].host_name : (at + 1);
int host_name_len = strlen(host) + 1;
strncpy(response->host_name, host, host_name_len);
// Reply to the client
send_msg(fd, response, sizeof(*response) + host_name_len);
}
// Returns false if the message was invalid (so the connection will be closed)
static bool process_server_message(int fd)
{
log_write("%s Receiving a server message\n", current_time_str());
char req_buffer[MAX_MSG_LEN] = {0};
if(!recv_msg(fd, req_buffer, MAX_MSG_LEN, MSG_MSERVER_CTRL_REQ)) {
log_write("Error server message, closing connection\n");
return false;
}
mserver_ctrl_request *request = (mserver_ctrl_request *)req_buffer;
switch(request->type) {
case HEARTBEAT:
server_nodes[request->server_id].latest_alive = time(NULL);
return true;
case UPDATED_PRIMARY:
log_write("received UPDATED_PRIMARY\n");
recovery.updated_primary = true;
if (recovery.updated_secondary) {
recovery.stop_write = true;
recovery.updated_primary = false;
recovery.updated_secondary = false;
send_switch_primary(recovery.secondary_sid);
}
return true;
case UPDATED_SECONDARY:
log_write("received UPDATED_SECONDARY\n");
recovery.updated_secondary = true;
if (recovery.updated_primary) {
recovery.stop_write = true;
recovery.updated_primary = false;
recovery.updated_secondary = false;
send_switch_primary(recovery.secondary_sid);
}
return true;
case UPDATE_PRIMARY_FAILED:
case UPDATE_SECONDARY_FAILED:
default:
log_write("Server message Invalid command\n");
}
return false;
}
static const int select_timeout_interval = 1;// seconds
static time_t hbeat_timer = 0;
// returns -1 if no failures occured; otherwise returns the failed server id.
int monitor_heartbeat(void) {
if (hbeat_timer == 0) {
hbeat_timer = time(NULL); // initialization
}
else if ((time(NULL) - hbeat_timer) >= default_server_timeout) {
for (int i = 0; i < num_servers; i++) {
if (server_nodes[i].latest_alive <= hbeat_timer) {
hbeat_timer = time(NULL); //reset
return i;
}
}
hbeat_timer = time(NULL); //reset
}
return -1;
}
// Returns false if stopped due to errors, true if shutdown was requested
static bool run_mserver_loop()
{
// Usual preparation stuff for select()
fd_set rset, allset;
FD_ZERO(&allset);
// End-of-file on stdin (e.g. Ctrl+D in a terminal) is used to request shutdown
FD_SET(fileno(stdin), &allset);
FD_SET(servers_fd, &allset);
FD_SET(clients_fd, &allset);
int max_server_fd = -1;
for (int i = 0; i < num_servers; i++) {
FD_SET(server_nodes[i].socket_fd_in, &allset);
max_server_fd = max(max_server_fd, server_nodes[i].socket_fd_in);
server_nodes[i].latest_alive = time(NULL);
}
int maxfd = max(clients_fd, servers_fd);
maxfd = max(maxfd, max_server_fd);
// Metadata server sits in an infinite loop waiting for incoming connections from clients
// and for incoming messages from already connected servers and clients
for (;;) {
rset = allset;
struct timeval time_out;
time_out.tv_sec = select_timeout_interval;
time_out.tv_usec = 0;
int num_ready_fds = select(maxfd + 1, &rset, NULL, NULL, &time_out);
if (num_ready_fds < 0) {
log_perror("select");
return false;
}
if (num_ready_fds <= 0 ) {
// Due to time out
continue;
}
// Stop if detected EOF on stdin
if (FD_ISSET(fileno(stdin), &rset)) {
char buffer[1024];
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
return true;
}
}
// Check for any messages from connected servers
for (int i = 0; i < num_servers; i++) {
server_node *node = &(server_nodes[i]);
if ((node->socket_fd_in != -1) && FD_ISSET(node->socket_fd_in, &rset)) {
if (!process_server_message(node->socket_fd_in)) {
// Received an invalid message, close the connection
log_error("Closing server %d connection\n", i);
FD_CLR(node->socket_fd_in, &allset);
close_safe(&(node->socket_fd_in));
}
if (--num_ready_fds <= 0) {
break;
}
}
}
// failure detection and recovery
int failure_detected = monitor_heartbeat();
if (failure_detected != -1) {
if (!recovery.is_in) {
// detected a new failure, start recovery process
recovery.is_in = true;
recovery.updated_primary = false;
recovery.updated_secondary = false;
recovery.stop_write = false;
recovery.failed_sid = failure_detected;
recovery.primary_sid = primary_server_id(failure_detected, num_servers);
recovery.secondary_sid = secondary_server_id(failure_detected, num_servers);
log_write("----------------------------------------------------------------------------\n");
log_error("New Server failure detected, server id %d, its primary %d, its secondary %d\n",
failure_detected, recovery.primary_sid, recovery.secondary_sid);
if (spawn_server(failure_detected) >= 0) {
// respawn server success, start select() on its fd
server_node *node = &(server_nodes[failure_detected]);
FD_SET(node->socket_fd_in, &allset);
if (!send_set_secondary(failure_detected)) {
log_error("Fatal: respawn failed: set_secondaryfailed, server %d failed, exit gracefully\n", recovery.failed_sid);
return false;
}
// you want to send the UPDATE_PRIMARY to the secondary of the failed server, vice versa
send_update_primary(recovery.secondary_sid);
send_update_secondary(recovery.primary_sid);
} else {
log_error("Fatal: respawn failed server %d failed, exit gracefully\n", recovery.failed_sid);
return false;
}
}
else if (recovery.failed_sid != failure_detected) {
// multiple failure detected
log_error("Fatal: multiple failure detected, new failure server %d, exit gracefully\n", failure_detected);
return false;
} else {
// failure is being handled currently, do nothing
}
}
if (num_ready_fds <= 0) {
continue;
}
// Incoming connection from a client
if (FD_ISSET(clients_fd, &rset)) {
int fd_idx = accept_connection(clients_fd, client_fd_table, MAX_CLIENT_SESSIONS);
if (fd_idx >= 0) {
FD_SET(client_fd_table[fd_idx], &allset);
maxfd = max(maxfd, client_fd_table[fd_idx]);
}
if (--num_ready_fds <= 0) {
continue;
}
}
// Check for any messages from connected clients
for (int i = 0; i < MAX_CLIENT_SESSIONS; i++) {
if ((client_fd_table[i] != -1) && FD_ISSET(client_fd_table[i], &rset)) {
process_client_message(client_fd_table[i]);
// Close connection after processing (semantics are "one connection per request")
FD_CLR(client_fd_table[i], &allset);
close_safe(&(client_fd_table[i]));
if (--num_ready_fds <= 0 ) {
break;
}
}
}
}
}
int main(int argc, char **argv)
{
signal(SIGPIPE, SIG_IGN);
if (!parse_args(argc, argv)) {
usage(argv);
return 1;
}
open_log(log_file_name);
if (!read_config_file()) {
log_error("Invalid configuraion file\n");
return 1;
}
if (!init_mserver()) {
return 1;
}
bool result = run_mserver_loop();
cleanup();
return result ? 0 : 1;
}