1 /* Copyright (c) 2010-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/asserts.h"
11 #include "simgrid/modelchecker.h"
12 #include <xbt/RngStream.h>
14 /** @addtogroup MSG_examples
16 * - <b>chord/chord.c: Classical Chord P2P protocol</b>
17 * This example implements the well known Chord P2P protocol. Its
18 * main advantage is that it constitute a fully working non-trivial
19 * example. In addition, its implementation is rather efficient, as
20 * demonstrated in http://hal.inria.fr/inria-00602216/
24 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
25 "Messages specific for this msg example");
29 #define MAILBOX_NAME_SIZE 10
31 static int nb_bits = 24;
32 static int nb_keys = 0;
33 static int timeout = 50;
34 static int max_simulation_time = 1000;
35 static int periodic_stabilize_delay = 20;
36 static int periodic_fix_fingers_delay = 120;
37 static int periodic_check_predecessor_delay = 120;
38 static int periodic_lookup_delay = 10;
40 static const double sleep_delay = 4.9999;
42 extern long int smx_total_comms;
47 typedef struct s_finger {
49 char mailbox[MAILBOX_NAME_SIZE]; // string representation of the id
50 } s_finger_t, *finger_t;
55 typedef struct s_node {
57 char mailbox[MAILBOX_NAME_SIZE]; // my mailbox name (string representation of the id)
58 s_finger_t *fingers; // finger table, of size nb_bits (fingers[0] is my successor)
59 int pred_id; // predecessor id
60 char pred_mailbox[MAILBOX_NAME_SIZE]; // predecessor's mailbox name
61 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
62 msg_comm_t comm_receive; // current communication to receive
63 double last_change_date; // last time I changed a finger or my predecessor
64 RngStream stream; //RngStream for
68 * Types of tasks exchanged between nodes.
72 TASK_FIND_SUCCESSOR_ANSWER,
74 TASK_GET_PREDECESSOR_ANSWER,
76 TASK_SUCCESSOR_LEAVING,
77 TASK_PREDECESSOR_LEAVING,
78 TASK_PREDECESSOR_ALIVE,
79 TASK_PREDECESSOR_ALIVE_ANSWER
83 * Data attached with the tasks sent and received
85 typedef struct s_task_data {
86 e_task_type_t type; // type of task
87 int request_id; // id paramater (used by some types of tasks)
88 int request_finger; // finger parameter (used by some types of tasks)
89 int answer_id; // answer (used by some types of tasks)
90 char answer_to[MAILBOX_NAME_SIZE]; // mailbox to send an answer to (if any)
91 const char* issuer_host_name; // used for logging
92 } s_task_data_t, *task_data_t;
97 static void chord_initialize(void);
98 static void chord_exit(void);
99 static int normalize(int id);
100 static int is_in_interval(int id, int start, int end);
101 static void get_mailbox(int host_id, char* mailbox);
102 static void task_free(void* task);
103 static void print_finger_table(node_t node);
104 static void set_finger(node_t node, int finger_index, int id);
105 static void set_predecessor(node_t node, int predecessor_id);
108 static int node(int argc, char *argv[]);
109 static void handle_task(node_t node, msg_task_t task);
112 static void create(node_t node);
113 static int join(node_t node, int known_id);
114 static void leave(node_t node);
115 static int find_successor(node_t node, int id);
116 static int remote_find_successor(node_t node, int ask_to_id, int id);
117 static int remote_get_predecessor(node_t node, int ask_to_id);
118 static int closest_preceding_node(node_t node, int id);
119 static void stabilize(node_t node);
120 static void notify(node_t node, int predecessor_candidate_id);
121 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
122 static void fix_fingers(node_t node);
123 static void check_predecessor(node_t node);
124 static void random_lookup(node_t);
125 static void quit_notify(node_t node);
128 * \brief Global initialization of the Chord simulation.
130 static void chord_initialize(void)
132 // compute the powers of 2 once for all
133 powers2 = xbt_new(int, nb_bits);
136 for (i = 0; i < nb_bits; i++) {
141 XBT_DEBUG("Sets nb_keys to %d", nb_keys);
144 static void chord_exit(void)
150 * \brief Turns an id into an equivalent id in [0, nb_keys).
152 * \return the corresponding normalized id
154 static int normalize(int id)
156 // like id % nb_keys, but works with negatives numbers (and faster)
157 return id & (nb_keys - 1);
161 * \brief Returns whether an id belongs to the interval [start, end].
163 * The parameters are noramlized to make sure they are between 0 and nb_keys - 1).
164 * 1 belongs to [62, 3]
165 * 1 does not belong to [3, 62]
166 * 63 belongs to [62, 3]
167 * 63 does not belong to [3, 62]
168 * 24 belongs to [21, 29]
169 * 24 does not belong to [29, 21]
171 * \param id id to check
172 * \param start lower bound
173 * \param end upper bound
174 * \return a non-zero value if id in in [start, end]
176 static int is_in_interval(int id, int start, int end)
179 start = normalize(start);
180 end = normalize(end);
182 // make sure end >= start and id >= start
195 * \brief Gets the mailbox name of a host given its chord id.
196 * \param node_id id of a node
197 * \param mailbox pointer to where the mailbox name should be written
198 * (there must be enough space)
200 static void get_mailbox(int node_id, char* mailbox)
202 snprintf(mailbox, MAILBOX_NAME_SIZE - 1, "%d", node_id);
206 * \brief Frees the memory used by a task.
207 * \param task the MSG task to destroy
209 static void task_free(void* task)
211 // TODO add a parameter data_free_function to MSG_task_create?
213 xbt_free(MSG_task_get_data(task));
214 MSG_task_destroy(task);
219 * \brief Displays the finger table of a node.
222 static void print_finger_table(node_t node)
224 if (XBT_LOG_ISENABLED(msg_chord, xbt_log_priority_verbose)) {
226 XBT_VERB("My finger table:");
227 XBT_VERB("Start | Succ ");
228 for (i = 0; i < nb_bits; i++) {
229 XBT_VERB(" %3d | %3d ", (node->id + powers2[i]) % nb_keys, node->fingers[i].id);
231 XBT_VERB("Predecessor: %d", node->pred_id);
236 * \brief Sets a finger of the current node.
237 * \param node the current node
238 * \param finger_index index of the finger to set (0 to nb_bits - 1)
239 * \param id the id to set for this finger
241 static void set_finger(node_t node, int finger_index, int id)
243 if (id != node->fingers[finger_index].id) {
244 node->fingers[finger_index].id = id;
245 get_mailbox(id, node->fingers[finger_index].mailbox);
246 node->last_change_date = MSG_get_clock();
247 XBT_DEBUG("My new finger #%d is %d", finger_index, id);
252 * \brief Sets the predecessor of the current node.
253 * \param node the current node
254 * \param id the id to predecessor, or -1 to unset the predecessor
256 static void set_predecessor(node_t node, int predecessor_id)
258 if (predecessor_id != node->pred_id) {
259 node->pred_id = predecessor_id;
261 if (predecessor_id != -1) {
262 get_mailbox(predecessor_id, node->pred_mailbox);
264 node->last_change_date = MSG_get_clock();
266 XBT_DEBUG("My new predecessor is %d", predecessor_id);
271 * \brief Node Function
274 * - the id of a guy I know in the system (except for the first node)
275 * - the time to sleep before I join (except for the first node)
277 int node(int argc, char *argv[])
280 /* Reduce the run size for the MC */
282 periodic_stabilize_delay = 8;
283 periodic_fix_fingers_delay = 8;
284 periodic_check_predecessor_delay = 8;
287 double init_time = MSG_get_clock();
288 msg_task_t task_received = NULL;
290 int join_success = 0;
292 double next_stabilize_date = init_time + periodic_stabilize_delay;
293 double next_fix_fingers_date = init_time + periodic_fix_fingers_delay;
294 double next_check_predecessor_date = init_time + periodic_check_predecessor_delay;
295 double next_lookup_date = init_time + periodic_lookup_delay;
299 int sub_protocol = 0;
301 xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
303 // initialize my node
305 node.id = atoi(argv[1]);
306 get_mailbox(node.id, node.mailbox);
307 node.next_finger_to_fix = 0;
308 node.fingers = xbt_new0(s_finger_t, nb_bits);
309 node.last_change_date = init_time;
311 for (i = 0; i < nb_bits; i++) {
312 node.fingers[i].id = -1;
313 set_finger(&node, i, node.id);
316 if (argc == 3) { // first ring
317 deadline = atof(argv[2]);
322 int known_id = atoi(argv[2]);
323 //double sleep_time = atof(argv[3]);
324 deadline = atof(argv[4]);
327 // sleep before starting
328 XBT_DEBUG("Let's sleep during %f", sleep_time);
329 MSG_process_sleep(sleep_time);
331 XBT_DEBUG("Hey! Let's join the system.");
333 join_success = join(&node, known_id);
337 while (MSG_get_clock() < init_time + deadline
338 // && MSG_get_clock() < node.last_change_date + 1000
339 && MSG_get_clock() < max_simulation_time) {
341 if (node.comm_receive == NULL) {
342 task_received = NULL;
343 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
344 // FIXME: do not make MSG_task_irecv() calls from several functions
347 //XBT_INFO("Node %d is ring member : %d", node.id, is_ring_member(known_id, node.id) != -1);
349 if (!MSG_comm_test(node.comm_receive)) {
351 // no task was received: make some periodic calls
354 if(!MC_visited_reduction() && no_op){
357 if(listen == 0 && (sub_protocol = MC_random(0, 4)) > 0){
358 if(sub_protocol == 1)
360 else if(sub_protocol == 2)
362 else if(sub_protocol == 3)
363 check_predecessor(&node);
365 random_lookup(&node);
368 MSG_process_sleep(sleep_delay);
369 if(!MC_visited_reduction())
373 if (MSG_get_clock() >= next_stabilize_date) {
375 next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
376 }else if (MSG_get_clock() >= next_fix_fingers_date) {
378 next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
379 }else if (MSG_get_clock() >= next_check_predecessor_date) {
380 check_predecessor(&node);
381 next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
382 }else if (MSG_get_clock() >= next_lookup_date) {
383 random_lookup(&node);
384 next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
386 // nothing to do: sleep for a while
387 MSG_process_sleep(sleep_delay);
392 // a transfer has occurred
394 msg_error_t status = MSG_comm_get_status(node.comm_receive);
396 if (status != MSG_OK) {
397 XBT_DEBUG("Failed to receive a task. Nevermind.");
398 MSG_comm_destroy(node.comm_receive);
399 node.comm_receive = NULL;
402 // the task was successfully received
403 MSG_comm_destroy(node.comm_receive);
404 node.comm_receive = NULL;
405 handle_task(&node, task_received);
410 if (node.comm_receive) {
411 /* handle last task if any */
412 if (MSG_comm_wait(node.comm_receive, 0) == MSG_OK)
413 task_free(task_received);
414 MSG_comm_destroy(node.comm_receive);
415 node.comm_receive = NULL;
422 // stop the simulation
423 xbt_free(node.fingers);
428 * \brief This function is called when the current node receives a task.
429 * \param node the current node
430 * \param task the task to handle (don't touch it then:
431 * it will be destroyed, reused or forwarded)
433 static void handle_task(node_t node, msg_task_t task) {
435 XBT_DEBUG("Handling task %p", task);
436 char mailbox[MAILBOX_NAME_SIZE];
437 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
438 e_task_type_t type = task_data->type;
442 case TASK_FIND_SUCCESSOR:
443 XBT_DEBUG("Receiving a 'Find Successor' request from %s for id %d",
444 task_data->issuer_host_name, task_data->request_id);
445 // is my successor the successor?
446 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
447 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
448 task_data->answer_id = node->fingers[0].id;
449 XBT_DEBUG("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
450 task_data->issuer_host_name,
451 task_data->answer_to,
452 task_data->request_id, task_data->answer_id);
453 MSG_task_dsend(task, task_data->answer_to, task_free);
456 // otherwise, forward the request to the closest preceding finger in my table
457 int closest = closest_preceding_node(node, task_data->request_id);
458 XBT_DEBUG("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
459 task_data->request_id, closest);
460 get_mailbox(closest, mailbox);
461 MSG_task_dsend(task, mailbox, task_free);
465 case TASK_GET_PREDECESSOR:
466 XBT_DEBUG("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
467 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
468 task_data->answer_id = node->pred_id;
469 XBT_DEBUG("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
470 task_data->issuer_host_name,
471 task_data->answer_to, task_data->answer_id);
472 MSG_task_dsend(task, task_data->answer_to, task_free);
476 // someone is telling me that he may be my new predecessor
477 XBT_DEBUG("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
478 notify(node, task_data->request_id);
482 case TASK_PREDECESSOR_LEAVING:
483 // my predecessor is about to quit
484 XBT_DEBUG("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
485 // modify my predecessor
486 set_predecessor(node, task_data->request_id);
489 >> notify my new predecessor
490 >> send a notify_predecessors !!
494 case TASK_SUCCESSOR_LEAVING:
495 // my successor is about to quit
496 XBT_DEBUG("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
497 // modify my successor FIXME : this should be implicit ?
498 set_finger(node, 0, task_data->request_id);
501 >> notify my new successor
502 >> update my table & predecessors table */
505 case TASK_FIND_SUCCESSOR_ANSWER:
506 case TASK_GET_PREDECESSOR_ANSWER:
507 case TASK_PREDECESSOR_ALIVE_ANSWER:
508 XBT_DEBUG("Ignoring unexpected task of type %d (%p)", (int)type, task);
512 case TASK_PREDECESSOR_ALIVE:
513 XBT_DEBUG("Receiving a 'Predecessor Alive' request from %s", task_data->issuer_host_name);
514 task_data->type = TASK_PREDECESSOR_ALIVE_ANSWER;
515 XBT_DEBUG("Sending back a 'Predecessor Alive Answer' to %s (mailbox %s)",
516 task_data->issuer_host_name,
517 task_data->answer_to);
518 MSG_task_dsend(task, task_data->answer_to, task_free);
527 * \brief Initializes the current node as the first one of the system.
528 * \param node the current node
530 static void create(node_t node)
532 XBT_DEBUG("Create a new Chord ring...");
533 set_predecessor(node, -1); // -1 means that I have no predecessor
534 print_finger_table(node);
538 * \brief Makes the current node join the ring, knowing the id of a node
539 * already in the ring
540 * \param node the current node
541 * \param known_id id of a node already in the ring
542 * \return 1 if the join operation succeeded, 0 otherwise
544 static int join(node_t node, int known_id)
546 XBT_INFO("Joining the ring with id %d, knowing node %d", node->id, known_id);
547 set_predecessor(node, -1); // no predecessor (yet)
551 for (i = 0; i < nb_bits; i++) {
552 set_finger(node, i, known_id);
556 int successor_id = remote_find_successor(node, known_id, node->id);
557 if (successor_id == -1) {
558 XBT_INFO("Cannot join the ring.");
561 set_finger(node, 0, successor_id);
562 print_finger_table(node);
565 return successor_id != -1;
569 * \brief Makes the current node quit the system
570 * \param node the current node
572 static void leave(node_t node)
574 XBT_DEBUG("Well Guys! I Think it's time for me to quit ;)");
576 RngStream_DeleteStream(&node->stream);
580 * \brief Notifies the successor and the predecessor of the current node
582 * \param node the current node
584 static void quit_notify(node_t node)
586 char mailbox[MAILBOX_NAME_SIZE];
587 //send the PREDECESSOR_LEAVING to our successor
588 task_data_t req_data = xbt_new0(s_task_data_t,1);
589 req_data->type = TASK_PREDECESSOR_LEAVING;
590 req_data->request_id = node->pred_id;
591 get_mailbox(node->id, req_data->answer_to);
592 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
594 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
595 XBT_DEBUG("Sending a 'PREDECESSOR_LEAVING' to my successor %d",node->fingers[0].id);
596 if (MSG_task_send_with_timeout(task_sent, node->fingers[0].mailbox, timeout)==
598 XBT_DEBUG("Timeout expired when sending a 'PREDECESSOR_LEAVING' to my successor %d",
599 node->fingers[0].id);
600 task_free(task_sent);
603 //send the SUCCESSOR_LEAVING to our predecessor
604 get_mailbox(node->pred_id, mailbox);
605 task_data_t req_data_s = xbt_new0(s_task_data_t,1);
606 req_data_s->type = TASK_SUCCESSOR_LEAVING;
607 req_data_s->request_id = node->fingers[0].id;
608 req_data_s->request_id = node->pred_id;
609 get_mailbox(node->id, req_data_s->answer_to);
610 req_data_s->issuer_host_name = MSG_host_get_name(MSG_host_self());
612 msg_task_t task_sent_s = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data_s);
613 XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d",node->pred_id);
614 if (MSG_task_send_with_timeout(task_sent_s, mailbox, timeout)==
616 XBT_DEBUG("Timeout expired when sending a 'SUCCESSOR_LEAVING' to my predecessor %d",
618 task_free(task_sent_s);
624 * \brief Makes the current node find the successor node of an id.
625 * \param node the current node
626 * \param id the id to find
627 * \return the id of the successor node, or -1 if the request failed
629 static int find_successor(node_t node, int id)
631 // is my successor the successor?
632 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
633 return node->fingers[0].id;
636 // otherwise, ask the closest preceding finger in my table
637 int closest = closest_preceding_node(node, id);
638 return remote_find_successor(node, closest, id);
642 * \brief Asks another node the successor node of an id.
643 * \param node the current node
644 * \param ask_to the node to ask to
645 * \param id the id to find
646 * \return the id of the successor node, or -1 if the request failed
648 static int remote_find_successor(node_t node, int ask_to, int id)
652 char mailbox[MAILBOX_NAME_SIZE];
653 get_mailbox(ask_to, mailbox);
654 task_data_t req_data = xbt_new0(s_task_data_t, 1);
655 req_data->type = TASK_FIND_SUCCESSOR;
656 req_data->request_id = id;
657 get_mailbox(node->id, req_data->answer_to);
658 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
660 // send a "Find Successor" request to ask_to_id
661 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
662 XBT_DEBUG("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
663 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
666 XBT_DEBUG("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
667 task_sent, ask_to, id);
668 task_free(task_sent);
672 // receive the answer
673 XBT_DEBUG("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
674 task_sent, ask_to, id);
677 if (node->comm_receive == NULL) {
678 msg_task_t task_received = NULL;
679 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
682 res = MSG_comm_wait(node->comm_receive, timeout);
685 XBT_DEBUG("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
686 task_sent, (int)res);
688 MSG_comm_destroy(node->comm_receive);
689 node->comm_receive = NULL;
692 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
693 XBT_DEBUG("Received a task (%p)", task_received);
694 task_data_t ans_data = MSG_task_get_data(task_received);
696 // Once upon a time, our code assumed that here, task_received != task_sent all the time
698 // This assumption is wrong (as messages from differing round can interleave), leading to a bug in our code.
699 // We failed to find this bug directly, as it only occured on large platforms, leading to hardly usable traces.
700 // Instead, we used the model-checker to track down the issue by adding the following test here in the code:
701 // if (MC_is_active()) {
702 // MC_assert(task_received == task_sent);
704 // That explained the bug in a snap, with a very cool example and everything.
706 // This MC_assert is now desactivated as the case is now properly handled in our code and we don't want the
707 // MC to fail any further under that condition, but this comment is here to as a memorial for this first
708 // brillant victory of the model-checking in the SimGrid community :)
710 if (task_received != task_sent ||
711 ans_data->type != TASK_FIND_SUCCESSOR_ANSWER) {
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 ans_data->type != TASK_GET_PREDECESSOR_ANSWER) {
793 MSG_comm_destroy(node->comm_receive);
794 node->comm_receive = NULL;
795 handle_task(node, task_received);
798 XBT_DEBUG("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
799 task_received, ask_to, ans_data->answer_id);
800 predecessor_id = ans_data->answer_id;
802 MSG_comm_destroy(node->comm_receive);
803 node->comm_receive = NULL;
804 task_free(task_received);
810 return predecessor_id;
814 * \brief Returns the closest preceding finger of an id
815 * with respect to the finger table of the current node.
816 * \param node the current node
817 * \param id the id to find
818 * \return the closest preceding finger of that id
820 int closest_preceding_node(node_t node, int id)
823 for (i = nb_bits - 1; i >= 0; i--) {
824 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
825 return node->fingers[i].id;
832 * \brief This function is called periodically. It checks the immediate
833 * successor of the current node.
834 * \param node the current node
836 static void stabilize(node_t node)
838 XBT_DEBUG("Stabilizing node");
840 // get the predecessor of my immediate successor
842 int successor_id = node->fingers[0].id;
843 if (successor_id != node->id) {
844 candidate_id = remote_get_predecessor(node, successor_id);
847 candidate_id = node->pred_id;
850 // this node is a candidate to become my new successor
851 if (candidate_id != -1
852 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
853 set_finger(node, 0, candidate_id);
855 if (successor_id != node->id) {
856 remote_notify(node, successor_id, node->id);
861 * \brief Notifies the current node that its predecessor may have changed.
862 * \param node the current node
863 * \param candidate_id the possible new predecessor
865 static void notify(node_t node, int predecessor_candidate_id) {
867 if (node->pred_id == -1
868 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
870 set_predecessor(node, predecessor_candidate_id);
871 print_finger_table(node);
874 XBT_DEBUG("I don't have to change my predecessor to %d", predecessor_candidate_id);
879 * \brief Notifies a remote node that its predecessor may have changed.
880 * \param node the current node
881 * \param notify_id id of the node to notify
882 * \param candidate_id the possible new predecessor
884 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
886 task_data_t req_data = xbt_new0(s_task_data_t, 1);
887 req_data->type = TASK_NOTIFY;
888 req_data->request_id = predecessor_candidate_id;
889 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
891 // send a "Notify" request to notify_id
892 msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
893 XBT_DEBUG("Sending a 'Notify' request (task %p) to %d", task, notify_id);
894 char mailbox[MAILBOX_NAME_SIZE];
895 get_mailbox(notify_id, mailbox);
896 MSG_task_dsend(task, mailbox, task_free);
900 * \brief This function is called periodically.
901 * It refreshes the finger table of the current node.
902 * \param node the current node
904 static void fix_fingers(node_t node) {
906 XBT_DEBUG("Fixing fingers");
907 int i = node->next_finger_to_fix;
908 int id = find_successor(node, node->id + powers2[i]);
911 if (id != node->fingers[i].id) {
912 set_finger(node, i, id);
913 print_finger_table(node);
915 node->next_finger_to_fix = (i + 1) % nb_bits;
920 * \brief This function is called periodically.
921 * It checks whether the predecessor has failed
922 * \param node the current node
924 static void check_predecessor(node_t node)
926 XBT_DEBUG("Checking whether my predecessor is alive");
928 if(node->pred_id == -1)
933 char mailbox[MAILBOX_NAME_SIZE];
934 get_mailbox(node->pred_id, mailbox);
935 task_data_t req_data = xbt_new0(s_task_data_t,1);
936 req_data->type = TASK_PREDECESSOR_ALIVE;
937 req_data->request_id = node->pred_id;
938 get_mailbox(node->id, req_data->answer_to);
939 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
941 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
942 XBT_DEBUG("Sending a 'Predecessor Alive' request to my predecessor %d", node->pred_id);
944 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
947 XBT_DEBUG("Failed to send the 'Predecessor Alive' request (task %p) to %d", task_sent, node->pred_id);
948 task_free(task_sent);
951 // receive the answer
952 XBT_DEBUG("Sent 'Predecessor Alive' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
953 task_sent, node->pred_id, req_data->answer_to);
956 if (node->comm_receive == NULL) { // FIXME simplify this
957 msg_task_t task_received = NULL;
958 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
961 res = MSG_comm_wait(node->comm_receive, timeout);
964 XBT_DEBUG("Failed to receive the answer to my 'Predecessor Alive' request (task %p): %d",
965 task_sent, (int)res);
967 MSG_comm_destroy(node->comm_receive);
968 node->comm_receive = NULL;
971 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
972 if (task_received != task_sent) {
973 MSG_comm_destroy(node->comm_receive);
974 node->comm_receive = NULL;
975 handle_task(node, task_received);
977 XBT_DEBUG("Received the answer to my 'Predecessor Alive' request (task %p) : my predecessor %d is alive", task_received, node->pred_id);
979 MSG_comm_destroy(node->comm_receive);
980 node->comm_receive = NULL;
981 task_free(task_received);
989 * \brief Performs a find successor request to a random id.
990 * \param node the current node
992 static void random_lookup(node_t node)
996 find_successor(node, id);
998 /*** Random lookup disabled for tesh examples ***/
999 /*if(node->stream == NULL)
1000 node->stream = RngStream_CreateStream("");
1001 int random_index = RngStream_RandInt (node->stream, 0, nb_bits - 1);
1002 int random_id = node->fingers[random_index].id;
1003 XBT_DEBUG("Making a lookup request for id %d", random_id);
1004 int res = find_successor(node, random_id);
1005 XBT_DEBUG("The successor of node %d is %d", random_id, res);*/
1010 * \brief Main function.
1012 int main(int argc, char *argv[])
1014 MSG_init(&argc, argv);
1016 printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
1017 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
1021 char **options = &argv[1];
1022 while (!strncmp(options[0], "-", 1)) {
1024 int length = strlen("-nb_bits=");
1025 if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
1026 nb_bits = atoi(options[0] + length);
1027 XBT_DEBUG("Set nb_bits to %d", nb_bits);
1031 length = strlen("-timeout=");
1032 if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
1033 timeout = atoi(options[0] + length);
1034 XBT_DEBUG("Set timeout to %d", timeout);
1037 xbt_die("Invalid chord option '%s'", options[0]);
1043 const char* platform_file = options[0];
1044 const char* application_file = options[1];
1048 MSG_create_environment(platform_file);
1050 MSG_function_register("node", node);
1051 MSG_launch_application(application_file);
1053 msg_error_t res = MSG_main();
1054 XBT_CRITICAL("Messages created: %ld", smx_total_comms);
1055 XBT_INFO("Simulated time: %g", MSG_get_clock());