Logo AND Algorithmique Numérique Distribuée

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