rippled
Loading...
Searching...
No Matches
Cluster.cpp
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#include <xrpld/core/Config.h>
21#include <xrpld/core/TimeKeeper.h>
22#include <xrpld/overlay/Cluster.h>
23#include <xrpld/overlay/ClusterNode.h>
24
25#include <xrpl/basics/Log.h>
26#include <xrpl/basics/StringUtilities.h>
27#include <xrpl/protocol/tokens.h>
28
29#include <boost/regex.hpp>
30
31namespace ripple {
32
36
38Cluster::member(PublicKey const& identity) const
39{
41
42 auto iter = nodes_.find(identity);
43 if (iter == nodes_.end())
44 return std::nullopt;
45 return iter->name();
46}
47
50{
52
53 return nodes_.size();
54}
55
56bool
58 PublicKey const& identity,
59 std::string name,
60 std::uint32_t loadFee,
61 NetClock::time_point reportTime)
62{
64
65 auto iter = nodes_.find(identity);
66
67 if (iter != nodes_.end())
68 {
69 if (reportTime <= iter->getReportTime())
70 return false;
71
72 if (name.empty())
73 name = iter->name();
74
75 iter = nodes_.erase(iter);
76 }
77
78 nodes_.emplace_hint(iter, identity, name, loadFee, reportTime);
79 return true;
80}
81
82void
84{
86 for (auto const& ni : nodes_)
87 func(ni);
88}
89
90bool
92{
93 static boost::regex const re(
94 "[[:space:]]*" // skip leading whitespace
95 "([[:alnum:]]+)" // node identity
96 "(?:" // begin optional comment block
97 "[[:space:]]+" // (skip all leading whitespace)
98 "(?:" // begin optional comment
99 "(.*[^[:space:]]+)" // the comment
100 "[[:space:]]*" // (skip all trailing whitespace)
101 ")?" // end optional comment
102 ")?" // end optional comment block
103 );
104
105 for (auto const& n : nodes.values())
106 {
107 boost::smatch match;
108
109 if (!boost::regex_match(n, match, re))
110 {
111 JLOG(j_.error()) << "Malformed entry: '" << n << "'";
112 return false;
113 }
114
115 auto const id =
116 parseBase58<PublicKey>(TokenType::NodePublic, match[1].str());
117
118 if (!id)
119 {
120 JLOG(j_.error()) << "Invalid node identity: " << match[1];
121 return false;
122 }
123
124 if (member(*id))
125 {
126 JLOG(j_.warn()) << "Duplicate node identity: " << match[1];
127 continue;
128 }
129
130 update(*id, trim_whitespace(match[2]));
131 }
132
133 return true;
134}
135
136} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:60
Stream error() const
Definition Journal.h:346
Stream warn() const
Definition Journal.h:340
std::set< ClusterNode, Comparator > nodes_
Definition Cluster.h:64
void for_each(std::function< void(ClusterNode const &)> func) const
Invokes the callback once for every cluster node.
Definition Cluster.cpp:83
std::size_t size() const
The number of nodes in the cluster list.
Definition Cluster.cpp:49
Cluster(beast::Journal j)
Definition Cluster.cpp:33
bool load(Section const &nodes)
Load the list of cluster nodes.
Definition Cluster.cpp:91
beast::Journal j_
Definition Cluster.h:66
std::mutex mutex_
Definition Cluster.h:65
bool update(PublicKey const &identity, std::string name, std::uint32_t loadFee=0, NetClock::time_point reportTime=NetClock::time_point{})
Store information about the state of a cluster node.
Definition Cluster.cpp:57
std::optional< std::string > member(PublicKey const &node) const
Determines whether a node belongs in the cluster.
Definition Cluster.cpp:38
A public key.
Definition PublicKey.h:61
Holds a collection of configuration values.
Definition BasicConfig.h:45
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition BasicConfig.h:79
T empty(T... args)
T erase(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string trim_whitespace(std::string str)