2 /* Copyright (c) 2010. The SimGrid Team.
3 * All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/asserts.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
13 "Messages specific for this msg example");
21 typedef struct finger {
24 } s_finger_t, *finger_t;
31 char* mailbox; // my usual mailbox name
32 s_finger_t fingers[NB_BITS]; // finger table (fingers[0] is my successor)
33 int pred_id; // predecessor id
34 char* pred_mailbox; // predecessor's mailbox name
35 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
36 xbt_dynar_t comms; // current communications pending
42 typedef struct task_data {
46 const char* answer_to;
47 const char* issuer_host_name; // used for logging
48 int successor_id; // used when quitting
49 int pred_id; // used when quitting
50 // FIXME: remove successor_id and pred_id, request_id is enough
51 } s_task_data_t, *task_data_t;
53 static int powers2[NB_BITS];
56 static void chord_initialize(void);
57 static int normalize(int id);
58 static int is_in_interval(int id, int start, int end);
59 static char* get_mailbox(int host_id);
60 static void print_finger_table(node_t node);
61 static void set_finger(node_t node, int finger_index, int id);
62 static void set_predecessor(node_t node, int predecessor_id);
65 static int node(int argc, char *argv[]);
68 static void create(node_t node);
69 static void join(node_t node, int known_id);
70 static void leave(node_t node);
71 static int find_successor(node_t node, int id);
72 static int remote_find_successor(node_t node, int ask_to_id, int id);
73 static int remote_get_predecessor(node_t node, int ask_to_id);
74 static int closest_preceding_node(node_t node, int id);
75 static void stabilize(node_t node);
76 static void notify(node_t node, int predecessor_candidate_id);
77 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
78 static void fix_fingers(node_t node);
79 static void check_predecessor(node_t node);
80 static void quit_notify(node_t node, int to);
82 // not implemented yet
83 //static void remote_move_keys(node_t node, int take_from_id);
87 static void bootstrap(node_t node, int known_id);
88 static int find_predecessor(node_t node, int id);
89 static int remote_find_predecessor(node_t node, int ask_to_id, int id);
90 static int remote_closest_preceding_finger(int ask_to_id, int id);
91 static void notify_predecessors(node_t node);
92 static void initialize_finger_table(node_t data, int known_id);
93 static void update_finger_table(node_t node, int candidate_id, int finger_index);
94 static void remote_update_finger_table(node_t node, int ask_to_id, int candidate_id, int finger_index);
95 static void notify_node_joined(node_t node, int new_node_id);
96 static void remote_notify_node_joined(node_t node, int notify_id);
99 static void chord_initialize(void)
101 // compute the powers of 2 once for all
104 for (i = 0; i < NB_BITS; i++) {
111 * \brief Turns an id into an equivalent id in [0, NB_KEYS[
113 * \return the corresponding normalized id
115 static int normalize(int id)
121 // make sure id < NB_KEYS
128 * \brief Returns whether a id belongs to the interval [start, end].
130 * The parameters are noramlized to make sure they are between 0 and CHORD_NB_KEYS - 1).
131 * 1 belongs to [62, 3]
132 * 1 does not belong to [3, 62]
133 * 63 belongs to [62, 3]
134 * 63 does not belong to [3, 62]
135 * 24 belongs to [21, 29]
136 * 24 does not belong to [29, 21]
138 * \param id id to check
139 * \param start lower bound
140 * \param end upper bound
141 * \return a non-zero value if id in in [start, end]
143 static int is_in_interval(int id, int start, int end)
146 start = normalize(start);
147 end = normalize(end);
149 // make sure end >= start and id >= start
162 * \brief Gets the mailbox name of a host given its chord id.
163 * \param node_id id of a node
164 * \return the name of its mailbox
165 * FIXME: free the memory
167 static char* get_mailbox(int node_id)
169 return bprintf("mailbox%d", node_id);
173 * \brief Displays the finger table of a node.
176 static void print_finger_table(node_t node)
180 INFO0("My finger table:");
181 INFO0("Start | Succ ");
182 for (i = 0; i < NB_BITS; i++) {
183 INFO2(" %3d | %3d ", (node->id + pow) % NB_KEYS, node->fingers[i].id);
186 INFO1("Predecessor: %d", node->pred_id);
190 * \brief Sets a finger of the current node.
191 * \param node the current node
192 * \param finger_index index of the finger to set (0 to NB_BITS - 1)
193 * \param id the id to set for this finger
195 static void set_finger(node_t node, int finger_index, int id)
197 node->fingers[finger_index].id = id;
198 xbt_free(node->fingers[finger_index].mailbox);
199 node->fingers[finger_index].mailbox = get_mailbox(id);
200 INFO2("My new finger #%d is %d", finger_index, id);
204 * \brief Sets the predecessor of the current node.
205 * \param node the current node
206 * \param id the id to predecessor, or -1 to unset the predecessor
208 static void set_predecessor(node_t node, int predecessor_id)
210 node->pred_id = predecessor_id;
211 xbt_free(node->pred_mailbox);
213 if (predecessor_id != -1) {
214 node->pred_mailbox = get_mailbox(predecessor_id);
217 INFO1("My new predecessor is %d", predecessor_id);
221 * \brief Node Function
224 * - the id of a guy I know in the system (except for the first node)
225 * - the time to sleep before I join (except for the first node)
227 int node(int argc, char *argv[])
229 double init_time = MSG_get_clock();
230 m_task_t task = NULL;
231 msg_comm_t comm_send = NULL;
232 msg_comm_t comm_receive = NULL;
234 char* mailbox = NULL;
236 double next_stabilize_date = init_time + 10;
237 double next_fix_fingers_date = init_time + 10;
238 double next_check_predecessor_date = init_time + 10;
240 xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
242 // initialize my node
244 node.id = atoi(argv[1]);
245 node.mailbox = get_mailbox(node.id);
246 node.next_finger_to_fix = 0;
247 node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
249 for (i = 0; i < NB_BITS; i++) {
250 set_finger(&node, i, node.id);
253 if (argc == 3) { // first ring
254 deadline = atof(argv[2]);
258 int known_id = atoi(argv[2]);
259 double sleep_time = atof(argv[3]);
260 deadline = atof(argv[4]);
262 // sleep before starting
263 INFO1("Let's sleep during %f", sleep_time);
264 MSG_process_sleep(sleep_time);
265 INFO0("Hey! Let's join the system.");
267 join(&node, known_id);
270 while (MSG_get_clock() < init_time + deadline) {
272 // see if some communications are finished
274 while ((index = MSG_comm_testany(node.comms)) != -1) {
275 comm_send = xbt_dynar_get_as(node.comms, index, msg_comm_t);
276 xbt_dynar_remove_at(node.comms, index, &comm_send);
277 MSG_comm_destroy(comm_send);
280 if (comm_receive == NULL) {
282 comm_receive = MSG_task_irecv(&task, node.mailbox);
285 if (!MSG_comm_test(comm_receive)) {
287 // no task was received: make some periodic calls
288 if (MSG_get_clock() >= next_stabilize_date) {
290 next_stabilize_date = MSG_get_clock() + 10;
292 else if (MSG_get_clock() >= next_fix_fingers_date) {
294 next_fix_fingers_date = MSG_get_clock() + 10;
296 else if (MSG_get_clock() >= next_check_predecessor_date) {
297 check_predecessor(&node);
298 next_check_predecessor_date = MSG_get_clock() + 10;
301 // nothing to do: sleep for a while
302 MSG_process_sleep(5);
306 // a task was received
308 MSG_comm_destroy(comm_receive);
310 const char* task_name = MSG_task_get_name(task);
311 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
313 if (!strcmp(task_name, "Find Successor")) {
314 INFO2("Receiving a 'Find Successor' Request from %s for id %d",
315 task_data->issuer_host_name, task_data->request_id);
316 // is my successor the successor?
317 if (is_in_interval(task_data->request_id, node.id + 1, node.fingers[0].id)) {
318 task_data->answer_id = node.fingers[0].id;
319 MSG_task_set_name(task, "Find Successor Answer");
320 INFO3("Sending back a 'Find Successor Answer' to %s: the successor of %d is %d",
321 task_data->issuer_host_name, task_data->request_id, task_data->answer_id);
322 comm_send = MSG_task_isend(task, task_data->answer_to);
323 xbt_dynar_push(node.comms, &comm_send);
326 // otherwise, forward the request to the closest preceding finger in my table
327 int closest = closest_preceding_node(&node, task_data->request_id);
328 INFO2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
329 task_data->request_id, closest);
330 mailbox = get_mailbox(closest);
331 comm_send = MSG_task_isend(task, mailbox);
332 xbt_dynar_push(node.comms, &comm_send);
337 else if (!strcmp(task_name, "Get Predecessor")) {
338 INFO1("Receiving a 'Get Predecessor' Request from %s", task_data->issuer_host_name);
339 task_data->answer_id = node.pred_id;
340 MSG_task_set_name(task, "Get Predecessor Answer");
341 INFO2("Sending back a 'Get Predecessor Answer' to %s: my predecessor is %d",
342 task_data->issuer_host_name, task_data->answer_id);
343 comm_send = MSG_task_isend(task, task_data->answer_to);
344 xbt_dynar_push(node.comms, &comm_send);
347 else if (!strcmp(task_name, "Find Predecessor")) {
348 INFO2("Receiving a 'Find Predecessor' Request from %s for id %d", task_data->issuer_host_name, task_data->request_id);
349 // am I the predecessor?
350 if (is_in_interval(task_data->request_id, node.id + 1, node.fingers[0].id)) {
351 task_data->answer_id = node.id;
352 MSG_task_set_name(task, "Find Predecessor Answer");
353 INFO3("Sending back a 'Find Predecessor' Answer to %s: the predecessor of %d is %d", task_data->issuer_host_name, task_data->request_id, task_data->answer_id);
354 comm = MSG_task_isend(task, task_data->answer_to);
355 xbt_dynar_push(node.comms, &comm);
358 // otherwise, forward the request to the closest preceding finger in my table
359 int closest = closest_preceding_node(&node, task_data->request_id);
360 INFO2("Forwarding 'Find Predecessor' request for id %d to my closest preceding finger %d", task_data->request_id, closest);
361 mailbox = get_mailbox(closest);
362 comm = MSG_task_isend(task, mailbox);
363 xbt_dynar_push(node.comms, &comm);
370 else if (!strcmp(task_name, "Notify Node Joined")) {
371 // someone may be my new neighboor
372 INFO1("Receiving a 'Notify Node Joine' request from %s", task_data->issuer_host_name);
373 notify_node_joined(&node, task_data->request_id);
375 else if (!strcmp(task_name, "Update Finger")) {
376 // someone is telling me that he may be my new finger
377 INFO1("Receiving an 'Update Finger' request from %s", task_data->issuer_host_name);
378 update_finger_table(&node, task_data->request_id, task_data->request_finger);
381 else if (!strcmp(task_name, "Notify")) {
382 // someone is telling me that he may be my new predecessor
383 INFO1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
384 notify(&node, task_data->request_id);
386 else if (!strcmp(task_name, "Predecessor Leaving")) {
387 // my predecessor is about to quit
388 INFO1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
389 // modify my predecessor
390 set_predecessor(&node, task_data->pred_id);
392 >> notify my new predecessor
393 >> send a notify_predecessors !!
396 else if (!strcmp(task_name, "Successor Leaving")) {
397 // my successor is about to quit
398 INFO1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
399 // modify my successor FIXME : this should be implicit ?
400 set_finger(&node, 0, task_data->successor_id);
402 >> notify my new successor
403 >> update my table & predecessors table */
408 // leave the ring and stop the simulation
410 xbt_dynar_free(&node.comms);
411 xbt_free(node.mailbox);
412 xbt_free(node.pred_mailbox);
413 for (i = 0; i < NB_BITS - 1; i++) {
414 xbt_free(node.fingers[i].mailbox);
420 * \brief Initializes the current node as the first one of the system.
421 * \param node the current node
423 static void create(node_t node)
425 INFO0("Create a new Chord ring...");
426 set_predecessor(node, -1); // -1 means that I have no predecessor
427 print_finger_table(node);
431 * \brief Makes the current node join the ring, knowing the id of a node
432 * already in the ring
433 * \param node the current node
434 * \param known_id id of a node already in the ring
436 static void join(node_t node, int known_id)
438 INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
439 set_predecessor(node, -1); // no predecessor (yet)
443 successor_id = remote_find_successor(node, known_id, node->id);
444 INFO0("My 'Find Successor' request has failed, let's try again");
445 } while (successor_id == -1);
447 set_finger(node, 0, successor_id);
448 print_finger_table(node);
452 * \brief Notifies the current node that a node has just joined the network.
453 * \param node the current node
454 * \param new_node_id id of the new node in the network
457 static void notify_node_joined(node_t node, int new_node_id)
459 INFO1("A new node %d has joined the network, let's see if it is a neighboor", new_node_id);
460 if (is_in_interval(new_node_id, node->id + 1, node->fingers[0].id - 1)) {
461 // new_node_id is my new successor
462 set_finger(node, 0, new_node_id);
463 bootstrap(node, new_node_id);
466 if (is_in_interval(new_node_id, node->pred_id + 1, node->id - 1)) {
467 // new_node_id is my new predecessor
468 set_predecessor(node, new_node_id);
469 bootstrap(node, new_node_id);
475 * \brief Notifies a remote node that the current node has just joined
477 * \param node the current node
478 * \param notify_id id of the remote node to notify
481 static void remote_notify_node_joined(node_t node, int notify_id)
483 task_data_t req_data = xbt_new0(s_task_data_t, 1);
484 req_data->request_id = node->id;
485 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
487 // send a "Notify" request to notify_id
488 INFO2("Sending a 'Notify' request to %d because I have joined the network with id %d", notify_id, node->id);
489 m_task_t task = MSG_task_create("Notify", 1000, 5000, req_data);
490 char* mailbox = get_mailbox(notify_id);
491 msg_comm_t comm = MSG_task_isend(task, mailbox);
492 xbt_dynar_push(node->comms, &comm);
498 * \brief Let the current node send queries to fill in its own finger table.
499 * \param node the current node
500 * \param ask_to_id id of a node to send queries to
503 static void bootstrap(node_t node, int ask_to_id)
505 INFO0("Filling my finger table");
506 int i, pred_id, succ_id;
509 for (i = 0; i < NB_BITS; i++)
511 pred_id = remote_find_successor(node, ask_to_id, pow);
514 pred_id = remote_find_predecessor(node, pred_id, pred_id);
515 } while (pred_id >= pow);
518 set_finger(node, i, succ_id);
524 * \brief Makes the current node quit the system
525 * \param node the current node
527 static void leave(node_t node)
529 INFO0("Well Guys! I Think it's time for me to quit ;)");
530 quit_notify(node, 1); // notify to my successor ( >>> 1 );
531 quit_notify(node, -1); // notify my predecessor ( >>> -1);
536 * \brief Notifies the successor or the predecessor of the current node
538 * \param node the current node
539 * \param to 1 to notify the successor, -1 to notify the predecessor
540 * FIXME: notify both nodes with only one call
542 static void quit_notify(node_t node, int to)
544 task_data_t req_data = xbt_new0(s_task_data_t, 1);
545 req_data->request_id = node->id;
546 req_data->successor_id = node->fingers[0].id;
547 req_data->pred_id = node->pred_id;
548 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
549 const char *task_name = NULL;
550 const char* to_mailbox = NULL;
551 if (to == 1) // notify my successor
553 to_mailbox = node->fingers[0].mailbox;
554 INFO2("Telling my Successor %d about my departure via mailbox %s",
555 node->fingers[0].id, to_mailbox);
556 task_name = "Predecessor Leaving";
559 else if (to == -1) // notify my predecessor
561 if (node->pred_id == -1) {
565 to_mailbox = node->pred_mailbox;
566 INFO2("Telling my Predecessor %d about my departure via mailbox %s",
567 node->pred_id, to_mailbox);
568 task_name = "Predecessor Leaving";
571 m_task_t task = MSG_task_create(task_name, 1000, 5000, req_data);
572 //char* mailbox = get_mailbox(to_mailbox);
573 msg_comm_t comm = MSG_task_isend(task, to_mailbox);
574 xbt_dynar_push(node->comms, &comm);
578 * \brief Initializes my finger table, knowing the id of a node already in the system.
579 * \param node the current node
580 * \param known_id id of a node already in the system
583 static void initialize_finger_table(node_t node, int known_id)
585 int my_id = node->id;
589 INFO0("Initializing my finger table...");
591 // ask known_id who is my immediate successor
592 node->fingers[0].id = remote_find_successor(node, known_id, my_id + 1);
593 node->fingers[0].mailbox = get_mailbox(node->fingers[0].id);
595 // find all other fingers
596 for (i = 0; i < NB_BITS - 1; i++) {
598 pow = pow << 1; // equivalent to pow = pow * 2
599 if (is_in_interval(my_id + pow, my_id, node->fingers[i].id - 1)) {
600 // I already have the info for this finger
601 node->fingers[i + 1].id = node->fingers[i].id;
604 // I don't have the info, ask the only guy I know
605 node->fingers[i + 1].id = remote_find_successor(node, known_id, my_id + pow);
607 node->fingers[i + 1].mailbox = get_mailbox(node->fingers[i + 1].id);
610 node->pred_id = find_predecessor(node, node->id);
611 node->pred_mailbox = get_mailbox(node->pred_id);
613 INFO0("Finger table initialized!");
614 print_finger_table(node);
619 * \brief Notifies some nodes that the current node may have became their finger.
620 * \param node the current node, which has just joined the system
623 static void notify_predecessors(node_t node)
627 for (i = 0; i < NB_BITS; i++) {
628 // find the closest node whose finger #i can be me
629 pred_id = find_predecessor(node, node->id - pow + 1); // note: no "+1" in the article!
630 if (pred_id != node->id) {
631 remote_update_finger_table(node, pred_id, node->id, i);
633 pow = pow << 1; // pow = pow * 2
639 * \brief Tells the current node that a node may have became its new finger.
640 * \param node the current node
641 * \param candidate_id id of the node that may be a new finger of the current node
642 * \param finger_index index of the finger to update
645 static void update_finger_table(node_t node, int candidate_id, int finger_index)
649 for (i = 0; i < finger_index; i++) {
653 // if (is_in_interval(candidate_id, node->id + pow, node->fingers[finger_index].id - 1)) {
654 if (is_in_interval(candidate_id, node->id, node->fingers[finger_index].id - 1)) {
655 // INFO3("Candidate %d is between %d and %d!", candidate_id, node->id + pow, node->fingers[finger_index].id - 1);
656 // candidate_id is my new finger
657 set_finger(node, finger_index, candidate_id);
658 print_finger_table(node);
660 if (node->pred_id != node->id) { // FIXME: is this necessary?
661 // my predecessor may be concerned too
662 remote_update_finger_table(node, node->pred_id, candidate_id, finger_index);
669 * \brief Tells a remote node that a node may have became its new finger.
670 * \param ask_to_id id of the remote node to update
671 * \param candidate_id id of the node that may be a new finger of the remote node
672 * \param finger_index index of the finger to update
675 static void remote_update_finger_table(node_t node, int ask_to_id, int candidate_id, int finger_index)
677 task_data_t req_data = xbt_new0(s_task_data_t, 1);
678 req_data->request_id = candidate_id;
679 req_data->request_finger = finger_index;
680 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
682 // send a "Update Finger" request to ask_to_id
683 INFO3("Sending an 'Update Finger' request to %d: his finger #%d may be %d now", ask_to_id, finger_index, candidate_id);
684 m_task_t task = MSG_task_create("Update Finger", 1000, 5000, req_data);
685 char* mailbox = get_mailbox(ask_to_id);
686 msg_comm_t comm = MSG_task_isend(task, mailbox);
687 xbt_dynar_push(node->comms, &comm);
693 * \brief Makes the current node find the successor node of an id.
694 * \param node the current node
695 * \param id the id to find
696 * \return the id of the successor node, or -1 if the request failed
698 static int find_successor(node_t node, int id)
700 // is my successor the successor?
701 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
702 return node->fingers[0].id;
705 // otherwise, ask the closest preceding finger in my table
706 int closest = closest_preceding_node(node, id);
707 return remote_find_successor(node, closest, id);
711 * \brief Asks another node the successor node of an id.
712 * \param node the current node
713 * \param ask_to the node to ask to
714 * \param id the id to find
715 * \return the id of the successor node, or -1 if the request failed
717 static int remote_find_successor(node_t node, int ask_to, int id)
720 s_task_data_t req_data;
721 char* mailbox = bprintf("%s Find Successor", node->mailbox);
722 req_data.request_id = id;
723 req_data.answer_to = mailbox;
724 req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
726 // send a "Find Successor" request to ask_to_id
727 INFO2("Sending a 'Find Successor' request to %d for key %d", ask_to, id);
728 m_task_t task = MSG_task_create("Find Successor", 1000, 5000, &req_data);
729 MSG_error_t res = MSG_task_send_with_timeout(task, get_mailbox(ask_to), 20);
732 // receive the answer
734 res = MSG_task_receive_with_timeout(&task, req_data.answer_to, 20);
738 task_data_t ans_data;
739 ans_data = MSG_task_get_data(task);
740 successor = ans_data->answer_id;
741 INFO2("Received the answer to my 'Find Successor' request: the successor of key %d is %d", id, successor);
750 * \brief Asks another node its predecessor.
751 * \param node the current node
752 * \param ask_to the node to ask to
753 * \return the id of its predecessor node, or -1 if the request failed
754 * (or if the node does not know its predecessor)
756 static int remote_get_predecessor(node_t node, int ask_to)
758 int predecessor_id = -1;
759 s_task_data_t req_data;
760 char* mailbox = bprintf("%s Get Predecessor", node->mailbox);
761 req_data.answer_to = mailbox;
762 req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
764 // send a "Get Predecessor" request to ask_to_id
765 INFO1("Sending a 'Get Predecessor' request to %d", ask_to);
766 m_task_t task = MSG_task_create("Get Predecessor", 1000, 5000, &req_data);
767 MSG_error_t res = MSG_task_send_with_timeout(task, get_mailbox(ask_to), 20);
771 // receive the answer
773 res = MSG_task_receive_with_timeout(&task, req_data.answer_to, 20);
777 task_data_t ans_data;
778 ans_data = MSG_task_get_data(task);
779 predecessor_id = ans_data->answer_id;
780 INFO2("Received the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to, predecessor_id);
785 return predecessor_id;
789 * \brief Makes the current node find the predecessor node of an id.
790 * \param node the current node
791 * \param id the id to find
792 * \return the id of the predecessor node
795 static int find_predecessor(node_t node, int id)
797 if (node->id == node->fingers[0].id) {
798 // I am the only node in the system
802 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
805 int ask_to = closest_preceding_node(node, id);
806 return remote_find_predecessor(node, ask_to, id);
811 * \brief Asks another node the predecessor node of an id.
812 * \param node the current node
813 * \param ask_to the node to ask to
814 * \param id the id to find
815 * \return the id of the predecessor node
818 static int remote_find_predecessor(node_t node, int ask_to, int id)
820 s_task_data_t req_data;
821 char* mailbox = bprintf("%s Find Predecessor", node->mailbox);
822 req_data.request_id = id;
823 req_data.answer_to = mailbox;
824 req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
826 // send a "Find Predecessor" request to ask_to
827 INFO2("Sending a 'Find Predecessor' request to %d for key %d", ask_to, id);
828 m_task_t task = MSG_task_create("Find Predecessor", 1000, 5000, &req_data);
829 MSG_task_send(task, get_mailbox(ask_to));
831 // receive the answer
833 MSG_task_receive(&task, req_data.answer_to);
834 task_data_t ans_data;
835 ans_data = MSG_task_get_data(task);
836 int predecessor = ans_data->answer_id;
838 INFO2("Received the answer to my 'Find Predecessor' request: the predecessor of key %d is %d", id, predecessor);
845 * \brief Returns the closest preceding finger of an id
846 * with respect to the finger table of the current node.
847 * \param node the current node
848 * \param id the id to find
849 * \return the closest preceding finger of that id
851 int closest_preceding_node(node_t node, int id)
854 for (i = NB_BITS - 1; i >= 0; i--) {
855 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
856 return node->fingers[i].id;
863 * \brief This function is called periodically. It checks the immediate
864 * successor of the current node.
865 * \param node the current node
867 static void stabilize(node_t node)
869 INFO0("Stabilizing node");
871 // get the predecessor of my immediate successor
873 int successor_id = node->fingers[0].id;
874 if (successor_id != node->id) {
875 candidate_id = remote_get_predecessor(node, successor_id);
878 candidate_id = node->pred_id;
881 // this node is a candidate to become my new successor
882 if (candidate_id != -1
883 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
884 set_finger(node, 0, candidate_id);
886 if (successor_id != node->id) {
887 remote_notify(node, successor_id, node->id);
892 * \brief Notifies the current node that its predecessor may have changed.
893 * \param node the current node
894 * \param candidate_id the possible new predecessor
896 static void notify(node_t node, int predecessor_candidate_id) {
898 if (node->pred_id == -1
899 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
901 set_predecessor(node, predecessor_candidate_id);
902 print_finger_table(node);
905 INFO1("I don't have to change my predecessor to %d", predecessor_candidate_id);
910 * \brief Notifies a remote node that its predecessor may have changed.
911 * \param node the current node
912 * \param notify_id id of the node to notify
913 * \param candidate_id the possible new predecessor
915 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
917 task_data_t req_data = xbt_new0(s_task_data_t, 1);
918 req_data->request_id = predecessor_candidate_id;
919 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
921 // send a "Notify" request to notify_id
922 INFO1("Sending a 'Notify' request to %d", notify_id);
923 m_task_t task = MSG_task_create("Notify", 1000, 5000, req_data);
924 char* mailbox = get_mailbox(notify_id);
925 msg_comm_t comm = MSG_task_isend(task, mailbox);
926 xbt_dynar_push(node->comms, &comm);
931 * \brief This function is called periodically.
932 * It refreshes the finger table of the current node.
933 * \param node the current node
935 static void fix_fingers(node_t node) {
937 INFO0("Fixing fingers");
938 int i = node->next_finger_to_fix;
939 int id = find_successor(node, node->id + powers2[i]);
942 if (id != node->fingers[i].id) {
943 set_finger(node, i, id);
944 print_finger_table(node);
946 node->next_finger_to_fix = (i + 1) % NB_BITS;
951 * \brief This function is called periodically.
952 * It checks whether the predecessor has failed
953 * \param node the current node
955 static void check_predecessor(node_t node)
957 INFO0("Checking whether my predecessor is alive");
962 * \brief Main function.
964 int main(int argc, char *argv[])
967 printf("Usage: %s platform_file deployment_file\n", argv[0]);
968 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
972 const char* platform_file = argv[1];
973 const char* application_file = argv[2];
977 MSG_global_init(&argc, argv);
978 MSG_set_channel_number(0);
979 MSG_create_environment(platform_file);
981 MSG_function_register("node", node);
982 MSG_launch_application(application_file);
984 MSG_error_t res = MSG_main();
985 INFO1("Simulation time: %g", MSG_get_clock());