Skip to content

Commit

Permalink
Cover LoadBalancer with tests (#1394)
Browse files Browse the repository at this point in the history
Fixes #680. Fixes #1222.
  • Loading branch information
kuznetsss authored May 15, 2024
1 parent f74b89c commit da10535
Show file tree
Hide file tree
Showing 61 changed files with 2,774 additions and 1,471 deletions.
12 changes: 9 additions & 3 deletions .githooks/check-format
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ function grep_code {
grep -l "${1}" ${sources} -r --include \*.hpp --include \*.cpp
}

if [[ "$OSTYPE" == "darwin"* ]]; then
GNU_SED=$(sed --version 2>&1 | grep -q 'GNU' && echo true || echo false)

if [[ "$GNU_SED" == "false" ]]; then # macOS sed
# make all includes to be <...> style
grep_code '#include ".*"' | xargs sed -i '' -E 's|#include "(.*)"|#include <\1>|g'

Expand All @@ -83,9 +85,10 @@ first=$(git diff $sources $cmake_files)
find $sources -type f \( -name '*.cpp' -o -name '*.hpp' -o -name '*.ipp' \) -print0 | xargs -0 $formatter
cmake-format -i $cmake_files
second=$(git diff $sources $cmake_files)
changes=$(diff <(echo "$first") <(echo "$second") | wc -l | sed -e 's/^[[:space:]]*//')
changes=$(diff <(echo "$first") <(echo "$second"))
changes_number=$(echo -n "$changes" | wc -l | sed -e 's/^[[:space:]]*//')

if [ "$changes" != "0" ]; then
if [ "$changes_number" != "0" ]; then
cat <<\EOF
WARNING
Expand All @@ -95,5 +98,8 @@ if [ "$changes" != "0" ]; then
-----------------------------------------------------------------------------
EOF
if [[ "$1" == "--diff" ]]; then
echo "$changes"
fi
exit 1
fi
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Run formatters
id: run_formatters
run: |
./.githooks/check-format
./.githooks/check-format --diff
shell: bash

check_docs:
Expand Down
3 changes: 2 additions & 1 deletion src/data/BackendInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ BackendInterface::fetchBookOffers(
LOG(gLog.debug()) << "Fetching " << std::to_string(keys.size()) << " offers took "
<< std::to_string(getMillis(mid - begin)) << " milliseconds. Fetching next dir took "
<< std::to_string(succMillis) << " milliseonds. Fetched next dir " << std::to_string(numSucc)
<< " times" << " Fetching next page of dir took " << std::to_string(pageMillis) << " milliseconds"
<< " times"
<< " Fetching next page of dir took " << std::to_string(pageMillis) << " milliseconds"
<< ". num pages = " << std::to_string(numPages) << ". Fetching all objects took "
<< std::to_string(getMillis(end - mid))
<< " milliseconds. total time = " << std::to_string(getMillis(end - begin)) << " milliseconds"
Expand Down
4 changes: 2 additions & 2 deletions src/data/CassandraBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ class BasicCassandraBackend : public BackendInterface {
void
writeSuccessor(std::string&& key, std::uint32_t const seq, std::string&& successor) override
{
LOG(log_.trace()) << "Writing successor. key = " << key.size() << " bytes. " << " seq = " << std::to_string(seq)
<< " successor = " << successor.size() << " bytes.";
LOG(log_.trace()) << "Writing successor. key = " << key.size() << " bytes. "
<< " seq = " << std::to_string(seq) << " successor = " << successor.size() << " bytes.";
ASSERT(!key.empty(), "Key must not be empty");
ASSERT(!successor.empty(), "Successor must not be empty");

Expand Down
3 changes: 2 additions & 1 deletion src/data/cassandra/impl/ExecutionStrategy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ class DefaultExecutionStrategy {
{
std::unique_lock<std::mutex> lck(throttleMutex_);
if (!canAddWriteRequest()) {
LOG(log_.trace()) << "Max outstanding requests reached. " << "Waiting for other requests to finish";
LOG(log_.trace()) << "Max outstanding requests reached. "
<< "Waiting for other requests to finish";
throttleCv_.wait(lck, [this]() { return canAddWriteRequest(); });
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/etl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ add_library(clio_etl)

target_sources(
clio_etl
PRIVATE NFTHelpers.cpp
PRIVATE CacheLoaderSettings.cpp
ETLHelpers.cpp
ETLService.cpp
ETLState.cpp
LoadBalancer.cpp
CacheLoaderSettings.cpp
NetworkValidatedLedgers.cpp
NFTHelpers.cpp
Source.cpp
impl/ForwardingCache.cpp
impl/ForwardingSource.cpp
Expand Down
46 changes: 46 additions & 0 deletions src/etl/ETLHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and 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 "etl/ETLHelpers.hpp"

#include "util/Assert.hpp"

#include <ripple/basics/base_uint.h>

#include <cstddef>
#include <vector>

namespace etl {
std::vector<ripple::uint256>
getMarkers(size_t numMarkers)
{
ASSERT(numMarkers <= 256, "Number of markers must be <= 256. Got: {}", numMarkers);

unsigned char const incr = 256 / numMarkers;

std::vector<ripple::uint256> markers;
markers.reserve(numMarkers);
ripple::uint256 base{0};
for (size_t i = 0; i < numMarkers; ++i) {
markers.push_back(base);
base.data()[0] += incr;
}
return markers;
}
} // namespace etl
99 changes: 3 additions & 96 deletions src/etl/ETLHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
/** @file */
#pragma once

#include "util/Assert.hpp"

#include <ripple/basics/base_uint.h>

#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
Expand All @@ -35,83 +32,6 @@
#include <vector>

namespace etl {
/**
* @brief This datastructure is used to keep track of the sequence of the most recent ledger validated by the network.
*
* There are two methods that will wait until certain conditions are met. This datastructure is able to be "stopped".
* When the datastructure is stopped, any threads currently waiting are unblocked.
* Any later calls to methods of this datastructure will not wait. Once the datastructure is stopped, the datastructure
* remains stopped for the rest of its lifetime.
*/
class NetworkValidatedLedgers {
// max sequence validated by network
std::optional<uint32_t> max_;

mutable std::mutex m_;
std::condition_variable cv_;

public:
/**
* @brief A factory function for NetworkValidatedLedgers
*
* @return A shared pointer to a new instance of NetworkValidatedLedgers
*/
static std::shared_ptr<NetworkValidatedLedgers>
make_ValidatedLedgers()
{
return std::make_shared<NetworkValidatedLedgers>();
}

/**
* @brief Notify the datastructure that idx has been validated by the network.
*
* @param idx Sequence validated by network
*/
void
push(uint32_t idx)
{
std::lock_guard const lck(m_);
if (!max_ || idx > *max_)
max_ = idx;
cv_.notify_all();
}

/**
* @brief Get most recently validated sequence.
*
* If no ledgers are known to have been validated, this function waits until the next ledger is validated
*
* @return Sequence of most recently validated ledger. empty optional if the datastructure has been stopped
*/
std::optional<uint32_t>
getMostRecent()
{
std::unique_lock lck(m_);
cv_.wait(lck, [this]() { return max_; });
return max_;
}

/**
* @brief Waits for the sequence to be validated by the network.
*
* @param sequence The sequence to wait for
* @param maxWaitMs Maximum time to wait for the sequence to be validated. If empty, wait indefinitely
* @return true if sequence was validated, false otherwise a return value of false means the datastructure has been
* stopped
*/
bool
waitUntilValidatedByNetwork(uint32_t sequence, std::optional<uint32_t> maxWaitMs = {})
{
std::unique_lock lck(m_);
auto pred = [sequence, this]() -> bool { return (max_ && sequence <= *max_); };
if (maxWaitMs) {
cv_.wait_for(lck, std::chrono::milliseconds(*maxWaitMs));
} else {
cv_.wait(lck, pred);
}
return pred();
}
};

// TODO: does the note make sense? lockfree queues provide the same blocking behaviour just without mutex, don't they?
/**
Expand Down Expand Up @@ -228,20 +148,7 @@ class ThreadSafeQueue {
* @param numMarkers Total markers to partition for
* @return The markers
*/
inline std::vector<ripple::uint256>
getMarkers(size_t numMarkers)
{
ASSERT(numMarkers <= 256, "Number of markers must be <= 256. Got: {}", numMarkers);

unsigned char const incr = 256 / numMarkers;

std::vector<ripple::uint256> markers;
markers.reserve(numMarkers);
ripple::uint256 base{0};
for (size_t i = 0; i < numMarkers; ++i) {
markers.push_back(base);
base.data()[0] += incr;
}
return markers;
}
std::vector<ripple::uint256>
getMarkers(size_t numMarkers);

} // namespace etl
6 changes: 4 additions & 2 deletions src/etl/ETLService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "data/BackendInterface.hpp"
#include "data/LedgerCache.hpp"
#include "etl/CorruptionDetector.hpp"
#include "etl/ETLHelpers.hpp"
#include "feed/SubscriptionManagerInterface.hpp"
#include "util/Assert.hpp"
#include "util/Constants.hpp"
#include "util/config/Config.hpp"
Expand Down Expand Up @@ -263,9 +265,9 @@ ETLService::ETLService(
util::Config const& config,
boost::asio::io_context& ioc,
std::shared_ptr<BackendInterface> backend,
std::shared_ptr<SubscriptionManagerType> subscriptions,
std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
std::shared_ptr<LoadBalancerType> balancer,
std::shared_ptr<NetworkValidatedLedgersType> ledgers
std::shared_ptr<NetworkValidatedLedgersInterface> ledgers
)
: backend_(backend)
, loadBalancer_(balancer)
Expand Down
18 changes: 8 additions & 10 deletions src/etl/ETLService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "etl/impl/LedgerLoader.hpp"
#include "etl/impl/LedgerPublisher.hpp"
#include "etl/impl/Transformer.hpp"
#include "feed/SubscriptionManager.hpp"
#include "feed/SubscriptionManagerInterface.hpp"
#include "util/log/Logger.hpp"

#include <boost/asio/io_context.hpp>
Expand Down Expand Up @@ -77,16 +77,14 @@ namespace etl {
*/
class ETLService {
// TODO: make these template parameters in ETLService
using SubscriptionManagerType = feed::SubscriptionManager;
using LoadBalancerType = LoadBalancer;
using NetworkValidatedLedgersType = NetworkValidatedLedgers;
using DataPipeType = etl::impl::ExtractionDataPipe<org::xrpl::rpc::v1::GetLedgerResponse>;
using CacheType = data::LedgerCache;
using CacheLoaderType = etl::CacheLoader<CacheType>;
using LedgerFetcherType = etl::impl::LedgerFetcher<LoadBalancerType>;
using ExtractorType = etl::impl::Extractor<DataPipeType, NetworkValidatedLedgersType, LedgerFetcherType>;
using ExtractorType = etl::impl::Extractor<DataPipeType, LedgerFetcherType>;
using LedgerLoaderType = etl::impl::LedgerLoader<LoadBalancerType, LedgerFetcherType>;
using LedgerPublisherType = etl::impl::LedgerPublisher<SubscriptionManagerType, CacheType>;
using LedgerPublisherType = etl::impl::LedgerPublisher<CacheType>;
using AmendmentBlockHandlerType = etl::impl::AmendmentBlockHandler<>;
using TransformerType =
etl::impl::Transformer<DataPipeType, LedgerLoaderType, LedgerPublisherType, AmendmentBlockHandlerType>;
Expand All @@ -95,7 +93,7 @@ class ETLService {

std::shared_ptr<BackendInterface> backend_;
std::shared_ptr<LoadBalancerType> loadBalancer_;
std::shared_ptr<NetworkValidatedLedgersType> networkValidatedLedgers_;
std::shared_ptr<NetworkValidatedLedgersInterface> networkValidatedLedgers_;

std::uint32_t extractorThreads_ = 1;
std::thread worker_;
Expand Down Expand Up @@ -128,9 +126,9 @@ class ETLService {
util::Config const& config,
boost::asio::io_context& ioc,
std::shared_ptr<BackendInterface> backend,
std::shared_ptr<SubscriptionManagerType> subscriptions,
std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
std::shared_ptr<LoadBalancerType> balancer,
std::shared_ptr<NetworkValidatedLedgersType> ledgers
std::shared_ptr<NetworkValidatedLedgersInterface> ledgers
);

/**
Expand All @@ -151,9 +149,9 @@ class ETLService {
util::Config const& config,
boost::asio::io_context& ioc,
std::shared_ptr<BackendInterface> backend,
std::shared_ptr<SubscriptionManagerType> subscriptions,
std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
std::shared_ptr<LoadBalancerType> balancer,
std::shared_ptr<NetworkValidatedLedgersType> ledgers
std::shared_ptr<NetworkValidatedLedgersInterface> ledgers
)
{
auto etl = std::make_shared<ETLService>(config, ioc, backend, subscriptions, balancer, ledgers);
Expand Down
18 changes: 10 additions & 8 deletions src/etl/ETLState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@
#include <ripple/protocol/jss.h>

#include <cstdint>
#include <optional>

namespace etl {

ETLState
tag_invoke(boost::json::value_to_tag<ETLState>, boost::json::value const& jv)
std::optional<ETLState>
tag_invoke(boost::json::value_to_tag<std::optional<ETLState>>, boost::json::value const& jv)
{
ETLState state;
auto const& jsonObject = jv.as_object();

if (!jsonObject.contains(JS(error))) {
if (jsonObject.contains(JS(result)) && jsonObject.at(JS(result)).as_object().contains(JS(info))) {
auto const rippledInfo = jsonObject.at(JS(result)).as_object().at(JS(info)).as_object();
if (rippledInfo.contains(JS(network_id)))
state.networkID.emplace(boost::json::value_to<int64_t>(rippledInfo.at(JS(network_id))));
}
if (jsonObject.contains(JS(error)))
return std::nullopt;

if (jsonObject.contains(JS(result)) && jsonObject.at(JS(result)).as_object().contains(JS(info))) {
auto const rippledInfo = jsonObject.at(JS(result)).as_object().at(JS(info)).as_object();
if (rippledInfo.contains(JS(network_id)))
state.networkID.emplace(boost::json::value_to<int64_t>(rippledInfo.at(JS(network_id))));
}

return state;
Expand Down
6 changes: 3 additions & 3 deletions src/etl/ETLState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct ETLState {
});

if (serverInfoRippled)
return boost::json::value_to<ETLState>(boost::json::value(*serverInfoRippled));
return boost::json::value_to<std::optional<ETLState>>(boost::json::value(*serverInfoRippled));

return std::nullopt;
}
Expand All @@ -63,7 +63,7 @@ struct ETLState {
* @param jv The json value to convert
* @return The ETLState
*/
ETLState
tag_invoke(boost::json::value_to_tag<ETLState>, boost::json::value const& jv);
std::optional<ETLState>
tag_invoke(boost::json::value_to_tag<std::optional<ETLState>>, boost::json::value const& jv);

} // namespace etl
Loading

0 comments on commit da10535

Please sign in to comment.