Skip to content

Commit

Permalink
FOLD: Make PRT thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
thejohnfreeman committed Aug 2, 2019
1 parent 2bc748c commit 423baab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
33 changes: 10 additions & 23 deletions src/ripple/overlay/PeerReservationTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#include <boost/optional.hpp>
#include <soci/soci.h>

#include <mutex>
#include <string>
#include <unordered_set>
#include <vector>

namespace ripple {

Expand All @@ -53,6 +55,11 @@ struct PeerReservation final
using beast::hash_append;
hash_append(h, x.nodeId);
}

friend bool operator<(PeerReservation const& a, PeerReservation const& b)
{
return a.nodeId < b.nodeId;
}
};

// TODO: When C++20 arrives, take advantage of "equivalence" instead of
Expand Down Expand Up @@ -80,33 +87,12 @@ class PeerReservationTable final
{
}

const_iterator
begin() const noexcept
{
return table_.begin();
}

const_iterator
cbegin() const noexcept
{
return table_.cbegin();
}

const_iterator
end() const noexcept
{
return table_.end();
}

const_iterator
cend() const noexcept
{
return table_.cend();
}
std::vector<PeerReservation> list() const;

bool
contains(PublicKey const& nodeId)
{
std::lock_guard<std::mutex> lock(this->mutex_);
return table_.find({nodeId}) != table_.end();
}

Expand All @@ -131,6 +117,7 @@ class PeerReservationTable final

private:
beast::Journal mutable journal_;
std::mutex mutable mutex_;
DatabaseCon* connection_;
table_type table_;
};
Expand Down
23 changes: 23 additions & 0 deletions src/ripple/overlay/impl/PeerReservationTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

#include <boost/optional.hpp>

#include <algorithm>
#include <iterator>
#include <mutex>
#include <string>
#include <vector>

namespace ripple {

Expand All @@ -43,6 +47,19 @@ PeerReservation::toJson() const -> Json::Value
return result;
}

auto
PeerReservationTable::list() const -> std::vector<PeerReservation>
{
std::vector<PeerReservation> list;
list.reserve(table_.size());
{
std::lock_guard<std::mutex> lock(mutex_);
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.

Expand All @@ -52,6 +69,8 @@ PeerReservation::toJson() const -> Json::Value
bool
PeerReservationTable::load(DatabaseCon& connection)
{
std::lock_guard<std::mutex> lock(mutex_);

connection_ = &connection;
auto db = connection_->checkoutDb();

Expand Down Expand Up @@ -92,6 +111,8 @@ PeerReservationTable::insert_or_assign(
{
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.
Expand Down Expand Up @@ -129,6 +150,8 @@ PeerReservationTable::erase(PublicKey const& nodeId)
{
boost::optional<PeerReservation> previous;

std::lock_guard<std::mutex> lock(mutex_);

auto const it = table_.find({nodeId});
if (it != table_.end())
{
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/rpc/handlers/Reservations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ doPeerReservationsDel(RPC::Context& context)
Json::Value
doPeerReservationsList(RPC::Context& context)
{
auto const& reservations = context.app.peerReservations();
auto const& reservations = context.app.peerReservations().list();
// Enumerate the reservations in context.app.peerReservations()
// as a Json::Value.
Json::Value result{Json::objectValue};
Expand Down

0 comments on commit 423baab

Please sign in to comment.