Logo AND Algorithmique Numérique Distribuée

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