Skip to content

Commit

Permalink
added generic helper function + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vlntb committed Aug 23, 2024
1 parent ea38d43 commit 33c8c9f
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 104 deletions.
14 changes: 6 additions & 8 deletions src/ripple/app/consensus/RCLValidations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/app/misc/ValidatorList.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/PerfLog.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/basics/chrono.h>
#include <ripple/consensus/LedgerTiming.h>
Expand Down Expand Up @@ -126,14 +127,11 @@ RCLValidationsAdaptor::now() const
std::optional<RCLValidatedLedger>
RCLValidationsAdaptor::acquire(LedgerHash const& hash)
{
auto start_time = std::chrono::high_resolution_clock::now();
auto ledger = app_.getLedgerMaster().getLedgerByHash(hash);
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
std::size_t const MAX_DELAY_MS = 10;
if (duration > MAX_DELAY_MS) {
JLOG(j_.warn()) << "getLedgerByHash took " << duration << " ms";
}
auto ledger = perf::measureDurationAndLog(
[&]() { return app_.getLedgerMaster().getLedgerByHash(hash); },
"getLedgerByHash",
10,
j_);

if (!ledger)
{
Expand Down
152 changes: 80 additions & 72 deletions src/ripple/app/ledger/impl/InboundLedgers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/basics/DecayingSample.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/PerfLog.h>
#include <ripple/beast/container/aged_map.h>
#include <ripple/beast/core/LexicalCast.h>
#include <ripple/core/JobQueue.h>
Expand Down Expand Up @@ -69,83 +70,90 @@ class InboundLedgersImp : public InboundLedgers
std::uint32_t seq,
InboundLedger::Reason reason) override
{
auto start_time = std::chrono::high_resolution_clock::now();
assert(hash.isNonZero());
assert(
reason != InboundLedger::Reason::SHARD ||
(seq != 0 && app_.getShardStore()));

// probably not the right rule
if (app_.getOPs().isNeedNetworkLedger() &&
(reason != InboundLedger::Reason::GENERIC) &&
(reason != InboundLedger::Reason::CONSENSUS))
return {};

bool isNew = true;
std::shared_ptr<InboundLedger> inbound;
{
ScopedLockType sl(mLock);
if (stopping_)
{
return {};
}
std::shared_ptr<Ledger const> ledger = perf::measureDurationAndLog(
[&, hash, seq, reason]() -> std::shared_ptr<Ledger const> {
assert(hash.isNonZero());
assert(
reason != InboundLedger::Reason::SHARD ||
(seq != 0 && app_.getShardStore()));

// probably not the right rule
if (app_.getOPs().isNeedNetworkLedger() &&
(reason != InboundLedger::Reason::GENERIC) &&
(reason != InboundLedger::Reason::CONSENSUS))
return {};

bool isNew = true;
std::shared_ptr<InboundLedger> inbound;
{
ScopedLockType sl(mLock);
if (stopping_)
{
return {};
}

auto it = mLedgers.find(hash);
if (it != mLedgers.end())
{
isNew = false;
inbound = it->second;
}
else
{
inbound = std::make_shared<InboundLedger>(
app_,
hash,
seq,
reason,
std::ref(m_clock),
mPeerSetBuilder->build());
mLedgers.emplace(hash, inbound);
inbound->init(sl);
++mCounter;
}
}

auto it = mLedgers.find(hash);
if (it != mLedgers.end())
{
isNew = false;
inbound = it->second;
}
else
{
inbound = std::make_shared<InboundLedger>(
app_,
hash,
seq,
reason,
std::ref(m_clock),
mPeerSetBuilder->build());
mLedgers.emplace(hash, inbound);
inbound->init(sl);
++mCounter;
}
}
if (inbound->isFailed())
return {};

if (inbound->isFailed())
return {};
if (!isNew)
inbound->update(seq);

if (!isNew)
inbound->update(seq);
if (!inbound->isComplete())
return {};

if (!inbound->isComplete())
return {};
if (reason == InboundLedger::Reason::HISTORY)
{
if (inbound->getLedger()
->stateMap()
.family()
.isShardBacked())
app_.getNodeStore().storeLedger(inbound->getLedger());
}
else if (reason == InboundLedger::Reason::SHARD)
{
auto shardStore = app_.getShardStore();
if (!shardStore)
{
JLOG(j_.error())
<< "Acquiring shard with no shard store available";
return {};
}
if (inbound->getLedger()
->stateMap()
.family()
.isShardBacked())
shardStore->setStored(inbound->getLedger());
else
shardStore->storeLedger(inbound->getLedger());
}

return inbound->getLedger();
},
"InboundLedgersImp::acquire",
500,
j_);

if (reason == InboundLedger::Reason::HISTORY)
{
if (inbound->getLedger()->stateMap().family().isShardBacked())
app_.getNodeStore().storeLedger(inbound->getLedger());
}
else if (reason == InboundLedger::Reason::SHARD)
{
auto shardStore = app_.getShardStore();
if (!shardStore)
{
JLOG(j_.error())
<< "Acquiring shard with no shard store available";
return {};
}
if (inbound->getLedger()->stateMap().family().isShardBacked())
shardStore->setStored(inbound->getLedger());
else
shardStore->storeLedger(inbound->getLedger());
}
auto ledger = inbound->getLedger();
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
std::size_t const MAX_DELAY_MS = 500;
if (duration > MAX_DELAY_MS) {
JLOG(j_.warn()) << "InboundLedgersImp::acquire took " << duration << " ms";
}
return ledger;
}

Expand Down
5 changes: 4 additions & 1 deletion src/ripple/app/rdb/Wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ makeWalletDB(DatabaseCon::Setup const& setup, beast::Journal j);
* @return Unique pointer to the database descriptor.
*/
std::unique_ptr<DatabaseCon>
makeTestWalletDB(DatabaseCon::Setup const& setup, std::string const& dbname, beast::Journal j);
makeTestWalletDB(
DatabaseCon::Setup const& setup,
std::string const& dbname,
beast::Journal j);

/**
* @brief getManifests Loads a manifest from the wallet database and stores it
Expand Down
5 changes: 4 additions & 1 deletion src/ripple/app/rdb/impl/Wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ makeWalletDB(DatabaseCon::Setup const& setup, beast::Journal j)
}

std::unique_ptr<DatabaseCon>
makeTestWalletDB(DatabaseCon::Setup const& setup, std::string const& dbname, beast::Journal j)
makeTestWalletDB(
DatabaseCon::Setup const& setup,
std::string const& dbname,
beast::Journal j)
{
// wallet database
return std::make_unique<DatabaseCon>(
Expand Down
25 changes: 25 additions & 0 deletions src/ripple/basics/PerfLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ make_PerfLog(
beast::Journal journal,
std::function<void()>&& signalStop);

template <typename Func>
auto
measureDurationAndLog(
Func&& func,
const std::string& actionDescription,
std::size_t maxDelay,
const beast::Journal& journal)
{
auto start_time = std::chrono::high_resolution_clock::now();

auto result = func();

auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time)
.count();
if (duration > maxDelay)
{
JLOG(journal.warn())
<< actionDescription << " took " << duration << " ms";
}

return result;
}

} // namespace perf
} // namespace ripple

Expand Down
17 changes: 6 additions & 11 deletions src/ripple/core/DatabaseCon.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define RIPPLE_APP_DATA_DATABASECON_H_INCLUDED

#include <ripple/app/main/DBInit.h>
#include <ripple/basics/PerfLog.h>
#include <ripple/core/Config.h>
#include <ripple/core/SociDB.h>
#include <boost/filesystem/path.hpp>
Expand Down Expand Up @@ -183,17 +184,11 @@ class DatabaseCon
LockedSociSession
checkoutDb()
{
auto start_time = std::chrono::high_resolution_clock::now();
auto session = LockedSociSession(session_, lock_);
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time - start_time)
.count();
std::size_t const MAX_DELAY_MS = 10;
if (duration > MAX_DELAY_MS)
{
JLOG(j_.warn()) << "checkoutDb took " << duration << " ms";
}
LockedSociSession session = perf::measureDurationAndLog(
[&]() { return LockedSociSession(session_, lock_); },
"checkoutDb",
10,
j_);

return session;
}
Expand Down
21 changes: 10 additions & 11 deletions src/ripple/overlay/impl/PeerImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <ripple/app/misc/Transaction.h>
#include <ripple/app/misc/ValidatorList.h>
#include <ripple/app/tx/apply.h>
#include <ripple/basics/PerfLog.h>
#include <ripple/basics/UptimeClock.h>
#include <ripple/basics/base64.h>
#include <ripple/basics/random.h>
Expand Down Expand Up @@ -603,7 +604,7 @@ PeerImp::fail(std::string const& reason)
return post(
strand_,
std::bind(
(void (Peer::*)(std::string const&)) & PeerImp::fail,
(void(Peer::*)(std::string const&)) & PeerImp::fail,
shared_from_this(),
reason));
if (journal_.active(beast::severities::kWarning) && socket_.is_open())
Expand Down Expand Up @@ -921,16 +922,14 @@ PeerImp::onReadMessage(error_code ec, std::size_t bytes_transferred)
{
std::size_t bytes_consumed;

auto start_time = std::chrono::high_resolution_clock::now();
std::tie(bytes_consumed, ec) =
invokeProtocolMessage(read_buffer_.data(), *this, hint);
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
std::size_t const MAX_DELAY_MS = 350;
if (duration > MAX_DELAY_MS) {
JLOG(journal_.warn()) << "invokeProtocolMessage took " << duration << " ms";
}

std::tie(bytes_consumed, ec) = perf::measureDurationAndLog(
[&]() {
return invokeProtocolMessage(read_buffer_.data(), *this, hint);
},
"invokeProtocolMessage",
350,
journal_);

if (ec)
return fail("onReadMessage", ec);
if (!socket_.is_open())
Expand Down

0 comments on commit 33c8c9f

Please sign in to comment.