Logo AND Algorithmique Numérique Distribuée

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