Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only forward to p2p nodes that are in sync #4028

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/ripple/app/reporting/ETLHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NetworkValidatedLedgers

mutable std::mutex m_;

std::condition_variable cv_;
mutable std::condition_variable cv_;

bool stopping_ = false;

Expand All @@ -64,13 +64,23 @@ class NetworkValidatedLedgers
/// @return sequence of most recently validated ledger. empty optional if
/// the datastructure has been stopped
std::optional<uint32_t>
getMostRecent()
getMostRecent() const
{
std::unique_lock lck(m_);
cv_.wait(lck, [this]() { return max_ || stopping_; });
return max_;
}

/// Get most recently validated sequence.
/// @return sequence of most recently validated ledger, or empty optional
/// if no ledgers are known to have been validated.
std::optional<uint32_t>
tryGetMostRecent() const
{
std::unique_lock lk(m_);
return max_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to read max_ with m_ locked. Otherwise you can get a torn optional.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should probably be const (as should getMostRecent() which is probably why this method is not).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make these methods const, std::condition_variable cv_ has to be made mutable. I'll make that change too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to read max_ with m_ locked. Otherwise you can get a torn optional.

Good catch

}

/// Waits for the sequence to be validated by the network
/// @param sequence to wait for
/// @return true if sequence was validated, false otherwise
Expand Down
17 changes: 14 additions & 3 deletions src/ripple/app/reporting/ETLSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,13 +760,24 @@ ETLLoadBalancer::forwardToP2p(RPC::JsonContext& context) const
srand((unsigned)time(0));
auto sourceIdx = rand() % sources_.size();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't fix this right now, but I think there's a better way to get a uniform int distribution post C++11: https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution This may not be a critical place for it, but we should still model good coding practices.

That's a good change to make some time in the future.

auto numAttempts = 0;

auto mostRecent = etl_.getNetworkValidatedLedgers().tryGetMostRecent();
while (numAttempts < sources_.size())
{
res = sources_[sourceIdx]->forwardToP2p(context);
if (!res.isMember("forwarded") || res["forwarded"] != true)
{
auto increment = [&]() {
sourceIdx = (sourceIdx + 1) % sources_.size();
++numAttempts;
};
auto& src = sources_[sourceIdx];
if (mostRecent && !src->hasLedger(*mostRecent))
{
increment();
continue;
}
res = src->forwardToP2p(context);
if (!res.isMember("forwarded") || res["forwarded"] != true)
{
increment();
continue;
}
return res;
Expand Down