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>
13 : m_store(store), m_clock(clock), m_journal(journal), m_whenUpdate(m_clock.now())
29Bootcache::map_type::size_type
73 auto const result(this->m_map.insert(value_type(endpoint, valence)));
76 JLOG(this->m_journal.error()) << beast::leftw(18) <<
"Bootcache discard " << endpoint;
82 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache loaded " << n
83 << ((n > 1) ?
" addresses" :
" address");
91 auto const result(m_map.insert(
value_type(endpoint, 0)));
94 JLOG(m_journal.trace()) <<
beast::leftw(18) <<
"Bootcache insert " << endpoint;
104 auto result(m_map.insert(
value_type(endpoint, staticValence)));
106 if (!result.second && (result.first->right.valence() < staticValence))
109 m_map.erase(result.first);
110 result = m_map.insert(
value_type(endpoint, staticValence));
115 JLOG(m_journal.trace()) <<
beast::leftw(18) <<
"Bootcache insert " << endpoint;
119 return result.second;
125 auto result(m_map.insert(
value_type(endpoint, 1)));
132 Entry entry(result.first->right);
133 entry.valence() =
std::max(entry.valence(), 0);
135 m_map.erase(result.first);
136 result = m_map.insert(
value_type(endpoint, entry));
137 XRPL_ASSERT(result.second,
"ripple:PeerFinder::Bootcache::on_success : endpoint inserted");
139 Entry const& entry(result.first->right);
140 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache connect " << endpoint <<
" with "
142 << ((entry.valence() > 1) ?
" successes" :
" success");
149 auto result(m_map.insert(
value_type(endpoint, -1)));
156 Entry entry(result.first->right);
157 entry.valence() =
std::min(entry.valence(), 0);
159 m_map.erase(result.first);
160 result = m_map.insert(
value_type(endpoint, entry));
161 XRPL_ASSERT(result.second,
"ripple:PeerFinder::Bootcache::on_failure : endpoint inserted");
163 Entry const& entry(result.first->right);
164 auto const n(std::abs(entry.valence()));
165 JLOG(m_journal.debug()) <<
beast::leftw(18) <<
"Bootcache failed " << endpoint <<
" with " << n
166 << ((n > 1) ?
" attempts" :
" attempt");
171Bootcache::periodicActivity()
182 for (
auto iter = m_map.right.begin(); iter != m_map.right.end(); ++iter)
185 entry[
"endpoint"] = iter->get_left().to_string();
186 entry[
"valence"] =
std::int32_t(iter->get_right().valence());
194 if (size() <= Tuning::bootcacheSize)
198 auto count((size() * Tuning::bootcachePrunePercent) / 100);
199 decltype(count) pruned(0);
204 for (
auto iter(m_map.right.end()); count-- > 0 && iter != m_map.right.begin(); ++pruned)
208 Entry const& entry(iter->get_right());
209 JLOG(m_journal.trace()) <<
beast::leftw(18) <<
"Bootcache pruned" << endpoint
210 <<
" at valence " << entry.valence();
211 iter = m_map.right.erase(iter);
214 JLOG(m_journal.debug()) <<
beast::leftw(18) <<
"Bootcache pruned " << pruned
226 for (
auto const& e : m_map)
230 se.
valence = e.get_right().valence();
235 m_needsUpdate =
false;
236 m_whenUpdate = m_clock.now() + Tuning::bootcacheCooldownTime;
241Bootcache::checkUpdate()
243 if (m_needsUpdate && m_whenUpdate < m_clock.now())
249Bootcache::flagForUpdate()
251 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