Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / examples / msg / chord / chord.c
1
2 /* Copyright (c) 2010. The SimGrid Team.
3  * All rights reserved.                                                     */
4
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. */
7
8 #include <stdio.h>
9 #include "msg/msg.h"
10 #include "xbt/log.h"
11 #include "xbt/asserts.h"
12 #include "simgrid/modelchecker.h"
13
14 /** @addtogroup MSG_examples
15  *
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/
21  */
22
23  
24  XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
25                              "Messages specific for this msg example");
26
27 #define COMM_SIZE 10
28 #define COMP_SIZE 0
29 #define MAILBOX_NAME_SIZE 10
30
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;
39
40 extern long int smx_total_comms;
41
42 /*
43  * Finger element.
44  */
45 typedef struct s_finger {
46   int id;
47   char mailbox[MAILBOX_NAME_SIZE]; // string representation of the id
48 } s_finger_t, *finger_t;
49
50 /*
51  * Node data.
52  */
53 typedef struct s_node {
54   int id;                                 // my id
55   char mailbox[MAILBOX_NAME_SIZE];        // my mailbox name (string representation of the id)
56   s_finger_t *fingers;                    // finger table, of size nb_bits (fingers[0] is my successor)
57   int pred_id;                            // predecessor id
58   char pred_mailbox[MAILBOX_NAME_SIZE];   // predecessor's mailbox name
59   int next_finger_to_fix;                 // index of the next finger to fix in fix_fingers()
60   msg_comm_t comm_receive;                // current communication to receive
61   double last_change_date;                // last time I changed a finger or my predecessor
62 } s_node_t, *node_t;
63
64 /**
65  * Types of tasks exchanged between nodes.
66  */
67 typedef enum {
68   TASK_FIND_SUCCESSOR,
69   TASK_FIND_SUCCESSOR_ANSWER,
70   TASK_GET_PREDECESSOR,
71   TASK_GET_PREDECESSOR_ANSWER,
72   TASK_NOTIFY,
73   TASK_SUCCESSOR_LEAVING,
74   TASK_PREDECESSOR_LEAVING
75 } e_task_type_t;
76
77 /*
78  * Data attached with the tasks sent and received
79  */
80 typedef struct s_task_data {
81   e_task_type_t type;                     // type of task
82   int request_id;                         // id paramater (used by some types of tasks)
83   int request_finger;                     // finger parameter (used by some types of tasks)
84   int answer_id;                          // answer (used by some types of tasks)
85   char answer_to[MAILBOX_NAME_SIZE];      // mailbox to send an answer to (if any)
86   const char* issuer_host_name;           // used for logging
87 } s_task_data_t, *task_data_t;
88
89 static int *powers2;
90
91 // utility functions
92 static void chord_initialize(void);
93 static void chord_exit(void);
94 static int normalize(int id);
95 static int is_in_interval(int id, int start, int end);
96 static void get_mailbox(int host_id, char* mailbox);
97 static void task_free(void* task);
98 static void print_finger_table(node_t node);
99 static void set_finger(node_t node, int finger_index, int id);
100 static void set_predecessor(node_t node, int predecessor_id);
101
102 // process functions
103 static int node(int argc, char *argv[]);
104 static void handle_task(node_t node, msg_task_t task);
105
106 // Chord core
107 static void create(node_t node);
108 static int join(node_t node, int known_id);
109 static void leave(node_t node);
110 static int find_successor(node_t node, int id);
111 static int remote_find_successor(node_t node, int ask_to_id, int id);
112 static int remote_get_predecessor(node_t node, int ask_to_id);
113 static int closest_preceding_node(node_t node, int id);
114 static void stabilize(node_t node);
115 static void notify(node_t node, int predecessor_candidate_id);
116 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
117 static void fix_fingers(node_t node);
118 static void check_predecessor(node_t node);
119 static void random_lookup(node_t);
120 static void quit_notify(node_t node);
121
122 /**
123  * \brief Global initialization of the Chord simulation.
124  */
125 static void chord_initialize(void)
126 {
127   // compute the powers of 2 once for all
128   powers2 = xbt_new(int, nb_bits);
129   int pow = 1;
130   int i;
131   for (i = 0; i < nb_bits; i++) {
132     powers2[i] = pow;
133     pow = pow << 1;
134   }
135   nb_keys = pow;
136   XBT_DEBUG("Sets nb_keys to %d", nb_keys);
137 }
138
139 static void chord_exit(void)
140 {
141   xbt_free(powers2);
142 }
143
144 /**
145  * \brief Turns an id into an equivalent id in [0, nb_keys).
146  * \param id an id
147  * \return the corresponding normalized id
148  */
149 static int normalize(int id)
150 {
151   // like id % nb_keys, but works with negatives numbers (and faster)
152   return id & (nb_keys - 1);
153 }
154
155 /**
156  * \brief Returns whether an id belongs to the interval [start, end].
157  *
158  * The parameters are noramlized to make sure they are between 0 and nb_keys - 1).
159  * 1 belongs to [62, 3]
160  * 1 does not belong to [3, 62]
161  * 63 belongs to [62, 3]
162  * 63 does not belong to [3, 62]
163  * 24 belongs to [21, 29]
164  * 24 does not belong to [29, 21]
165  *
166  * \param id id to check
167  * \param start lower bound
168  * \param end upper bound
169  * \return a non-zero value if id in in [start, end]
170  */
171 static int is_in_interval(int id, int start, int end)
172 {
173   id = normalize(id);
174   start = normalize(start);
175   end = normalize(end);
176
177   // make sure end >= start and id >= start
178   if (end < start) {
179     end += nb_keys;
180   }
181
182   if (id < start) {
183     id += nb_keys;
184   }
185
186   return id <= end;
187 }
188
189 /**
190  * \brief Gets the mailbox name of a host given its chord id.
191  * \param node_id id of a node
192  * \param mailbox pointer to where the mailbox name should be written
193  * (there must be enough space)
194  */
195 static void get_mailbox(int node_id, char* mailbox)
196 {
197   snprintf(mailbox, MAILBOX_NAME_SIZE - 1, "%d", node_id);
198 }
199
200 /**
201  * \brief Frees the memory used by a task.
202  * \param task the MSG task to destroy
203  */
204 static void task_free(void* task)
205 {
206   // TODO add a parameter data_free_function to MSG_task_create?
207   if(task != NULL){
208     xbt_free(MSG_task_get_data(task));
209     MSG_task_destroy(task);
210   }
211 }
212
213 /**
214  * \brief Displays the finger table of a node.
215  * \param node a node
216  */
217 static void print_finger_table(node_t node)
218 {
219   if (XBT_LOG_ISENABLED(msg_chord, xbt_log_priority_verbose)) {
220     int i;
221     XBT_VERB("My finger table:");
222     XBT_VERB("Start | Succ ");
223     for (i = 0; i < nb_bits; i++) {
224       XBT_VERB(" %3d  | %3d ", (node->id + powers2[i]) % nb_keys, node->fingers[i].id);
225     }
226     XBT_VERB("Predecessor: %d", node->pred_id);
227   }
228 }
229
230 /**
231  * \brief Sets a finger of the current node.
232  * \param node the current node
233  * \param finger_index index of the finger to set (0 to nb_bits - 1)
234  * \param id the id to set for this finger
235  */
236 static void set_finger(node_t node, int finger_index, int id)
237 {
238   if (id != node->fingers[finger_index].id) {
239     node->fingers[finger_index].id = id;
240     get_mailbox(id, node->fingers[finger_index].mailbox);
241     node->last_change_date = MSG_get_clock();
242     XBT_DEBUG("My new finger #%d is %d", finger_index, id);
243   }
244 }
245
246 /**
247  * \brief Sets the predecessor of the current node.
248  * \param node the current node
249  * \param id the id to predecessor, or -1 to unset the predecessor
250  */
251 static void set_predecessor(node_t node, int predecessor_id)
252 {
253   if (predecessor_id != node->pred_id) {
254     node->pred_id = predecessor_id;
255
256     if (predecessor_id != -1) {
257       get_mailbox(predecessor_id, node->pred_mailbox);
258     }
259     node->last_change_date = MSG_get_clock();
260
261     XBT_DEBUG("My new predecessor is %d", predecessor_id);
262   }
263 }
264
265 /**
266  * \brief Node Function
267  * Arguments:
268  * - my id
269  * - the id of a guy I know in the system (except for the first node)
270  * - the time to sleep before I join (except for the first node)
271  */
272 int node(int argc, char *argv[])
273 {
274   
275   /* Reduce the run size for the MC */
276   if(MC_is_active()){
277     periodic_stabilize_delay = 8;
278     periodic_fix_fingers_delay = 8;
279     periodic_check_predecessor_delay = 8;
280   }
281
282   double init_time = MSG_get_clock();
283   msg_task_t task_received = NULL;
284   int i;
285   int join_success = 0;
286   double deadline;
287   double next_stabilize_date = init_time + periodic_stabilize_delay;
288   double next_fix_fingers_date = init_time + periodic_fix_fingers_delay;
289   double next_check_predecessor_date = init_time + periodic_check_predecessor_delay;
290   double next_lookup_date = init_time + periodic_lookup_delay;
291
292   xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
293
294   // initialize my node
295   s_node_t node = {0};
296   node.id = atoi(argv[1]);
297   get_mailbox(node.id, node.mailbox);
298   node.next_finger_to_fix = 0;
299   node.fingers = xbt_new0(s_finger_t, nb_bits);
300   node.last_change_date = init_time;
301
302   for (i = 0; i < nb_bits; i++) {
303     node.fingers[i].id = -1;
304     set_finger(&node, i, node.id);
305   }
306
307   if (argc == 3) { // first ring
308     deadline = atof(argv[2]);
309     create(&node);
310     join_success = 1;
311   }
312   else {
313     int known_id = atoi(argv[2]);
314     //double sleep_time = atof(argv[3]);
315     deadline = atof(argv[4]);
316
317     /*
318     // sleep before starting
319     XBT_DEBUG("Let's sleep during %f", sleep_time);
320     MSG_process_sleep(sleep_time);
321     */
322     XBT_DEBUG("Hey! Let's join the system.");
323
324     join_success = join(&node, known_id);
325   }
326
327   if (join_success) {
328     while (MSG_get_clock() < init_time + deadline
329 //      && MSG_get_clock() < node.last_change_date + 1000
330         && MSG_get_clock() < max_simulation_time) {
331
332       if (node.comm_receive == NULL) {
333         task_received = NULL;
334         node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
335         // FIXME: do not make MSG_task_irecv() calls from several functions
336       }
337
338       if (!MSG_comm_test(node.comm_receive)) {
339
340         // no task was received: make some periodic calls
341
342         #ifdef HAVE_MC
343           if(MC_is_active()){
344             if(MC_random()){
345               stabilize(&node);
346             }else if(MC_random()){
347               fix_fingers(&node);
348             }else if(MC_random()){
349               check_predecessor(&node);
350             }else if(MC_random()){
351               random_lookup(&node);
352             }else{
353               MSG_process_sleep(5);
354             }
355           }else{
356             if (MSG_get_clock() >= next_stabilize_date) {
357               stabilize(&node);
358               next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
359             }
360             else if (MSG_get_clock() >= next_fix_fingers_date) {
361               fix_fingers(&node);
362               next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
363             }
364             else if (MSG_get_clock() >= next_check_predecessor_date) {
365               check_predecessor(&node);
366               next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
367             }
368             else if (MSG_get_clock() >= next_lookup_date) {
369               random_lookup(&node);
370               next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
371             }
372             else {
373               // nothing to do: sleep for a while
374               MSG_process_sleep(5);
375             }
376           }
377         #else
378           if (MSG_get_clock() >= next_stabilize_date) {
379             stabilize(&node);
380             next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
381           }
382           else if (MSG_get_clock() >= next_fix_fingers_date) {
383             fix_fingers(&node);
384             next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
385           }
386           else if (MSG_get_clock() >= next_check_predecessor_date) {
387             check_predecessor(&node);
388             next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
389           }
390           else if (MSG_get_clock() >= next_lookup_date) {
391             random_lookup(&node);
392             next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
393           }
394           else {
395             // nothing to do: sleep for a while
396             MSG_process_sleep(5);
397           }
398         #endif
399
400       } else {
401         // a transfer has occurred
402
403         msg_error_t status = MSG_comm_get_status(node.comm_receive);
404
405         if (status != MSG_OK) {
406           XBT_DEBUG("Failed to receive a task. Nevermind.");
407           MSG_comm_destroy(node.comm_receive);
408           node.comm_receive = NULL;
409         }
410         else {
411           // the task was successfully received
412           MSG_comm_destroy(node.comm_receive);
413           node.comm_receive = NULL;
414           handle_task(&node, task_received);
415         }
416       }
417     }
418
419     if (node.comm_receive) {
420       MSG_comm_destroy(node.comm_receive);
421       node.comm_receive = NULL;
422     }
423
424     // leave the ring
425     leave(&node);
426   }
427
428   // stop the simulation
429   xbt_free(node.fingers);
430   return 0;
431 }
432
433 /**
434  * \brief This function is called when the current node receives a task.
435  * \param node the current node
436  * \param task the task to handle (don't touch it then:
437  * it will be destroyed, reused or forwarded)
438  */
439 static void handle_task(node_t node, msg_task_t task) {
440
441   XBT_DEBUG("Handling task %p", task);
442   char mailbox[MAILBOX_NAME_SIZE];
443   task_data_t task_data = (task_data_t) MSG_task_get_data(task);
444   e_task_type_t type = task_data->type;
445
446   switch (type) {
447
448     case TASK_FIND_SUCCESSOR:
449       XBT_DEBUG("Receiving a 'Find Successor' request from %s for id %d",
450           task_data->issuer_host_name, task_data->request_id);
451       // is my successor the successor?
452       if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
453         task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
454         task_data->answer_id = node->fingers[0].id;
455         XBT_DEBUG("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
456             task_data->issuer_host_name,
457             task_data->answer_to,
458             task_data->request_id, task_data->answer_id);
459         MSG_task_dsend(task, task_data->answer_to, task_free);
460       }
461       else {
462         // otherwise, forward the request to the closest preceding finger in my table
463         int closest = closest_preceding_node(node, task_data->request_id);
464         XBT_DEBUG("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
465             task_data->request_id, closest);
466         get_mailbox(closest, mailbox);
467         MSG_task_dsend(task, mailbox, task_free);
468       }
469       break;
470
471     case TASK_GET_PREDECESSOR:
472       XBT_DEBUG("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
473       task_data->type = TASK_GET_PREDECESSOR_ANSWER;
474       task_data->answer_id = node->pred_id;
475       XBT_DEBUG("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
476           task_data->issuer_host_name,
477           task_data->answer_to, task_data->answer_id);
478       MSG_task_dsend(task, task_data->answer_to, task_free);
479       break;
480
481     case TASK_NOTIFY:
482       // someone is telling me that he may be my new predecessor
483       XBT_DEBUG("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
484       notify(node, task_data->request_id);
485       task_free(task);
486       break;
487
488     case TASK_PREDECESSOR_LEAVING:
489       // my predecessor is about to quit
490       XBT_DEBUG("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
491       // modify my predecessor
492       set_predecessor(node, task_data->request_id);
493       task_free(task);
494       /*TODO :
495       >> notify my new predecessor
496       >> send a notify_predecessors !!
497        */
498       break;
499
500     case TASK_SUCCESSOR_LEAVING:
501       // my successor is about to quit
502       XBT_DEBUG("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
503       // modify my successor FIXME : this should be implicit ?
504       set_finger(node, 0, task_data->request_id);
505       task_free(task);
506       /* TODO
507       >> notify my new successor
508       >> update my table & predecessors table */
509       break;
510
511     case TASK_FIND_SUCCESSOR_ANSWER:
512     case TASK_GET_PREDECESSOR_ANSWER:
513       XBT_DEBUG("Ignoring unexpected task of type %d (%p)", (int)type, task);
514       task_free(task);
515       break;
516   }
517 }
518
519 /**
520  * \brief Initializes the current node as the first one of the system.
521  * \param node the current node
522  */
523 static void create(node_t node)
524 {
525   XBT_DEBUG("Create a new Chord ring...");
526   set_predecessor(node, -1); // -1 means that I have no predecessor
527   print_finger_table(node);
528 }
529
530 /**
531  * \brief Makes the current node join the ring, knowing the id of a node
532  * already in the ring
533  * \param node the current node
534  * \param known_id id of a node already in the ring
535  * \return 1 if the join operation succeeded, 0 otherwise
536  */
537 static int join(node_t node, int known_id)
538 {
539   XBT_INFO("Joining the ring with id %d, knowing node %d", node->id, known_id);
540   set_predecessor(node, -1); // no predecessor (yet)
541
542   /*
543   int i;
544   for (i = 0; i < nb_bits; i++) {
545     set_finger(node, i, known_id);
546   }
547   */
548
549   int successor_id = remote_find_successor(node, known_id, node->id);
550   if (successor_id == -1) {
551     XBT_INFO("Cannot join the ring.");
552   }
553   else {
554     set_finger(node, 0, successor_id);
555     print_finger_table(node);
556   }
557
558   return successor_id != -1;
559 }
560
561 /**
562  * \brief Makes the current node quit the system
563  * \param node the current node
564  */
565 static void leave(node_t node)
566 {
567   XBT_DEBUG("Well Guys! I Think it's time for me to quit ;)");
568   quit_notify(node);
569 }
570
571 /*
572  * \brief Notifies the successor and the predecessor of the current node
573  * of the departure
574  * \param node the current node
575  */
576 static void quit_notify(node_t node)
577 {
578   char mailbox[MAILBOX_NAME_SIZE];
579   //send the PREDECESSOR_LEAVING to our successor
580   task_data_t req_data = xbt_new0(s_task_data_t,1);
581   req_data->type = TASK_PREDECESSOR_LEAVING;
582   req_data->request_id = node->pred_id;
583   get_mailbox(node->id, req_data->answer_to);
584   req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
585
586   msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
587   XBT_DEBUG("Sending a 'PREDECESSOR_LEAVING' to my successor %d",node->fingers[0].id);
588   MSG_task_send_with_timeout(task_sent, node->fingers[0].mailbox, timeout);
589
590   //send the SUCCESSOR_LEAVING to our predecessor
591   get_mailbox(node->pred_id, mailbox);
592   task_data_t req_data_s = xbt_new0(s_task_data_t,1);
593   req_data_s->type = TASK_SUCCESSOR_LEAVING;
594   req_data_s->request_id = node->fingers[0].id;
595   req_data_s->request_id = node->pred_id;
596   get_mailbox(node->id, req_data_s->answer_to);
597   req_data_s->issuer_host_name = MSG_host_get_name(MSG_host_self());
598
599   msg_task_t task_sent_s = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data_s);
600   XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d",node->pred_id);
601   MSG_task_send_with_timeout(task_sent_s, mailbox, timeout);
602
603 }
604
605 /**
606  * \brief Makes the current node find the successor node of an id.
607  * \param node the current node
608  * \param id the id to find
609  * \return the id of the successor node, or -1 if the request failed
610  */
611 static int find_successor(node_t node, int id)
612 {
613   // is my successor the successor?
614   if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
615     return node->fingers[0].id;
616   }
617
618   // otherwise, ask the closest preceding finger in my table
619   int closest = closest_preceding_node(node, id);
620   return remote_find_successor(node, closest, id);
621 }
622
623 /**
624  * \brief Asks another node the successor node of an id.
625  * \param node the current node
626  * \param ask_to the node to ask to
627  * \param id the id to find
628  * \return the id of the successor node, or -1 if the request failed
629  */
630 static int remote_find_successor(node_t node, int ask_to, int id)
631 {
632   int successor = -1;
633   int stop = 0;
634   char mailbox[MAILBOX_NAME_SIZE];
635   get_mailbox(ask_to, mailbox);
636   task_data_t req_data = xbt_new0(s_task_data_t, 1);
637   req_data->type = TASK_FIND_SUCCESSOR;
638   req_data->request_id = id;
639   get_mailbox(node->id, req_data->answer_to);
640   req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
641
642   // send a "Find Successor" request to ask_to_id
643   msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
644   XBT_DEBUG("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
645   msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
646
647   if (res != MSG_OK) {
648     XBT_DEBUG("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
649         task_sent, ask_to, id);
650     task_free(task_sent);
651   }
652   else {
653
654     // receive the answer
655     XBT_DEBUG("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
656         task_sent, ask_to, id);
657
658     do {
659       if (node->comm_receive == NULL) {
660         msg_task_t task_received = NULL;
661         node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
662       }
663
664       res = MSG_comm_wait(node->comm_receive, timeout);
665
666       if (res != MSG_OK) {
667         XBT_DEBUG("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
668                   task_sent, (int)res);
669         stop = 1;
670         MSG_comm_destroy(node->comm_receive);
671         node->comm_receive = NULL;
672       }
673       else {
674         msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
675         XBT_DEBUG("Received a task (%p)", task_received);
676         task_data_t ans_data = MSG_task_get_data(task_received);
677
678         // Once upon a time, our code assumed that here, task_received != task_sent all the time
679         // 
680         // This assumption is wrong (as messages from differing round can interleave), leading to a bug in our code.
681         // We failed to find this bug directly, as it only occured on large platforms, leading to hardly usable traces.
682         // Instead, we used the model-checker to track down the issue by adding the following test here in the code:
683         //   if (MC_is_active()) {
684         //      MC_assert(task_received == task_sent);
685         //   }
686         // That explained the bug in a snap, with a very cool example and everything.
687         // 
688         // This MC_assert is now desactivated as the case is now properly handled in our code and we don't want the
689         //   MC to fail any further under that condition, but this comment is here to as a memorial for this first 
690         //   brillant victory of the model-checking in the SimGrid community :)
691
692         if (task_received != task_sent) {
693           // this is not the expected answer
694           MSG_comm_destroy(node->comm_receive);
695           node->comm_receive = NULL;
696           handle_task(node, task_received);
697         }
698         else {
699           // this is our answer
700           XBT_DEBUG("Received the answer to my 'Find Successor' request for id %d (task %p): the successor of key %d is %d",
701               ans_data->request_id, task_received, id, ans_data->answer_id);
702           successor = ans_data->answer_id;
703           stop = 1;
704           MSG_comm_destroy(node->comm_receive);
705           node->comm_receive = NULL;
706           task_free(task_received);
707         }
708       }
709     } while (!stop);
710   }
711
712   return successor;
713 }
714
715 /**
716  * \brief Asks another node its predecessor.
717  * \param node the current node
718  * \param ask_to the node to ask to
719  * \return the id of its predecessor node, or -1 if the request failed
720  * (or if the node does not know its predecessor)
721  */
722 static int remote_get_predecessor(node_t node, int ask_to)
723 {
724   int predecessor_id = -1;
725   int stop = 0;
726   char mailbox[MAILBOX_NAME_SIZE];
727   get_mailbox(ask_to, mailbox);
728   task_data_t req_data = xbt_new0(s_task_data_t, 1);
729   req_data->type = TASK_GET_PREDECESSOR;
730   get_mailbox(node->id, req_data->answer_to);
731   req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
732
733   // send a "Get Predecessor" request to ask_to_id
734   XBT_DEBUG("Sending a 'Get Predecessor' request to %d", ask_to);
735   msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
736   msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
737
738   if (res != MSG_OK) {
739     XBT_DEBUG("Failed to send the 'Get Predecessor' request (task %p) to %d",
740         task_sent, ask_to);
741     task_free(task_sent);
742   }
743   else {
744
745     // receive the answer
746     XBT_DEBUG("Sent 'Get Predecessor' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
747         task_sent, ask_to, req_data->answer_to);
748
749     do {
750       if (node->comm_receive == NULL) { // FIXME simplify this
751         msg_task_t task_received = NULL;
752         node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
753       }
754
755       res = MSG_comm_wait(node->comm_receive, timeout);
756
757       if (res != MSG_OK) {
758         XBT_DEBUG("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
759             task_sent, (int)res);
760         stop = 1;
761         MSG_comm_destroy(node->comm_receive);
762         node->comm_receive = NULL;
763       }
764       else {
765         msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
766         task_data_t ans_data = MSG_task_get_data(task_received);
767
768         if (MC_is_active()) {
769           MC_assert(task_received == task_sent);
770         }
771
772         if (task_received != task_sent) {
773           MSG_comm_destroy(node->comm_receive);
774           node->comm_receive = NULL;
775           handle_task(node, task_received);
776         }
777         else {
778           XBT_DEBUG("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
779               task_received, ask_to, ans_data->answer_id);
780           predecessor_id = ans_data->answer_id;
781           stop = 1;
782           MSG_comm_destroy(node->comm_receive);
783           node->comm_receive = NULL;
784           task_free(task_received);
785         }
786       }
787     } while (!stop);
788   }
789
790   return predecessor_id;
791 }
792
793 /**
794  * \brief Returns the closest preceding finger of an id
795  * with respect to the finger table of the current node.
796  * \param node the current node
797  * \param id the id to find
798  * \return the closest preceding finger of that id
799  */
800 int closest_preceding_node(node_t node, int id)
801 {
802   int i;
803   for (i = nb_bits - 1; i >= 0; i--) {
804     if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
805       return node->fingers[i].id;
806     }
807   }
808   return node->id;
809 }
810
811 /**
812  * \brief This function is called periodically. It checks the immediate
813  * successor of the current node.
814  * \param node the current node
815  */
816 static void stabilize(node_t node)
817 {
818   XBT_DEBUG("Stabilizing node");
819
820   // get the predecessor of my immediate successor
821   int candidate_id;
822   int successor_id = node->fingers[0].id;
823   if (successor_id != node->id) {
824     candidate_id = remote_get_predecessor(node, successor_id);
825   }
826   else {
827     candidate_id = node->pred_id;
828   }
829
830   // this node is a candidate to become my new successor
831   if (candidate_id != -1
832       && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
833     set_finger(node, 0, candidate_id);
834   }
835   if (successor_id != node->id) {
836     remote_notify(node, successor_id, node->id);
837   }
838 }
839
840 /**
841  * \brief Notifies the current node that its predecessor may have changed.
842  * \param node the current node
843  * \param candidate_id the possible new predecessor
844  */
845 static void notify(node_t node, int predecessor_candidate_id) {
846
847   if (node->pred_id == -1
848     || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
849
850     set_predecessor(node, predecessor_candidate_id);
851     print_finger_table(node);
852   }
853   else {
854     XBT_DEBUG("I don't have to change my predecessor to %d", predecessor_candidate_id);
855   }
856 }
857
858 /**
859  * \brief Notifies a remote node that its predecessor may have changed.
860  * \param node the current node
861  * \param notify_id id of the node to notify
862  * \param candidate_id the possible new predecessor
863  */
864 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
865
866       task_data_t req_data = xbt_new0(s_task_data_t, 1);
867       req_data->type = TASK_NOTIFY;
868       req_data->request_id = predecessor_candidate_id;
869       req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
870
871       // send a "Notify" request to notify_id
872       msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
873       XBT_DEBUG("Sending a 'Notify' request (task %p) to %d", task, notify_id);
874       char mailbox[MAILBOX_NAME_SIZE];
875       get_mailbox(notify_id, mailbox);
876       MSG_task_dsend(task, mailbox, task_free);
877     }
878
879 /**
880  * \brief This function is called periodically.
881  * It refreshes the finger table of the current node.
882  * \param node the current node
883  */
884 static void fix_fingers(node_t node) {
885
886   XBT_DEBUG("Fixing fingers");
887   int i = node->next_finger_to_fix;
888   int id = find_successor(node, node->id + powers2[i]);
889   if (id != -1) {
890
891     if (id != node->fingers[i].id) {
892       set_finger(node, i, id);
893       print_finger_table(node);
894     }
895     node->next_finger_to_fix = (i + 1) % nb_bits;
896   }
897 }
898
899 /**
900  * \brief This function is called periodically.
901  * It checks whether the predecessor has failed
902  * \param node the current node
903  */
904 static void check_predecessor(node_t node)
905 {
906   XBT_DEBUG("Checking whether my predecessor is alive");
907   // TODO
908 }
909
910 /**
911  * \brief Performs a find successor request to a random id.
912  * \param node the current node
913  */
914 static void random_lookup(node_t node)
915 {
916   int id = 1337; // TODO pick a pseudorandom id
917   XBT_DEBUG("Making a lookup request for id %d", id);
918   find_successor(node, id);
919 }
920
921 /**
922  * \brief Main function.
923  */
924 int main(int argc, char *argv[])
925 {
926   MSG_init(&argc, argv);
927   if (argc < 3) {
928     printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
929     printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
930     exit(1);
931   }
932
933   char **options = &argv[1];
934   while (!strncmp(options[0], "-", 1)) {
935
936     int length = strlen("-nb_bits=");
937     if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
938       nb_bits = atoi(options[0] + length);
939       XBT_DEBUG("Set nb_bits to %d", nb_bits);
940     }
941     else {
942
943       length = strlen("-timeout=");
944       if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
945         timeout = atoi(options[0] + length);
946         XBT_DEBUG("Set timeout to %d", timeout);
947       }
948       else {
949         xbt_die("Invalid chord option '%s'", options[0]);
950       }
951     }
952     options++;
953   }
954
955   const char* platform_file = options[0];
956   const char* application_file = options[1];
957
958   chord_initialize();
959
960   MSG_create_environment(platform_file);
961
962   MSG_function_register("node", node);
963   MSG_launch_application(application_file);
964
965   msg_error_t res = MSG_main();
966   XBT_CRITICAL("Messages created: %ld", smx_total_comms);
967   XBT_INFO("Simulated time: %g", MSG_get_clock());
968
969   chord_exit();
970
971   if (res == MSG_OK)
972     return 0;
973   else
974     return 1;
975 }