make threadpool a class object

This commit is contained in:
CJ Cobb
2021-04-27 16:19:45 +00:00
parent 3be12dd4b4
commit 3aed708262
2 changed files with 80 additions and 49 deletions

View File

@@ -411,17 +411,15 @@ std::vector<TransactionAndMetadata>
PostgresBackend::fetchTransactions( PostgresBackend::fetchTransactions(
std::vector<ripple::uint256> const& hashes) const std::vector<ripple::uint256> const& hashes) const
{ {
PgQuery pgQuery(pgPool_); std::vector<TransactionAndMetadata> results;
pgQuery("SET statement_timeout TO 10000"); constexpr bool doAsync = true;
std::stringstream sql; if (doAsync)
bool first = true; {
auto start = std::chrono::system_clock::now(); auto start = std::chrono::system_clock::now();
boost::asio::thread_pool pool{hashes.size()};
auto end = std::chrono::system_clock::now(); auto end = std::chrono::system_clock::now();
auto duration = ((end - start).count()) / 1000000000.0; auto duration = ((end - start).count()) / 1000000000.0;
BOOST_LOG_TRIVIAL(info) BOOST_LOG_TRIVIAL(info) << __func__ << " created threadpool. took "
<< __func__ << " created threadpool. took " << std::to_string(duration); << std::to_string(duration);
std::vector<TransactionAndMetadata> results;
results.resize(hashes.size()); results.resize(hashes.size());
std::condition_variable cv; std::condition_variable cv;
std::mutex mtx; std::mutex mtx;
@@ -429,16 +427,14 @@ PostgresBackend::fetchTransactions(
for (size_t i = 0; i < hashes.size(); ++i) for (size_t i = 0; i < hashes.size(); ++i)
{ {
auto const& hash = hashes[i]; auto const& hash = hashes[i];
boost::asio::post([this, boost::asio::post(
&hash, pool_, [this, &hash, &results, &numRemaining, &cv, &mtx, i]() {
&results, BOOST_LOG_TRIVIAL(debug)
&numRemaining, << __func__ << " getting txn = " << i;
&cv,
&mtx,
i]() {
PgQuery pgQuery(pgPool_); PgQuery pgQuery(pgPool_);
std::stringstream sql; std::stringstream sql;
sql << "SELECT transaction,metadata,ledger_seq FROM transactions " sql << "SELECT transaction,metadata,ledger_seq FROM "
"transactions "
"WHERE HASH = \'\\x" "WHERE HASH = \'\\x"
<< ripple::strHex(hash) << "\'"; << ripple::strHex(hash) << "\'";
@@ -463,9 +459,43 @@ PostgresBackend::fetchTransactions(
duration = ((end2 - end).count()) / 1000000000.0; duration = ((end2 - end).count()) / 1000000000.0;
BOOST_LOG_TRIVIAL(info) BOOST_LOG_TRIVIAL(info)
<< __func__ << " fetched " << std::to_string(hashes.size()) << __func__ << " fetched " << std::to_string(hashes.size())
<< " transactions. took " << std::to_string(duration); << " transactions with threadpool. took "
<< std::to_string(duration);
}
else
{
PgQuery pgQuery(pgPool_);
pgQuery("SET statement_timeout TO 10000");
std::stringstream sql;
for (size_t i = 0; i < hashes.size(); ++i)
{
auto const& hash = hashes[i];
sql << "SELECT transaction,metadata,ledger_seq FROM "
"transactions "
"WHERE HASH = \'\\x"
<< ripple::strHex(hash) << "\'";
if (i + 1 < hashes.size())
sql << " UNION ALL ";
}
auto start = std::chrono::system_clock::now();
auto res = pgQuery(sql.str().data());
auto end = std::chrono::system_clock::now();
auto duration = ((end - start).count()) / 1000000000.0;
BOOST_LOG_TRIVIAL(info)
<< __func__ << " fetched " << std::to_string(hashes.size())
<< " transactions with union all. took "
<< std::to_string(duration);
if (size_t numRows = checkResult(res, 3))
{
for (size_t i = 0; i < numRows; ++i)
results.push_back(
{res.asUnHexedBlob(i, 0),
res.asUnHexedBlob(i, 1),
res.asBigInt(i, 2)});
}
}
return results; return results;
} // namespace Backend }
std::vector<Blob> std::vector<Blob>
PostgresBackend::fetchLedgerObjects( PostgresBackend::fetchLedgerObjects(

View File

@@ -15,6 +15,7 @@ private:
std::shared_ptr<PgPool> pgPool_; std::shared_ptr<PgPool> pgPool_;
mutable PgQuery writeConnection_; mutable PgQuery writeConnection_;
mutable bool abortWrite_ = false; mutable bool abortWrite_ = false;
mutable boost::asio::thread_pool pool_{200};
public: public:
PostgresBackend(boost::json::object const& config); PostgresBackend(boost::json::object const& config);