rippled
Loading...
Searching...
No Matches
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 <xrpld/peerfinder/PeerfinderManager.h>
24#include <xrpld/peerfinder/Slot.h>
25#include <xrpld/peerfinder/detail/Tuning.h>
26#include <xrpl/basics/random.h>
27
28#include <cmath>
29
30namespace ripple {
31namespace PeerFinder {
32
34class Counts
35{
36public:
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)
46 , m_reserved(0)
47
48 , m_acceptCount(0)
50 {
51 }
52
53 //--------------------------------------------------------------------------
54
56 void
57 add(Slot const& s)
58 {
59 adjust(s, 1);
60 }
61
63 void
64 remove(Slot const& s)
65 {
66 adjust(s, -1);
67 }
68
70 bool
71 can_activate(Slot const& s) const
72 {
73 // Must be handshaked and in the right state
74 XRPL_ASSERT(
75 s.state() == Slot::connected || s.state() == Slot::accept,
76 "ripple::PeerFinder::Counts::can_activate : valid input state");
77
78 if (s.fixed() || s.reserved())
79 return true;
80
81 if (s.inbound())
82 return m_in_active < m_in_max;
83
84 return m_out_active < m_out_max;
85 }
86
90 {
92 return 0;
94 }
95
98 attempts() const
99 {
100 return m_attempts;
101 }
102
104 int
105 out_max() const
106 {
107 return m_out_max;
108 }
109
113 int
115 {
116 return m_out_active;
117 }
118
121 fixed() const
122 {
123 return m_fixed;
124 }
125
129 {
130 return m_fixed_active;
131 }
132
133 //--------------------------------------------------------------------------
134
136 void
137 onConfig(Config const& config)
138 {
139 m_out_max = config.outPeers;
140 if (config.wantIncoming)
141 m_in_max = config.inPeers;
142 }
143
145 int
147 {
148 return m_acceptCount;
149 }
150
152 int
154 {
155 return m_attempts;
156 }
157
159 int
161 {
162 return m_closingCount;
163 }
164
166 int
168 {
169 return m_in_max;
170 }
171
173 int
175 {
176 return m_in_active;
177 }
178
180 int
182 {
183 return m_in_active + m_out_active;
184 }
185
189 int
191 {
192 if (m_in_active < m_in_max)
193 return m_in_max - m_in_active;
194 return 0;
195 }
196
200 int
202 {
204 return m_out_max - m_out_active;
205 return 0;
206 }
207
208 //--------------------------------------------------------------------------
209
212 bool
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
228 void
230 {
231 map["accept"] = acceptCount();
232 map["connect"] = connectCount();
233 map["close"] = closingCount();
234 map["in"] << m_in_active << "/" << m_in_max;
235 map["out"] << m_out_active << "/" << m_out_max;
236 map["fixed"] = m_fixed_active;
237 map["reserved"] = m_reserved;
238 map["total"] = m_active;
239 }
240
244 {
246 ss << m_out_active << "/" << m_out_max << " out, " << m_in_active << "/"
247 << m_in_max << " in, " << connectCount() << " connecting, "
248 << closingCount() << " closing";
249 return ss.str();
250 }
251
252 //--------------------------------------------------------------------------
253private:
254 // Adjusts counts based on the specified slot, in the direction indicated.
255 void
256 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 XRPL_ASSERT(
268 s.inbound(),
269 "ripple::PeerFinder::Counts::adjust : input is inbound");
270 m_acceptCount += n;
271 break;
272
273 case Slot::connect:
274 case Slot::connected:
275 XRPL_ASSERT(
276 !s.inbound(),
277 "ripple::PeerFinder::Counts::adjust : input is not "
278 "inbound");
279 m_attempts += n;
280 break;
281
282 case Slot::active:
283 if (s.fixed())
284 m_fixed_active += n;
285 if (!s.fixed() && !s.reserved())
286 {
287 if (s.inbound())
288 m_in_active += n;
289 else
290 m_out_active += n;
291 }
292 m_active += n;
293 break;
294
295 case Slot::closing:
296 m_closingCount += n;
297 break;
298
299 default:
300 UNREACHABLE(
301 "ripple::PeerFinder::Counts::adjust : invalid input state");
302 break;
303 };
304 }
305
306private:
309
312
315
318
321
324
327
330
333
334 // Number of inbound connections that are
335 // not active or gracefully closing.
337
338 // Number of connections that are gracefully closing.
340};
341
342} // namespace PeerFinder
343} // namespace ripple
344
345#endif
Manages the count of available connections for the various slots.
Definition: Counts.h:35
std::size_t m_out_active
Active outbound slots.
Definition: Counts.h:323
std::size_t fixed_active() const
Returns the number of active fixed connections.
Definition: Counts.h:128
std::string state_string() const
Records the state for diagnostics.
Definition: Counts.h:243
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition: Counts.h:201
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition: Counts.h:160
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition: Counts.h:146
std::size_t fixed() const
Returns the number of fixed connections.
Definition: Counts.h:121
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition: Counts.h:213
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition: Counts.h:114
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition: Counts.h:174
std::size_t m_reserved
Reserved connections.
Definition: Counts.h:332
std::size_t attempts_needed() const
Returns the number of attempts needed to bring us to the max.
Definition: Counts.h:89
int inboundSlots() const
Returns the total number of inbound slots.
Definition: Counts.h:167
int m_attempts
Outbound connection attempts.
Definition: Counts.h:308
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
Definition: Counts.h:229
std::size_t m_in_active
Number of inbound slots assigned to active peers.
Definition: Counts.h:317
int out_max() const
Returns the total number of outbound slots.
Definition: Counts.h:105
std::size_t m_fixed_active
Active fixed connections.
Definition: Counts.h:329
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition: Counts.h:57
int inboundSlotsFree() const
Returns the number of unused inbound slots.
Definition: Counts.h:190
int connectCount() const
Returns the number of connection attempts currently active.
Definition: Counts.h:153
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition: Counts.h:181
std::size_t m_fixed
Fixed connections.
Definition: Counts.h:326
void adjust(Slot const &s, int const n)
Definition: Counts.h:256
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition: Counts.h:64
std::size_t m_active
Active connections, including fixed and reserved.
Definition: Counts.h:311
void onConfig(Config const &config)
Called when the config is set or changed.
Definition: Counts.h:137
std::size_t m_in_max
Total number of inbound slots.
Definition: Counts.h:314
std::size_t m_out_max
Maximum desired outbound slots.
Definition: Counts.h:320
bool can_activate(Slot const &s) const
Returns true if the slot can become active.
Definition: Counts.h:71
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition: Counts.h:98
Properties and state associated with a peer to peer overlay connection.
virtual bool inbound() const =0
Returns true if this is an inbound connection.
virtual bool fixed() const =0
Returns true if this is a fixed connection.
virtual State state() const =0
Returns the state of the connection.
virtual bool reserved() const =0
Returns true if this is a reserved connection.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T str(T... args)
PeerFinder configuration settings.
std::size_t inPeers
The number of automatic inbound connections to maintain.
bool wantIncoming
true if we want to accept incoming connections.
std::size_t outPeers
The number of automatic outbound connections to maintain.