2 /* Copyright (c) 2010. The SimGrid Team.
3 * All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/asserts.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
13 "Messages specific for this msg example");
23 typedef struct finger {
26 } s_finger_t, *finger_t;
33 char* mailbox; // my usual mailbox name
34 s_finger_t fingers[NB_BITS]; // finger table (fingers[0] is my successor)
35 int pred_id; // predecessor id
36 char* pred_mailbox; // predecessor's mailbox name
37 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
38 msg_comm_t comm_receive; // current communication to receive
39 xbt_dynar_t comms; // current communications being sent
43 * Types of tasks exchanged between nodes.
47 TASK_FIND_SUCCESSOR_ANSWER,
49 TASK_GET_PREDECESSOR_ANSWER,
51 TASK_SUCCESSOR_LEAVING,
52 TASK_PREDECESSOR_LEAVING
56 * Data attached with the tasks sent and received
58 typedef struct task_data {
59 e_task_type_t type; // type of task
60 int request_id; // id paramater (used by some types of tasks)
61 int request_finger; // finger parameter (used by some types of tasks)
62 int answer_id; // answer (used by some types of tasks)
63 const char* answer_to; // mailbox to send an answer to
64 const char* issuer_host_name; // used for logging
65 } s_task_data_t, *task_data_t;
67 static int powers2[NB_BITS];
70 static void chord_initialize(void);
71 static int normalize(int id);
72 static int is_in_interval(int id, int start, int end);
73 static char* get_mailbox(int host_id);
74 static void print_finger_table(node_t node);
75 static void set_finger(node_t node, int finger_index, int id);
76 static void set_predecessor(node_t node, int predecessor_id);
79 static int node(int argc, char *argv[]);
80 static void handle_task(node_t node, m_task_t task);
83 static void create(node_t node);
84 static int join(node_t node, int known_id);
85 static void leave(node_t node);
86 static int find_successor(node_t node, int id);
87 static int remote_find_successor(node_t node, int ask_to_id, int id);
88 static int remote_get_predecessor(node_t node, int ask_to_id);
89 static int closest_preceding_node(node_t node, int id);
90 static void stabilize(node_t node);
91 static void notify(node_t node, int predecessor_candidate_id);
92 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
93 static void fix_fingers(node_t node);
94 static void check_predecessor(node_t node);
95 static void quit_notify(node_t node, int to);
98 * \brief Global initialization of the Chord simulation.
100 static void chord_initialize(void)
102 // compute the powers of 2 once for all
105 for (i = 0; i < NB_BITS; i++) {
112 * \brief Turns an id into an equivalent id in [0, NB_KEYS).
114 * \return the corresponding normalized id
116 static int normalize(int id)
122 // make sure id < NB_KEYS
129 * \brief Returns whether a id belongs to the interval [start, end].
131 * The parameters are noramlized to make sure they are between 0 and CHORD_NB_KEYS - 1).
132 * 1 belongs to [62, 3]
133 * 1 does not belong to [3, 62]
134 * 63 belongs to [62, 3]
135 * 63 does not belong to [3, 62]
136 * 24 belongs to [21, 29]
137 * 24 does not belong to [29, 21]
139 * \param id id to check
140 * \param start lower bound
141 * \param end upper bound
142 * \return a non-zero value if id in in [start, end]
144 static int is_in_interval(int id, int start, int end)
147 start = normalize(start);
148 end = normalize(end);
150 // make sure end >= start and id >= start
163 * \brief Gets the mailbox name of a host given its chord id.
164 * \param node_id id of a node
165 * \return the name of its mailbox
167 static char* get_mailbox(int node_id)
169 return bprintf("mailbox%d", node_id);
173 * \brief Displays the finger table of a node.
176 static void print_finger_table(node_t node)
180 INFO0("My finger table:");
181 INFO0("Start | Succ ");
182 for (i = 0; i < NB_BITS; i++) {
183 INFO2(" %3d | %3d ", (node->id + pow) % NB_KEYS, node->fingers[i].id);
186 INFO1("Predecessor: %d", node->pred_id);
190 * \brief Sets a finger of the current node.
191 * \param node the current node
192 * \param finger_index index of the finger to set (0 to NB_BITS - 1)
193 * \param id the id to set for this finger
195 static void set_finger(node_t node, int finger_index, int id)
197 node->fingers[finger_index].id = id;
198 xbt_free(node->fingers[finger_index].mailbox);
199 node->fingers[finger_index].mailbox = get_mailbox(id);
200 DEBUG2("My new finger #%d is %d", finger_index, id);
204 * \brief Sets the predecessor of the current node.
205 * \param node the current node
206 * \param id the id to predecessor, or -1 to unset the predecessor
208 static void set_predecessor(node_t node, int predecessor_id)
210 node->pred_id = predecessor_id;
211 xbt_free(node->pred_mailbox);
213 if (predecessor_id != -1) {
214 node->pred_mailbox = get_mailbox(predecessor_id);
217 DEBUG1("My new predecessor is %d", predecessor_id);
221 * \brief Node Function
224 * - the id of a guy I know in the system (except for the first node)
225 * - the time to sleep before I join (except for the first node)
227 int node(int argc, char *argv[])
229 double init_time = MSG_get_clock();
230 m_task_t task = NULL;
231 m_task_t task_received = NULL;
232 msg_comm_t comm_send = NULL;
235 int join_success = 0;
237 double next_stabilize_date = init_time + 10;
238 double next_fix_fingers_date = init_time + 10;
239 double next_check_predecessor_date = init_time + 10;
241 xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
243 // initialize my node
245 node.id = atoi(argv[1]);
246 node.mailbox = get_mailbox(node.id);
247 node.next_finger_to_fix = 0;
248 node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
250 for (i = 0; i < NB_BITS; i++) {
251 set_finger(&node, i, node.id);
254 if (argc == 3) { // first ring
255 deadline = atof(argv[2]);
260 int known_id = atoi(argv[2]);
261 double sleep_time = atof(argv[3]);
262 deadline = atof(argv[4]);
264 // sleep before starting
265 DEBUG1("Let's sleep during %f", sleep_time);
266 MSG_process_sleep(sleep_time);
267 DEBUG0("Hey! Let's join the system.");
269 join_success = join(&node, known_id);
273 while (MSG_get_clock() < init_time + deadline) {
275 if (node.comm_receive == NULL) {
276 task_received = NULL;
277 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
278 // FIXME: do not make MSG_task_irecv() calls from several functions
281 if (!MSG_comm_test(node.comm_receive)) {
283 // no task was received: make some periodic calls
284 if (MSG_get_clock() >= next_stabilize_date) {
286 next_stabilize_date = MSG_get_clock() + 10;
288 else if (MSG_get_clock() >= next_fix_fingers_date) {
290 next_fix_fingers_date = MSG_get_clock() + 10;
292 else if (MSG_get_clock() >= next_check_predecessor_date) {
293 check_predecessor(&node);
294 next_check_predecessor_date = MSG_get_clock() + 10;
297 // nothing to do: sleep for a while
298 MSG_process_sleep(5);
302 // a transfer has occured
304 MSG_error_t status = MSG_comm_get_status(node.comm_receive);
305 MSG_comm_destroy(node.comm_receive);
306 node.comm_receive = NULL;
308 if (status != MSG_OK) {
309 DEBUG0("Failed to receive a task. Nevermind.");
312 // the task was successfully received
313 handle_task(&node, task_received);
317 // see if some communications are finished
318 while ((index = MSG_comm_testany(node.comms)) != -1) {
319 comm_send = xbt_dynar_get_as(node.comms, index, msg_comm_t);
320 MSG_error_t status = MSG_comm_get_status(comm_send);
321 xbt_dynar_remove_at(node.comms, index, &comm_send);
322 DEBUG3("Communication %p is finished with status %d, dynar size is now %lu",
323 comm_send, status, xbt_dynar_length(node.comms));
324 MSG_comm_destroy(comm_send);
328 // clean unfinished comms sent
330 xbt_dynar_foreach(node.comms, cursor, comm_send) {
331 task = MSG_comm_get_task(comm_send);
332 MSG_task_cancel(task);
333 xbt_free(MSG_task_get_data(task));
334 MSG_task_destroy(task);
335 MSG_comm_destroy(comm_send);
336 // FIXME: the task is actually not destroyed because MSG thinks that the other side (whose process is dead) is still using it
343 // stop the simulation
344 xbt_dynar_free(&node.comms);
345 xbt_free(node.mailbox);
346 xbt_free(node.pred_mailbox);
347 for (i = 0; i < NB_BITS - 1; i++) {
348 xbt_free(node.fingers[i].mailbox);
354 * \brief This function is called when the current node receives a task.
355 * \param node the current node
356 * \param task the task to handle (don't touch it then:
357 * it will be destroyed, reused or forwarded)
359 static void handle_task(node_t node, m_task_t task) {
361 DEBUG1("Handling task %p", task);
362 msg_comm_t comm = NULL;
363 char* mailbox = NULL;
364 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
365 e_task_type_t type = task_data->type;
369 case TASK_FIND_SUCCESSOR:
370 DEBUG2("Receiving a 'Find Successor' request from %s for id %d",
371 task_data->issuer_host_name, task_data->request_id);
372 // is my successor the successor?
373 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
374 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
375 task_data->answer_id = node->fingers[0].id;
376 comm = MSG_task_isend(task, task_data->answer_to);
377 xbt_dynar_push(node->comms, &comm);
378 DEBUG3("Sending back a 'Find Successor Answer' to %s: the successor of %d is %d",
379 task_data->issuer_host_name,
380 task_data->request_id, task_data->answer_id);
383 // otherwise, forward the request to the closest preceding finger in my table
384 int closest = closest_preceding_node(node, task_data->request_id);
385 DEBUG2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
386 task_data->request_id, closest);
387 mailbox = get_mailbox(closest);
388 comm = MSG_task_isend(task, mailbox);
389 xbt_dynar_push(node->comms, &comm);
394 case TASK_GET_PREDECESSOR:
395 DEBUG1("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
396 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
397 task_data->answer_id = node->pred_id;
398 comm = MSG_task_isend(task, task_data->answer_to);
399 xbt_dynar_push(node->comms, &comm);
400 DEBUG3("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
401 task_data->issuer_host_name,
402 task_data->answer_to, task_data->answer_id);
406 // someone is telling me that he may be my new predecessor
407 DEBUG1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
408 notify(node, task_data->request_id);
410 MSG_task_destroy(task);
413 case TASK_PREDECESSOR_LEAVING:
414 // my predecessor is about to quit
415 DEBUG1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
416 // modify my predecessor
417 set_predecessor(node, task_data->request_id);
419 MSG_task_destroy(task);
421 >> notify my new predecessor
422 >> send a notify_predecessors !!
426 case TASK_SUCCESSOR_LEAVING:
427 // my successor is about to quit
428 DEBUG1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
429 // modify my successor FIXME : this should be implicit ?
430 set_finger(node, 0, task_data->request_id);
432 MSG_task_destroy(task);
434 >> notify my new successor
435 >> update my table & predecessors table */
439 CRITICAL1("Received an unexpected task: %d", type);
445 * \brief Initializes the current node as the first one of the system.
446 * \param node the current node
448 static void create(node_t node)
450 DEBUG0("Create a new Chord ring...");
451 set_predecessor(node, -1); // -1 means that I have no predecessor
452 print_finger_table(node);
456 * \brief Makes the current node join the ring, knowing the id of a node
457 * already in the ring
458 * \param node the current node
459 * \param known_id id of a node already in the ring
460 * \return 1 if the join operation succeeded, 0 otherwise
462 static int join(node_t node, int known_id)
464 INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
465 set_predecessor(node, -1); // no predecessor (yet)
467 int successor_id = remote_find_successor(node, known_id, node->id);
468 if (successor_id == -1) {
469 DEBUG0("Cannot join the ring.");
472 set_finger(node, 0, successor_id);
473 print_finger_table(node);
476 return successor_id != -1;
480 * \brief Makes the current node quit the system
481 * \param node the current node
483 static void leave(node_t node)
485 DEBUG0("Well Guys! I Think it's time for me to quit ;)");
486 quit_notify(node, 1); // notify to my successor ( >>> 1 );
487 quit_notify(node, -1); // notify my predecessor ( >>> -1);
492 * \brief Notifies the successor or the predecessor of the current node
494 * \param node the current node
495 * \param to 1 to notify the successor, -1 to notify the predecessor
496 * FIXME: notify both nodes with only one call
498 static void quit_notify(node_t node, int to)
501 task_data_t req_data = xbt_new0(s_task_data_t, 1);
502 req_data->request_id = node->id;
503 req_data->successor_id = node->fingers[0].id;
504 req_data->pred_id = node->pred_id;
505 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
506 const char* task_name = NULL;
507 const char* to_mailbox = NULL;
508 if (to == 1) { // notify my successor
509 to_mailbox = node->fingers[0].mailbox;
510 INFO2("Telling my Successor %d about my departure via mailbox %s",
511 node->fingers[0].id, to_mailbox);
512 req_data->type = TASK_PREDECESSOR_LEAVING;
514 else if (to == -1) { // notify my predecessor
516 if (node->pred_id == -1) {
520 to_mailbox = node->pred_mailbox;
521 INFO2("Telling my Predecessor %d about my departure via mailbox %s",
522 node->pred_id, to_mailbox);
523 req_data->type = TASK_SUCCESSOR_LEAVING;
525 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
526 //char* mailbox = get_mailbox(to_mailbox);
527 msg_comm_t comm = MSG_task_isend(task, to_mailbox);
528 xbt_dynar_push(node->comms, &comm);
533 * \brief Makes the current node find the successor node of an id.
534 * \param node the current node
535 * \param id the id to find
536 * \return the id of the successor node, or -1 if the request failed
538 static int find_successor(node_t node, int id)
540 // is my successor the successor?
541 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
542 return node->fingers[0].id;
545 // otherwise, ask the closest preceding finger in my table
546 int closest = closest_preceding_node(node, id);
547 return remote_find_successor(node, closest, id);
551 * \brief Asks another node the successor node of an id.
552 * \param node the current node
553 * \param ask_to the node to ask to
554 * \param id the id to find
555 * \return the id of the successor node, or -1 if the request failed
557 static int remote_find_successor(node_t node, int ask_to, int id)
561 char* mailbox = get_mailbox(ask_to);
562 task_data_t req_data = xbt_new0(s_task_data_t, 1);
563 req_data->type = TASK_FIND_SUCCESSOR;
564 req_data->request_id = id;
565 req_data->answer_to = node->mailbox;
566 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
568 // send a "Find Successor" request to ask_to_id
569 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
570 DEBUG3("Sending a 'Find Successor' request (task %p) to %d for id %d", task, ask_to, id);
571 MSG_error_t res = MSG_task_send_with_timeout(task, mailbox, 50);
574 DEBUG2("Failed to send the 'Find Successor' request to %d for id %d", ask_to, id);
575 MSG_task_destroy(task);
580 // receive the answer
581 DEBUG3("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
585 if (node->comm_receive == NULL) {
587 node->comm_receive = MSG_task_irecv(&task, node->mailbox);
590 res = MSG_comm_wait(node->comm_receive, 50);
593 DEBUG2("Failed to receive the answer to my 'Find Successor' request (task %p): %d", task, res);
595 // MSG_comm_destroy(node->comm_receive);
598 task = MSG_comm_get_task(node->comm_receive);
599 task_data_t ans_data = MSG_task_get_data(task);
601 if (ans_data->type != TASK_FIND_SUCCESSOR_ANSWER) {
602 handle_task(node, task);
605 DEBUG3("Received the answer to my 'Find Successor' request (task %p): the successor of key %d is %d",
606 task, id, successor);
607 successor = ans_data->answer_id;
609 MSG_task_destroy(task);
613 node->comm_receive = NULL;
622 * \brief Asks another node its predecessor.
623 * \param node the current node
624 * \param ask_to the node to ask to
625 * \return the id of its predecessor node, or -1 if the request failed
626 * (or if the node does not know its predecessor)
628 static int remote_get_predecessor(node_t node, int ask_to)
630 int predecessor_id = -1;
632 char* mailbox = get_mailbox(ask_to);
633 task_data_t req_data = xbt_new0(s_task_data_t, 1);
634 req_data->type = TASK_GET_PREDECESSOR;
635 req_data->answer_to = node->mailbox;
636 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
638 // send a "Get Predecessor" request to ask_to_id
639 DEBUG1("Sending a 'Get Predecessor' request to %d", ask_to);
640 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
641 MSG_error_t res = MSG_task_send_with_timeout(task, mailbox, 50);
644 DEBUG1("Failed to send the 'Get Predecessor' request to %d", ask_to);
645 MSG_task_destroy(task);
650 // receive the answer
651 DEBUG2("Sent 'Get Predecessor' request to %d, waiting for the answer on my mailbox '%s'", ask_to, req_data->answer_to);
654 if (node->comm_receive == NULL) { // FIXME simplify this
656 node->comm_receive = MSG_task_irecv(&task, node->mailbox);
659 res = MSG_comm_wait(node->comm_receive, 50);
662 DEBUG1("Failed to receive the answer to my 'Get Predecessor' request: %d", res);
664 // MSG_comm_destroy(node->comm_receive);
667 task = MSG_comm_get_task(node->comm_receive);
668 task_data_t ans_data = MSG_task_get_data(task);
670 if (ans_data->type != TASK_GET_PREDECESSOR_ANSWER) {
671 handle_task(node, task);
674 DEBUG2("Received the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to, predecessor_id);
675 predecessor_id = ans_data->answer_id;
677 MSG_task_destroy(task);
681 node->comm_receive = NULL;
686 return predecessor_id;
690 * \brief Returns the closest preceding finger of an id
691 * with respect to the finger table of the current node.
692 * \param node the current node
693 * \param id the id to find
694 * \return the closest preceding finger of that id
696 int closest_preceding_node(node_t node, int id)
699 for (i = NB_BITS - 1; i >= 0; i--) {
700 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
701 return node->fingers[i].id;
708 * \brief This function is called periodically. It checks the immediate
709 * successor of the current node.
710 * \param node the current node
712 static void stabilize(node_t node)
714 DEBUG0("Stabilizing node");
716 // get the predecessor of my immediate successor
718 int successor_id = node->fingers[0].id;
719 if (successor_id != node->id) {
720 candidate_id = remote_get_predecessor(node, successor_id);
723 candidate_id = node->pred_id;
726 // this node is a candidate to become my new successor
727 if (candidate_id != -1
728 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
729 set_finger(node, 0, candidate_id);
731 if (successor_id != node->id) {
732 remote_notify(node, successor_id, node->id);
737 * \brief Notifies the current node that its predecessor may have changed.
738 * \param node the current node
739 * \param candidate_id the possible new predecessor
741 static void notify(node_t node, int predecessor_candidate_id) {
743 if (node->pred_id == -1
744 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
746 set_predecessor(node, predecessor_candidate_id);
747 print_finger_table(node);
750 DEBUG1("I don't have to change my predecessor to %d", predecessor_candidate_id);
755 * \brief Notifies a remote node that its predecessor may have changed.
756 * \param node the current node
757 * \param notify_id id of the node to notify
758 * \param candidate_id the possible new predecessor
760 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
762 task_data_t req_data = xbt_new0(s_task_data_t, 1);
763 req_data->type = TASK_NOTIFY;
764 req_data->request_id = predecessor_candidate_id;
765 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
767 // send a "Notify" request to notify_id
768 DEBUG1("Sending a 'Notify' request to %d", notify_id);
769 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
770 char* mailbox = get_mailbox(notify_id);
771 msg_comm_t comm = MSG_task_isend(task, mailbox);
772 xbt_dynar_push(node->comms, &comm);
777 * \brief This function is called periodically.
778 * It refreshes the finger table of the current node.
779 * \param node the current node
781 static void fix_fingers(node_t node) {
783 DEBUG0("Fixing fingers");
784 int i = node->next_finger_to_fix;
785 int id = find_successor(node, node->id + powers2[i]);
788 if (id != node->fingers[i].id) {
789 set_finger(node, i, id);
790 print_finger_table(node);
792 node->next_finger_to_fix = (i + 1) % NB_BITS;
797 * \brief This function is called periodically.
798 * It checks whether the predecessor has failed
799 * \param node the current node
801 static void check_predecessor(node_t node)
803 DEBUG0("Checking whether my predecessor is alive");
808 * \brief Main function.
810 int main(int argc, char *argv[])
813 printf("Usage: %s platform_file deployment_file\n", argv[0]);
814 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
818 const char* platform_file = argv[1];
819 const char* application_file = argv[2];
823 MSG_global_init(&argc, argv);
824 MSG_set_channel_number(0);
825 MSG_create_environment(platform_file);
827 MSG_function_register("node", node);
828 MSG_launch_application(application_file);
830 MSG_error_t res = MSG_main();
831 INFO1("Simulation time: %g", MSG_get_clock());