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");
24 typedef struct finger {
27 } s_finger_t, *finger_t;
34 char* mailbox; // my usual mailbox name
35 s_finger_t fingers[NB_BITS]; // finger table (fingers[0] is my successor)
36 int pred_id; // predecessor id
37 char* pred_mailbox; // predecessor's mailbox name
38 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
39 msg_comm_t comm_receive; // current communication to receive
40 xbt_dynar_t comms; // current communications being sent
44 * Types of tasks exchanged between nodes.
48 TASK_FIND_SUCCESSOR_ANSWER,
50 TASK_GET_PREDECESSOR_ANSWER,
52 TASK_SUCCESSOR_LEAVING,
53 TASK_PREDECESSOR_LEAVING
57 * Data attached with the tasks sent and received
59 typedef struct task_data {
60 e_task_type_t type; // type of task
61 int request_id; // id paramater (used by some types of tasks)
62 int request_finger; // finger parameter (used by some types of tasks)
63 int answer_id; // answer (used by some types of tasks)
64 char* answer_to; // mailbox to send an answer to (or NULL)
65 const char* issuer_host_name; // used for logging
66 } s_task_data_t, *task_data_t;
68 static int powers2[NB_BITS];
71 static void chord_initialize(void);
72 static int normalize(int id);
73 static int is_in_interval(int id, int start, int end);
74 static char* get_mailbox(int host_id);
75 static void task_data_destroy(task_data_t task_data);
76 static void print_finger_table(node_t node);
77 static void set_finger(node_t node, int finger_index, int id);
78 static void set_predecessor(node_t node, int predecessor_id);
81 static int node(int argc, char *argv[]);
82 static void handle_task(node_t node, m_task_t task);
85 static void create(node_t node);
86 static int join(node_t node, int known_id);
87 static void leave(node_t node);
88 static int find_successor(node_t node, int id);
89 static int remote_find_successor(node_t node, int ask_to_id, int id);
90 static int remote_get_predecessor(node_t node, int ask_to_id);
91 static int closest_preceding_node(node_t node, int id);
92 static void stabilize(node_t node);
93 static void notify(node_t node, int predecessor_candidate_id);
94 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
95 static void fix_fingers(node_t node);
96 static void check_predecessor(node_t node);
97 static void quit_notify(node_t node, int to);
100 * \brief Global initialization of the Chord simulation.
102 static void chord_initialize(void)
104 // compute the powers of 2 once for all
107 for (i = 0; i < NB_BITS; i++) {
114 * \brief Turns an id into an equivalent id in [0, NB_KEYS).
116 * \return the corresponding normalized id
118 static int normalize(int id)
124 // make sure id < NB_KEYS
131 * \brief Returns whether a id belongs to the interval [start, end].
133 * The parameters are noramlized to make sure they are between 0 and CHORD_NB_KEYS - 1).
134 * 1 belongs to [62, 3]
135 * 1 does not belong to [3, 62]
136 * 63 belongs to [62, 3]
137 * 63 does not belong to [3, 62]
138 * 24 belongs to [21, 29]
139 * 24 does not belong to [29, 21]
141 * \param id id to check
142 * \param start lower bound
143 * \param end upper bound
144 * \return a non-zero value if id in in [start, end]
146 static int is_in_interval(int id, int start, int end)
149 start = normalize(start);
150 end = normalize(end);
152 // make sure end >= start and id >= start
165 * \brief Gets the mailbox name of a host given its chord id.
166 * \param node_id id of a node
167 * \return the name of its mailbox
169 static char* get_mailbox(int node_id)
171 return bprintf("mailbox%d", node_id);
175 * \brief Frees the memory used by some task data.
176 * \param task_data the task data to destroy
178 static void task_data_destroy(task_data_t task_data)
180 xbt_free(task_data->answer_to);
185 * \brief Displays the finger table of a node.
188 static void print_finger_table(node_t node)
192 INFO0("My finger table:");
193 INFO0("Start | Succ ");
194 for (i = 0; i < NB_BITS; i++) {
195 INFO2(" %3d | %3d ", (node->id + pow) % NB_KEYS, node->fingers[i].id);
198 INFO1("Predecessor: %d", node->pred_id);
202 * \brief Sets a finger of the current node.
203 * \param node the current node
204 * \param finger_index index of the finger to set (0 to NB_BITS - 1)
205 * \param id the id to set for this finger
207 static void set_finger(node_t node, int finger_index, int id)
209 node->fingers[finger_index].id = id;
210 xbt_free(node->fingers[finger_index].mailbox);
211 node->fingers[finger_index].mailbox = get_mailbox(id);
212 DEBUG2("My new finger #%d is %d", finger_index, id);
216 * \brief Sets the predecessor of the current node.
217 * \param node the current node
218 * \param id the id to predecessor, or -1 to unset the predecessor
220 static void set_predecessor(node_t node, int predecessor_id)
222 node->pred_id = predecessor_id;
223 xbt_free(node->pred_mailbox);
225 if (predecessor_id != -1) {
226 node->pred_mailbox = get_mailbox(predecessor_id);
229 DEBUG1("My new predecessor is %d", predecessor_id);
233 * \brief Node Function
236 * - the id of a guy I know in the system (except for the first node)
237 * - the time to sleep before I join (except for the first node)
239 int node(int argc, char *argv[])
241 double init_time = MSG_get_clock();
242 m_task_t task = NULL;
243 m_task_t task_received = NULL;
244 msg_comm_t comm_send = NULL;
247 int join_success = 0;
249 double next_stabilize_date = init_time + 10;
250 double next_fix_fingers_date = init_time + 10;
251 double next_check_predecessor_date = init_time + 10;
253 xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
255 // initialize my node
257 node.id = atoi(argv[1]);
258 node.mailbox = get_mailbox(node.id);
259 node.next_finger_to_fix = 0;
260 node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
262 for (i = 0; i < NB_BITS; i++) {
263 set_finger(&node, i, node.id);
266 if (argc == 3) { // first ring
267 deadline = atof(argv[2]);
272 int known_id = atoi(argv[2]);
273 double sleep_time = atof(argv[3]);
274 deadline = atof(argv[4]);
276 // sleep before starting
277 DEBUG1("Let's sleep during %f", sleep_time);
278 MSG_process_sleep(sleep_time);
279 DEBUG0("Hey! Let's join the system.");
281 join_success = join(&node, known_id);
285 while (MSG_get_clock() < init_time + deadline) {
287 if (node.comm_receive == NULL) {
288 task_received = NULL;
289 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
290 // FIXME: do not make MSG_task_irecv() calls from several functions
293 if (!MSG_comm_test(node.comm_receive)) {
295 // no task was received: make some periodic calls
296 if (MSG_get_clock() >= next_stabilize_date) {
298 next_stabilize_date = MSG_get_clock() + 10;
300 else if (MSG_get_clock() >= next_fix_fingers_date) {
302 next_fix_fingers_date = MSG_get_clock() + 10;
304 else if (MSG_get_clock() >= next_check_predecessor_date) {
305 check_predecessor(&node);
306 next_check_predecessor_date = MSG_get_clock() + 10;
309 // nothing to do: sleep for a while
310 MSG_process_sleep(5);
314 // a transfer has occured
316 MSG_error_t status = MSG_comm_get_status(node.comm_receive);
318 if (status != MSG_OK) {
319 DEBUG0("Failed to receive a task. Nevermind.");
320 node.comm_receive = NULL;
323 // the task was successfully received
324 MSG_comm_destroy(node.comm_receive);
325 node.comm_receive = NULL;
326 handle_task(&node, task_received);
330 // see if some communications are finished
331 while ((index = MSG_comm_testany(node.comms)) != -1) {
332 comm_send = xbt_dynar_get_as(node.comms, index, msg_comm_t);
333 MSG_error_t status = MSG_comm_get_status(comm_send);
334 xbt_dynar_remove_at(node.comms, index, &comm_send);
335 DEBUG3("Communication %p is finished with status %d, dynar size is now %lu",
336 comm_send, status, xbt_dynar_length(node.comms));
337 MSG_comm_destroy(comm_send);
341 // clean unfinished comms sent
343 xbt_dynar_foreach(node.comms, cursor, comm_send) {
344 task = MSG_comm_get_task(comm_send);
345 MSG_task_cancel(task);
346 task_data_destroy(MSG_task_get_data(task));
347 MSG_task_destroy(task);
348 MSG_comm_destroy(comm_send);
349 // FIXME: the task is actually not destroyed because MSG thinks that the other side (whose process is dead) is still using it
356 // stop the simulation
357 xbt_dynar_free(&node.comms);
358 xbt_free(node.mailbox);
359 xbt_free(node.pred_mailbox);
360 for (i = 0; i < NB_BITS - 1; i++) {
361 xbt_free(node.fingers[i].mailbox);
367 * \brief This function is called when the current node receives a task.
368 * \param node the current node
369 * \param task the task to handle (don't touch it then:
370 * it will be destroyed, reused or forwarded)
372 static void handle_task(node_t node, m_task_t task) {
374 DEBUG1("Handling task %p", task);
375 msg_comm_t comm = NULL;
376 char* mailbox = NULL;
377 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
378 e_task_type_t type = task_data->type;
382 case TASK_FIND_SUCCESSOR:
383 DEBUG2("Receiving a 'Find Successor' request from %s for id %d",
384 task_data->issuer_host_name, task_data->request_id);
385 // is my successor the successor?
386 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
387 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
388 task_data->answer_id = node->fingers[0].id;
389 DEBUG3("Sending back a 'Find Successor Answer' to %s: the successor of %d is %d",
390 task_data->issuer_host_name,
391 task_data->request_id, task_data->answer_id);
392 comm = MSG_task_isend(task, task_data->answer_to);
393 xbt_dynar_push(node->comms, &comm);
396 // otherwise, forward the request to the closest preceding finger in my table
397 int closest = closest_preceding_node(node, task_data->request_id);
398 DEBUG2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
399 task_data->request_id, closest);
400 mailbox = get_mailbox(closest);
401 comm = MSG_task_isend(task, mailbox);
402 xbt_dynar_push(node->comms, &comm);
407 case TASK_GET_PREDECESSOR:
408 DEBUG1("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
409 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
410 task_data->answer_id = node->pred_id;
411 DEBUG3("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
412 task_data->issuer_host_name,
413 task_data->answer_to, task_data->answer_id);
414 comm = MSG_task_isend(task, task_data->answer_to);
415 xbt_dynar_push(node->comms, &comm);
419 // someone is telling me that he may be my new predecessor
420 DEBUG1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
421 notify(node, task_data->request_id);
422 task_data_destroy(task_data);
423 MSG_task_destroy(task);
426 case TASK_PREDECESSOR_LEAVING:
427 // my predecessor is about to quit
428 DEBUG1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
429 // modify my predecessor
430 set_predecessor(node, task_data->request_id);
431 task_data_destroy(task_data);
432 MSG_task_destroy(task);
434 >> notify my new predecessor
435 >> send a notify_predecessors !!
439 case TASK_SUCCESSOR_LEAVING:
440 // my successor is about to quit
441 DEBUG1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
442 // modify my successor FIXME : this should be implicit ?
443 set_finger(node, 0, task_data->request_id);
444 task_data_destroy(task_data);
445 MSG_task_destroy(task);
447 >> notify my new successor
448 >> update my table & predecessors table */
451 case TASK_FIND_SUCCESSOR_ANSWER:
452 case TASK_GET_PREDECESSOR_ANSWER:
453 DEBUG2("Ignoring unexpected task of type %d (%p)", type, task);
459 * \brief Initializes the current node as the first one of the system.
460 * \param node the current node
462 static void create(node_t node)
464 DEBUG0("Create a new Chord ring...");
465 set_predecessor(node, -1); // -1 means that I have no predecessor
466 print_finger_table(node);
470 * \brief Makes the current node join the ring, knowing the id of a node
471 * already in the ring
472 * \param node the current node
473 * \param known_id id of a node already in the ring
474 * \return 1 if the join operation succeeded, 0 otherwise
476 static int join(node_t node, int known_id)
478 INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
479 set_predecessor(node, -1); // no predecessor (yet)
481 int successor_id = remote_find_successor(node, known_id, node->id);
482 if (successor_id == -1) {
483 DEBUG0("Cannot join the ring.");
486 set_finger(node, 0, successor_id);
487 print_finger_table(node);
490 return successor_id != -1;
494 * \brief Makes the current node quit the system
495 * \param node the current node
497 static void leave(node_t node)
499 DEBUG0("Well Guys! I Think it's time for me to quit ;)");
500 quit_notify(node, 1); // notify to my successor ( >>> 1 );
501 quit_notify(node, -1); // notify my predecessor ( >>> -1);
506 * \brief Notifies the successor or the predecessor of the current node
508 * \param node the current node
509 * \param to 1 to notify the successor, -1 to notify the predecessor
510 * FIXME: notify both nodes with only one call
512 static void quit_notify(node_t node, int to)
515 task_data_t req_data = xbt_new0(s_task_data_t, 1);
516 req_data->request_id = node->id;
517 req_data->successor_id = node->fingers[0].id;
518 req_data->pred_id = node->pred_id;
519 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
520 req_data->answer_to = NULL;
521 const char* task_name = NULL;
522 const char* to_mailbox = NULL;
523 if (to == 1) { // notify my successor
524 to_mailbox = node->fingers[0].mailbox;
525 INFO2("Telling my Successor %d about my departure via mailbox %s",
526 node->fingers[0].id, to_mailbox);
527 req_data->type = TASK_PREDECESSOR_LEAVING;
529 else if (to == -1) { // notify my predecessor
531 if (node->pred_id == -1) {
535 to_mailbox = node->pred_mailbox;
536 INFO2("Telling my Predecessor %d about my departure via mailbox %s",
537 node->pred_id, to_mailbox);
538 req_data->type = TASK_SUCCESSOR_LEAVING;
540 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
541 //char* mailbox = get_mailbox(to_mailbox);
542 msg_comm_t comm = MSG_task_isend(task, to_mailbox);
543 xbt_dynar_push(node->comms, &comm);
548 * \brief Makes the current node find the successor node of an id.
549 * \param node the current node
550 * \param id the id to find
551 * \return the id of the successor node, or -1 if the request failed
553 static int find_successor(node_t node, int id)
555 // is my successor the successor?
556 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
557 return node->fingers[0].id;
560 // otherwise, ask the closest preceding finger in my table
561 int closest = closest_preceding_node(node, id);
562 return remote_find_successor(node, closest, id);
566 * \brief Asks another node the successor node of an id.
567 * \param node the current node
568 * \param ask_to the node to ask to
569 * \param id the id to find
570 * \return the id of the successor node, or -1 if the request failed
572 static int remote_find_successor(node_t node, int ask_to, int id)
576 char* mailbox = get_mailbox(ask_to);
577 task_data_t req_data = xbt_new0(s_task_data_t, 1);
578 req_data->type = TASK_FIND_SUCCESSOR;
579 req_data->request_id = id;
580 req_data->answer_to = xbt_strdup(node->mailbox);
581 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
583 // send a "Find Successor" request to ask_to_id
584 m_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
585 DEBUG3("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
586 MSG_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, 50);
589 DEBUG3("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
590 task_sent, ask_to, id);
591 MSG_task_destroy(task_sent);
592 task_data_destroy(req_data);
596 // receive the answer
597 DEBUG3("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
598 task_sent, ask_to, id);
601 if (node->comm_receive == NULL) {
602 m_task_t task_received = NULL;
603 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
606 res = MSG_comm_wait(node->comm_receive, 50);
609 DEBUG2("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
612 //MSG_comm_destroy(node->comm_receive);
615 m_task_t task_received = MSG_comm_get_task(node->comm_receive);
616 DEBUG1("Received a task (%p)", task_received);
617 task_data_t ans_data = MSG_task_get_data(task_received);
619 if (task_received != task_sent) {
620 // this is not the expected answer
621 handle_task(node, task_received);
624 // this is our answer
625 DEBUG4("Received the answer to my 'Find Successor' request for id %d (task %p): the successor of key %d is %d",
626 ans_data->request_id, task_received, id, ans_data->answer_id);
627 successor = ans_data->answer_id;
629 MSG_task_destroy(task_received);
630 task_data_destroy(req_data);
633 node->comm_receive = NULL;
642 * \brief Asks another node its predecessor.
643 * \param node the current node
644 * \param ask_to the node to ask to
645 * \return the id of its predecessor node, or -1 if the request failed
646 * (or if the node does not know its predecessor)
648 static int remote_get_predecessor(node_t node, int ask_to)
650 int predecessor_id = -1;
652 char* mailbox = get_mailbox(ask_to);
653 task_data_t req_data = xbt_new0(s_task_data_t, 1);
654 req_data->type = TASK_GET_PREDECESSOR;
655 req_data->answer_to = xbt_strdup(node->mailbox);
656 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
658 // send a "Get Predecessor" request to ask_to_id
659 DEBUG1("Sending a 'Get Predecessor' request to %d", ask_to);
660 m_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
661 MSG_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, 50);
664 DEBUG2("Failed to send the 'Get Predecessor' request (task %p) to %d",
666 MSG_task_destroy(task_sent);
667 task_data_destroy(req_data);
671 // receive the answer
672 DEBUG3("Sent 'Get Predecessor' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
673 task_sent, ask_to, req_data->answer_to);
676 if (node->comm_receive == NULL) { // FIXME simplify this
677 m_task_t task_received = NULL;
678 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
681 res = MSG_comm_wait(node->comm_receive, 50);
684 DEBUG2("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
687 //MSG_comm_destroy(node->comm_receive);
690 m_task_t task_received = MSG_comm_get_task(node->comm_receive);
691 task_data_t ans_data = MSG_task_get_data(task_received);
693 if (task_received != task_sent) {
694 handle_task(node, task_received);
697 DEBUG3("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
698 task_received, ask_to, ans_data->answer_id);
699 predecessor_id = ans_data->answer_id;
701 MSG_task_destroy(task_received);
702 task_data_destroy(req_data);
705 node->comm_receive = NULL;
710 return predecessor_id;
714 * \brief Returns the closest preceding finger of an id
715 * with respect to the finger table of the current node.
716 * \param node the current node
717 * \param id the id to find
718 * \return the closest preceding finger of that id
720 int closest_preceding_node(node_t node, int id)
723 for (i = NB_BITS - 1; i >= 0; i--) {
724 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
725 return node->fingers[i].id;
732 * \brief This function is called periodically. It checks the immediate
733 * successor of the current node.
734 * \param node the current node
736 static void stabilize(node_t node)
738 DEBUG0("Stabilizing node");
740 // get the predecessor of my immediate successor
742 int successor_id = node->fingers[0].id;
743 if (successor_id != node->id) {
744 candidate_id = remote_get_predecessor(node, successor_id);
747 candidate_id = node->pred_id;
750 // this node is a candidate to become my new successor
751 if (candidate_id != -1
752 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
753 set_finger(node, 0, candidate_id);
755 if (successor_id != node->id) {
756 remote_notify(node, successor_id, node->id);
761 * \brief Notifies the current node that its predecessor may have changed.
762 * \param node the current node
763 * \param candidate_id the possible new predecessor
765 static void notify(node_t node, int predecessor_candidate_id) {
767 if (node->pred_id == -1
768 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
770 set_predecessor(node, predecessor_candidate_id);
771 print_finger_table(node);
774 DEBUG1("I don't have to change my predecessor to %d", predecessor_candidate_id);
779 * \brief Notifies a remote node that its predecessor may have changed.
780 * \param node the current node
781 * \param notify_id id of the node to notify
782 * \param candidate_id the possible new predecessor
784 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
786 task_data_t req_data = xbt_new0(s_task_data_t, 1);
787 req_data->type = TASK_NOTIFY;
788 req_data->request_id = predecessor_candidate_id;
789 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
790 req_data->answer_to = NULL;
792 // send a "Notify" request to notify_id
793 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
794 DEBUG2("Sending a 'Notify' request (task %p) to %d", task, notify_id);
795 char* mailbox = get_mailbox(notify_id);
796 msg_comm_t comm = MSG_task_isend(task, mailbox);
797 xbt_dynar_push(node->comms, &comm);
802 * \brief This function is called periodically.
803 * It refreshes the finger table of the current node.
804 * \param node the current node
806 static void fix_fingers(node_t node) {
808 DEBUG0("Fixing fingers");
809 int i = node->next_finger_to_fix;
810 int id = find_successor(node, node->id + powers2[i]);
813 if (id != node->fingers[i].id) {
814 set_finger(node, i, id);
815 print_finger_table(node);
817 node->next_finger_to_fix = (i + 1) % NB_BITS;
822 * \brief This function is called periodically.
823 * It checks whether the predecessor has failed
824 * \param node the current node
826 static void check_predecessor(node_t node)
828 DEBUG0("Checking whether my predecessor is alive");
833 * \brief Main function.
835 int main(int argc, char *argv[])
838 printf("Usage: %s platform_file deployment_file\n", argv[0]);
839 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
843 const char* platform_file = argv[1];
844 const char* application_file = argv[2];
848 MSG_global_init(&argc, argv);
849 MSG_set_channel_number(0);
850 MSG_create_environment(platform_file);
852 MSG_function_register("node", node);
853 MSG_launch_application(application_file);
855 MSG_error_t res = MSG_main();
856 INFO1("Simulation time: %g", MSG_get_clock());