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 #include "simgrid/modelchecker.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(chord_liveness,
26 "Messages specific for this 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 //extern long int smx_total_comms;
46 typedef struct s_finger {
48 char mailbox[MAILBOX_NAME_SIZE]; // string representation of the id
49 } s_finger_t, *finger_t;
54 typedef struct s_node {
56 char mailbox[MAILBOX_NAME_SIZE]; // my mailbox name (string representation of the id)
57 s_finger_t *fingers; // finger table, of size nb_bits (fingers[0] is my successor)
58 int pred_id; // predecessor id
59 char pred_mailbox[MAILBOX_NAME_SIZE]; // predecessor's mailbox name
60 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
61 msg_comm_t comm_receive; // current communication to receive
62 double last_change_date; // last time I changed a finger or my predecessor
66 * Types of tasks exchanged between nodes.
70 TASK_FIND_SUCCESSOR_ANSWER,
72 TASK_GET_PREDECESSOR_ANSWER,
74 TASK_SUCCESSOR_LEAVING,
75 TASK_PREDECESSOR_LEAVING
79 * Data attached with the tasks sent and received
81 typedef struct s_task_data {
82 e_task_type_t type; // type of task
83 int request_id; // id paramater (used by some types of tasks)
84 int request_finger; // finger parameter (used by some types of tasks)
85 int answer_id; // answer (used by some types of tasks)
86 char answer_to[MAILBOX_NAME_SIZE]; // mailbox to send an answer to (if any)
87 const char* issuer_host_name; // used for logging
88 } s_task_data_t, *task_data_t;
93 static void chord_initialize(void);
94 static void chord_exit(void);
95 static int normalize(int id);
96 static int is_in_interval(int id, int start, int end);
97 static void get_mailbox(int host_id, char* mailbox);
98 static void task_free(void* task);
99 static void print_finger_table(node_t node);
100 static void set_finger(node_t node, int finger_index, int id);
101 static void set_predecessor(node_t node, int predecessor_id);
104 static int node(int argc, char *argv[]);
105 static void handle_task(node_t node, msg_task_t task);
108 static void create(node_t node);
109 static int join(node_t node, int known_id);
110 static void leave(node_t node);
111 static int find_successor(node_t node, int id);
112 static int remote_find_successor(node_t node, int ask_to_id, int id);
113 static int remote_get_predecessor(node_t node, int ask_to_id);
114 static int closest_preceding_node(node_t node, int id);
115 static void stabilize(node_t node);
116 static void notify(node_t node, int predecessor_candidate_id);
117 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
118 static void fix_fingers(node_t node);
119 static void check_predecessor(node_t node);
120 static void random_lookup(node_t);
121 static void quit_notify(node_t node);
123 /* Global variables corresponding to atomic propositions */
125 int node_deliver = 0;
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);
220 * \brief Displays the finger table of a node.
223 static void print_finger_table(node_t node)
225 if (XBT_LOG_ISENABLED(chord_liveness, 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[])
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;
297 xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
299 // initialize my node
301 node.id = atoi(argv[1]);
302 get_mailbox(node.id, node.mailbox);
303 node.next_finger_to_fix = 0;
304 node.fingers = xbt_new0(s_finger_t, nb_bits);
305 node.last_change_date = init_time;
307 for (i = 0; i < nb_bits; i++) {
308 node.fingers[i].id = -1;
309 set_finger(&node, i, node.id);
312 if (argc == 3) { // first ring
313 deadline = atof(argv[2]);
318 int known_id = atoi(argv[2]);
319 //double sleep_time = atof(argv[3]);
320 deadline = atof(argv[4]);
323 // sleep before starting
324 XBT_DEBUG("Let's sleep during %f", sleep_time);
325 MSG_process_sleep(sleep_time);
327 XBT_DEBUG("Hey! Let's join the system.");
329 join_success = join(&node, known_id);
332 XBT_INFO("Node %d joined the ring", node.id);
337 MC_assert(!node_join);
341 while (MSG_get_clock() < init_time + deadline
342 // && MSG_get_clock() < node.last_change_date + 1000
343 && MSG_get_clock() < max_simulation_time) {
345 if (node.comm_receive == NULL) {
346 task_received = NULL;
347 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
348 // FIXME: do not make MSG_task_irecv() calls from several functions
351 if (!MSG_comm_test(node.comm_receive)) {
357 }else if(MC_random()){
359 }else if(MC_random()){
360 check_predecessor(&node);
361 }else if(MC_random()){
362 random_lookup(&node);
364 MSG_process_sleep(5);
367 if (MSG_get_clock() >= next_stabilize_date) {
369 next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
371 else if (MSG_get_clock() >= next_fix_fingers_date) {
373 next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
375 else if (MSG_get_clock() >= next_check_predecessor_date) {
376 check_predecessor(&node);
377 next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
379 else if (MSG_get_clock() >= next_lookup_date) {
380 random_lookup(&node);
381 next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
384 // nothing to do: sleep for a while
385 MSG_process_sleep(5);
389 if (MSG_get_clock() >= next_stabilize_date) {
391 next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
393 else if (MSG_get_clock() >= next_fix_fingers_date) {
395 next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
397 else if (MSG_get_clock() >= next_check_predecessor_date) {
398 check_predecessor(&node);
399 next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
401 else if (MSG_get_clock() >= next_lookup_date) {
402 random_lookup(&node);
403 next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
406 // nothing to do: sleep for a while
407 MSG_process_sleep(5);
413 if (node.comm_receive) {
417 XBT_INFO("A transfer has occured");
419 // a transfer has occured
421 msg_error_t status = MSG_comm_get_status(node.comm_receive);
423 if (status != MSG_OK) {
424 XBT_INFO("Failed to receive a task. Nevermind.");
425 MSG_comm_destroy(node.comm_receive);
426 node.comm_receive = NULL;
429 // the task was successfully received
430 XBT_INFO("The task was successfully received by node %d", node.id);
431 MSG_comm_destroy(node.comm_receive);
432 node.comm_receive = NULL;
433 handle_task(&node, task_received);
439 if (node.comm_receive) {
440 MSG_comm_destroy(node.comm_receive);
441 node.comm_receive = NULL;
448 // stop the simulation
449 xbt_free(node.fingers);
454 * \brief This function is called when the current node receives a task.
455 * \param node the current node
456 * \param task the task to handle (don't touch it then:
457 * it will be destroyed, reused or forwarded)
459 static void handle_task(node_t node, msg_task_t task) {
461 XBT_DEBUG("Handling task %p", task);
462 char mailbox[MAILBOX_NAME_SIZE];
463 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
464 e_task_type_t type = task_data->type;
468 case TASK_FIND_SUCCESSOR:
470 XBT_DEBUG("Receiving a 'Find Successor' request from %s for id %d",
471 task_data->issuer_host_name, task_data->request_id);
472 // is my successor the successor?
473 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
474 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
475 task_data->answer_id = node->fingers[0].id;
476 XBT_DEBUG("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
477 task_data->issuer_host_name,
478 task_data->answer_to,
479 task_data->request_id, task_data->answer_id);
480 MSG_task_dsend(task, task_data->answer_to, task_free);
483 // otherwise, forward the request to the closest preceding finger in my table
484 int closest = closest_preceding_node(node, task_data->request_id);
485 XBT_DEBUG("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
486 task_data->request_id, closest);
487 get_mailbox(closest, mailbox);
488 MSG_task_dsend(task, mailbox, task_free);
492 case TASK_GET_PREDECESSOR:
493 XBT_DEBUG("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
494 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
495 task_data->answer_id = node->pred_id;
496 XBT_DEBUG("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
497 task_data->issuer_host_name,
498 task_data->answer_to, task_data->answer_id);
499 MSG_task_dsend(task, task_data->answer_to, task_free);
503 // someone is telling me that he may be my new predecessor
504 XBT_DEBUG("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
505 notify(node, task_data->request_id);
509 case TASK_PREDECESSOR_LEAVING:
510 // my predecessor is about to quit
511 XBT_DEBUG("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
512 // modify my predecessor
513 set_predecessor(node, task_data->request_id);
516 >> notify my new predecessor
517 >> send a notify_predecessors !!
521 case TASK_SUCCESSOR_LEAVING:
522 // my successor is about to quit
523 XBT_DEBUG("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
524 // modify my successor FIXME : this should be implicit ?
525 set_finger(node, 0, task_data->request_id);
528 >> notify my new successor
529 >> update my table & predecessors table */
532 case TASK_FIND_SUCCESSOR_ANSWER:
533 case TASK_GET_PREDECESSOR_ANSWER:
534 XBT_DEBUG("Ignoring unexpected task of type %d (%p)", (int)type, task);
541 * \brief Initializes the current node as the first one of the system.
542 * \param node the current node
544 static void create(node_t node)
546 XBT_DEBUG("Create a new Chord ring...");
547 set_predecessor(node, -1); // -1 means that I have no predecessor
548 print_finger_table(node);
552 * \brief Makes the current node join the ring, knowing the id of a node
553 * already in the ring
554 * \param node the current node
555 * \param known_id id of a node already in the ring
556 * \return 1 if the join operation succeeded, 0 otherwise
558 static int join(node_t node, int known_id)
560 XBT_INFO("Joining the ring with id %d, knowing node %d", node->id, known_id);
561 set_predecessor(node, -1); // no predecessor (yet)
565 for (i = 0; i < nb_bits; i++) {
566 set_finger(node, i, known_id);
570 int successor_id = remote_find_successor(node, known_id, node->id);
571 if (successor_id == -1) {
572 XBT_INFO("Cannot join the ring.");
575 set_finger(node, 0, successor_id);
576 print_finger_table(node);
579 return successor_id != -1;
583 * \brief Makes the current node quit the system
584 * \param node the current node
586 static void leave(node_t node)
588 XBT_DEBUG("Well Guys! I Think it's time for me to quit ;)");
593 * \brief Notifies the successor and the predecessor of the current node
595 * \param node the current node
597 static void quit_notify(node_t node)
599 char mailbox[MAILBOX_NAME_SIZE];
600 //send the PREDECESSOR_LEAVING to our successor
601 task_data_t req_data = xbt_new0(s_task_data_t,1);
602 req_data->type = TASK_PREDECESSOR_LEAVING;
603 req_data->request_id = node->pred_id;
604 get_mailbox(node->id, req_data->answer_to);
605 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
607 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
608 XBT_DEBUG("Sending a 'PREDECESSOR_LEAVING' to my successor %d",node->fingers[0].id);
609 MSG_task_send_with_timeout(task_sent, node->fingers[0].mailbox, timeout);
611 //send the SUCCESSOR_LEAVING to our predecessor
612 get_mailbox(node->pred_id, mailbox);
613 task_data_t req_data_s = xbt_new0(s_task_data_t,1);
614 req_data_s->type = TASK_SUCCESSOR_LEAVING;
615 req_data_s->request_id = node->fingers[0].id;
616 req_data_s->request_id = node->pred_id;
617 get_mailbox(node->id, req_data_s->answer_to);
618 req_data_s->issuer_host_name = MSG_host_get_name(MSG_host_self());
620 msg_task_t task_sent_s = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data_s);
621 XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d",node->pred_id);
622 MSG_task_send_with_timeout(task_sent_s, mailbox, timeout);
627 * \brief Makes the current node find the successor node of an id.
628 * \param node the current node
629 * \param id the id to find
630 * \return the id of the successor node, or -1 if the request failed
632 static int find_successor(node_t node, int id)
634 // is my successor the successor?
635 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
636 return node->fingers[0].id;
639 // otherwise, ask the closest preceding finger in my table
640 int closest = closest_preceding_node(node, id);
641 return remote_find_successor(node, closest, id);
645 * \brief Asks another node the successor node of an id.
646 * \param node the current node
647 * \param ask_to the node to ask to
648 * \param id the id to find
649 * \return the id of the successor node, or -1 if the request failed
651 static int remote_find_successor(node_t node, int ask_to, int id)
655 char mailbox[MAILBOX_NAME_SIZE];
656 get_mailbox(ask_to, mailbox);
657 task_data_t req_data = xbt_new0(s_task_data_t, 1);
658 req_data->type = TASK_FIND_SUCCESSOR;
659 req_data->request_id = id;
660 get_mailbox(node->id, req_data->answer_to);
661 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
663 // send a "Find Successor" request to ask_to_id
664 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
665 XBT_DEBUG("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
666 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
669 XBT_DEBUG("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
670 task_sent, ask_to, id);
671 task_free(task_sent);
675 // receive the answer
676 XBT_DEBUG("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
677 task_sent, ask_to, id);
680 if (node->comm_receive == NULL) {
681 msg_task_t task_received = NULL;
682 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
685 res = MSG_comm_wait(node->comm_receive, timeout);
688 XBT_DEBUG("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
689 task_sent, (int)res);
691 MSG_comm_destroy(node->comm_receive);
692 node->comm_receive = NULL;
695 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
696 XBT_DEBUG("Received a task (%p)", task_received);
697 task_data_t ans_data = MSG_task_get_data(task_received);
699 if (MC_is_active()) {
700 // the model-checker is expected to find a counter-example here.
702 // As you can see in the test right below, task_received is not always equal to task_sent
703 // (as messages from differing round can interleave). But the previous version of this code
704 // wrongly assumed that, leading to problems. But this only occured on large platforms,
705 // leading to hardly usable traces. So we used the model-checker to track down the issue,
706 // and we came down to this test, that explained the bug in a snap.
707 //MC_assert(task_received == task_sent);
710 if (task_received != task_sent) {
711 // this is not the expected answer
712 MSG_comm_destroy(node->comm_receive);
713 node->comm_receive = NULL;
714 handle_task(node, task_received);
717 // this is our answer
718 XBT_DEBUG("Received the answer to my 'Find Successor' request for id %d (task %p): the successor of key %d is %d",
719 ans_data->request_id, task_received, id, ans_data->answer_id);
720 successor = ans_data->answer_id;
722 MSG_comm_destroy(node->comm_receive);
723 node->comm_receive = NULL;
724 task_free(task_received);
734 * \brief Asks another node its predecessor.
735 * \param node the current node
736 * \param ask_to the node to ask to
737 * \return the id of its predecessor node, or -1 if the request failed
738 * (or if the node does not know its predecessor)
740 static int remote_get_predecessor(node_t node, int ask_to)
742 int predecessor_id = -1;
744 char mailbox[MAILBOX_NAME_SIZE];
745 get_mailbox(ask_to, mailbox);
746 task_data_t req_data = xbt_new0(s_task_data_t, 1);
747 req_data->type = TASK_GET_PREDECESSOR;
748 get_mailbox(node->id, req_data->answer_to);
749 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
751 // send a "Get Predecessor" request to ask_to_id
752 XBT_DEBUG("Sending a 'Get Predecessor' request to %d", ask_to);
753 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
754 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
757 XBT_DEBUG("Failed to send the 'Get Predecessor' request (task %p) to %d",
759 task_free(task_sent);
763 // receive the answer
764 XBT_DEBUG("Sent 'Get Predecessor' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
765 task_sent, ask_to, req_data->answer_to);
768 if (node->comm_receive == NULL) { // FIXME simplify this
769 msg_task_t task_received = NULL;
770 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
773 res = MSG_comm_wait(node->comm_receive, timeout);
776 XBT_DEBUG("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
777 task_sent, (int)res);
779 MSG_comm_destroy(node->comm_receive);
780 node->comm_receive = NULL;
783 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
784 task_data_t ans_data = MSG_task_get_data(task_received);
786 /*if (MC_is_active()) {
787 MC_assert(task_received == task_sent);
790 if (task_received != task_sent) {
791 MSG_comm_destroy(node->comm_receive);
792 node->comm_receive = NULL;
793 handle_task(node, task_received);
796 XBT_DEBUG("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
797 task_received, ask_to, ans_data->answer_id);
798 predecessor_id = ans_data->answer_id;
800 MSG_comm_destroy(node->comm_receive);
801 node->comm_receive = NULL;
802 task_free(task_received);
808 return predecessor_id;
812 * \brief Returns the closest preceding finger of an id
813 * with respect to the finger table of the current node.
814 * \param node the current node
815 * \param id the id to find
816 * \return the closest preceding finger of that id
818 int closest_preceding_node(node_t node, int id)
821 for (i = nb_bits - 1; i >= 0; i--) {
822 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
823 return node->fingers[i].id;
830 * \brief This function is called periodically. It checks the immediate
831 * successor of the current node.
832 * \param node the current node
834 static void stabilize(node_t node)
836 XBT_DEBUG("Stabilizing node %d", node->id);
838 // get the predecessor of my immediate successor
840 int successor_id = node->fingers[0].id;
841 if (successor_id != node->id) {
842 candidate_id = remote_get_predecessor(node, successor_id);
845 candidate_id = node->pred_id;
848 // this node is a candidate to become my new successor
849 if (candidate_id != -1
850 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
851 set_finger(node, 0, candidate_id);
853 if (successor_id != node->id) {
854 remote_notify(node, successor_id, node->id);
859 * \brief Notifies the current node that its predecessor may have changed.
860 * \param node the current node
861 * \param candidate_id the possible new predecessor
863 static void notify(node_t node, int predecessor_candidate_id) {
865 XBT_DEBUG("Notifying node %d", node->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");
931 * \brief Performs a find successor request to a random id.
932 * \param node the current node
934 static void random_lookup(node_t node)
936 int id = 1337; // TODO pick a pseudorandom id
937 XBT_DEBUG("Making a lookup request for id %d", id);
938 find_successor(node, id);
941 static int predJoin(void){
945 static int predDeliver(void){
951 * \brief Main function.
953 int main(int argc, char *argv[])
955 MSG_init(&argc, argv);
957 printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
958 printf("example: %s ../../msg_platform.xml deploy_chord_liveness.xml\n", argv[0]);
962 char **options = &argv[1];
963 while (!strncmp(options[0], "-", 1)) {
965 int length = strlen("-nb_bits=");
966 if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
967 nb_bits = atoi(options[0] + length);
968 XBT_DEBUG("Set nb_bits to %d", nb_bits);
972 length = strlen("-timeout=");
973 if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
974 timeout = atoi(options[0] + length);
975 XBT_DEBUG("Set timeout to %d", timeout);
978 xbt_die("Invalid chord option '%s'", options[0]);
984 const char* platform_file = options[0];
985 const char* application_file = options[1];
989 MC_automaton_new_propositional_symbol("join", &predJoin);
990 MC_automaton_new_propositional_symbol("deliver", &predDeliver);
992 MSG_create_environment(platform_file);
994 MSG_function_register("node", node);
995 MSG_launch_application(application_file);
997 msg_error_t res = MSG_main();
998 XBT_INFO("Simulated time: %g", MSG_get_clock());