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
27#include <xrpl/basics/random.h>
28
29namespace ripple {
30namespace PeerFinder {
31
33class Counts
34{
35public:
37 : m_attempts(0)
38 , m_active(0)
39 , m_in_max(0)
40 , m_in_active(0)
41 , m_out_max(0)
42 , m_out_active(0)
43 , m_fixed(0)
45 , m_reserved(0)
46
47 , m_acceptCount(0)
49 {
50 }
51
52 //--------------------------------------------------------------------------
53
55 void
56 add(Slot const& s)
57 {
58 adjust(s, 1);
59 }
60
62 void
63 remove(Slot const& s)
64 {
65 adjust(s, -1);
66 }
67
69 bool
70 can_activate(Slot const& s) const
71 {
72 // Must be handshaked and in the right state
73 XRPL_ASSERT(
74 s.state() == Slot::connected || s.state() == Slot::accept,
75 "ripple::PeerFinder::Counts::can_activate : valid input state");
76
77 if (s.fixed() || s.reserved())
78 return true;
79
80 if (s.inbound())
81 return m_in_active < m_in_max;
82
83 return m_out_active < m_out_max;
84 }
85
89 {
91 return 0;
93 }
94
97 attempts() const
98 {
99 return m_attempts;
100 }
101
103 int
104 out_max() const
105 {
106 return m_out_max;
107 }
108
112 int
114 {
115 return m_out_active;
116 }
117
120 fixed() const
121 {
122 return m_fixed;
123 }
124
128 {
129 return m_fixed_active;
130 }
131
132 //--------------------------------------------------------------------------
133
135 void
136 onConfig(Config const& config)
137 {
138 m_out_max = config.outPeers;
139 if (config.wantIncoming)
140 m_in_max = config.inPeers;
141 }
142
144 int
146 {
147 return m_acceptCount;
148 }
149
151 int
153 {
154 return m_attempts;
155 }
156
158 int
160 {
161 return m_closingCount;
162 }
163
165 int
166 in_max() const
167 {
168 return m_in_max;
169 }
170
172 int
174 {
175 return m_in_active;
176 }
177
179 int
181 {
182 return m_in_active + m_out_active;
183 }
184
188 int
190 {
191 if (m_in_active < m_in_max)
192 return m_in_max - m_in_active;
193 return 0;
194 }
195
199 int
201 {
203 return m_out_max - m_out_active;
204 return 0;
205 }
206
207 //--------------------------------------------------------------------------
208
211 bool
213 {
214 // We will consider ourselves connected if we have reached
215 // the number of outgoing connections desired, or if connect
216 // automatically is false.
217 //
218 // Fixed peers do not count towards the active outgoing total.
219
220 if (m_out_max > 0)
221 return false;
222
223 return true;
224 }
225
227 void
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
243 {
245 ss << m_out_active << "/" << m_out_max << " out, " << m_in_active << "/"
246 << m_in_max << " in, " << connectCount() << " connecting, "
247 << closingCount() << " closing";
248 return ss.str();
249 }
250
251 //--------------------------------------------------------------------------
252private:
253 // Adjusts counts based on the specified slot, in the direction indicated.
254 void
255 adjust(Slot const& s, int const n)
256 {
257 if (s.fixed())
258 m_fixed += n;
259
260 if (s.reserved())
261 m_reserved += n;
262
263 switch (s.state())
264 {
265 case Slot::accept:
266 XRPL_ASSERT(
267 s.inbound(),
268 "ripple::PeerFinder::Counts::adjust : input is inbound");
269 m_acceptCount += n;
270 break;
271
272 case Slot::connect:
273 case Slot::connected:
274 XRPL_ASSERT(
275 !s.inbound(),
276 "ripple::PeerFinder::Counts::adjust : input is not "
277 "inbound");
278 m_attempts += n;
279 break;
280
281 case Slot::active:
282 if (s.fixed())
283 m_fixed_active += n;
284 if (!s.fixed() && !s.reserved())
285 {
286 if (s.inbound())
287 m_in_active += n;
288 else
289 m_out_active += n;
290 }
291 m_active += n;
292 break;
293
294 case Slot::closing:
295 m_closingCount += n;
296 break;
297
298 default:
299 UNREACHABLE(
300 "ripple::PeerFinder::Counts::adjust : invalid input state");
301 break;
302 };
303 }
304
305private:
308
311
314
317
320
323
326
329
332
333 // Number of inbound connections that are
334 // not active or gracefully closing.
336
337 // Number of connections that are gracefully closing.
339};
340
341} // namespace PeerFinder
342} // namespace ripple
343
344#endif
Manages the count of available connections for the various slots.
Definition Counts.h:34
std::size_t m_out_active
Active outbound slots.
Definition Counts.h:322
std::size_t fixed_active() const
Returns the number of active fixed connections.
Definition Counts.h:127
std::string state_string() const
Records the state for diagnostics.
Definition Counts.h:242
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition Counts.h:200
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition Counts.h:159
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition Counts.h:145
std::size_t fixed() const
Returns the number of fixed connections.
Definition Counts.h:120
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition Counts.h:212
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:113
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:173
std::size_t m_reserved
Reserved connections.
Definition Counts.h:331
std::size_t attempts_needed() const
Returns the number of attempts needed to bring us to the max.
Definition Counts.h:88
int m_attempts
Outbound connection attempts.
Definition Counts.h:307
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
Definition Counts.h:228
std::size_t m_in_active
Number of inbound slots assigned to active peers.
Definition Counts.h:316
int out_max() const
Returns the total number of outbound slots.
Definition Counts.h:104
std::size_t m_fixed_active
Active fixed connections.
Definition Counts.h:328
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition Counts.h:56
int inboundSlotsFree() const
Returns the number of unused inbound slots.
Definition Counts.h:189
int in_max() const
Returns the total number of inbound slots.
Definition Counts.h:166
int connectCount() const
Returns the number of connection attempts currently active.
Definition Counts.h:152
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition Counts.h:180
std::size_t m_fixed
Fixed connections.
Definition Counts.h:325
void adjust(Slot const &s, int const n)
Definition Counts.h:255
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition Counts.h:63
std::size_t m_active
Active connections, including fixed and reserved.
Definition Counts.h:310
void onConfig(Config const &config)
Called when the config is set or changed.
Definition Counts.h:136
std::size_t m_in_max
Total number of inbound slots.
Definition Counts.h:313
std::size_t m_out_max
Maximum desired outbound slots.
Definition Counts.h:319
bool can_activate(Slot const &s) const
Returns true if the slot can become active.
Definition Counts.h:70
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition Counts.h:97
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:25
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.