Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar by killing useless parameters and cleanups
[simgrid.git] / examples / s4u / dht-chord / s4u_dht-chord.hpp
1 /* Copyright (c) 2016-2017. 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 extern int* powers2;
25
26 class HostChord {
27   RngStream stream_;
28   simgrid::s4u::Host* host = nullptr;
29
30 public:
31   static simgrid::xbt::Extension<simgrid::s4u::Host, HostChord> EXTENSION_ID;
32
33   explicit HostChord(simgrid::s4u::Host* ptr) : host(ptr)
34   {
35     std::string descr = std::string("RngSream<") + host->cname() + ">";
36     stream_           = RngStream_CreateStream(descr.c_str());
37   }
38
39   ~HostChord() { RngStream_DeleteStream(&stream_); };
40
41   RngStream getStream() { return stream_; };
42 };
43
44 /* Types of tasks exchanged between nodes. */
45 typedef enum {
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 } e_message_type_t;
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   ChordMessage(e_message_type_t type) : type(type) { issuer_host_name = simgrid::s4u::this_actor::host()->name(); }
67
68   ~ChordMessage() = default;
69 };
70
71 class Node {
72   int known_id_      = -1;
73   double start_time_ = -1;
74   double deadline_   = -1;
75   bool joined        = false;
76   int id_;                           // my id
77   int pred_id_ = -1;                 // predecessor id
78   simgrid::s4u::MailboxPtr mailbox_; // my mailbox
79   int* fingers_;                     // finger table,(fingers[0] is my successor)
80   int next_finger_to_fix;            // index of the next finger to fix in fix_fingers()
81   RngStream stream;
82
83 public:
84   explicit Node(std::vector<std::string> args);
85   ~Node();
86   void join(int known_id);
87   void leave();
88   void notifyAndQuit();
89
90   void randomLookup();
91   void setFinger(int finger_index, int id);
92   void fixFingers();
93   void printFingerTable();
94
95   void setPredecessor(int predecessor_id);
96   void checkPredecessor();
97   int remoteGetPredecessor(int ask_to);
98   int closestPrecedingFinger(int id);
99   int findSuccessor(int id);
100   int remoteFindSuccessor(int ask_to, int id);
101
102   void notify(int predecessor_candidate_id);
103   void remoteNotify(int notify_id, int predecessor_candidate_id);
104   void stabilize();
105   void handleMessage(ChordMessage* message);
106
107   void operator()()
108   {
109     simgrid::s4u::this_actor::sleep_for(start_time_);
110     if (known_id_ == -1) {
111       setPredecessor(-1); // -1 means that I have no predecessor
112       printFingerTable();
113       joined = true;
114     } else {
115       join(known_id_);
116     }
117
118     if (!joined)
119       return;
120     ChordMessage* message              = nullptr;
121     void* data                         = nullptr;
122     double now                         = simgrid::s4u::Engine::getClock();
123     double next_stabilize_date         = start_time_ + PERIODIC_STABILIZE_DELAY;
124     double next_fix_fingers_date       = start_time_ + PERIODIC_FIX_FINGERS_DELAY;
125     double next_check_predecessor_date = start_time_ + PERIODIC_CHECK_PREDECESSOR_DELAY;
126     double next_lookup_date            = start_time_ + PERIODIC_LOOKUP_DELAY;
127
128     while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME) {
129       data                             = nullptr;
130       simgrid::s4u::Comm& comm_receive = simgrid::s4u::this_actor::irecv(mailbox_, &data);
131       while ((now < (start_time_ + deadline_)) && now < MAX_SIMULATION_TIME && !comm_receive.test()) {
132         // no task was received: make some periodic calls
133         if (now >= next_stabilize_date) {
134           stabilize();
135           next_stabilize_date = simgrid::s4u::Engine::getClock() + PERIODIC_STABILIZE_DELAY;
136         } else if (now >= next_fix_fingers_date) {
137           fixFingers();
138           next_fix_fingers_date = simgrid::s4u::Engine::getClock() + PERIODIC_FIX_FINGERS_DELAY;
139         } else if (now >= next_check_predecessor_date) {
140           checkPredecessor();
141           next_check_predecessor_date = simgrid::s4u::Engine::getClock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
142         } else if (now >= next_lookup_date) {
143           randomLookup();
144           next_lookup_date = simgrid::s4u::Engine::getClock() + PERIODIC_LOOKUP_DELAY;
145         } else {
146           // nothing to do: sleep for a while
147           simgrid::s4u::this_actor::sleep_for(SLEEP_DELAY);
148         }
149         now = simgrid::s4u::Engine::getClock();
150       }
151
152       if (data != nullptr) {
153         message = static_cast<ChordMessage*>(data);
154         handleMessage(message);
155       }
156       now = simgrid::s4u::Engine::getClock();
157     }
158     if (data != nullptr) {
159       delete static_cast<ChordMessage*>(data);
160     }
161     // leave the ring
162     leave();
163   }
164 };
165
166 #endif