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 msg_comm_t comm = NULL;
232 char* mailbox = NULL;
234 double next_stabilize_date = init_time + 10;
235 double next_fix_fingers_date = init_time + 10;
236 double next_check_predecessor_date = init_time + 10;
238 xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
240 // initialize my node
242 node.id = atoi(argv[1]);
243 node.mailbox = get_mailbox(node.id);
244 node.next_finger_to_fix = 0;
245 node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
247 for (i = 0; i < NB_BITS; i++) {
248 set_finger(&node, i, node.id);
251 if (argc == 3) { // first ring
252 deadline = atof(argv[2]);
256 int known_id = atoi(argv[2]);
257 double sleep_time = atof(argv[3]);
258 deadline = atof(argv[4]);
260 // sleep before starting
261 INFO1("Let's sleep during %f", sleep_time);
262 MSG_process_sleep(sleep_time);
263 INFO0("Hey! Let's join the system.");
265 join(&node, known_id);
268 while (MSG_get_clock() < init_time + deadline) {
272 xbt_dynar_foreach(node.comms, cursor, comm) {
273 if (MSG_comm_test(comm)) { // FIXME: try with MSG_comm_testany instead
274 xbt_dynar_cursor_rm(node.comms, &cursor);
275 MSG_comm_destroy(comm);
280 if (MSG_get_clock() >= next_stabilize_date) {
282 next_stabilize_date = MSG_get_clock() + 10;
284 if (MSG_get_clock() >= next_fix_fingers_date) {
286 next_fix_fingers_date = MSG_get_clock() + 10;
288 if (MSG_get_clock() >= next_check_predecessor_date) {
289 check_predecessor(&node);
290 next_check_predecessor_date = MSG_get_clock() + 10;
293 m_task_t task = NULL;
294 MSG_error_t res = MSG_task_receive_with_timeout(&task, node.mailbox, 45); // FIXME >> find the right timeout !!
296 if (res == MSG_OK) { // else check deadline condition and keep waiting for a task
298 // a task was received, get the data
299 const char* task_name = MSG_task_get_name(task);
300 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
302 if (!strcmp(task_name, "Find Successor")) {
303 INFO2("Receiving a 'Find Successor' Request from %s for id %d",
304 task_data->issuer_host_name, task_data->request_id);
305 // is my successor the successor?
306 if (is_in_interval(task_data->request_id, node.id + 1, node.fingers[0].id)) {
307 task_data->answer_id = node.fingers[0].id;
308 MSG_task_set_name(task, "Find Successor Answer");
309 INFO3("Sending back a 'Find Successor Answer' to %s: the successor of %d is %d",
310 task_data->issuer_host_name, task_data->request_id, task_data->answer_id);
311 comm = MSG_task_isend(task, task_data->answer_to);
312 xbt_dynar_push(node.comms, &comm);
315 // otherwise, forward the request to the closest preceding finger in my table
316 int closest = closest_preceding_node(&node, task_data->request_id);
317 INFO2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
318 task_data->request_id, closest);
319 mailbox = get_mailbox(closest);
320 comm = MSG_task_isend(task, mailbox);
321 xbt_dynar_push(node.comms, &comm);
326 else if (!strcmp(task_name, "Get Predecessor")) {
327 INFO1("Receiving a 'Get Predecessor' Request from %s", task_data->issuer_host_name);
328 task_data->answer_id = node.pred_id;
329 MSG_task_set_name(task, "Get Predecessor Answer");
330 INFO2("Sending back a 'Get Predecessor Answer' to %s: my predecessor is %d",
331 task_data->issuer_host_name, task_data->answer_id);
332 comm = MSG_task_isend(task, task_data->answer_to);
333 xbt_dynar_push(node.comms, &comm);
336 else if (!strcmp(task_name, "Find Predecessor")) {
337 INFO2("Receiving a 'Find Predecessor' Request from %s for id %d", task_data->issuer_host_name, task_data->request_id);
338 // am I the predecessor?
339 if (is_in_interval(task_data->request_id, node.id + 1, node.fingers[0].id)) {
340 task_data->answer_id = node.id;
341 MSG_task_set_name(task, "Find Predecessor Answer");
342 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);
343 comm = MSG_task_isend(task, task_data->answer_to);
344 xbt_dynar_push(node.comms, &comm);
347 // otherwise, forward the request to the closest preceding finger in my table
348 int closest = closest_preceding_node(&node, task_data->request_id);
349 INFO2("Forwarding 'Find Predecessor' request for id %d to my closest preceding finger %d", task_data->request_id, closest);
350 mailbox = get_mailbox(closest);
351 comm = MSG_task_isend(task, mailbox);
352 xbt_dynar_push(node.comms, &comm);
359 else if (!strcmp(task_name, "Notify Node Joined")) {
360 // someone may be my new neighboor
361 INFO1("Receiving a 'Notify Node Joine' request from %s", task_data->issuer_host_name);
362 notify_node_joined(&node, task_data->request_id);
364 else if (!strcmp(task_name, "Update Finger")) {
365 // someone is telling me that he may be my new finger
366 INFO1("Receiving an 'Update Finger' request from %s", task_data->issuer_host_name);
367 update_finger_table(&node, task_data->request_id, task_data->request_finger);
370 else if (!strcmp(task_name, "Notify")) {
371 // someone is telling me that he may be my new predecessor
372 INFO1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
373 notify(&node, task_data->request_id);
375 else if (!strcmp(task_name, "Predecessor Leaving")) {
376 // my predecessor is about to quit
377 INFO1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
378 // modify my predecessor
379 set_predecessor(&node, task_data->pred_id);
381 >> notify my new predecessor
382 >> send a notify_predecessors !!
385 else if (!strcmp(task_name, "Successor Leaving")) {
386 // my successor is about to quit
387 INFO1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
388 // modify my successor FIXME : this should be implicit ?
389 set_finger(&node, 0, task_data->successor_id);
391 >> notify my new successor
392 >> update my table & predecessors table */
397 // leave the ring and stop the simulation
399 xbt_dynar_free(&node.comms);
400 xbt_free(node.mailbox);
401 xbt_free(node.pred_mailbox);
402 for (i = 0; i < NB_BITS - 1; i++) {
403 xbt_free(node.fingers[i].mailbox);
409 * \brief Initializes the current node as the first one of the system.
410 * \param node the current node
412 static void create(node_t node)
414 INFO0("Create a new Chord ring...");
415 set_predecessor(node, -1); // -1 means that I have no predecessor
416 print_finger_table(node);
420 * \brief Makes the current node join the ring, knowing the id of a node
421 * already in the ring
422 * \param node the current node
423 * \param known_id id of a node already in the ring
425 static void join(node_t node, int known_id)
427 INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
428 set_predecessor(node, -1); // no predecessor (yet)
429 int successor_id = remote_find_successor(node, known_id, node->id);
430 set_finger(node, 0, successor_id);
431 print_finger_table(node);
435 * \brief Notifies the current node that a node has just joined the network.
436 * \param node the current node
437 * \param new_node_id id of the new node in the network
440 static void notify_node_joined(node_t node, int new_node_id)
442 INFO1("A new node %d has joined the network, let's see if it is a neighboor", new_node_id);
443 if (is_in_interval(new_node_id, node->id + 1, node->fingers[0].id - 1)) {
444 // new_node_id is my new successor
445 set_finger(node, 0, new_node_id);
446 bootstrap(node, new_node_id);
449 if (is_in_interval(new_node_id, node->pred_id + 1, node->id - 1)) {
450 // new_node_id is my new predecessor
451 set_predecessor(node, new_node_id);
452 bootstrap(node, new_node_id);
458 * \brief Notifies a remote node that the current node has just joined
460 * \param node the current node
461 * \param notify_id id of the remote node to notify
464 static void remote_notify_node_joined(node_t node, int notify_id)
466 task_data_t req_data = xbt_new0(s_task_data_t, 1);
467 req_data->request_id = node->id;
468 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
470 // send a "Notify" request to notify_id
471 INFO2("Sending a 'Notify' request to %d because I have joined the network with id %d", notify_id, node->id);
472 m_task_t task = MSG_task_create("Notify", 1000, 5000, req_data);
473 char* mailbox = get_mailbox(notify_id);
474 msg_comm_t comm = MSG_task_isend(task, mailbox);
475 xbt_dynar_push(node->comms, &comm);
481 * \brief Let the current node send queries to fill in its own finger table.
482 * \param node the current node
483 * \param ask_to_id id of a node to send queries to
486 static void bootstrap(node_t node, int ask_to_id)
488 INFO0("Filling my finger table");
489 int i, pred_id, succ_id;
492 for (i = 0; i < NB_BITS; i++)
494 pred_id = remote_find_successor(node, ask_to_id, pow);
497 pred_id = remote_find_predecessor(node, pred_id, pred_id);
498 } while (pred_id >= pow);
501 set_finger(node, i, succ_id);
507 * \brief Makes the current node quit the system
508 * \param node the current node
510 static void leave(node_t node)
512 INFO0("Well Guys! I Think it's time for me to quit ;)");
513 quit_notify(node, 1); // notify to my successor ( >>> 1 );
514 quit_notify(node, -1); // notify my predecessor ( >>> -1);
519 * \brief Notifies the successor or the predecessor of the current node
521 * \param node the current node
522 * \param to 1 to notify the successor, -1 to notify the predecessor
523 * FIXME: notify both nodes with only one call
525 static void quit_notify(node_t node, int to)
527 task_data_t req_data = xbt_new0(s_task_data_t, 1);
528 req_data->request_id = node->id;
529 req_data->successor_id = node->fingers[0].id;
530 req_data->pred_id = node->pred_id;
531 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
532 const char *task_name = NULL;
533 const char* to_mailbox = NULL;
534 if (to == 1) // notify my successor
536 to_mailbox = node->fingers[0].mailbox;
537 INFO2("Telling my Successor %d about my departure via mailbox %s",
538 node->fingers[0].id, to_mailbox);
539 task_name = "Predecessor Leaving";
542 else if (to == -1) // notify my predecessor
544 if (node->pred_id == -1) {
548 to_mailbox = node->pred_mailbox;
549 INFO2("Telling my Predecessor %d about my departure via mailbox %s",
550 node->pred_id, to_mailbox);
551 task_name = "Predecessor Leaving";
554 m_task_t task = MSG_task_create(task_name, 1000, 5000, req_data);
555 //char* mailbox = get_mailbox(to_mailbox);
556 msg_comm_t comm = MSG_task_isend(task, to_mailbox);
557 xbt_dynar_push(node->comms, &comm);
561 * \brief Initializes my finger table, knowing the id of a node already in the system.
562 * \param node the current node
563 * \param known_id id of a node already in the system
566 static void initialize_finger_table(node_t node, int known_id)
568 int my_id = node->id;
572 INFO0("Initializing my finger table...");
574 // ask known_id who is my immediate successor
575 node->fingers[0].id = remote_find_successor(node, known_id, my_id + 1);
576 node->fingers[0].mailbox = get_mailbox(node->fingers[0].id);
578 // find all other fingers
579 for (i = 0; i < NB_BITS - 1; i++) {
581 pow = pow << 1; // equivalent to pow = pow * 2
582 if (is_in_interval(my_id + pow, my_id, node->fingers[i].id - 1)) {
583 // I already have the info for this finger
584 node->fingers[i + 1].id = node->fingers[i].id;
587 // I don't have the info, ask the only guy I know
588 node->fingers[i + 1].id = remote_find_successor(node, known_id, my_id + pow);
590 node->fingers[i + 1].mailbox = get_mailbox(node->fingers[i + 1].id);
593 node->pred_id = find_predecessor(node, node->id);
594 node->pred_mailbox = get_mailbox(node->pred_id);
596 INFO0("Finger table initialized!");
597 print_finger_table(node);
602 * \brief Notifies some nodes that the current node may have became their finger.
603 * \param node the current node, which has just joined the system
606 static void notify_predecessors(node_t node)
610 for (i = 0; i < NB_BITS; i++) {
611 // find the closest node whose finger #i can be me
612 pred_id = find_predecessor(node, node->id - pow + 1); // note: no "+1" in the article!
613 if (pred_id != node->id) {
614 remote_update_finger_table(node, pred_id, node->id, i);
616 pow = pow << 1; // pow = pow * 2
622 * \brief Tells the current node that a node may have became its new finger.
623 * \param node the current node
624 * \param candidate_id id of the node that may be a new finger of the current node
625 * \param finger_index index of the finger to update
628 static void update_finger_table(node_t node, int candidate_id, int finger_index)
632 for (i = 0; i < finger_index; i++) {
636 // if (is_in_interval(candidate_id, node->id + pow, node->fingers[finger_index].id - 1)) {
637 if (is_in_interval(candidate_id, node->id, node->fingers[finger_index].id - 1)) {
638 // INFO3("Candidate %d is between %d and %d!", candidate_id, node->id + pow, node->fingers[finger_index].id - 1);
639 // candidate_id is my new finger
640 set_finger(node, finger_index, candidate_id);
641 print_finger_table(node);
643 if (node->pred_id != node->id) { // FIXME: is this necessary?
644 // my predecessor may be concerned too
645 remote_update_finger_table(node, node->pred_id, candidate_id, finger_index);
652 * \brief Tells a remote node that a node may have became its new finger.
653 * \param ask_to_id id of the remote node to update
654 * \param candidate_id id of the node that may be a new finger of the remote node
655 * \param finger_index index of the finger to update
658 static void remote_update_finger_table(node_t node, int ask_to_id, int candidate_id, int finger_index)
660 task_data_t req_data = xbt_new0(s_task_data_t, 1);
661 req_data->request_id = candidate_id;
662 req_data->request_finger = finger_index;
663 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
665 // send a "Update Finger" request to ask_to_id
666 INFO3("Sending an 'Update Finger' request to %d: his finger #%d may be %d now", ask_to_id, finger_index, candidate_id);
667 m_task_t task = MSG_task_create("Update Finger", 1000, 5000, req_data);
668 char* mailbox = get_mailbox(ask_to_id);
669 msg_comm_t comm = MSG_task_isend(task, mailbox);
670 xbt_dynar_push(node->comms, &comm);
676 * \brief Makes the current node find the successor node of an id.
677 * \param node the current node
678 * \param id the id to find
679 * \return the id of the successor node
681 static int find_successor(node_t node, int id)
683 // is my successor the successor?
684 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
685 return node->fingers[0].id;
688 // otherwise, ask the closest preceding finger in my table
689 int closest = closest_preceding_node(node, id);
690 return remote_find_successor(node, closest, id);
694 * \brief Asks another node the successor node of an id.
695 * \param node the current node
696 * \param ask_to the node to ask to
697 * \param id the id to find
698 * \return the id of the successor node
700 static int remote_find_successor(node_t node, int ask_to, int id)
702 s_task_data_t req_data;
703 char* mailbox = bprintf("%s Find Successor", node->mailbox);
704 req_data.request_id = id;
705 req_data.answer_to = mailbox;
706 req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
708 // send a "Find Successor" request to ask_to_id
709 INFO2("Sending a 'Find Successor' request to %d for key %d", ask_to, id);
710 m_task_t task = MSG_task_create("Find Successor", 1000, 5000, &req_data);
711 MSG_task_send(task, get_mailbox(ask_to));
713 // receive the answer
715 MSG_task_receive(&task, req_data.answer_to);
716 task_data_t ans_data;
717 ans_data = MSG_task_get_data(task);
718 int successor = ans_data->answer_id;
720 INFO2("Received the answer to my 'Find Successor' request: the successor of key %d is %d", id, successor);
726 * \brief Asks another node its predecessor.
727 * \param node the current node
728 * \param ask_to the node to ask to
729 * \return the id of its predecessor node
731 static int remote_get_predecessor(node_t node, int ask_to)
733 s_task_data_t req_data;
734 char* mailbox = bprintf("%s Get Predecessor", node->mailbox);
735 req_data.answer_to = mailbox;
736 req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
738 // send a "Get Predecessor" request to ask_to_id
739 INFO1("Sending a 'Get Predecessor' request to %d", ask_to);
740 m_task_t task = MSG_task_create("Get Predecessor", 1000, 5000, &req_data);
741 MSG_task_send(task, get_mailbox(ask_to));
743 // receive the answer
745 MSG_task_receive(&task, req_data.answer_to);
746 task_data_t ans_data;
747 ans_data = MSG_task_get_data(task);
748 int predecessor_id = ans_data->answer_id;
750 INFO2("Received the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to, predecessor_id);
752 return predecessor_id;
756 * \brief Makes the current node find the predecessor node of an id.
757 * \param node the current node
758 * \param id the id to find
759 * \return the id of the predecessor node
762 static int find_predecessor(node_t node, int id)
764 if (node->id == node->fingers[0].id) {
765 // I am the only node in the system
769 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
772 int ask_to = closest_preceding_node(node, id);
773 return remote_find_predecessor(node, ask_to, id);
778 * \brief Asks another node the predecessor node of an id.
779 * \param node the current node
780 * \param ask_to the node to ask to
781 * \param id the id to find
782 * \return the id of the predecessor node
785 static int remote_find_predecessor(node_t node, int ask_to, int id)
787 s_task_data_t req_data;
788 char* mailbox = bprintf("%s Find Predecessor", node->mailbox);
789 req_data.request_id = id;
790 req_data.answer_to = mailbox;
791 req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
793 // send a "Find Predecessor" request to ask_to
794 INFO2("Sending a 'Find Predecessor' request to %d for key %d", ask_to, id);
795 m_task_t task = MSG_task_create("Find Predecessor", 1000, 5000, &req_data);
796 MSG_task_send(task, get_mailbox(ask_to));
798 // receive the answer
800 MSG_task_receive(&task, req_data.answer_to);
801 task_data_t ans_data;
802 ans_data = MSG_task_get_data(task);
803 int predecessor = ans_data->answer_id;
805 INFO2("Received the answer to my 'Find Predecessor' request: the predecessor of key %d is %d", id, predecessor);
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 INFO0("Stabilizing node");
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 if (node->pred_id == -1
866 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
868 set_predecessor(node, predecessor_candidate_id);
869 print_finger_table(node);
872 INFO1("I don't have to change my predecessor to %d", predecessor_candidate_id);
877 * \brief Notifies a remote node that its predecessor may have changed.
878 * \param node the current node
879 * \param notify_id id of the node to notify
880 * \param candidate_id the possible new predecessor
882 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
884 task_data_t req_data = xbt_new0(s_task_data_t, 1);
885 req_data->request_id = predecessor_candidate_id;
886 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
888 // send a "Notify" request to notify_id
889 INFO1("Sending a 'Notify' request to %d", notify_id);
890 m_task_t task = MSG_task_create("Notify", 1000, 5000, req_data);
891 char* mailbox = get_mailbox(notify_id);
892 msg_comm_t comm = MSG_task_isend(task, mailbox);
893 xbt_dynar_push(node->comms, &comm);
898 * \brief This function is called periodically.
899 * It refreshes the finger table of the current node.
900 * \param node the current node
902 static void fix_fingers(node_t node) {
904 INFO0("Fixing fingers");
905 int i = node->next_finger_to_fix;
906 int id = find_successor(node, node->id + powers2[i]);
907 if (id != -1 && id != node->fingers[i].id) {
908 set_finger(node, i, id);
909 print_finger_table(node);
911 node->next_finger_to_fix = (i + 1) % NB_BITS;
915 * \brief This function is called periodically.
916 * It checks whether the predecessor has failed
917 * \param node the current node
919 static void check_predecessor(node_t node)
921 INFO0("Checking whether my predecessor is alive");
926 * \brief Main function.
928 int main(int argc, char *argv[])
931 printf("Usage: %s platform_file deployment_file\n", argv[0]);
932 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
936 const char* platform_file = argv[1];
937 const char* application_file = argv[2];
941 MSG_global_init(&argc, argv);
942 MSG_set_channel_number(0);
943 MSG_create_environment(platform_file);
945 MSG_function_register("node", node);
946 MSG_launch_application(application_file);
948 MSG_error_t res = MSG_main();
949 INFO1("Simulation time: %g", MSG_get_clock());