Add support for reserved peer slots:

This commit allows server operators to reserve slots for specific
peers (identified by the peer's public node identity) and to make
changes to the reservations while the server is operating.

This commit closes #2938
This commit is contained in:
John Freeman
2019-05-14 14:50:37 -05:00
committed by Nik Bougalis
parent 20cc5df5fe
commit 87e9ee5ce9
25 changed files with 553 additions and 52 deletions

View File

@@ -278,18 +278,25 @@ OverlayImpl::onHandoff (std::unique_ptr <beast::asio::ssl_bundle>&& ssl_bundle,
return handoff;
}
auto const result = m_peerFinder->activate (slot, *publicKey,
static_cast<bool>(app_.cluster().member(*publicKey)));
if (result != PeerFinder::Result::success)
{
m_peerFinder->on_closed(slot);
JLOG(journal.debug()) <<
"Peer " << remote_endpoint << " redirected, slots full";
handoff.moved = false;
handoff.response = makeRedirectResponse(slot, request,
remote_endpoint.address());
handoff.keep_alive = beast::rfc2616::is_keep_alive(request);
return handoff;
// The node gets a reserved slot if it is in our cluster
// or if it has a reservation.
bool const reserved {
static_cast<bool>(app_.cluster().member(*publicKey))
|| app_.peerReservations().contains(*publicKey)
};
auto const result = m_peerFinder->activate(slot, *publicKey, reserved);
if (result != PeerFinder::Result::success)
{
m_peerFinder->on_closed(slot);
JLOG(journal.debug())
<< "Peer " << remote_endpoint << " redirected, slots full";
handoff.moved = false;
handoff.response = makeRedirectResponse(
slot, request, remote_endpoint.address());
handoff.keep_alive = beast::rfc2616::is_keep_alive(request);
return handoff;
}
}
auto const peer = std::make_shared<PeerImp>(app_, id,

View File

@@ -254,6 +254,12 @@ PeerImp::crawl() const
return boost::beast::detail::iequals(iter->value(), "public");
}
bool
PeerImp::cluster() const
{
return static_cast<bool>(app_.cluster().member(publicKey_));
}
std::string
PeerImp::getVersion() const
{
@@ -1614,7 +1620,7 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMProposeSet> const& m)
{
protocol::TMProposeSet& set = *m;
if (set.has_hops() && ! slot_->cluster())
if (set.has_hops() && ! cluster())
set.set_hops(set.hops() + 1);
auto const sig = makeSlice(set.signature());
@@ -1983,7 +1989,7 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMValidation> const& m)
{
auto const closeTime = app_.timeKeeper().closeTime();
if (m->has_hops() && ! slot_->cluster())
if (m->has_hops() && ! cluster())
m->set_hops(m->hops() + 1);
if (m->validation ().size () < 50)

View File

@@ -281,10 +281,7 @@ public:
crawl() const;
bool
cluster() const override
{
return slot_->cluster();
}
cluster() const override;
void
check();

View File

@@ -0,0 +1,168 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2019 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 <ripple/overlay/PeerReservationTable.h>
#include <ripple/basics/Log.h>
#include <ripple/core/DatabaseCon.h>
#include <ripple/json/json_value.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/protocol/jss.h>
#include <boost/optional.hpp>
#include <algorithm>
#include <iterator>
#include <mutex>
#include <string>
#include <vector>
namespace ripple {
auto
PeerReservation::toJson() const -> Json::Value
{
Json::Value result{Json::objectValue};
result[jss::node] = toBase58(TokenType::NodePublic, nodeId);
if (!description.empty())
{
result[jss::description] = description;
}
return result;
}
auto
PeerReservationTable::list() const -> std::vector<PeerReservation>
{
std::vector<PeerReservation> list;
{
std::lock_guard<std::mutex> lock(mutex_);
list.reserve(table_.size());
std::copy(table_.begin(), table_.end(), std::back_inserter(list));
}
std::sort(list.begin(), list.end());
return list;
}
// See `ripple/app/main/DBInit.cpp` for the `CREATE TABLE` statement.
// It is unfortunate that we do not get to define a function for it.
// We choose a `bool` return type to fit in with the error handling scheme
// of other functions called from `ApplicationImp::setup`, but we always
// return "no error" (`true`) because we can always return an empty table.
bool
PeerReservationTable::load(DatabaseCon& connection)
{
std::lock_guard<std::mutex> lock(mutex_);
connection_ = &connection;
auto db = connection_->checkoutDb();
boost::optional<std::string> valPubKey, valDesc;
// We should really abstract the table and column names into constants,
// but no one else does. Because it is too tedious? It would be easy if we
// had a jOOQ for C++.
soci::statement st =
(db->prepare << "SELECT PublicKey, Description FROM PeerReservations;",
soci::into(valPubKey),
soci::into(valDesc));
st.execute();
while (st.fetch())
{
if (!valPubKey || !valDesc)
{
// This represents a `NULL` in a `NOT NULL` column. It should be
// unreachable.
continue;
}
auto const optNodeId =
parseBase58<PublicKey>(TokenType::NodePublic, *valPubKey);
if (!optNodeId)
{
JLOG(journal_.warn()) << "load: not a public key: " << valPubKey;
continue;
}
table_.insert(PeerReservation{*optNodeId, *valDesc});
}
return true;
}
auto
PeerReservationTable::insert_or_assign(
PeerReservation const& reservation)
-> boost::optional<PeerReservation>
{
boost::optional<PeerReservation> previous;
std::lock_guard<std::mutex> lock(mutex_);
auto hint = table_.find(reservation);
if (hint != table_.end()) {
// The node already has a reservation. Remove it.
// `std::unordered_set` does not have an `insert_or_assign` method,
// and sadly makes it impossible for us to implement one efficiently:
// https://stackoverflow.com/q/49651835/618906
// Regardless, we don't expect this function to be called often, or
// for the table to be very large, so this less-than-ideal
// remove-then-insert is acceptable in order to present a better API.
previous = *hint;
// We should pick an adjacent location for the insertion hint.
// Decrementing may be illegal if the found reservation is at the
// beginning. Incrementing is always legal; at worst we'll point to
// the end.
auto const deleteme = hint;
++hint;
table_.erase(deleteme);
}
table_.insert(hint, reservation);
auto db = connection_->checkoutDb();
*db << "INSERT INTO PeerReservations (PublicKey, Description) "
"VALUES (:nodeId, :desc) "
"ON CONFLICT (PublicKey) DO UPDATE SET "
"Description=excluded.Description",
soci::use(toBase58(TokenType::NodePublic, reservation.nodeId)),
soci::use(reservation.description);
return previous;
}
auto
PeerReservationTable::erase(PublicKey const& nodeId)
-> boost::optional<PeerReservation>
{
boost::optional<PeerReservation> previous;
std::lock_guard<std::mutex> lock(mutex_);
auto const it = table_.find({nodeId});
if (it != table_.end())
{
previous = *it;
table_.erase(it);
auto db = connection_->checkoutDb();
*db << "DELETE FROM PeerReservations WHERE PublicKey = :nodeId",
soci::use(toBase58(TokenType::NodePublic, nodeId));
}
return previous;
}
} // namespace ripple