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