rippled
Counts.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_PEERFINDER_COUNTS_H_INCLUDED
21 #define RIPPLE_PEERFINDER_COUNTS_H_INCLUDED
22 
23 #include <ripple/basics/random.h>
24 #include <ripple/peerfinder/PeerfinderManager.h>
25 #include <ripple/peerfinder/Slot.h>
26 #include <ripple/peerfinder/impl/Tuning.h>
27 
28 #include <cmath>
29 
30 namespace ripple {
31 namespace PeerFinder {
32 
34 class Counts
35 {
36 public:
37  Counts ()
38  : m_attempts (0)
39  , m_active (0)
40  , m_in_max (0)
41  , m_in_active (0)
42  , m_out_max (0)
43  , m_out_active (0)
44  , m_fixed (0)
45  , m_fixed_active (0)
46  , m_reserved (0)
47 
48  , m_acceptCount (0)
49  , m_closingCount (0)
50  {
52  std::generate_canonical <double, 10> (
53  default_prng());
54  }
55 
56  //--------------------------------------------------------------------------
57 
59  void add (Slot const& s)
60  {
61  adjust (s, 1);
62  }
63 
65  void remove (Slot const& s)
66  {
67  adjust (s, -1);
68  }
69 
71  bool can_activate (Slot const& s) const
72  {
73  // Must be handshaked and in the right state
74  assert (s.state() == Slot::connected || s.state() == Slot::accept);
75 
76  if (s.fixed () || s.reserved ())
77  return true;
78 
79  if (s.inbound ())
80  return m_in_active < m_in_max;
81 
82  return m_out_active < m_out_max;
83  }
84 
87  {
89  return 0;
91  }
92 
95  {
96  return m_attempts;
97  }
98 
100  int out_max () const
101  {
102  return m_out_max;
103  }
104 
108  int out_active () const
109  {
110  return m_out_active;
111  }
112 
115  {
116  return m_fixed;
117  }
118 
121  {
122  return m_fixed_active;
123  }
124 
125  //--------------------------------------------------------------------------
126 
128  void onConfig (Config const& config)
129  {
130  // Calculate the number of outbound peers we want. If we dont want or can't
131  // accept incoming, this will simply be equal to maxPeers. Otherwise
132  // we calculate a fractional amount based on percentages and pseudo-randomly
133  // round up or down.
134  //
135  if (config.wantIncoming)
136  {
137  // Round outPeers upwards using a Bernoulli distribution
138  m_out_max = std::floor (config.outPeers);
139  if (m_roundingThreshold < (config.outPeers - m_out_max))
140  ++m_out_max;
141  }
142  else
143  {
144  m_out_max = config.maxPeers;
145  }
146 
147  // Calculate the largest number of inbound connections we could take.
148  if (config.maxPeers >= m_out_max)
149  m_in_max = config.maxPeers - m_out_max;
150  else
151  m_in_max = 0;
152  }
153 
155  int acceptCount() const
156  {
157  return m_acceptCount;
158  }
159 
161  int connectCount() const
162  {
163  return m_attempts;
164  }
165 
167  int closingCount () const
168  {
169  return m_closingCount;
170  }
171 
173  int inboundSlots () const
174  {
175  return m_in_max;
176  }
177 
179  int inboundActive () const
180  {
181  return m_in_active;
182  }
183 
185  int totalActive () const
186  {
187  return m_in_active + m_out_active;
188  }
189 
193  int inboundSlotsFree () const
194  {
195  if (m_in_active < m_in_max)
196  return m_in_max - m_in_active;
197  return 0;
198  }
199 
203  int outboundSlotsFree () const
204  {
205  if (m_out_active < m_out_max)
206  return m_out_max - m_out_active;
207  return 0;
208  }
209 
210  //--------------------------------------------------------------------------
211 
213  bool isConnectedToNetwork () const
214  {
215  // We will consider ourselves connected if we have reached
216  // the number of outgoing connections desired, or if connect
217  // automatically is false.
218  //
219  // Fixed peers do not count towards the active outgoing total.
220 
221  if (m_out_max > 0)
222  return false;
223 
224  return true;
225  }
226 
229  {
230  map ["accept"] = acceptCount ();
231  map ["connect"] = connectCount ();
232  map ["close"] = closingCount ();
233  map ["in"] << m_in_active << "/" << m_in_max;
234  map ["out"] << m_out_active << "/" << m_out_max;
235  map ["fixed"] = m_fixed_active;
236  map ["reserved"] = m_reserved;
237  map ["total"] = m_active;
238  }
239 
242  {
244  ss <<
245  m_out_active << "/" << m_out_max << " out, " <<
246  m_in_active << "/" << m_in_max << " in, " <<
247  connectCount() << " connecting, " <<
248  closingCount() << " closing"
249  ;
250  return ss.str();
251  }
252 
253  //--------------------------------------------------------------------------
254 private:
255  // Adjusts counts based on the specified slot, in the direction indicated.
256  void adjust (Slot const& s, int const n)
257  {
258  if (s.fixed ())
259  m_fixed += n;
260 
261  if (s.reserved ())
262  m_reserved += n;
263 
264  switch (s.state ())
265  {
266  case Slot::accept:
267  assert (s.inbound ());
268  m_acceptCount += n;
269  break;
270 
271  case Slot::connect:
272  case Slot::connected:
273  assert (! s.inbound ());
274  m_attempts += n;
275  break;
276 
277  case Slot::active:
278  if (s.fixed ())
279  m_fixed_active += n;
280  if (! s.fixed () && ! s.reserved ())
281  {
282  if (s.inbound ())
283  m_in_active += n;
284  else
285  m_out_active += n;
286  }
287  m_active += n;
288  break;
289 
290  case Slot::closing:
291  m_closingCount += n;
292  break;
293 
294  default:
295  assert (false);
296  break;
297  };
298  }
299 
300 private:
303 
306 
309 
312 
315 
318 
321 
324 
327 
328 
329 
330  // Number of inbound connections that are
331  // not active or gracefully closing.
333 
334  // Number of connections that are gracefully closing.
336 
343 };
344 
345 }
346 }
347 
348 #endif
ripple::PeerFinder::Counts::inboundActive
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition: Counts.h:179
ripple::PeerFinder::Counts::state_string
std::string state_string() const
Records the state for diagnostics.
Definition: Counts.h:241
ripple::PeerFinder::Slot::state
virtual State state() const =0
Returns the state of the connection.
ripple::PeerFinder::Counts::m_acceptCount
int m_acceptCount
Definition: Counts.h:332
std::floor
T floor(T... args)
std::string
STL class.
ripple::PeerFinder::Slot::accept
@ accept
Definition: Slot.h:39
ripple::PeerFinder::Counts::m_roundingThreshold
double m_roundingThreshold
Fractional threshold below which we round down.
Definition: Counts.h:342
beast::PropertyStream::Map
Definition: PropertyStream.h:185
ripple::PeerFinder::Counts::fixed
std::size_t fixed() const
Returns the number of fixed connections.
Definition: Counts.h:114
ripple::PeerFinder::Slot::fixed
virtual bool fixed() const =0
Returns true if this is a fixed connection.
ripple::PeerFinder::Counts::m_attempts
int m_attempts
Outbound connection attempts.
Definition: Counts.h:302
ripple::PeerFinder::Slot::active
@ active
Definition: Slot.h:42
ripple::PeerFinder::Slot::reserved
virtual bool reserved() const =0
Returns true if this is a reserved connection.
ripple::PeerFinder::Counts::isConnectedToNetwork
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition: Counts.h:213
std::stringstream
STL class.
ripple::PeerFinder::Counts::connectCount
int connectCount() const
Returns the number of connection attempts currently active.
Definition: Counts.h:161
cmath
ripple::PeerFinder::Counts::can_activate
bool can_activate(Slot const &s) const
Returns true if the slot can become active.
Definition: Counts.h:71
ripple::PeerFinder::Counts::m_active
std::size_t m_active
Active connections, including fixed and reserved.
Definition: Counts.h:305
ripple::PeerFinder::Counts::m_in_max
std::size_t m_in_max
Total number of inbound slots.
Definition: Counts.h:308
ripple::PeerFinder::Counts::inboundSlots
int inboundSlots() const
Returns the total number of inbound slots.
Definition: Counts.h:173
ripple::PeerFinder::Counts::acceptCount
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition: Counts.h:155
ripple::PeerFinder::Counts::out_max
int out_max() const
Returns the total number of outbound slots.
Definition: Counts.h:100
ripple::PeerFinder::Counts
Manages the count of available connections for the various slots.
Definition: Counts.h:34
ripple::PeerFinder::Config::wantIncoming
bool wantIncoming
true if we want to accept incoming connections.
Definition: PeerfinderManager.h:64
ripple::PeerFinder::Tuning::maxConnectAttempts
@ maxConnectAttempts
Definition: peerfinder/impl/Tuning.h:44
ripple::PeerFinder::Counts::closingCount
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition: Counts.h:167
ripple::PeerFinder::Counts::outboundSlotsFree
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition: Counts.h:203
ripple::PeerFinder::Counts::out_active
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition: Counts.h:108
ripple::PeerFinder::Slot::connected
@ connected
Definition: Slot.h:41
ripple::default_prng
beast::xor_shift_engine & default_prng()
Return the default random engine.
Definition: ripple/basics/random.h:68
ripple::PeerFinder::Config::outPeers
double outPeers
The number of automatic outbound connections to maintain.
Definition: PeerfinderManager.h:58
ripple::PeerFinder::Config::maxPeers
int maxPeers
The largest number of public peer slots to allow.
Definition: PeerfinderManager.h:46
ripple::PeerFinder::Counts::onConfig
void onConfig(Config const &config)
Called when the config is set or changed.
Definition: Counts.h:128
ripple::PeerFinder::Counts::m_in_active
std::size_t m_in_active
Number of inbound slots assigned to active peers.
Definition: Counts.h:311
ripple::PeerFinder::Slot::closing
@ closing
Definition: Slot.h:43
ripple::PeerFinder::Counts::m_closingCount
int m_closingCount
Definition: Counts.h:335
ripple::PeerFinder::Counts::inboundSlotsFree
int inboundSlotsFree() const
Returns the number of unused inbound slots.
Definition: Counts.h:193
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Counts::m_out_max
std::size_t m_out_max
Maximum desired outbound slots.
Definition: Counts.h:314
ripple::PeerFinder::Counts::fixed_active
std::size_t fixed_active() const
Returns the number of active fixed connections.
Definition: Counts.h:120
ripple::PeerFinder::Counts::Counts
Counts()
Definition: Counts.h:37
ripple::PeerFinder::Counts::m_out_active
std::size_t m_out_active
Active outbound slots.
Definition: Counts.h:317
ripple::PeerFinder::Counts::attempts
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition: Counts.h:94
ripple::PeerFinder::Counts::adjust
void adjust(Slot const &s, int const n)
Definition: Counts.h:256
ripple::PeerFinder::Counts::add
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition: Counts.h:59
ripple::PeerFinder::Counts::onWrite
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
Definition: Counts.h:228
std::stringstream::str
T str(T... args)
std::size_t
ripple::PeerFinder::Slot::inbound
virtual bool inbound() const =0
Returns true if this is an inbound connection.
ripple::PeerFinder::Config
PeerFinder configuration settings.
Definition: PeerfinderManager.h:40
ripple::PeerFinder::Counts::attempts_needed
std::size_t attempts_needed() const
Returns the number of attempts needed to bring us to the max.
Definition: Counts.h:86
ripple::PeerFinder::Slot::connect
@ connect
Definition: Slot.h:40
ripple::PeerFinder::Counts::remove
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition: Counts.h:65
ripple::PeerFinder::Counts::m_fixed
std::size_t m_fixed
Fixed connections.
Definition: Counts.h:320
ripple::PeerFinder::Counts::m_reserved
std::size_t m_reserved
Reserved connections.
Definition: Counts.h:326
ripple::PeerFinder::Counts::totalActive
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition: Counts.h:185
ripple::PeerFinder::Slot
Properties and state associated with a peer to peer overlay connection.
Definition: Slot.h:32
ripple::PeerFinder::Counts::m_fixed_active
std::size_t m_fixed_active
Active fixed connections.
Definition: Counts.h:323