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");
23 typedef struct finger {
26 } s_finger_t, *finger_t;
33 char* mailbox; // my usual mailbox name
34 s_finger_t fingers[NB_BITS]; // finger table (fingers[0] is my successor)
35 int pred_id; // predecessor id
36 char* pred_mailbox; // predecessor's mailbox name
37 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
38 msg_comm_t comm_receive; // current communication to receive
39 xbt_dynar_t comms; // current communications being sent
45 typedef struct task_data {
49 const char* answer_to;
50 const char* issuer_host_name; // used for logging
51 int successor_id; // used when quitting
52 int pred_id; // used when quitting
53 // FIXME: remove successor_id and pred_id, request_id is enough
54 } s_task_data_t, *task_data_t;
56 static int powers2[NB_BITS];
59 static void chord_initialize(void);
60 static int normalize(int id);
61 static int is_in_interval(int id, int start, int end);
62 static char* get_mailbox(int host_id);
63 static void print_finger_table(node_t node);
64 static void set_finger(node_t node, int finger_index, int id);
65 static void set_predecessor(node_t node, int predecessor_id);
68 static int node(int argc, char *argv[]);
69 static void handle_task(node_t node, m_task_t task);
72 static void create(node_t node);
73 static void join(node_t node, int known_id);
74 static void leave(node_t node);
75 static int find_successor(node_t node, int id);
76 static int remote_find_successor(node_t node, int ask_to_id, int id);
77 static int remote_get_predecessor(node_t node, int ask_to_id);
78 static int closest_preceding_node(node_t node, int id);
79 static void stabilize(node_t node);
80 static void notify(node_t node, int predecessor_candidate_id);
81 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
82 static void fix_fingers(node_t node);
83 static void check_predecessor(node_t node);
84 static void quit_notify(node_t node, int to);
86 static void chord_initialize(void)
88 // compute the powers of 2 once for all
91 for (i = 0; i < NB_BITS; i++) {
98 * \brief Turns an id into an equivalent id in [0, NB_KEYS[
100 * \return the corresponding normalized id
102 static int normalize(int id)
108 // make sure id < NB_KEYS
115 * \brief Returns whether a id belongs to the interval [start, end].
117 * The parameters are noramlized to make sure they are between 0 and CHORD_NB_KEYS - 1).
118 * 1 belongs to [62, 3]
119 * 1 does not belong to [3, 62]
120 * 63 belongs to [62, 3]
121 * 63 does not belong to [3, 62]
122 * 24 belongs to [21, 29]
123 * 24 does not belong to [29, 21]
125 * \param id id to check
126 * \param start lower bound
127 * \param end upper bound
128 * \return a non-zero value if id in in [start, end]
130 static int is_in_interval(int id, int start, int end)
133 start = normalize(start);
134 end = normalize(end);
136 // make sure end >= start and id >= start
149 * \brief Gets the mailbox name of a host given its chord id.
150 * \param node_id id of a node
151 * \return the name of its mailbox
152 * FIXME: free the memory
154 static char* get_mailbox(int node_id)
156 return bprintf("mailbox%d", node_id);
160 * \brief Displays the finger table of a node.
163 static void print_finger_table(node_t node)
167 INFO0("My finger table:");
168 INFO0("Start | Succ ");
169 for (i = 0; i < NB_BITS; i++) {
170 INFO2(" %3d | %3d ", (node->id + pow) % NB_KEYS, node->fingers[i].id);
173 INFO1("Predecessor: %d", node->pred_id);
177 * \brief Sets a finger of the current node.
178 * \param node the current node
179 * \param finger_index index of the finger to set (0 to NB_BITS - 1)
180 * \param id the id to set for this finger
182 static void set_finger(node_t node, int finger_index, int id)
184 node->fingers[finger_index].id = id;
185 xbt_free(node->fingers[finger_index].mailbox);
186 node->fingers[finger_index].mailbox = get_mailbox(id);
187 INFO2("My new finger #%d is %d", finger_index, id);
191 * \brief Sets the predecessor of the current node.
192 * \param node the current node
193 * \param id the id to predecessor, or -1 to unset the predecessor
195 static void set_predecessor(node_t node, int predecessor_id)
197 node->pred_id = predecessor_id;
198 xbt_free(node->pred_mailbox);
200 if (predecessor_id != -1) {
201 node->pred_mailbox = get_mailbox(predecessor_id);
204 INFO1("My new predecessor is %d", predecessor_id);
208 * \brief Node Function
211 * - the id of a guy I know in the system (except for the first node)
212 * - the time to sleep before I join (except for the first node)
214 int node(int argc, char *argv[])
216 double init_time = MSG_get_clock();
217 m_task_t task = NULL;
218 msg_comm_t comm_send = NULL;
222 double next_stabilize_date = init_time + 10;
223 double next_fix_fingers_date = init_time + 10;
224 double next_check_predecessor_date = init_time + 10;
226 xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
228 // initialize my node
230 node.id = atoi(argv[1]);
231 node.mailbox = get_mailbox(node.id);
232 node.next_finger_to_fix = 0;
233 node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
235 for (i = 0; i < NB_BITS; i++) {
236 set_finger(&node, i, node.id);
239 if (argc == 3) { // first ring
240 deadline = atof(argv[2]);
244 int known_id = atoi(argv[2]);
245 double sleep_time = atof(argv[3]);
246 deadline = atof(argv[4]);
248 // sleep before starting
249 INFO1("Let's sleep during %f", sleep_time);
250 MSG_process_sleep(sleep_time);
251 INFO0("Hey! Let's join the system.");
253 join(&node, known_id);
256 while (MSG_get_clock() < init_time + deadline) {
258 if (node.comm_receive == NULL) {
260 node.comm_receive = MSG_task_irecv(&task, node.mailbox);
261 // FIXME: do not make MSG_task_irecv() calls from several functions
264 if (!MSG_comm_test(node.comm_receive)) {
266 // no task was received: make some periodic calls
267 if (MSG_get_clock() >= next_stabilize_date) {
269 next_stabilize_date = MSG_get_clock() + 10;
271 else if (MSG_get_clock() >= next_fix_fingers_date) {
273 next_fix_fingers_date = MSG_get_clock() + 10;
275 else if (MSG_get_clock() >= next_check_predecessor_date) {
276 check_predecessor(&node);
277 next_check_predecessor_date = MSG_get_clock() + 10;
280 // nothing to do: sleep for a while
281 MSG_process_sleep(5);
285 // a transfer has occured
287 MSG_error_t status = MSG_comm_get_status(node.comm_receive);
288 MSG_comm_destroy(node.comm_receive);
289 node.comm_receive = NULL;
291 if (status != MSG_OK) {
292 INFO0("Failed to receive a task. Nevermind.");
295 // the task was successfully received
296 handle_task(&node, task);
300 // see if some communications are finished
301 while ((index = MSG_comm_testany(node.comms)) != -1) {
302 comm_send = xbt_dynar_get_as(node.comms, index, msg_comm_t);
303 xbt_dynar_remove_at(node.comms, index, &comm_send);
304 DEBUG3("Communication %p is finished with status %d, dynar size is now %lu",
305 comm_send, MSG_comm_get_status(comm_send), xbt_dynar_length(node.comms));
306 MSG_comm_destroy(comm_send);
310 // clean unfinished comms sent
312 xbt_dynar_foreach(node.comms, cursor, comm_send) {
313 task = MSG_comm_get_task(comm_send);
314 MSG_task_cancel(task);
315 xbt_free(MSG_task_get_data(task));
316 MSG_task_destroy(task);
317 MSG_comm_destroy(comm_send);
320 // leave the ring and stop the simulation
322 xbt_dynar_free(&node.comms);
323 xbt_free(node.mailbox);
324 xbt_free(node.pred_mailbox);
325 for (i = 0; i < NB_BITS - 1; i++) {
326 xbt_free(node.fingers[i].mailbox);
332 * \brief This function is called when the current node receives a task.
333 * \param node the current node
334 * \param task the task to handle (don't touch it then:
335 * it will be destroyed, reused or forwarded)
337 static void handle_task(node_t node, m_task_t task) {
339 msg_comm_t comm = NULL;
340 char* mailbox = NULL;
341 const char* task_name = MSG_task_get_name(task);
342 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
344 if (!strcmp(task_name, "Find Successor")) {
345 INFO2("Receiving a 'Find Successor' request from %s for id %d",
346 task_data->issuer_host_name, task_data->request_id);
347 // is my successor the successor?
348 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
349 task_data->answer_id = node->fingers[0].id;
350 MSG_task_set_name(task, "Find Successor Answer");
351 comm = MSG_task_isend(task, task_data->answer_to);
352 xbt_dynar_push(node->comms, &comm);
353 INFO3("Sending back a 'Find Successor Answer' to %s: the successor of %d is %d",
354 task_data->issuer_host_name,
355 task_data->request_id, task_data->answer_id);
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 the 'Find Successor' request for id %d to my closest preceding finger %d",
361 task_data->request_id, closest);
362 mailbox = get_mailbox(closest);
363 comm = MSG_task_isend(task, mailbox);
364 xbt_dynar_push(node->comms, &comm);
369 else if (!strcmp(task_name, "Get Predecessor")) {
370 INFO1("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
371 task_data->answer_id = node->pred_id;
372 MSG_task_set_name(task, "Get Predecessor Answer");
373 comm = MSG_task_isend(task, task_data->answer_to);
374 xbt_dynar_push(node->comms, &comm);
375 INFO3("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
376 task_data->issuer_host_name,
377 task_data->answer_to, task_data->answer_id);
380 else if (!strcmp(task_name, "Notify")) {
381 // someone is telling me that he may be my new predecessor
382 INFO1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
383 notify(node, task_data->request_id);
385 MSG_task_destroy(task);
388 else if (!strcmp(task_name, "Predecessor Leaving")) {
389 // my predecessor is about to quit
390 INFO1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
391 // modify my predecessor
392 set_predecessor(node, task_data->pred_id);
394 MSG_task_destroy(task);
396 >> notify my new predecessor
397 >> send a notify_predecessors !!
401 else if (!strcmp(task_name, "Successor Leaving")) {
402 // my successor is about to quit
403 INFO1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
404 // modify my successor FIXME : this should be implicit ?
405 set_finger(node, 0, task_data->successor_id);
407 MSG_task_destroy(task);
409 >> notify my new successor
410 >> update my table & predecessors table */
415 * \brief Initializes the current node as the first one of the system.
416 * \param node the current node
418 static void create(node_t node)
420 INFO0("Create a new Chord ring...");
421 set_predecessor(node, -1); // -1 means that I have no predecessor
422 print_finger_table(node);
426 * \brief Makes the current node join the ring, knowing the id of a node
427 * already in the ring
428 * \param node the current node
429 * \param known_id id of a node already in the ring
431 static void join(node_t node, int known_id)
433 INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
434 set_predecessor(node, -1); // no predecessor (yet)
438 successor_id = remote_find_successor(node, known_id, node->id);
439 if (successor_id == -1) {
440 INFO0("I really want to join the ring. Let's try again.");
442 } while (successor_id == -1);
444 set_finger(node, 0, successor_id);
445 print_finger_table(node);
449 * \brief Makes the current node quit the system
450 * \param node the current node
452 static void leave(node_t node)
454 INFO0("Well Guys! I Think it's time for me to quit ;)");
455 quit_notify(node, 1); // notify to my successor ( >>> 1 );
456 quit_notify(node, -1); // notify my predecessor ( >>> -1);
461 * \brief Notifies the successor or the predecessor of the current node
463 * \param node the current node
464 * \param to 1 to notify the successor, -1 to notify the predecessor
465 * FIXME: notify both nodes with only one call
467 static void quit_notify(node_t node, int to)
470 task_data_t req_data = xbt_new0(s_task_data_t, 1);
471 req_data->request_id = node->id;
472 req_data->successor_id = node->fingers[0].id;
473 req_data->pred_id = node->pred_id;
474 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
475 const char* task_name = NULL;
476 const char* to_mailbox = NULL;
477 if (to == 1) { // notify my successor
478 to_mailbox = node->fingers[0].mailbox;
479 INFO2("Telling my Successor %d about my departure via mailbox %s",
480 node->fingers[0].id, to_mailbox);
481 task_name = "Predecessor Leaving";
483 else if (to == -1) { // notify my predecessor
485 if (node->pred_id == -1) {
489 to_mailbox = node->pred_mailbox;
490 INFO2("Telling my Predecessor %d about my departure via mailbox %s",
491 node->pred_id, to_mailbox);
492 task_name = "Successor Leaving";
494 m_task_t task = MSG_task_create(task_name, COMP_SIZE, COMM_SIZE, req_data);
495 //char* mailbox = get_mailbox(to_mailbox);
496 msg_comm_t comm = MSG_task_isend(task, to_mailbox);
497 xbt_dynar_push(node->comms, &comm);
502 * \brief Makes the current node find the successor node of an id.
503 * \param node the current node
504 * \param id the id to find
505 * \return the id of the successor node, or -1 if the request failed
507 static int find_successor(node_t node, int id)
509 // is my successor the successor?
510 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
511 return node->fingers[0].id;
514 // otherwise, ask the closest preceding finger in my table
515 int closest = closest_preceding_node(node, id);
516 return remote_find_successor(node, closest, id);
520 * \brief Asks another node the successor node of an id.
521 * \param node the current node
522 * \param ask_to the node to ask to
523 * \param id the id to find
524 * \return the id of the successor node, or -1 if the request failed
526 static int remote_find_successor(node_t node, int ask_to, int id)
530 char* mailbox = get_mailbox(ask_to);
531 task_data_t req_data = xbt_new0(s_task_data_t, 1);
532 req_data->request_id = id;
533 req_data->answer_to = node->mailbox;
534 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
536 // send a "Find Successor" request to ask_to_id
537 INFO2("Sending a 'Find Successor' request to %d for id %d", ask_to, id);
538 m_task_t task = MSG_task_create("Find Successor", COMP_SIZE, COMM_SIZE, req_data);
539 MSG_error_t res = MSG_task_send_with_timeout(task, mailbox, 50);
542 INFO2("Failed to send the 'Find Successor' request to %d for id %d", ask_to, id);
543 MSG_task_destroy(task);
548 // receive the answer
549 INFO2("Sent a 'Find Successor' request to %d for key %d, waiting for the answer", ask_to, id);
552 if (node->comm_receive == NULL) {
554 node->comm_receive = MSG_task_irecv(&task, node->mailbox);
557 res = MSG_comm_wait(node->comm_receive, 50);
560 INFO1("Failed to receive the answer to my 'Find Successor' request: %d", res);
562 // MSG_comm_destroy(node->comm_receive);
565 task = MSG_comm_get_task(node->comm_receive);
566 INFO1("Received a task '%s'", MSG_task_get_name(task));
568 if (strcmp(MSG_task_get_name(task), "Find Successor Answer")) {
569 handle_task(node, task);
572 INFO2("This is the answer to my 'Find Successor' request: the successor of key %d is %d", id, successor);
573 task_data_t ans_data = MSG_task_get_data(task);
574 successor = ans_data->answer_id;
576 MSG_task_destroy(task);
580 node->comm_receive = NULL;
589 * \brief Asks another node its predecessor.
590 * \param node the current node
591 * \param ask_to the node to ask to
592 * \return the id of its predecessor node, or -1 if the request failed
593 * (or if the node does not know its predecessor)
595 static int remote_get_predecessor(node_t node, int ask_to)
597 int predecessor_id = -1;
599 char* mailbox = get_mailbox(ask_to);
600 task_data_t req_data = xbt_new0(s_task_data_t, 1);
601 req_data->answer_to = node->mailbox;
602 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
604 // send a "Get Predecessor" request to ask_to_id
605 INFO1("Sending a 'Get Predecessor' request to %d", ask_to);
606 m_task_t task = MSG_task_create("Get Predecessor", COMP_SIZE, COMM_SIZE, req_data);
607 MSG_error_t res = MSG_task_send_with_timeout(task, mailbox, 50);
610 INFO1("Failed to send the 'Get Predecessor' request to %d", ask_to);
611 MSG_task_destroy(task);
616 // receive the answer
617 INFO2("Sent 'Get Predecessor' request to %d, waiting for the answer on my mailbox '%s'", ask_to, req_data->answer_to);
620 if (node->comm_receive == NULL) { // FIXME simplify this
622 node->comm_receive = MSG_task_irecv(&task, node->mailbox);
625 res = MSG_comm_wait(node->comm_receive, 50);
628 INFO1("Failed to receive the answer to my 'Get Predecessor' request: %d", res);
630 // MSG_comm_destroy(node->comm_receive);
633 task = MSG_comm_get_task(node->comm_receive);
634 INFO1("Received a task '%s'", MSG_task_get_name(task));
636 if (strcmp(MSG_task_get_name(task), "Get Predecessor Answer")) {
637 handle_task(node, task);
640 INFO2("This is the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to, predecessor_id);
641 task_data_t ans_data = MSG_task_get_data(task);
642 predecessor_id = ans_data->answer_id;
644 MSG_task_destroy(task);
648 node->comm_receive = NULL;
653 return predecessor_id;
657 * \brief Returns the closest preceding finger of an id
658 * with respect to the finger table of the current node.
659 * \param node the current node
660 * \param id the id to find
661 * \return the closest preceding finger of that id
663 int closest_preceding_node(node_t node, int id)
666 for (i = NB_BITS - 1; i >= 0; i--) {
667 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
668 return node->fingers[i].id;
675 * \brief This function is called periodically. It checks the immediate
676 * successor of the current node.
677 * \param node the current node
679 static void stabilize(node_t node)
681 INFO0("Stabilizing node");
683 // get the predecessor of my immediate successor
685 int successor_id = node->fingers[0].id;
686 if (successor_id != node->id) {
687 candidate_id = remote_get_predecessor(node, successor_id);
690 candidate_id = node->pred_id;
693 // this node is a candidate to become my new successor
694 if (candidate_id != -1
695 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
696 set_finger(node, 0, candidate_id);
698 if (successor_id != node->id) {
699 remote_notify(node, successor_id, node->id);
704 * \brief Notifies the current node that its predecessor may have changed.
705 * \param node the current node
706 * \param candidate_id the possible new predecessor
708 static void notify(node_t node, int predecessor_candidate_id) {
710 if (node->pred_id == -1
711 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
713 set_predecessor(node, predecessor_candidate_id);
714 print_finger_table(node);
717 INFO1("I don't have to change my predecessor to %d", predecessor_candidate_id);
722 * \brief Notifies a remote node that its predecessor may have changed.
723 * \param node the current node
724 * \param notify_id id of the node to notify
725 * \param candidate_id the possible new predecessor
727 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
729 task_data_t req_data = xbt_new0(s_task_data_t, 1);
730 req_data->request_id = predecessor_candidate_id;
731 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
733 // send a "Notify" request to notify_id
734 INFO1("Sending a 'Notify' request to %d", notify_id);
735 m_task_t task = MSG_task_create("Notify", COMP_SIZE, COMM_SIZE, req_data);
736 char* mailbox = get_mailbox(notify_id);
737 msg_comm_t comm = MSG_task_isend(task, mailbox);
738 xbt_dynar_push(node->comms, &comm);
743 * \brief This function is called periodically.
744 * It refreshes the finger table of the current node.
745 * \param node the current node
747 static void fix_fingers(node_t node) {
749 INFO0("Fixing fingers");
750 int i = node->next_finger_to_fix;
751 int id = find_successor(node, node->id + powers2[i]);
754 if (id != node->fingers[i].id) {
755 set_finger(node, i, id);
756 print_finger_table(node);
758 node->next_finger_to_fix = (i + 1) % NB_BITS;
763 * \brief This function is called periodically.
764 * It checks whether the predecessor has failed
765 * \param node the current node
767 static void check_predecessor(node_t node)
769 INFO0("Checking whether my predecessor is alive");
774 * \brief Main function.
776 int main(int argc, char *argv[])
779 printf("Usage: %s platform_file deployment_file\n", argv[0]);
780 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
784 const char* platform_file = argv[1];
785 const char* application_file = argv[2];
789 MSG_global_init(&argc, argv);
790 MSG_set_channel_number(0);
791 MSG_create_environment(platform_file);
793 MSG_function_register("node", node);
794 MSG_launch_application(application_file);
796 MSG_error_t res = MSG_main();
797 INFO1("Simulation time: %g", MSG_get_clock());