1#include <xrpld/peerfinder/detail/Bootcache.h>
2#include <xrpld/peerfinder/detail/Tuning.h>
3#include <xrpld/peerfinder/detail/iosformat.h>
5#include <xrpl/basics/Log.h>
11 : m_store(store), m_clock(clock), m_journal(journal), m_whenUpdate(m_clock.now()), m_needsUpdate(false)
26Bootcache::map_type::size_type
70 auto const result(this->m_map.insert(value_type(endpoint, valence)));
73 JLOG(this->m_journal.error()) << beast::leftw(18) <<
"Bootcache discard " << endpoint;
79 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache loaded " << n << ((n > 1) ?
" addresses" :
" address");
87 auto const result(m_map.insert(
value_type(endpoint, 0)));
90 JLOG(m_journal.trace()) <<
beast::leftw(18) <<
"Bootcache insert " << endpoint;
100 auto result(m_map.insert(
value_type(endpoint, staticValence)));
102 if (!result.second && (result.first->right.valence() < staticValence))
105 m_map.erase(result.first);
106 result = m_map.insert(
value_type(endpoint, staticValence));
111 JLOG(m_journal.trace()) <<
beast::leftw(18) <<
"Bootcache insert " << endpoint;
115 return result.second;
121 auto result(m_map.insert(
value_type(endpoint, 1)));
128 Entry entry(result.first->right);
129 if (entry.valence() < 0)
132 m_map.erase(result.first);
133 result = m_map.insert(
value_type(endpoint, entry));
134 XRPL_ASSERT(result.second,
"ripple:PeerFinder::Bootcache::on_success : endpoint inserted");
136 Entry const& entry(result.first->right);
137 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache connect " << endpoint <<
" with " << entry.valence()
138 << ((entry.valence() > 1) ?
" successes" :
" success");
145 auto result(m_map.insert(
value_type(endpoint, -1)));
152 Entry entry(result.first->right);
153 if (entry.valence() > 0)
156 m_map.erase(result.first);
157 result = m_map.insert(
value_type(endpoint, entry));
158 XRPL_ASSERT(result.second,
"ripple:PeerFinder::Bootcache::on_failure : endpoint inserted");
160 Entry const& entry(result.first->right);
161 auto const n(std::abs(entry.valence()));
162 JLOG(m_journal.debug()) <<
beast::leftw(18) <<
"Bootcache failed " << endpoint <<
" with " << n
163 << ((n > 1) ?
" attempts" :
" attempt");
168Bootcache::periodicActivity()
179 for (
auto iter = m_map.right.begin(); iter != m_map.right.end(); ++iter)
182 entry[
"endpoint"] = iter->get_left().to_string();
183 entry[
"valence"] =
std::int32_t(iter->get_right().valence());
191 if (size() <= Tuning::bootcacheSize)
195 auto count((size() * Tuning::bootcachePrunePercent) / 100);
196 decltype(count) pruned(0);
201 for (
auto iter(m_map.right.end()); count-- > 0 && iter != m_map.right.begin(); ++pruned)
205 Entry const& entry(iter->get_right());
206 JLOG(m_journal.trace()) <<
beast::leftw(18) <<
"Bootcache pruned" << endpoint <<
" at valence "
208 iter = m_map.right.erase(iter);
211 JLOG(m_journal.debug()) <<
beast::leftw(18) <<
"Bootcache pruned " << pruned <<
" entries total";
222 for (
auto const& e : m_map)
226 se.
valence = e.get_right().valence();
231 m_needsUpdate =
false;
232 m_whenUpdate = m_clock.now() + Tuning::bootcacheCooldownTime;
237Bootcache::checkUpdate()
239 if (m_needsUpdate && m_whenUpdate < m_clock.now())
245Bootcache::flagForUpdate()
247 m_needsUpdate =
true;
A version-independent IP address and port combination.
A generic endpoint for log messages.
const_iterator cbegin() const
const_iterator cend() const
map_type::value_type value_type
const_iterator begin() const
IP::Endpoint iterators that traverse in decreasing valence.
void load()
Load the persisted data from the Store into the container.
const_iterator end() const
bool empty() const
Returns true if the cache is empty.
map_type::size_type size() const
Returns the number of entries in the cache.
Bootcache(Store &store, clock_type &clock, beast::Journal journal)
Abstract persistence for PeerFinder data.
virtual std::size_t load(load_callback const &cb)=0
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Left justifies a field at the specified width.
beast::IP::Endpoint endpoint