20#include <xrpld/peerfinder/detail/Bootcache.h>
21#include <xrpld/peerfinder/detail/Tuning.h>
22#include <xrpld/peerfinder/detail/iosformat.h>
24#include <xrpl/basics/Log.h>
33 , m_whenUpdate(m_clock.now())
34 , m_needsUpdate(false)
49Bootcache::map_type::size_type
95 this->m_map.insert(value_type(endpoint, valence)));
98 JLOG(this->m_journal.error())
99 << beast::leftw(18) <<
"Bootcache discard " << endpoint;
105 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache loaded " << n
106 << ((n > 1) ?
" addresses" :
" address");
114 auto const result(m_map.insert(
value_type(endpoint, 0)));
117 JLOG(m_journal.trace())
122 return result.second;
128 auto result(m_map.insert(
value_type(endpoint, staticValence)));
130 if (!result.second && (result.first->right.valence() < staticValence))
133 m_map.erase(result.first);
134 result = m_map.insert(
value_type(endpoint, staticValence));
139 JLOG(m_journal.trace())
144 return result.second;
150 auto result(m_map.insert(
value_type(endpoint, 1)));
157 Entry entry(result.first->right);
158 if (entry.valence() < 0)
161 m_map.erase(result.first);
162 result = m_map.insert(
value_type(endpoint, entry));
165 "ripple:PeerFinder::Bootcache::on_success : endpoint inserted");
167 Entry const& entry(result.first->right);
168 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache connect "
169 << endpoint <<
" with " << entry.valence()
170 << ((entry.valence() > 1) ?
" successes"
178 auto result(m_map.insert(
value_type(endpoint, -1)));
185 Entry entry(result.first->right);
186 if (entry.valence() > 0)
189 m_map.erase(result.first);
190 result = m_map.insert(
value_type(endpoint, entry));
193 "ripple:PeerFinder::Bootcache::on_failure : endpoint inserted");
195 Entry const& entry(result.first->right);
196 auto const n(std::abs(entry.valence()));
197 JLOG(m_journal.debug())
198 <<
beast::leftw(18) <<
"Bootcache failed " << endpoint <<
" with " << n
199 << ((n > 1) ?
" attempts" :
" attempt");
204Bootcache::periodicActivity()
215 for (
auto iter = m_map.right.begin(); iter != m_map.right.end(); ++iter)
218 entry[
"endpoint"] = iter->get_left().to_string();
219 entry[
"valence"] =
std::int32_t(iter->get_right().valence());
227 if (size() <= Tuning::bootcacheSize)
231 auto count((size() * Tuning::bootcachePrunePercent) / 100);
232 decltype(count) pruned(0);
237 for (
auto iter(m_map.right.end());
238 count-- > 0 && iter != m_map.right.begin();
243 Entry const& entry(iter->get_right());
244 JLOG(m_journal.trace())
246 <<
" at valence " << entry.valence();
247 iter = m_map.right.erase(iter);
250 JLOG(m_journal.debug()) <<
beast::leftw(18) <<
"Bootcache pruned " << pruned
262 for (
auto const& e : m_map)
266 se.
valence = e.get_right().valence();
271 m_needsUpdate =
false;
272 m_whenUpdate = m_clock.now() + Tuning::bootcacheCooldownTime;
277Bootcache::checkUpdate()
279 if (m_needsUpdate && m_whenUpdate < m_clock.now())
285Bootcache::flagForUpdate()
287 m_needsUpdate =
true;
A version-independent IP address and port combination.
A generic endpoint for log messages.
map_type::size_type size() const
Returns the number of entries in the cache.
const_iterator cbegin() const
const_iterator end() const
const_iterator cend() const
Bootcache(Store &store, clock_type &clock, beast::Journal journal)
map_type::value_type value_type
const_iterator begin() const
IP::Endpoint iterators that traverse in decreasing valence.
bool empty() const
Returns true if the cache is empty.
void load()
Load the persisted data from the Store into the container.
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