Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add option smpi/privatize-lib to add extra library to privatization.
[simgrid.git] / examples / s4u / dht-chord / s4u-dht-chord.hpp
1 /* Copyright (c) 2016-2018. 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/ex.hpp>
12 #include <xbt/str.h>
13
14 #define MAX_SIMULATION_TIME 1000
15 #define PERIODIC_STABILIZE_DELAY 20
16 #define PERIODIC_FIX_FINGERS_DELAY 120
17 #define PERIODIC_CHECK_PREDECESSOR_DELAY 120
18 #define PERIODIC_LOOKUP_DELAY 10
19 #define SLEEP_DELAY 4.9999
20
21 extern int nb_bits;
22 extern int nb_keys;
23 extern int timeout;
24
25 class HostChord {
26   RngStream 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_           = RngStream_CreateStream(descr.c_str());
36   }
37
38   ~HostChord() { RngStream_DeleteStream(&stream_); };
39
40   RngStream getStream() { return stream_; };
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::MailboxPtr answer_to; // 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   ~ChordMessage() = default;
71
72   static void destroy(void* message);
73 };
74
75 class Node {
76   int known_id_      = -1;
77   double start_time_ = -1;
78   double deadline_   = -1;
79   bool joined        = false;
80   int id_;                           // my id
81   int pred_id_ = -1;                 // predecessor id
82   simgrid::s4u::MailboxPtr mailbox_; // my mailbox
83   int* fingers_;                     // finger table,(fingers[0] is my successor)
84   int next_finger_to_fix;            // index of the next finger to fix in fix_fingers()
85   RngStream stream;
86
87 public:
88   explicit Node(std::vector<std::string> args);
89   Node(const Node&) = delete;
90   Node& operator=(const Node&) = delete;
91   ~Node();
92   void join(int known_id);
93   void leave();
94   void notifyAndQuit();
95
96   void randomLookup();
97   void setFinger(int finger_index, int id);
98   void fixFingers();
99   void printFingerTable();
100
101   void setPredecessor(int predecessor_id);
102   void checkPredecessor();
103   int remoteGetPredecessor(int ask_to);
104   int closestPrecedingFinger(int id);
105   int findSuccessor(int id);
106   int remoteFindSuccessor(int ask_to, int id);
107
108   void notify(int predecessor_candidate_id);
109   void remoteNotify(int notify_id, int predecessor_candidate_id);
110   void stabilize();
111   void handleMessage(ChordMessage* message);
112
113   void operator()()
114   {
115     simgrid::s4u::this_actor::sleep_for(start_time_);
116     if (known_id_ == -1) {
117       setPredecessor(-1); // -1 means that I have no predecessor
118       printFingerTable();
119       joined = true;
120     } else {
121       join(known_id_);
122     }
123
124     if (not joined)
125       return;
126     void* data                         = nullptr;
127     double now                         = simgrid::s4u::Engine::get_clock();
128     double next_stabilize_date         = start_time_ + PERIODIC_STABILIZE_DELAY;
129     double next_fix_fingers_date       = start_time_ + PERIODIC_FIX_FINGERS_DELAY;
130     double next_check_predecessor_date = start_time_ + PERIODIC_CHECK_PREDECESSOR_DELAY;
131     double next_lookup_date            = start_time_ + PERIODIC_LOOKUP_DELAY;
132     simgrid::s4u::CommPtr comm_receive = nullptr;
133     while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME) {
134       if (comm_receive == nullptr)
135         comm_receive = mailbox_->get_async(&data);
136       while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME && not comm_receive->test()) {
137         // no task was received: make some periodic calls
138         if (now >= next_stabilize_date) {
139           stabilize();
140           next_stabilize_date = simgrid::s4u::Engine::get_clock() + PERIODIC_STABILIZE_DELAY;
141         } else if (now >= next_fix_fingers_date) {
142           fixFingers();
143           next_fix_fingers_date = simgrid::s4u::Engine::get_clock() + PERIODIC_FIX_FINGERS_DELAY;
144         } else if (now >= next_check_predecessor_date) {
145           checkPredecessor();
146           next_check_predecessor_date = simgrid::s4u::Engine::get_clock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
147         } else if (now >= next_lookup_date) {
148           randomLookup();
149           next_lookup_date = simgrid::s4u::Engine::get_clock() + PERIODIC_LOOKUP_DELAY;
150         } else {
151           // nothing to do: sleep for a while
152           simgrid::s4u::this_actor::sleep_for(SLEEP_DELAY);
153         }
154         now = simgrid::s4u::Engine::get_clock();
155       }
156
157       if (data != nullptr) {
158         ChordMessage* message = static_cast<ChordMessage*>(data);
159         handleMessage(message);
160         comm_receive = nullptr;
161         data         = nullptr;
162       }
163       now = simgrid::s4u::Engine::get_clock();
164     }
165     if (comm_receive != nullptr) {
166       if (comm_receive->test())
167         delete static_cast<ChordMessage*>(data);
168       else
169         comm_receive->cancel();
170     }
171     // leave the ring
172     leave();
173   }
174 };
175
176 #endif