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;
32 s_finger_t fingers[NB_BITS]; // finger table (fingers[0] is my successor)
33 int pred_id; // predecessor id
35 xbt_dynar_t comms; // current communications to finish
41 typedef struct task_data {
45 const char* answer_to;
46 const char* issuer_host_name; // used for logging
47 } s_task_data_t, *task_data_t;
50 static int normalize(int id);
51 static int is_in_interval(int id, int start, int end);
52 static char* get_mailbox(int host_id);
53 static void print_finger_table(node_t node);
56 static int node(int argc, char *argv[]);
59 static void initialize_first_node(node_t node);
60 static void initialize_finger_table(node_t data, int known_id);
61 static void join(node_t node, int known_id);
64 static int find_successor(node_t node, int id);
65 static int remote_find_successor(node_t node, int ask_to_id, int id);
66 static int find_predecessor(node_t node, int id);
67 static int remote_find_predecessor(node_t node, int ask_to_id, int id);
68 static int closest_preceding_finger(node_t node, int id);
69 static int remote_closest_preceding_finger(int ask_to_id, int id);
70 static void notify_predecessors(node_t node);
71 static void remote_move_keys(node_t node, int take_from_id);
72 static void update_finger_table(node_t node, int candidate_id, int finger_index);
73 static void remote_update_finger_table(node_t node, int ask_to_id, int candidate_id, int finger_index);
74 static void notify(node_t node, int predecessor_candidate_id);
75 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
76 static void stabilize(node_t node);
79 * \brief Turns an id into an equivalent id in [0, NB_KEYS[
81 * \return the corresponding normalized id
83 static int normalize(int id) {
90 // make sure id < NB_KEYS
97 * \brief Returns whether a id belongs to the interval [start, end].
99 * The parameters are noramlized to make sure they are between 0 and CHORD_NB_KEYS - 1).
100 * 1 belongs to [62, 3]
101 * 1 does not belong to [3, 62]
102 * 63 belongs to [62, 3]
103 * 63 does not belong to [3, 62]
104 * 24 belongs to [21, 29]
105 * 24 does not belong to [29, 21]
107 * \param id id to check
108 * \param start lower bound
109 * \param end upper bound
110 * \return a non-zero value if id in in [start, end]
112 static int is_in_interval(int id, int start, int end) {
115 start = normalize(start);
116 end = normalize(end);
118 // make sure end >= start and id >= start
131 * \brief Gets the mailbox name of a host given its chord id.
132 * \param node_id id of a node
133 * \return the name of its mailbox
134 * FIXME: free the memory
136 static char* get_mailbox(int node_id) {
138 return bprintf("mailbox%d", node_id);
142 * \brief Displays the finger table of a node.
145 static void print_finger_table(node_t node) {
149 INFO0("My finger table:");
150 INFO0("Start | Succ ");
151 for (i = 0; i < NB_BITS; i++) {
152 INFO2(" %3d | %3d ", (node->id + pow) % NB_KEYS, node->fingers[i].id);
155 INFO1("Predecessor: %d", node->pred_id);
159 * \brief Node Function
162 * - the id of a guy I know in the system (except for the first node)
163 * - the time to sleep before I join (except for the first node)
165 int node(int argc, char *argv[])
167 msg_comm_t comm = NULL;
171 xbt_assert0(argc == 2 || argc == 4, "Wrong number of arguments for this node");
173 // initialize my node
175 node.id = atoi(argv[1]);
176 node.mailbox = get_mailbox(node.id);
177 node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
179 if (argc == 2) { // first ring
180 initialize_first_node(&node);
183 int known_id = atoi(argv[2]);
184 double sleep_time = atof(argv[3]);
186 // sleep before starting
187 INFO1("Let's sleep >>%f", sleep_time);
188 MSG_process_sleep(sleep_time);
189 INFO0("Hey! Let's join the system.");
191 join(&node, known_id);
198 xbt_dynar_foreach(node.comms, cursor, comm) {
199 if (MSG_comm_test(comm)) { // FIXME: try with MSG_comm_testany instead
200 xbt_dynar_cursor_rm(node.comms, &cursor);
201 MSG_comm_destroy(comm);
205 m_task_t task = NULL;
206 MSG_error_t res = MSG_task_receive(&task, node.mailbox);
208 xbt_assert0(res == MSG_OK, "MSG_task_receive failed");
211 const char* task_name = MSG_task_get_name(task);
212 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
214 if (!strcmp(task_name, "Find Successor")) {
215 INFO2("Receiving a 'Find Successor' Request from %s for id %d", task_data->issuer_host_name, task_data->request_id);
216 // is my successor the successor?
217 if (is_in_interval(task_data->request_id, node.id + 1, node.fingers[0].id)) {
218 task_data->answer_id = node.fingers[0].id;
219 MSG_task_set_name(task, "Find Successor Answer");
220 INFO3("Sending back a 'Find Successor' Answer to %s: the successor of %d is %d", task_data->issuer_host_name, task_data->request_id, task_data->answer_id);
221 comm = MSG_task_isend(task, task_data->answer_to);
222 xbt_dynar_push(node.comms, &comm);
225 // otherwise, forward the request to the closest preceding finger in my table
226 int closest = closest_preceding_finger(&node, task_data->request_id);
227 INFO2("Forwarding 'Find Successor' request for id %d to my closest preceding finger %d", task_data->request_id, closest);
228 mailbox = get_mailbox(closest);
229 comm = MSG_task_isend(task, mailbox);
230 xbt_dynar_push(node.comms, &comm);
235 else if (!strcmp(task_name, "Find Successor Answer")) {
239 else if (!strcmp(task_name, "Find Predecessor")) {
240 INFO2("Receiving a 'Find Predecessor' Request from %s for id %d", task_data->issuer_host_name, task_data->request_id);
241 // am I the predecessor?
242 if (is_in_interval(task_data->request_id, node.id + 1, node.fingers[0].id)) {
243 task_data->answer_id = node.id;
244 MSG_task_set_name(task, "Find Predecessor Answer");
245 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);
246 comm = MSG_task_isend(task, task_data->answer_to);
247 xbt_dynar_push(node.comms, &comm);
250 // otherwise, forward the request to the closest preceding finger in my table
251 int closest = closest_preceding_finger(&node, task_data->request_id);
252 INFO2("Forwarding 'Find Predecessor' request for id %d to my closest preceding finger %d", task_data->request_id, closest);
253 mailbox = get_mailbox(closest);
254 comm = MSG_task_isend(task, mailbox);
255 xbt_dynar_push(node.comms, &comm);
260 else if (!strcmp(task_name, "Find Predecessor Answer")) {
264 else if (!strcmp(task_name, "Update Finger")) {
265 // someone is telling me that he may be my new finger
266 INFO1("Receiving an 'Update Finger' request from %s", task_data->issuer_host_name);
267 update_finger_table(&node, task_data->request_id, task_data->request_finger);
269 else if (!strcmp(task_name, "Notify")) {
270 // someone is telling me that he may be my new predecessor
271 INFO1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
272 notify(&node, task_data->request_id);
275 else if (!strcmp(task_name, "Fix Fingers"))
278 for (i = KEY_BITS - 1 ; i >= 0; i--)
280 data->fingers[i] = find_finger_elem(data,(data->id)+pow(2,i-1));
286 xbt_dynar_free(&node.comms);
287 xbt_free(node.mailbox);
288 xbt_free(node.pred_mailbox);
289 for (i = 0; i < NB_BITS - 1; i++) {
290 xbt_free(node.fingers[i].mailbox);
295 * \brief Initializes the current node as the first one of the system.
296 * \param node the current node
298 static void initialize_first_node(node_t node)
300 INFO0("Create a new Chord ring...");
302 // I am my own successor and predecessor
304 for (i = 0; i < NB_BITS; i++) {
305 node->fingers[i].id = node->id;
306 node->fingers[i].mailbox = xbt_strdup(node->mailbox);
308 node->pred_id = node->id;
309 node->pred_mailbox = node->mailbox;
310 print_finger_table(node);
314 * \brief Makes the current node join the system, knowing the id of a node already in the system
315 * \param node the current node
316 * \param known_id id of a node already in the system
318 static void join(node_t node, int known_id)
320 initialize_finger_table(node, known_id); // determine my fingers, asking to known_id
321 remote_notify(node, node->fingers[0].id, node->id); // tell my successor that I'm his new predecessor
322 notify_predecessors(node); // tell others that I may have became their finger
323 remote_move_keys(node, node->fingers[0].id); // take some key-value pairs from my sucessor
327 * \brief Initializes my finger table, knowing the id of a node already in the system.
328 * \param node the current node
329 * \param known_id id of a node already in the system
331 static void initialize_finger_table(node_t node, int known_id)
333 int my_id = node->id;
337 INFO0("Initializing my finger table...");
339 // ask known_id who is my immediate successor
340 node->fingers[0].id = remote_find_successor(node, known_id, my_id + 1);
341 node->fingers[0].mailbox = get_mailbox(node->fingers[0].id);
343 // find all other fingers
344 for (i = 0; i < NB_BITS - 1; i++) {
346 pow = pow << 1; // equivalent to pow = pow * 2
347 if (is_in_interval(my_id + pow, my_id, node->fingers[i].id - 1)) {
348 // I already have the info for this finger
349 node->fingers[i + 1].id = node->fingers[i].id;
352 // I don't have the info, ask the only guy I know
353 node->fingers[i + 1].id = remote_find_successor(node, known_id, my_id + pow);
355 node->fingers[i + 1].mailbox = get_mailbox(node->fingers[i + 1].id);
358 node->pred_id = find_predecessor(node, node->id);
359 node->pred_mailbox = get_mailbox(node->pred_id);
361 INFO0("Finger table initialized!");
362 print_finger_table(node);
366 * \brief Notifies some nodes that the current node may have became their finger.
367 * \param node the current node, which has just joined the system
369 static void notify_predecessors(node_t node)
373 for (i = 0; i < NB_BITS; i++) {
374 // find the closest node whose finger #i can be me
375 pred_id = find_predecessor(node, node->id - pow + 1); // note: no "+1" in the article!
376 if (pred_id != node->id) {
377 remote_update_finger_table(node, pred_id, node->id, i);
379 pow = pow << 1; // pow = pow * 2
384 * \brief Tells the current node that a node may have became its new finger.
385 * \param node the current node
386 * \param candidate_id id of the node that may be a new finger of the current node
387 * \param finger_index index of the finger to update
389 static void update_finger_table(node_t node, int candidate_id, int finger_index)
393 for (i = 0; i < finger_index; i++) {
397 // if (is_in_interval(candidate_id, node->id + pow, node->fingers[finger_index].id - 1)) {
398 if (is_in_interval(candidate_id, node->id, node->fingers[finger_index].id - 1)) {
399 // INFO3("Candidate %d is between %d and %d!", candidate_id, node->id + pow, node->fingers[finger_index].id - 1);
400 // candidate_id is my new finger
401 xbt_free(node->fingers[finger_index].mailbox);
402 node->fingers[finger_index].id = candidate_id;
403 node->fingers[finger_index].mailbox = get_mailbox(candidate_id);
404 INFO2("My new finger #%d is %d", finger_index, candidate_id);
405 print_finger_table(node);
407 if (node->pred_id != node->id) { // FIXME: is this necessary?
408 // my predecessor may be concerned too
409 remote_update_finger_table(node, node->pred_id, candidate_id, finger_index);
415 * \brief Tells a remote node that a node may have became its new finger.
416 * \param ask_to_id id of the remote node to update
417 * \param candidate_id id of the node that may be a new finger of the remote node
418 * \param finger_index index of the finger to update
420 static void remote_update_finger_table(node_t node, int ask_to_id, int candidate_id, int finger_index)
422 task_data_t req_data = xbt_new0(s_task_data_t, 1);
423 req_data->request_id = candidate_id;
424 req_data->request_finger = finger_index;
425 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
427 // send a "Update Finger" request to ask_to_id
428 INFO3("Sending an 'Update Finger' request to %d: his finger #%d may be %d now", ask_to_id, finger_index, candidate_id);
429 m_task_t task = MSG_task_create("Update Finger", 1000, 5000, req_data);
430 char* mailbox = get_mailbox(ask_to_id);
431 msg_comm_t comm = MSG_task_isend(task, mailbox);
432 xbt_dynar_push(node->comms, &comm);
437 * \brief Makes the current node find the successor node of an id.
438 * \param node the current node
439 * \param id the id to find
440 * \return the id of the successor node
442 static int find_successor(node_t node, int id)
444 // is my successor the successor?
445 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
446 return node->fingers[0].id;
449 // otherwise, ask the closest preceding finger in my table
450 int closest = closest_preceding_finger(node, id);
451 return remote_find_successor(node, closest, id);
455 * \brief Asks another node the successor node of an id.
456 * \param node the current node
457 * \param ask_to the node to ask to
458 * \param id the id to find
459 * \return the id of the successor node
461 static int remote_find_successor(node_t node, int ask_to, int id)
463 s_task_data_t req_data;
464 char* mailbox = bprintf("%s Find Successor", node->mailbox);
465 req_data.request_id = id;
466 req_data.answer_to = mailbox;
467 req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
469 // send a "Find Successor" request to ask_to_id
470 INFO2("Sending a 'Find Successor' request to %d for key %d", ask_to, id);
471 m_task_t task = MSG_task_create("Find Successor", 1000, 5000, &req_data);
472 MSG_task_send(task, get_mailbox(ask_to));
474 // receive the answer
476 MSG_task_receive(&task, req_data.answer_to);
477 task_data_t ans_data;
478 ans_data = MSG_task_get_data(task);
479 int successor = ans_data->answer_id;
481 INFO2("Received the answer to my Find Successor request: the successor of key %d is %d", id, successor);
487 * \brief Makes the current node find the predecessor node of an id.
488 * \param node the current node
489 * \param id the id to find
490 * \return the id of the predecessor node
492 static int find_predecessor(node_t node, int id)
494 if (node->id == node->fingers[0].id) {
495 // I am the only node in the system
499 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
502 int ask_to = closest_preceding_finger(node, id);
503 return remote_find_predecessor(node, ask_to, id);
507 * \brief Asks another node the predecessor node of an id.
508 * \param node the current node
509 * \param ask_to the node to ask to
510 * \param id the id to find
511 * \return the id of the predecessor node
513 static int remote_find_predecessor(node_t node, int ask_to, int id)
515 s_task_data_t req_data;
516 char* mailbox = bprintf("%s Find Predecessor", node->mailbox);
517 req_data.request_id = id;
518 req_data.answer_to = mailbox;
519 req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
521 // send a "Find Predecessor" request to ask_to
522 INFO2("Sending a 'Find Predecessor' request to %d for key %d", ask_to, id);
523 m_task_t task = MSG_task_create("Find Predecessor", 1000, 5000, &req_data);
524 MSG_task_send(task, get_mailbox(ask_to));
526 // receive the answer
528 MSG_task_receive(&task, req_data.answer_to);
529 task_data_t ans_data;
530 ans_data = MSG_task_get_data(task);
531 int predecessor = ans_data->answer_id;
533 INFO2("Received the answer to my 'Find Predecessor' request: the predecessor of key %d is %d", id, predecessor);
539 * \brief Returns the closest preceding finger of an id
540 * with respect to the finger table of the current node.
541 * \param node the current node
542 * \param id the id to find
543 * \return the closest preceding finger of that id
545 int closest_preceding_finger(node_t node, int id)
548 for (i = NB_BITS - 1; i >= 0; i--) {
549 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
550 return node->fingers[i].id;
557 * \brief This function is called periodically. It checks the immediate
558 * successor of the current node.
559 * \param node the current node
561 static void stabilize(node_t node) {
563 int x = find_predecessor(node, node->fingers[0].id);
564 if (is_in_interval(x, node->id + 1, node->fingers[0].id)) {
565 xbt_free(node->fingers[0].mailbox);
566 node->fingers[0].id = x;
567 node->fingers[0].mailbox = get_mailbox(x);
569 remote_notify(node, node->fingers[0].id, node->id);
573 * \brief Notifies the current node that its predecessor may have changed.
574 * \param node the current node
575 * \param candidate_id the possible new predecessor
577 static void notify(node_t node, int predecessor_candidate_id) {
579 if (node->pred_id == node->id
580 || is_in_interval(predecessor_candidate_id, node->pred_id, node->id)) {
582 node->pred_id = predecessor_candidate_id;
583 node->pred_mailbox = get_mailbox(predecessor_candidate_id);
585 INFO1("My new predecessor is %d", predecessor_candidate_id);
586 print_finger_table(node);
589 INFO1("I don't have to change my predecessor to %d", predecessor_candidate_id);
594 * \brief Notifies a remote node that its predecessor may have changed.
595 * \param node the current node
596 * \param notify_id id of the node to notify
597 * \param candidate_id the possible new predecessor
599 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
601 task_data_t req_data = xbt_new0(s_task_data_t, 1);
602 req_data->request_id = predecessor_candidate_id;
603 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
605 // send a "Notify" request to notify_id
606 INFO1("Sending a 'Notify' request to %d", notify_id);
607 m_task_t task = MSG_task_create("Notify", 1000, 5000, req_data);
608 char* mailbox = get_mailbox(notify_id);
609 msg_comm_t comm = MSG_task_isend(task, mailbox);
610 xbt_dynar_push(node->comms, &comm);
615 * \brief Asks a node to take some of its keys.
616 * \param node the current node, which has just joined the system
617 * \param take_from_id id of a node who may have keys to give to the current node
619 static void remote_move_keys(node_t node, int take_from_id) {
624 * \brief Main function.
626 int main(int argc, char *argv[])
629 printf("Usage: %s platform_file deployment_file\n", argv[0]);
630 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
634 MSG_global_init(&argc, argv);
636 const char* platform_file = argv[1];
637 const char* application_file = argv[2];
639 /* MSG_config("workstation/model","KCCFLN05"); */
640 MSG_set_channel_number(0);
641 MSG_create_environment(platform_file);
643 MSG_function_register("node", node);
644 MSG_launch_application(application_file);
646 MSG_error_t res = MSG_main();
647 INFO1("Simulation time: %g", MSG_get_clock());