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
43 * Types of tasks exchanged between nodes.
47 TASK_FIND_SUCCESSOR_ANSWER,
49 TASK_GET_PREDECESSOR_ANSWER,
51 TASK_SUCCESSOR_LEAVING,
52 TASK_PREDECESSOR_LEAVING
56 * Data attached with the tasks sent and received
58 typedef struct task_data {
59 e_task_type_t type; // type of task
60 int request_id; // id paramater (used by some types of tasks)
61 int request_finger; // finger parameter (used by some types of tasks)
62 int answer_id; // answer (used by some types of tasks)
63 const char* answer_to; // mailbox to send an answer to
64 const char* issuer_host_name; // used for logging
65 } s_task_data_t, *task_data_t;
67 static int powers2[NB_BITS];
70 static void chord_initialize(void);
71 static int normalize(int id);
72 static int is_in_interval(int id, int start, int end);
73 static char* get_mailbox(int host_id);
74 static void print_finger_table(node_t node);
75 static void set_finger(node_t node, int finger_index, int id);
76 static void set_predecessor(node_t node, int predecessor_id);
79 static int node(int argc, char *argv[]);
80 static void handle_task(node_t node, m_task_t task);
83 static void create(node_t node);
84 static void join(node_t node, int known_id);
85 static void leave(node_t node);
86 static int find_successor(node_t node, int id);
87 static int remote_find_successor(node_t node, int ask_to_id, int id);
88 static int remote_get_predecessor(node_t node, int ask_to_id);
89 static int closest_preceding_node(node_t node, int id);
90 static void stabilize(node_t node);
91 static void notify(node_t node, int predecessor_candidate_id);
92 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
93 static void fix_fingers(node_t node);
94 static void check_predecessor(node_t node);
95 static void quit_notify(node_t node, int to);
98 * \brief Global initialization of the Chord simulation.
100 static void chord_initialize(void)
102 // compute the powers of 2 once for all
105 for (i = 0; i < NB_BITS; i++) {
112 * \brief Turns an id into an equivalent id in [0, NB_KEYS).
114 * \return the corresponding normalized id
116 static int normalize(int id)
122 // make sure id < NB_KEYS
129 * \brief Returns whether a id belongs to the interval [start, end].
131 * The parameters are noramlized to make sure they are between 0 and CHORD_NB_KEYS - 1).
132 * 1 belongs to [62, 3]
133 * 1 does not belong to [3, 62]
134 * 63 belongs to [62, 3]
135 * 63 does not belong to [3, 62]
136 * 24 belongs to [21, 29]
137 * 24 does not belong to [29, 21]
139 * \param id id to check
140 * \param start lower bound
141 * \param end upper bound
142 * \return a non-zero value if id in in [start, end]
144 static int is_in_interval(int id, int start, int end)
147 start = normalize(start);
148 end = normalize(end);
150 // make sure end >= start and id >= start
163 * \brief Gets the mailbox name of a host given its chord id.
164 * \param node_id id of a node
165 * \return the name of its mailbox
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;
235 double next_stabilize_date = init_time + 10;
236 double next_fix_fingers_date = init_time + 10;
237 double next_check_predecessor_date = init_time + 10;
239 xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
241 // initialize my node
243 node.id = atoi(argv[1]);
244 node.mailbox = get_mailbox(node.id);
245 node.next_finger_to_fix = 0;
246 node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
248 for (i = 0; i < NB_BITS; i++) {
249 set_finger(&node, i, node.id);
252 if (argc == 3) { // first ring
253 deadline = atof(argv[2]);
257 int known_id = atoi(argv[2]);
258 double sleep_time = atof(argv[3]);
259 deadline = atof(argv[4]);
261 // sleep before starting
262 INFO1("Let's sleep during %f", sleep_time);
263 MSG_process_sleep(sleep_time);
264 INFO0("Hey! Let's join the system.");
266 join(&node, known_id);
269 while (MSG_get_clock() < init_time + deadline) {
271 if (node.comm_receive == NULL) {
273 node.comm_receive = MSG_task_irecv(&task, node.mailbox);
274 // FIXME: do not make MSG_task_irecv() calls from several functions
277 if (!MSG_comm_test(node.comm_receive)) {
279 // no task was received: make some periodic calls
280 if (MSG_get_clock() >= next_stabilize_date) {
282 next_stabilize_date = MSG_get_clock() + 10;
284 else if (MSG_get_clock() >= next_fix_fingers_date) {
286 next_fix_fingers_date = MSG_get_clock() + 10;
288 else if (MSG_get_clock() >= next_check_predecessor_date) {
289 check_predecessor(&node);
290 next_check_predecessor_date = MSG_get_clock() + 10;
293 // nothing to do: sleep for a while
294 MSG_process_sleep(5);
298 // a transfer has occured
300 MSG_error_t status = MSG_comm_get_status(node.comm_receive);
301 MSG_comm_destroy(node.comm_receive);
302 node.comm_receive = NULL;
304 if (status != MSG_OK) {
305 INFO0("Failed to receive a task. Nevermind.");
308 // the task was successfully received
309 handle_task(&node, task);
313 // see if some communications are finished
314 while ((index = MSG_comm_testany(node.comms)) != -1) {
315 comm_send = xbt_dynar_get_as(node.comms, index, msg_comm_t);
316 MSG_error_t status = MSG_comm_get_status(comm_send);
317 xbt_dynar_remove_at(node.comms, index, &comm_send);
318 DEBUG3("Communication %p is finished with status %d, dynar size is now %lu",
319 comm_send, status, xbt_dynar_length(node.comms));
320 MSG_comm_destroy(comm_send);
324 // clean unfinished comms sent
326 xbt_dynar_foreach(node.comms, cursor, comm_send) {
327 task = MSG_comm_get_task(comm_send);
328 MSG_task_cancel(task);
329 xbt_free(MSG_task_get_data(task));
330 MSG_task_destroy(task);
331 MSG_comm_destroy(comm_send);
332 // FIXME: the task is actually not destroyed because MSG thinks that the other side (whose process is dead) is still using it
335 // leave the ring and stop the simulation
337 xbt_dynar_free(&node.comms);
338 xbt_free(node.mailbox);
339 xbt_free(node.pred_mailbox);
340 for (i = 0; i < NB_BITS - 1; i++) {
341 xbt_free(node.fingers[i].mailbox);
347 * \brief This function is called when the current node receives a task.
348 * \param node the current node
349 * \param task the task to handle (don't touch it then:
350 * it will be destroyed, reused or forwarded)
352 static void handle_task(node_t node, m_task_t task) {
354 msg_comm_t comm = NULL;
355 char* mailbox = NULL;
356 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
357 e_task_type_t type = task_data->type;
361 case TASK_FIND_SUCCESSOR:
362 INFO2("Receiving a 'Find Successor' request from %s for id %d",
363 task_data->issuer_host_name, task_data->request_id);
364 // is my successor the successor?
365 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
366 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
367 task_data->answer_id = node->fingers[0].id;
368 comm = MSG_task_isend(task, task_data->answer_to);
369 xbt_dynar_push(node->comms, &comm);
370 INFO3("Sending back a 'Find Successor Answer' to %s: the successor of %d is %d",
371 task_data->issuer_host_name,
372 task_data->request_id, task_data->answer_id);
375 // otherwise, forward the request to the closest preceding finger in my table
376 int closest = closest_preceding_node(node, task_data->request_id);
377 INFO2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
378 task_data->request_id, closest);
379 mailbox = get_mailbox(closest);
380 comm = MSG_task_isend(task, mailbox);
381 xbt_dynar_push(node->comms, &comm);
386 case TASK_GET_PREDECESSOR:
387 INFO1("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
388 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
389 task_data->answer_id = node->pred_id;
390 comm = MSG_task_isend(task, task_data->answer_to);
391 xbt_dynar_push(node->comms, &comm);
392 INFO3("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
393 task_data->issuer_host_name,
394 task_data->answer_to, task_data->answer_id);
398 // someone is telling me that he may be my new predecessor
399 INFO1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
400 notify(node, task_data->request_id);
402 MSG_task_destroy(task);
405 case TASK_PREDECESSOR_LEAVING:
406 // my predecessor is about to quit
407 INFO1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
408 // modify my predecessor
409 set_predecessor(node, task_data->request_id);
411 MSG_task_destroy(task);
413 >> notify my new predecessor
414 >> send a notify_predecessors !!
418 case TASK_SUCCESSOR_LEAVING:
419 // my successor is about to quit
420 INFO1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
421 // modify my successor FIXME : this should be implicit ?
422 set_finger(node, 0, task_data->request_id);
424 MSG_task_destroy(task);
426 >> notify my new successor
427 >> update my table & predecessors table */
431 CRITICAL1("Received an unexpected task: %d", type);
437 * \brief Initializes the current node as the first one of the system.
438 * \param node the current node
440 static void create(node_t node)
442 INFO0("Create a new Chord ring...");
443 set_predecessor(node, -1); // -1 means that I have no predecessor
444 print_finger_table(node);
448 * \brief Makes the current node join the ring, knowing the id of a node
449 * already in the ring
450 * \param node the current node
451 * \param known_id id of a node already in the ring
453 static void join(node_t node, int known_id)
455 INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
456 set_predecessor(node, -1); // no predecessor (yet)
460 successor_id = remote_find_successor(node, known_id, node->id);
461 if (successor_id == -1) {
462 INFO0("I really want to join the ring. Let's try again.");
464 } while (successor_id == -1);
466 set_finger(node, 0, successor_id);
467 print_finger_table(node);
471 * \brief Makes the current node quit the system
472 * \param node the current node
474 static void leave(node_t node)
476 INFO0("Well Guys! I Think it's time for me to quit ;)");
477 quit_notify(node, 1); // notify to my successor ( >>> 1 );
478 quit_notify(node, -1); // notify my predecessor ( >>> -1);
483 * \brief Notifies the successor or the predecessor of the current node
485 * \param node the current node
486 * \param to 1 to notify the successor, -1 to notify the predecessor
487 * FIXME: notify both nodes with only one call
489 static void quit_notify(node_t node, int to)
492 task_data_t req_data = xbt_new0(s_task_data_t, 1);
493 req_data->request_id = node->id;
494 req_data->successor_id = node->fingers[0].id;
495 req_data->pred_id = node->pred_id;
496 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
497 const char* task_name = NULL;
498 const char* to_mailbox = NULL;
499 if (to == 1) { // notify my successor
500 to_mailbox = node->fingers[0].mailbox;
501 INFO2("Telling my Successor %d about my departure via mailbox %s",
502 node->fingers[0].id, to_mailbox);
503 req_data->type = TASK_PREDECESSOR_LEAVING;
505 else if (to == -1) { // notify my predecessor
507 if (node->pred_id == -1) {
511 to_mailbox = node->pred_mailbox;
512 INFO2("Telling my Predecessor %d about my departure via mailbox %s",
513 node->pred_id, to_mailbox);
514 req_data->type = TASK_SUCCESSOR_LEAVING;
516 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
517 //char* mailbox = get_mailbox(to_mailbox);
518 msg_comm_t comm = MSG_task_isend(task, to_mailbox);
519 xbt_dynar_push(node->comms, &comm);
524 * \brief Makes the current node find the successor node of an id.
525 * \param node the current node
526 * \param id the id to find
527 * \return the id of the successor node, or -1 if the request failed
529 static int find_successor(node_t node, int id)
531 // is my successor the successor?
532 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
533 return node->fingers[0].id;
536 // otherwise, ask the closest preceding finger in my table
537 int closest = closest_preceding_node(node, id);
538 return remote_find_successor(node, closest, id);
542 * \brief Asks another node the successor node of an id.
543 * \param node the current node
544 * \param ask_to the node to ask to
545 * \param id the id to find
546 * \return the id of the successor node, or -1 if the request failed
548 static int remote_find_successor(node_t node, int ask_to, int id)
552 char* mailbox = get_mailbox(ask_to);
553 task_data_t req_data = xbt_new0(s_task_data_t, 1);
554 req_data->type = TASK_FIND_SUCCESSOR;
555 req_data->request_id = id;
556 req_data->answer_to = node->mailbox;
557 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
559 // send a "Find Successor" request to ask_to_id
560 INFO2("Sending a 'Find Successor' request to %d for id %d", ask_to, id);
561 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
562 MSG_error_t res = MSG_task_send_with_timeout(task, mailbox, 50);
565 INFO2("Failed to send the 'Find Successor' request to %d for id %d", ask_to, id);
566 MSG_task_destroy(task);
571 // receive the answer
572 DEBUG2("Sent a 'Find Successor' request to %d for key %d, waiting for the answer", ask_to, id);
575 if (node->comm_receive == NULL) {
577 node->comm_receive = MSG_task_irecv(&task, node->mailbox);
580 res = MSG_comm_wait(node->comm_receive, 50);
583 INFO1("Failed to receive the answer to my 'Find Successor' request: %d", res);
585 // MSG_comm_destroy(node->comm_receive);
588 task = MSG_comm_get_task(node->comm_receive);
589 task_data_t ans_data = MSG_task_get_data(task);
591 if (ans_data->type != TASK_FIND_SUCCESSOR_ANSWER) {
592 handle_task(node, task);
595 INFO2("Received the answer to my 'Find Successor' request: the successor of key %d is %d", id, successor);
596 successor = ans_data->answer_id;
598 MSG_task_destroy(task);
602 node->comm_receive = NULL;
611 * \brief Asks another node its predecessor.
612 * \param node the current node
613 * \param ask_to the node to ask to
614 * \return the id of its predecessor node, or -1 if the request failed
615 * (or if the node does not know its predecessor)
617 static int remote_get_predecessor(node_t node, int ask_to)
619 int predecessor_id = -1;
621 char* mailbox = get_mailbox(ask_to);
622 task_data_t req_data = xbt_new0(s_task_data_t, 1);
623 req_data->type = TASK_GET_PREDECESSOR;
624 req_data->answer_to = node->mailbox;
625 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
627 // send a "Get Predecessor" request to ask_to_id
628 INFO1("Sending a 'Get Predecessor' request to %d", ask_to);
629 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
630 MSG_error_t res = MSG_task_send_with_timeout(task, mailbox, 50);
633 INFO1("Failed to send the 'Get Predecessor' request to %d", ask_to);
634 MSG_task_destroy(task);
639 // receive the answer
640 DEBUG2("Sent 'Get Predecessor' request to %d, waiting for the answer on my mailbox '%s'", ask_to, req_data->answer_to);
643 if (node->comm_receive == NULL) { // FIXME simplify this
645 node->comm_receive = MSG_task_irecv(&task, node->mailbox);
648 res = MSG_comm_wait(node->comm_receive, 50);
651 INFO1("Failed to receive the answer to my 'Get Predecessor' request: %d", res);
653 // MSG_comm_destroy(node->comm_receive);
656 task = MSG_comm_get_task(node->comm_receive);
657 task_data_t ans_data = MSG_task_get_data(task);
659 if (ans_data->type != TASK_GET_PREDECESSOR_ANSWER) {
660 handle_task(node, task);
663 INFO2("Received the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to, predecessor_id);
664 predecessor_id = ans_data->answer_id;
666 MSG_task_destroy(task);
670 node->comm_receive = NULL;
675 return predecessor_id;
679 * \brief Returns the closest preceding finger of an id
680 * with respect to the finger table of the current node.
681 * \param node the current node
682 * \param id the id to find
683 * \return the closest preceding finger of that id
685 int closest_preceding_node(node_t node, int id)
688 for (i = NB_BITS - 1; i >= 0; i--) {
689 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
690 return node->fingers[i].id;
697 * \brief This function is called periodically. It checks the immediate
698 * successor of the current node.
699 * \param node the current node
701 static void stabilize(node_t node)
703 INFO0("Stabilizing node");
705 // get the predecessor of my immediate successor
707 int successor_id = node->fingers[0].id;
708 if (successor_id != node->id) {
709 candidate_id = remote_get_predecessor(node, successor_id);
712 candidate_id = node->pred_id;
715 // this node is a candidate to become my new successor
716 if (candidate_id != -1
717 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
718 set_finger(node, 0, candidate_id);
720 if (successor_id != node->id) {
721 remote_notify(node, successor_id, node->id);
726 * \brief Notifies the current node that its predecessor may have changed.
727 * \param node the current node
728 * \param candidate_id the possible new predecessor
730 static void notify(node_t node, int predecessor_candidate_id) {
732 if (node->pred_id == -1
733 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
735 set_predecessor(node, predecessor_candidate_id);
736 print_finger_table(node);
739 INFO1("I don't have to change my predecessor to %d", predecessor_candidate_id);
744 * \brief Notifies a remote node that its predecessor may have changed.
745 * \param node the current node
746 * \param notify_id id of the node to notify
747 * \param candidate_id the possible new predecessor
749 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
751 task_data_t req_data = xbt_new0(s_task_data_t, 1);
752 req_data->type = TASK_NOTIFY;
753 req_data->request_id = predecessor_candidate_id;
754 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
756 // send a "Notify" request to notify_id
757 INFO1("Sending a 'Notify' request to %d", notify_id);
758 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
759 char* mailbox = get_mailbox(notify_id);
760 msg_comm_t comm = MSG_task_isend(task, mailbox);
761 xbt_dynar_push(node->comms, &comm);
766 * \brief This function is called periodically.
767 * It refreshes the finger table of the current node.
768 * \param node the current node
770 static void fix_fingers(node_t node) {
772 INFO0("Fixing fingers");
773 int i = node->next_finger_to_fix;
774 int id = find_successor(node, node->id + powers2[i]);
777 if (id != node->fingers[i].id) {
778 set_finger(node, i, id);
779 print_finger_table(node);
781 node->next_finger_to_fix = (i + 1) % NB_BITS;
786 * \brief This function is called periodically.
787 * It checks whether the predecessor has failed
788 * \param node the current node
790 static void check_predecessor(node_t node)
792 INFO0("Checking whether my predecessor is alive");
797 * \brief Main function.
799 int main(int argc, char *argv[])
802 printf("Usage: %s platform_file deployment_file\n", argv[0]);
803 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
807 const char* platform_file = argv[1];
808 const char* application_file = argv[2];
812 MSG_global_init(&argc, argv);
813 MSG_set_channel_number(0);
814 MSG_create_environment(platform_file);
816 MSG_function_register("node", node);
817 MSG_launch_application(application_file);
819 MSG_error_t res = MSG_main();
820 INFO1("Simulation time: %g", MSG_get_clock());