20#include <xrpld/peerfinder/detail/Bootcache.h>
21#include <xrpld/peerfinder/detail/Tuning.h>
22#include <xrpld/peerfinder/detail/iosformat.h>
23#include <xrpl/basics/Log.h>
32 , m_whenUpdate(m_clock.now())
33 , m_needsUpdate(false)
48Bootcache::map_type::size_type
94 this->m_map.insert(value_type(endpoint, valence)));
97 JLOG(this->m_journal.error())
98 << beast::leftw(18) <<
"Bootcache discard " << endpoint;
104 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache loaded " << n
105 << ((n > 1) ?
" addresses" :
" address");
113 auto const result(m_map.insert(
value_type(endpoint, 0)));
116 JLOG(m_journal.trace())
121 return result.second;
127 auto result(m_map.insert(
value_type(endpoint, staticValence)));
129 if (!result.second && (result.first->right.valence() < staticValence))
132 m_map.erase(result.first);
133 result = m_map.insert(
value_type(endpoint, staticValence));
138 JLOG(m_journal.trace())
143 return result.second;
149 auto result(m_map.insert(
value_type(endpoint, 1)));
156 Entry entry(result.first->right);
160 m_map.erase(result.first);
161 result = m_map.insert(
value_type(endpoint, entry));
164 "ripple:PeerFinder::Bootcache::on_success : endpoint inserted");
166 Entry const& entry(result.first->right);
167 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache connect "
168 << endpoint <<
" with " << entry.
valence()
169 << ((entry.
valence() > 1) ?
" successes"
177 auto result(m_map.insert(
value_type(endpoint, -1)));
184 Entry entry(result.first->right);
188 m_map.erase(result.first);
189 result = m_map.insert(
value_type(endpoint, entry));
192 "ripple:PeerFinder::Bootcache::on_failure : endpoint inserted");
194 Entry const& entry(result.first->right);
195 auto const n(std::abs(entry.
valence()));
196 JLOG(m_journal.debug())
197 <<
beast::leftw(18) <<
"Bootcache failed " << endpoint <<
" with " << n
198 << ((n > 1) ?
" attempts" :
" attempt");
203Bootcache::periodicActivity()
214 for (
auto iter = m_map.right.begin(); iter != m_map.right.end(); ++iter)
217 entry[
"endpoint"] = iter->get_left().to_string();
218 entry[
"valence"] =
std::int32_t(iter->get_right().valence());
226 if (size() <= Tuning::bootcacheSize)
230 auto count((size() * Tuning::bootcachePrunePercent) / 100);
231 decltype(count) pruned(0);
236 for (
auto iter(m_map.right.end());
237 count-- > 0 && iter != m_map.right.begin();
242 Entry const& entry(iter->get_right());
243 JLOG(m_journal.trace())
245 <<
" at valence " << entry.
valence();
246 iter = m_map.right.erase(iter);
249 JLOG(m_journal.debug()) <<
beast::leftw(18) <<
"Bootcache pruned " << pruned
261 for (
auto const& e : m_map)
265 se.
valence = e.get_right().valence();
270 m_needsUpdate =
false;
271 m_whenUpdate = m_clock.now() + Tuning::bootcacheCooldownTime;
276Bootcache::checkUpdate()
278 if (m_needsUpdate && m_whenUpdate < m_clock.now())
284Bootcache::flagForUpdate()
286 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