Files
clio/src/cluster/ClusterCommunicationService.cpp
2026-03-24 15:25:32 +00:00

86 lines
2.4 KiB
C++

#include "cluster/ClusterCommunicationService.hpp"
#include "data/BackendInterface.hpp"
#include "data/LedgerCacheLoadingState.hpp"
#include "etl/SystemState.hpp"
#include "etl/WriterState.hpp"
#include "util/config/ConfigDefinition.hpp"
#include <chrono>
#include <ctime>
#include <memory>
#include <utility>
namespace cluster {
ClusterCommunicationService::ClusterCommunicationService(
std::shared_ptr<data::BackendInterface> backend,
std::unique_ptr<etl::WriterStateInterface> writerState,
std::unique_ptr<data::LedgerCacheLoadingStateInterface> cacheLoadingState,
std::chrono::steady_clock::duration readInterval,
std::chrono::steady_clock::duration writeInterval
)
: backend_(
ctx_,
std::move(backend),
writerState->clone(),
cacheLoadingState->clone(),
readInterval,
writeInterval
)
, writerDecider_(ctx_, std::move(writerState))
, cacheLoaderDecider_(ctx_, std::move(cacheLoadingState))
{
}
void
ClusterCommunicationService::run()
{
backend_.subscribeToNewState([this](auto&&... args) {
metrics_.onNewState(std::forward<decltype(args)>(args)...);
});
backend_.subscribeToNewState([this](auto&&... args) {
writerDecider_.onNewState(std::forward<decltype(args)>(args)...);
});
backend_.subscribeToNewState([this](auto&&... args) {
cacheLoaderDecider_.onNewState(std::forward<decltype(args)>(args)...);
});
backend_.run();
}
ClusterCommunicationService::~ClusterCommunicationService()
{
stop();
}
void
ClusterCommunicationService::stop()
{
backend_.stop();
}
ClusterCommunicationService::MakeResult
ClusterCommunicationService::make(
util::config::ClioConfigDefinition const& config,
std::shared_ptr<BackendInterface> backend,
std::shared_ptr<etl::SystemState> state
)
{
auto const& cache = backend->cache();
auto cacheLoadingState = std::make_unique<data::LedgerCacheLoadingState>(cache);
if (not config.get<bool>("cache.limit_load_in_cluster")) {
cacheLoadingState->allowLoading();
}
auto cacheLoadingStateClone = cacheLoadingState->clone();
return MakeResult{
.service = std::make_unique<ClusterCommunicationService>(
std::move(backend),
std::make_unique<etl::WriterState>(std::move(state), cache),
std::move(cacheLoadingState)
),
.cacheLoadingState = std::move(cacheLoadingStateClone)
};
}
} // namespace cluster