Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
353a10cbd3fc3bd113e91247ba903a6165c096f6
[simgrid.git] / examples / s4u / dht-chord / s4u-dht-chord.hpp
1 /* Copyright (c) 2016-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef S4U_CHORD_HPP
7 #define S4U_CHORD_HPP
8 #include "simgrid/s4u.hpp"
9 #include <random>
10 #include <string>
11 #include <xbt/str.h>
12
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;
19
20 extern int nb_bits;
21 extern int nb_keys;
22 extern int timeout;
23
24 extern std::default_random_engine generator;
25
26 class HostChord {
27   simgrid::s4u::Host* host = nullptr;
28
29 public:
30   static simgrid::xbt::Extension<simgrid::s4u::Host, HostChord> EXTENSION_ID;
31
32   explicit HostChord(simgrid::s4u::Host* ptr) : host(ptr) {}
33   HostChord(const HostChord&) = delete;
34   HostChord& operator=(const HostChord&) = delete;
35
36 };
37
38 /* Types of tasks exchanged between nodes. */
39 enum e_message_type_t {
40   FIND_SUCCESSOR,
41   FIND_SUCCESSOR_ANSWER,
42   GET_PREDECESSOR,
43   GET_PREDECESSOR_ANSWER,
44   NOTIFY,
45   SUCCESSOR_LEAVING,
46   PREDECESSOR_LEAVING,
47   PREDECESSOR_ALIVE,
48   PREDECESSOR_ALIVE_ANSWER
49 };
50
51 class ChordMessage {
52 public:
53   e_message_type_t type;              // type of message
54   std::string issuer_host_name;       // used for logging
55   int request_id     = -1;            // id (used by some types of messages)
56   int request_finger = 1;             // finger parameter (used by some types of messages)
57   int answer_id      = -1;            // answer (used by some types of messages)
58   simgrid::s4u::Mailbox* answer_to = nullptr;       // mailbox to send an answer to (if any)
59
60   explicit ChordMessage(e_message_type_t type)
61       : type(type), issuer_host_name(simgrid::s4u::this_actor::get_host()->get_name())
62   {
63   }
64
65   static void destroy(void* message);
66 };
67
68 class Node {
69   int known_id_      = -1;
70   double start_time_ = -1;
71   double deadline_   = -1;
72   bool joined        = false;
73   int id_;                           // my id
74   int pred_id_ = -1;                 // predecessor id
75   simgrid::s4u::Mailbox* mailbox_;   // my mailbox
76   std::vector<int> fingers_;         // finger table,(fingers[0] is my successor)
77   int next_finger_to_fix;            // index of the next finger to fix in fix_fingers()
78
79 public:
80   explicit Node(std::vector<std::string> args);
81   Node(const Node&) = delete;
82   Node& operator=(const Node&) = delete;
83   void join(int known_id);
84   void leave();
85   void notifyAndQuit();
86
87   void randomLookup();
88   void setFinger(int finger_index, int id);
89   void fixFingers();
90   void printFingerTable();
91
92   void setPredecessor(int predecessor_id);
93   void checkPredecessor();
94   int remoteGetPredecessor(int ask_to);
95   int closestPrecedingFinger(int id);
96   int findSuccessor(int id);
97   int remoteFindSuccessor(int ask_to, int id);
98
99   void notify(int predecessor_candidate_id);
100   void remoteNotify(int notify_id, int predecessor_candidate_id);
101   void stabilize();
102   void handleMessage(ChordMessage* message);
103
104   void operator()()
105   {
106     simgrid::s4u::this_actor::sleep_for(start_time_);
107     if (known_id_ == -1) {
108       setPredecessor(-1); // -1 means that I have no predecessor
109       printFingerTable();
110       joined = true;
111     } else {
112       join(known_id_);
113     }
114
115     if (not joined)
116       return;
117     void* data                         = nullptr;
118     double now                         = simgrid::s4u::Engine::get_clock();
119     double next_stabilize_date         = start_time_ + PERIODIC_STABILIZE_DELAY;
120     double next_fix_fingers_date       = start_time_ + PERIODIC_FIX_FINGERS_DELAY;
121     double next_check_predecessor_date = start_time_ + PERIODIC_CHECK_PREDECESSOR_DELAY;
122     double next_lookup_date            = start_time_ + PERIODIC_LOOKUP_DELAY;
123     simgrid::s4u::CommPtr comm_receive = nullptr;
124     while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME) {
125       if (comm_receive == nullptr)
126         comm_receive = mailbox_->get_async(&data);
127       while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME && not comm_receive->test()) {
128         // no task was received: make some periodic calls
129         if (now >= next_stabilize_date) {
130           stabilize();
131           next_stabilize_date = simgrid::s4u::Engine::get_clock() + PERIODIC_STABILIZE_DELAY;
132         } else if (now >= next_fix_fingers_date) {
133           fixFingers();
134           next_fix_fingers_date = simgrid::s4u::Engine::get_clock() + PERIODIC_FIX_FINGERS_DELAY;
135         } else if (now >= next_check_predecessor_date) {
136           checkPredecessor();
137           next_check_predecessor_date = simgrid::s4u::Engine::get_clock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
138         } else if (now >= next_lookup_date) {
139           randomLookup();
140           next_lookup_date = simgrid::s4u::Engine::get_clock() + PERIODIC_LOOKUP_DELAY;
141         } else {
142           // nothing to do: sleep for a while
143           simgrid::s4u::this_actor::sleep_for(SLEEP_DELAY);
144         }
145         now = simgrid::s4u::Engine::get_clock();
146       }
147
148       if (data != nullptr) {
149         ChordMessage* message = static_cast<ChordMessage*>(data);
150         handleMessage(message);
151         comm_receive = nullptr;
152         data         = nullptr;
153       }
154       now = simgrid::s4u::Engine::get_clock();
155     }
156     if (comm_receive != nullptr) {
157       if (comm_receive->test())
158         delete static_cast<ChordMessage*>(data);
159       else
160         comm_receive->cancel();
161     }
162     // leave the ring
163     leave();
164   }
165 };
166
167 #endif