Logo AND Algorithmique Numérique Distribuée

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