Files
rippled/src/xrpld/app/misc/HashRouter.cpp
Ed Hennis dd5e6559dd Reduce duplicate peer traffic for ledger data (#5126)
- Drop duplicate outgoing TMGetLedger messages per peer
  - Allow a retry after 30s in case of peer or network congestion.
  - Addresses RIPD-1870
  - (Changes levelization. That is not desirable, and will need to be fixed.)
- Drop duplicate incoming TMGetLedger messages per peer
  - Allow a retry after 15s in case of peer or network congestion.
  - The requestCookie is ignored when computing the hash, thus increasing
    the chances of detecting duplicate messages.
  - With duplicate messages, keep track of the different requestCookies
    (or lack of cookie). When work is finally done for a given request,
    send the response to all the peers that are waiting on the request,
    sending one message per peer, including all the cookies and
    a "directResponse" flag indicating the data is intended for the
    sender, too.
  - Addresses RIPD-1871
- Drop duplicate incoming TMLedgerData messages
  - Addresses RIPD-1869
- Improve logging related to ledger acquisition
- Class "CanProcess" to keep track of processing of distinct items

---------

Co-authored-by: Valentin Balaschenko <13349202+vlntb@users.noreply.github.com>
2025-02-14 18:51:51 -05:00

155 lines
3.8 KiB
C++

//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <xrpld/app/misc/HashRouter.h>
namespace ripple {
auto
HashRouter::emplace(uint256 const& key) -> std::pair<Entry&, bool>
{
auto iter = suppressionMap_.find(key);
if (iter != suppressionMap_.end())
{
suppressionMap_.touch(iter);
return std::make_pair(std::ref(iter->second), false);
}
// See if any supressions need to be expired
expire(suppressionMap_, holdTime_);
return std::make_pair(
std::ref(suppressionMap_.emplace(key, Entry()).first->second), true);
}
void
HashRouter::addSuppression(uint256 const& key)
{
std::lock_guard lock(mutex_);
emplace(key);
}
bool
HashRouter::addSuppressionPeer(uint256 const& key, PeerShortID peer)
{
return addSuppressionPeerWithStatus(key, peer).first;
}
std::pair<bool, std::optional<Stopwatch::time_point>>
HashRouter::addSuppressionPeerWithStatus(const uint256& key, PeerShortID peer)
{
std::lock_guard lock(mutex_);
auto result = emplace(key);
result.first.addPeer(peer);
return {result.second, result.first.relayed()};
}
bool
HashRouter::addSuppressionPeer(uint256 const& key, PeerShortID peer, int& flags)
{
std::lock_guard lock(mutex_);
auto [s, created] = emplace(key);
s.addPeer(peer);
flags = s.getFlags();
return created;
}
bool
HashRouter::shouldProcess(
uint256 const& key,
PeerShortID peer,
int& flags,
std::chrono::seconds tx_interval)
{
std::lock_guard lock(mutex_);
auto result = emplace(key);
auto& s = result.first;
s.addPeer(peer);
flags = s.getFlags();
return s.shouldProcess(suppressionMap_.clock().now(), tx_interval);
}
bool
HashRouter::shouldProcessForPeer(
uint256 const& key,
PeerShortID peer,
std::chrono::seconds interval)
{
std::lock_guard lock(mutex_);
auto& entry = emplace(key).first;
return entry.shouldProcessForPeer(
peer, suppressionMap_.clock().now(), interval);
}
int
HashRouter::getFlags(uint256 const& key)
{
std::lock_guard lock(mutex_);
return emplace(key).first.getFlags();
}
bool
HashRouter::setFlags(uint256 const& key, int flags)
{
XRPL_ASSERT(flags, "ripple::HashRouter::setFlags : valid input");
std::lock_guard lock(mutex_);
auto& s = emplace(key).first;
if ((s.getFlags() & flags) == flags)
return false;
s.setFlags(flags);
return true;
}
auto
HashRouter::shouldRelay(uint256 const& key)
-> std::optional<std::set<PeerShortID>>
{
std::lock_guard lock(mutex_);
auto& s = emplace(key).first;
if (!s.shouldRelay(suppressionMap_.clock().now(), holdTime_))
return {};
return s.releasePeerSet();
}
auto
HashRouter::getPeers(uint256 const& key) -> std::set<PeerShortID>
{
std::lock_guard lock(mutex_);
auto& s = emplace(key).first;
return s.peekPeerSet();
}
} // namespace ripple