1 /* Copyright (c) 2016-2020. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simgrid/s4u.hpp"
10 #include <xbt/random.hpp>
13 constexpr double MAX_SIMULATION_TIME = 1000;
14 constexpr double PERIODIC_STABILIZE_DELAY = 20;
15 constexpr double PERIODIC_FIX_FINGERS_DELAY = 120;
16 constexpr double PERIODIC_CHECK_PREDECESSOR_DELAY = 120;
17 constexpr double PERIODIC_LOOKUP_DELAY = 10;
18 constexpr double SLEEP_DELAY = 4.9999;
24 /* Types of tasks exchanged between nodes. */
25 enum e_message_type_t {
27 FIND_SUCCESSOR_ANSWER,
29 GET_PREDECESSOR_ANSWER,
34 PREDECESSOR_ALIVE_ANSWER
39 e_message_type_t type; // type of message
40 std::string issuer_host_name = simgrid::s4u::this_actor::get_host()->get_name(); // used for logging
41 int request_id = -1; // id (used by some types of messages)
42 int request_finger = 1; // finger parameter (used by some types of messages)
43 int answer_id = -1; // answer (used by some types of messages)
44 simgrid::s4u::Mailbox* answer_to = nullptr; // mailbox to send an answer to (if any)
46 explicit ChordMessage(e_message_type_t type) : type(type) {}
48 static void destroy(void* message);
53 double start_time_ = -1;
54 double deadline_ = -1;
57 int pred_id_ = -1; // predecessor id
58 simgrid::s4u::Mailbox* mailbox_; // my mailbox
59 std::vector<int> fingers_; // finger table,(fingers[0] is my successor)
60 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
63 explicit Node(std::vector<std::string> args);
64 Node(const Node&) = delete;
65 Node& operator=(const Node&) = delete;
66 void join(int known_id);
71 void setFinger(int finger_index, int id);
73 void printFingerTable();
75 void setPredecessor(int predecessor_id);
76 void checkPredecessor();
77 int remoteGetPredecessor(int ask_to);
78 int closestPrecedingFinger(int id);
79 int findSuccessor(int id);
80 int remoteFindSuccessor(int ask_to, int id);
82 void notify(int predecessor_candidate_id);
83 void remoteNotify(int notify_id, int predecessor_candidate_id);
85 void handleMessage(ChordMessage* message);
89 simgrid::s4u::this_actor::sleep_for(start_time_);
90 if (known_id_ == -1) {
91 setPredecessor(-1); // -1 means that I have no predecessor
100 void* data = nullptr;
101 double now = simgrid::s4u::Engine::get_clock();
102 double next_stabilize_date = start_time_ + PERIODIC_STABILIZE_DELAY;
103 double next_fix_fingers_date = start_time_ + PERIODIC_FIX_FINGERS_DELAY;
104 double next_check_predecessor_date = start_time_ + PERIODIC_CHECK_PREDECESSOR_DELAY;
105 double next_lookup_date = start_time_ + PERIODIC_LOOKUP_DELAY;
106 simgrid::s4u::CommPtr comm_receive = nullptr;
107 while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME) {
108 if (comm_receive == nullptr)
109 comm_receive = mailbox_->get_async(&data);
110 while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME && not comm_receive->test()) {
111 // no task was received: make some periodic calls
112 if (now >= next_stabilize_date) {
114 next_stabilize_date = simgrid::s4u::Engine::get_clock() + PERIODIC_STABILIZE_DELAY;
115 } else if (now >= next_fix_fingers_date) {
117 next_fix_fingers_date = simgrid::s4u::Engine::get_clock() + PERIODIC_FIX_FINGERS_DELAY;
118 } else if (now >= next_check_predecessor_date) {
120 next_check_predecessor_date = simgrid::s4u::Engine::get_clock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
121 } else if (now >= next_lookup_date) {
123 next_lookup_date = simgrid::s4u::Engine::get_clock() + PERIODIC_LOOKUP_DELAY;
125 // nothing to do: sleep for a while
126 simgrid::s4u::this_actor::sleep_for(SLEEP_DELAY);
128 now = simgrid::s4u::Engine::get_clock();
131 if (data != nullptr) {
132 ChordMessage* message = static_cast<ChordMessage*>(data);
133 handleMessage(message);
134 comm_receive = nullptr;
137 now = simgrid::s4u::Engine::get_clock();
139 if (comm_receive != nullptr) {
140 if (comm_receive->test())
141 delete static_cast<ChordMessage*>(data);
143 comm_receive->cancel();