2 /* Copyright (c) 2010-2013. 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 #include "simgrid/modelchecker.h"
13 #include <xbt/RngStream.h>
15 /** @addtogroup MSG_examples
17 * - <b>chord/chord.c: Classical Chord P2P protocol</b>
18 * This example implements the well known Chord P2P protocol. Its
19 * main advantage is that it constitute a fully working non-trivial
20 * example. In addition, its implementation is rather efficient, as
21 * demonstrated in http://hal.inria.fr/inria-00602216/
25 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
26 "Messages specific for this msg example");
30 #define MAILBOX_NAME_SIZE 10
32 static int nb_bits = 24;
33 static int nb_keys = 0;
34 static int timeout = 50;
35 static int max_simulation_time = 1000;
36 static int periodic_stabilize_delay = 20;
37 static int periodic_fix_fingers_delay = 120;
38 static int periodic_check_predecessor_delay = 120;
39 static int periodic_lookup_delay = 10;
41 static const double sleep_delay = 4.9999;
43 extern long int smx_total_comms;
48 typedef struct s_finger {
50 char mailbox[MAILBOX_NAME_SIZE]; // string representation of the id
51 } s_finger_t, *finger_t;
56 typedef struct s_node {
58 char mailbox[MAILBOX_NAME_SIZE]; // my mailbox name (string representation of the id)
59 s_finger_t *fingers; // finger table, of size nb_bits (fingers[0] is my successor)
60 int pred_id; // predecessor id
61 char pred_mailbox[MAILBOX_NAME_SIZE]; // predecessor's mailbox name
62 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
63 msg_comm_t comm_receive; // current communication to receive
64 double last_change_date; // last time I changed a finger or my predecessor
65 RngStream stream; //RngStream for
69 * Types of tasks exchanged between nodes.
73 TASK_FIND_SUCCESSOR_ANSWER,
75 TASK_GET_PREDECESSOR_ANSWER,
77 TASK_SUCCESSOR_LEAVING,
78 TASK_PREDECESSOR_LEAVING,
79 TASK_PREDECESSOR_ALIVE,
80 TASK_PREDECESSOR_ALIVE_ANSWER
84 * Data attached with the tasks sent and received
86 typedef struct s_task_data {
87 e_task_type_t type; // type of task
88 int request_id; // id paramater (used by some types of tasks)
89 int request_finger; // finger parameter (used by some types of tasks)
90 int answer_id; // answer (used by some types of tasks)
91 char answer_to[MAILBOX_NAME_SIZE]; // mailbox to send an answer to (if any)
92 const char* issuer_host_name; // used for logging
93 } s_task_data_t, *task_data_t;
98 static void chord_initialize(void);
99 static void chord_exit(void);
100 static int normalize(int id);
101 static int is_in_interval(int id, int start, int end);
102 static void get_mailbox(int host_id, char* mailbox);
103 static void task_free(void* task);
104 static void print_finger_table(node_t node);
105 static void set_finger(node_t node, int finger_index, int id);
106 static void set_predecessor(node_t node, int predecessor_id);
109 static int node(int argc, char *argv[]);
110 static void handle_task(node_t node, msg_task_t task);
113 static void create(node_t node);
114 static int join(node_t node, int known_id);
115 static void leave(node_t node);
116 static int find_successor(node_t node, int id);
117 static int remote_find_successor(node_t node, int ask_to_id, int id);
118 static int remote_get_predecessor(node_t node, int ask_to_id);
119 static int closest_preceding_node(node_t node, int id);
120 static void stabilize(node_t node);
121 static void notify(node_t node, int predecessor_candidate_id);
122 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
123 static void fix_fingers(node_t node);
124 static void check_predecessor(node_t node);
125 static void random_lookup(node_t);
126 static void quit_notify(node_t node);
129 * \brief Global initialization of the Chord simulation.
131 static void chord_initialize(void)
133 // compute the powers of 2 once for all
134 powers2 = xbt_new(int, nb_bits);
137 for (i = 0; i < nb_bits; i++) {
142 XBT_DEBUG("Sets nb_keys to %d", nb_keys);
145 static void chord_exit(void)
151 * \brief Turns an id into an equivalent id in [0, nb_keys).
153 * \return the corresponding normalized id
155 static int normalize(int id)
157 // like id % nb_keys, but works with negatives numbers (and faster)
158 return id & (nb_keys - 1);
162 * \brief Returns whether an id belongs to the interval [start, end].
164 * The parameters are noramlized to make sure they are between 0 and nb_keys - 1).
165 * 1 belongs to [62, 3]
166 * 1 does not belong to [3, 62]
167 * 63 belongs to [62, 3]
168 * 63 does not belong to [3, 62]
169 * 24 belongs to [21, 29]
170 * 24 does not belong to [29, 21]
172 * \param id id to check
173 * \param start lower bound
174 * \param end upper bound
175 * \return a non-zero value if id in in [start, end]
177 static int is_in_interval(int id, int start, int end)
180 start = normalize(start);
181 end = normalize(end);
183 // make sure end >= start and id >= start
196 * \brief Gets the mailbox name of a host given its chord id.
197 * \param node_id id of a node
198 * \param mailbox pointer to where the mailbox name should be written
199 * (there must be enough space)
201 static void get_mailbox(int node_id, char* mailbox)
203 snprintf(mailbox, MAILBOX_NAME_SIZE - 1, "%d", node_id);
207 * \brief Frees the memory used by a task.
208 * \param task the MSG task to destroy
210 static void task_free(void* task)
212 // TODO add a parameter data_free_function to MSG_task_create?
214 xbt_free(MSG_task_get_data(task));
215 MSG_task_destroy(task);
220 * \brief Displays the finger table of a node.
223 static void print_finger_table(node_t node)
225 if (XBT_LOG_ISENABLED(msg_chord, xbt_log_priority_verbose)) {
227 XBT_VERB("My finger table:");
228 XBT_VERB("Start | Succ ");
229 for (i = 0; i < nb_bits; i++) {
230 XBT_VERB(" %3d | %3d ", (node->id + powers2[i]) % nb_keys, node->fingers[i].id);
232 XBT_VERB("Predecessor: %d", node->pred_id);
237 * \brief Sets a finger of the current node.
238 * \param node the current node
239 * \param finger_index index of the finger to set (0 to nb_bits - 1)
240 * \param id the id to set for this finger
242 static void set_finger(node_t node, int finger_index, int id)
244 if (id != node->fingers[finger_index].id) {
245 node->fingers[finger_index].id = id;
246 get_mailbox(id, node->fingers[finger_index].mailbox);
247 node->last_change_date = MSG_get_clock();
248 XBT_DEBUG("My new finger #%d is %d", finger_index, id);
253 * \brief Sets the predecessor of the current node.
254 * \param node the current node
255 * \param id the id to predecessor, or -1 to unset the predecessor
257 static void set_predecessor(node_t node, int predecessor_id)
259 if (predecessor_id != node->pred_id) {
260 node->pred_id = predecessor_id;
262 if (predecessor_id != -1) {
263 get_mailbox(predecessor_id, node->pred_mailbox);
265 node->last_change_date = MSG_get_clock();
267 XBT_DEBUG("My new predecessor is %d", predecessor_id);
272 * \brief Node Function
275 * - the id of a guy I know in the system (except for the first node)
276 * - the time to sleep before I join (except for the first node)
278 int node(int argc, char *argv[])
281 /* Reduce the run size for the MC */
283 periodic_stabilize_delay = 8;
284 periodic_fix_fingers_delay = 8;
285 periodic_check_predecessor_delay = 8;
288 double init_time = MSG_get_clock();
289 msg_task_t task_received = NULL;
291 int join_success = 0;
293 double next_stabilize_date = init_time + periodic_stabilize_delay;
294 double next_fix_fingers_date = init_time + periodic_fix_fingers_delay;
295 double next_check_predecessor_date = init_time + periodic_check_predecessor_delay;
296 double next_lookup_date = init_time + periodic_lookup_delay;
300 int sub_protocol = 0;
302 xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
304 // initialize my node
306 node.id = atoi(argv[1]);
307 get_mailbox(node.id, node.mailbox);
308 node.next_finger_to_fix = 0;
309 node.fingers = xbt_new0(s_finger_t, nb_bits);
310 node.last_change_date = init_time;
312 for (i = 0; i < nb_bits; i++) {
313 node.fingers[i].id = -1;
314 set_finger(&node, i, node.id);
317 if (argc == 3) { // first ring
318 deadline = atof(argv[2]);
323 int known_id = atoi(argv[2]);
324 //double sleep_time = atof(argv[3]);
325 deadline = atof(argv[4]);
328 // sleep before starting
329 XBT_DEBUG("Let's sleep during %f", sleep_time);
330 MSG_process_sleep(sleep_time);
332 XBT_DEBUG("Hey! Let's join the system.");
334 join_success = join(&node, known_id);
338 while (MSG_get_clock() < init_time + deadline
339 // && MSG_get_clock() < node.last_change_date + 1000
340 && MSG_get_clock() < max_simulation_time) {
342 if (node.comm_receive == NULL) {
343 task_received = NULL;
344 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
345 // FIXME: do not make MSG_task_irecv() calls from several functions
348 //XBT_INFO("Node %d is ring member : %d", node.id, is_ring_member(known_id, node.id) != -1);
350 if (!MSG_comm_test(node.comm_receive)) {
352 // no task was received: make some periodic calls
355 if(!MC_visited_reduction() && no_op){
358 if(listen == 0 && (sub_protocol = MC_random(0, 4)) > 0){
359 if(sub_protocol == 1)
361 else if(sub_protocol == 2)
363 else if(sub_protocol == 3)
364 check_predecessor(&node);
366 random_lookup(&node);
369 MSG_process_sleep(sleep_delay);
370 if(!MC_visited_reduction())
374 if (MSG_get_clock() >= next_stabilize_date) {
376 next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
377 }else if (MSG_get_clock() >= next_fix_fingers_date) {
379 next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
380 }else if (MSG_get_clock() >= next_check_predecessor_date) {
381 check_predecessor(&node);
382 next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
383 }else if (MSG_get_clock() >= next_lookup_date) {
384 random_lookup(&node);
385 next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
387 // nothing to do: sleep for a while
388 MSG_process_sleep(sleep_delay);
393 // a transfer has occurred
395 msg_error_t status = MSG_comm_get_status(node.comm_receive);
397 if (status != MSG_OK) {
398 XBT_DEBUG("Failed to receive a task. Nevermind.");
399 MSG_comm_destroy(node.comm_receive);
400 node.comm_receive = NULL;
403 // the task was successfully received
404 MSG_comm_destroy(node.comm_receive);
405 node.comm_receive = NULL;
406 handle_task(&node, task_received);
411 if (node.comm_receive) {
412 /* handle last task if any */
413 if (MSG_comm_wait(node.comm_receive, 0) == MSG_OK)
414 task_free(task_received);
415 MSG_comm_destroy(node.comm_receive);
416 node.comm_receive = NULL;
423 // stop the simulation
424 xbt_free(node.fingers);
429 * \brief This function is called when the current node receives a task.
430 * \param node the current node
431 * \param task the task to handle (don't touch it then:
432 * it will be destroyed, reused or forwarded)
434 static void handle_task(node_t node, msg_task_t task) {
436 XBT_DEBUG("Handling task %p", task);
437 char mailbox[MAILBOX_NAME_SIZE];
438 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
439 e_task_type_t type = task_data->type;
443 case TASK_FIND_SUCCESSOR:
444 XBT_DEBUG("Receiving a 'Find Successor' request from %s for id %d",
445 task_data->issuer_host_name, task_data->request_id);
446 // is my successor the successor?
447 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
448 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
449 task_data->answer_id = node->fingers[0].id;
450 XBT_DEBUG("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
451 task_data->issuer_host_name,
452 task_data->answer_to,
453 task_data->request_id, task_data->answer_id);
454 MSG_task_dsend(task, task_data->answer_to, task_free);
457 // otherwise, forward the request to the closest preceding finger in my table
458 int closest = closest_preceding_node(node, task_data->request_id);
459 XBT_DEBUG("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
460 task_data->request_id, closest);
461 get_mailbox(closest, mailbox);
462 MSG_task_dsend(task, mailbox, task_free);
466 case TASK_GET_PREDECESSOR:
467 XBT_DEBUG("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
468 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
469 task_data->answer_id = node->pred_id;
470 XBT_DEBUG("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
471 task_data->issuer_host_name,
472 task_data->answer_to, task_data->answer_id);
473 MSG_task_dsend(task, task_data->answer_to, task_free);
477 // someone is telling me that he may be my new predecessor
478 XBT_DEBUG("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
479 notify(node, task_data->request_id);
483 case TASK_PREDECESSOR_LEAVING:
484 // my predecessor is about to quit
485 XBT_DEBUG("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
486 // modify my predecessor
487 set_predecessor(node, task_data->request_id);
490 >> notify my new predecessor
491 >> send a notify_predecessors !!
495 case TASK_SUCCESSOR_LEAVING:
496 // my successor is about to quit
497 XBT_DEBUG("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
498 // modify my successor FIXME : this should be implicit ?
499 set_finger(node, 0, task_data->request_id);
502 >> notify my new successor
503 >> update my table & predecessors table */
506 case TASK_FIND_SUCCESSOR_ANSWER:
507 case TASK_GET_PREDECESSOR_ANSWER:
508 case TASK_PREDECESSOR_ALIVE_ANSWER:
509 XBT_DEBUG("Ignoring unexpected task of type %d (%p)", (int)type, task);
513 case TASK_PREDECESSOR_ALIVE:
514 XBT_DEBUG("Receiving a 'Predecessor Alive' request from %s", task_data->issuer_host_name);
515 task_data->type = TASK_PREDECESSOR_ALIVE_ANSWER;
516 XBT_DEBUG("Sending back a 'Predecessor Alive Answer' to %s (mailbox %s)",
517 task_data->issuer_host_name,
518 task_data->answer_to);
519 MSG_task_dsend(task, task_data->answer_to, task_free);
528 * \brief Initializes the current node as the first one of the system.
529 * \param node the current node
531 static void create(node_t node)
533 XBT_DEBUG("Create a new Chord ring...");
534 set_predecessor(node, -1); // -1 means that I have no predecessor
535 print_finger_table(node);
539 * \brief Makes the current node join the ring, knowing the id of a node
540 * already in the ring
541 * \param node the current node
542 * \param known_id id of a node already in the ring
543 * \return 1 if the join operation succeeded, 0 otherwise
545 static int join(node_t node, int known_id)
547 XBT_INFO("Joining the ring with id %d, knowing node %d", node->id, known_id);
548 set_predecessor(node, -1); // no predecessor (yet)
552 for (i = 0; i < nb_bits; i++) {
553 set_finger(node, i, known_id);
557 int successor_id = remote_find_successor(node, known_id, node->id);
558 if (successor_id == -1) {
559 XBT_INFO("Cannot join the ring.");
562 set_finger(node, 0, successor_id);
563 print_finger_table(node);
566 return successor_id != -1;
570 * \brief Makes the current node quit the system
571 * \param node the current node
573 static void leave(node_t node)
575 XBT_DEBUG("Well Guys! I Think it's time for me to quit ;)");
577 RngStream_DeleteStream(&node->stream);
581 * \brief Notifies the successor and the predecessor of the current node
583 * \param node the current node
585 static void quit_notify(node_t node)
587 char mailbox[MAILBOX_NAME_SIZE];
588 //send the PREDECESSOR_LEAVING to our successor
589 task_data_t req_data = xbt_new0(s_task_data_t,1);
590 req_data->type = TASK_PREDECESSOR_LEAVING;
591 req_data->request_id = node->pred_id;
592 get_mailbox(node->id, req_data->answer_to);
593 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
595 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
596 XBT_DEBUG("Sending a 'PREDECESSOR_LEAVING' to my successor %d",node->fingers[0].id);
597 if (MSG_task_send_with_timeout(task_sent, node->fingers[0].mailbox, timeout)==
599 XBT_DEBUG("Timeout expired when sending a 'PREDECESSOR_LEAVING' to my successor %d",
600 node->fingers[0].id);
601 task_free(task_sent);
604 //send the SUCCESSOR_LEAVING to our predecessor
605 get_mailbox(node->pred_id, mailbox);
606 task_data_t req_data_s = xbt_new0(s_task_data_t,1);
607 req_data_s->type = TASK_SUCCESSOR_LEAVING;
608 req_data_s->request_id = node->fingers[0].id;
609 req_data_s->request_id = node->pred_id;
610 get_mailbox(node->id, req_data_s->answer_to);
611 req_data_s->issuer_host_name = MSG_host_get_name(MSG_host_self());
613 msg_task_t task_sent_s = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data_s);
614 XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d",node->pred_id);
615 if (MSG_task_send_with_timeout(task_sent_s, mailbox, timeout)==
617 XBT_DEBUG("Timeout expired when sending a 'SUCCESSOR_LEAVING' to my predecessor %d",
619 task_free(task_sent_s);
625 * \brief Makes the current node find the successor node of an id.
626 * \param node the current node
627 * \param id the id to find
628 * \return the id of the successor node, or -1 if the request failed
630 static int find_successor(node_t node, int id)
632 // is my successor the successor?
633 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
634 return node->fingers[0].id;
637 // otherwise, ask the closest preceding finger in my table
638 int closest = closest_preceding_node(node, id);
639 return remote_find_successor(node, closest, id);
643 * \brief Asks another node the successor node of an id.
644 * \param node the current node
645 * \param ask_to the node to ask to
646 * \param id the id to find
647 * \return the id of the successor node, or -1 if the request failed
649 static int remote_find_successor(node_t node, int ask_to, int id)
653 char mailbox[MAILBOX_NAME_SIZE];
654 get_mailbox(ask_to, mailbox);
655 task_data_t req_data = xbt_new0(s_task_data_t, 1);
656 req_data->type = TASK_FIND_SUCCESSOR;
657 req_data->request_id = id;
658 get_mailbox(node->id, req_data->answer_to);
659 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
661 // send a "Find Successor" request to ask_to_id
662 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
663 XBT_DEBUG("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
664 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
667 XBT_DEBUG("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
668 task_sent, ask_to, id);
669 task_free(task_sent);
673 // receive the answer
674 XBT_DEBUG("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
675 task_sent, ask_to, id);
678 if (node->comm_receive == NULL) {
679 msg_task_t task_received = NULL;
680 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
683 res = MSG_comm_wait(node->comm_receive, timeout);
686 XBT_DEBUG("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
687 task_sent, (int)res);
689 MSG_comm_destroy(node->comm_receive);
690 node->comm_receive = NULL;
693 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
694 XBT_DEBUG("Received a task (%p)", task_received);
695 task_data_t ans_data = MSG_task_get_data(task_received);
697 // Once upon a time, our code assumed that here, task_received != task_sent all the time
699 // This assumption is wrong (as messages from differing round can interleave), leading to a bug in our code.
700 // We failed to find this bug directly, as it only occured on large platforms, leading to hardly usable traces.
701 // Instead, we used the model-checker to track down the issue by adding the following test here in the code:
702 // if (MC_is_active()) {
703 // MC_assert(task_received == task_sent);
705 // That explained the bug in a snap, with a very cool example and everything.
707 // This MC_assert is now desactivated as the case is now properly handled in our code and we don't want the
708 // MC to fail any further under that condition, but this comment is here to as a memorial for this first
709 // brillant victory of the model-checking in the SimGrid community :)
711 if (task_received != task_sent) {
712 // this is not the expected answer
713 MSG_comm_destroy(node->comm_receive);
714 node->comm_receive = NULL;
715 handle_task(node, task_received);
718 // this is our answer
719 XBT_DEBUG("Received the answer to my 'Find Successor' request for id %d (task %p): the successor of key %d is %d",
720 ans_data->request_id, task_received, id, ans_data->answer_id);
721 successor = ans_data->answer_id;
723 MSG_comm_destroy(node->comm_receive);
724 node->comm_receive = NULL;
725 task_free(task_received);
735 * \brief Asks another node its predecessor.
736 * \param node the current node
737 * \param ask_to the node to ask to
738 * \return the id of its predecessor node, or -1 if the request failed
739 * (or if the node does not know its predecessor)
741 static int remote_get_predecessor(node_t node, int ask_to)
743 int predecessor_id = -1;
745 char mailbox[MAILBOX_NAME_SIZE];
746 get_mailbox(ask_to, mailbox);
747 task_data_t req_data = xbt_new0(s_task_data_t, 1);
748 req_data->type = TASK_GET_PREDECESSOR;
749 get_mailbox(node->id, req_data->answer_to);
750 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
752 // send a "Get Predecessor" request to ask_to_id
753 XBT_DEBUG("Sending a 'Get Predecessor' request to %d", ask_to);
754 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
755 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
758 XBT_DEBUG("Failed to send the 'Get Predecessor' request (task %p) to %d",
760 task_free(task_sent);
764 // receive the answer
765 XBT_DEBUG("Sent 'Get Predecessor' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
766 task_sent, ask_to, req_data->answer_to);
769 if (node->comm_receive == NULL) { // FIXME simplify this
770 msg_task_t task_received = NULL;
771 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
774 res = MSG_comm_wait(node->comm_receive, timeout);
777 XBT_DEBUG("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
778 task_sent, (int)res);
780 MSG_comm_destroy(node->comm_receive);
781 node->comm_receive = NULL;
784 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
785 task_data_t ans_data = MSG_task_get_data(task_received);
787 /*if (MC_is_active()) {
788 MC_assert(task_received == task_sent);
791 if (task_received != task_sent) {
792 MSG_comm_destroy(node->comm_receive);
793 node->comm_receive = NULL;
794 handle_task(node, task_received);
797 XBT_DEBUG("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
798 task_received, ask_to, ans_data->answer_id);
799 predecessor_id = ans_data->answer_id;
801 MSG_comm_destroy(node->comm_receive);
802 node->comm_receive = NULL;
803 task_free(task_received);
809 return predecessor_id;
813 * \brief Returns the closest preceding finger of an id
814 * with respect to the finger table of the current node.
815 * \param node the current node
816 * \param id the id to find
817 * \return the closest preceding finger of that id
819 int closest_preceding_node(node_t node, int id)
822 for (i = nb_bits - 1; i >= 0; i--) {
823 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
824 return node->fingers[i].id;
831 * \brief This function is called periodically. It checks the immediate
832 * successor of the current node.
833 * \param node the current node
835 static void stabilize(node_t node)
837 XBT_DEBUG("Stabilizing node");
839 // get the predecessor of my immediate successor
841 int successor_id = node->fingers[0].id;
842 if (successor_id != node->id) {
843 candidate_id = remote_get_predecessor(node, successor_id);
846 candidate_id = node->pred_id;
849 // this node is a candidate to become my new successor
850 if (candidate_id != -1
851 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
852 set_finger(node, 0, candidate_id);
854 if (successor_id != node->id) {
855 remote_notify(node, successor_id, node->id);
860 * \brief Notifies the current node that its predecessor may have changed.
861 * \param node the current node
862 * \param candidate_id the possible new predecessor
864 static void notify(node_t node, int predecessor_candidate_id) {
866 if (node->pred_id == -1
867 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
869 set_predecessor(node, predecessor_candidate_id);
870 print_finger_table(node);
873 XBT_DEBUG("I don't have to change my predecessor to %d", predecessor_candidate_id);
878 * \brief Notifies a remote node that its predecessor may have changed.
879 * \param node the current node
880 * \param notify_id id of the node to notify
881 * \param candidate_id the possible new predecessor
883 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
885 task_data_t req_data = xbt_new0(s_task_data_t, 1);
886 req_data->type = TASK_NOTIFY;
887 req_data->request_id = predecessor_candidate_id;
888 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
890 // send a "Notify" request to notify_id
891 msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
892 XBT_DEBUG("Sending a 'Notify' request (task %p) to %d", task, notify_id);
893 char mailbox[MAILBOX_NAME_SIZE];
894 get_mailbox(notify_id, mailbox);
895 MSG_task_dsend(task, mailbox, task_free);
899 * \brief This function is called periodically.
900 * It refreshes the finger table of the current node.
901 * \param node the current node
903 static void fix_fingers(node_t node) {
905 XBT_DEBUG("Fixing fingers");
906 int i = node->next_finger_to_fix;
907 int id = find_successor(node, node->id + powers2[i]);
910 if (id != node->fingers[i].id) {
911 set_finger(node, i, id);
912 print_finger_table(node);
914 node->next_finger_to_fix = (i + 1) % nb_bits;
919 * \brief This function is called periodically.
920 * It checks whether the predecessor has failed
921 * \param node the current node
923 static void check_predecessor(node_t node)
925 XBT_DEBUG("Checking whether my predecessor is alive");
927 if(node->pred_id == -1)
932 char mailbox[MAILBOX_NAME_SIZE];
933 get_mailbox(node->pred_id, mailbox);
934 task_data_t req_data = xbt_new0(s_task_data_t,1);
935 req_data->type = TASK_PREDECESSOR_ALIVE;
936 req_data->request_id = node->pred_id;
937 get_mailbox(node->id, req_data->answer_to);
938 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
940 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
941 XBT_DEBUG("Sending a 'Predecessor Alive' request to my predecessor %d", node->pred_id);
943 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
946 XBT_DEBUG("Failed to send the 'Predecessor Alive' request (task %p) to %d", task_sent, node->pred_id);
947 task_free(task_sent);
950 // receive the answer
951 XBT_DEBUG("Sent 'Predecessor Alive' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
952 task_sent, node->pred_id, req_data->answer_to);
955 if (node->comm_receive == NULL) { // FIXME simplify this
956 msg_task_t task_received = NULL;
957 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
960 res = MSG_comm_wait(node->comm_receive, timeout);
963 XBT_DEBUG("Failed to receive the answer to my 'Predecessor Alive' request (task %p): %d",
964 task_sent, (int)res);
966 MSG_comm_destroy(node->comm_receive);
967 node->comm_receive = NULL;
970 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
971 if (task_received != task_sent) {
972 MSG_comm_destroy(node->comm_receive);
973 node->comm_receive = NULL;
974 handle_task(node, task_received);
976 XBT_DEBUG("Received the answer to my 'Predecessor Alive' request (task %p) : my predecessor %d is alive", task_received, node->pred_id);
978 MSG_comm_destroy(node->comm_receive);
979 node->comm_receive = NULL;
980 task_free(task_received);
988 * \brief Performs a find successor request to a random id.
989 * \param node the current node
991 static void random_lookup(node_t node)
995 find_successor(node, id);
997 /*** Random lookup disabled for tesh examples ***/
998 /*if(node->stream == NULL)
999 node->stream = RngStream_CreateStream("");
1000 int random_index = RngStream_RandInt (node->stream, 0, nb_bits - 1);
1001 int random_id = node->fingers[random_index].id;
1002 XBT_DEBUG("Making a lookup request for id %d", random_id);
1003 int res = find_successor(node, random_id);
1004 XBT_DEBUG("The successor of node %d is %d", random_id, res);*/
1009 * \brief Main function.
1011 int main(int argc, char *argv[])
1013 MSG_init(&argc, argv);
1015 printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
1016 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
1020 char **options = &argv[1];
1021 while (!strncmp(options[0], "-", 1)) {
1023 int length = strlen("-nb_bits=");
1024 if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
1025 nb_bits = atoi(options[0] + length);
1026 XBT_DEBUG("Set nb_bits to %d", nb_bits);
1030 length = strlen("-timeout=");
1031 if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
1032 timeout = atoi(options[0] + length);
1033 XBT_DEBUG("Set timeout to %d", timeout);
1036 xbt_die("Invalid chord option '%s'", options[0]);
1042 const char* platform_file = options[0];
1043 const char* application_file = options[1];
1047 MSG_create_environment(platform_file);
1049 MSG_function_register("node", node);
1050 MSG_launch_application(application_file);
1052 msg_error_t res = MSG_main();
1053 XBT_CRITICAL("Messages created: %ld", smx_total_comms);
1054 XBT_INFO("Simulated time: %g", MSG_get_clock());