mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-03 16:56:48 +00:00
# Conflicts: # .codecov.yml # .github/scripts/levelization/results/ordering.txt # .github/workflows/reusable-clang-tidy-files.yml # CMakeLists.txt # OpenTelemetryPlan/00-tracing-fundamentals.md # OpenTelemetryPlan/01-architecture-analysis.md # OpenTelemetryPlan/02-design-decisions.md # OpenTelemetryPlan/03-implementation-strategy.md # OpenTelemetryPlan/04-code-samples.md # OpenTelemetryPlan/05-configuration-reference.md # OpenTelemetryPlan/06-implementation-phases.md # OpenTelemetryPlan/07-observability-backends.md # OpenTelemetryPlan/08-appendix.md # OpenTelemetryPlan/09-data-collection-reference.md # OpenTelemetryPlan/OpenTelemetryPlan.md # OpenTelemetryPlan/POC_taskList.md # OpenTelemetryPlan/Phase2_taskList.md # OpenTelemetryPlan/Phase3_taskList.md # OpenTelemetryPlan/Phase4_taskList.md # OpenTelemetryPlan/Phase5_IntegrationTest_taskList.md # OpenTelemetryPlan/Phase5_taskList.md # OpenTelemetryPlan/presentation.md # cfg/xrpld-example.cfg # conan.lock # conanfile.py # cspell.config.yaml # docker/telemetry/TESTING.md # docker/telemetry/docker-compose.yml # docker/telemetry/grafana/dashboards/consensus-health.json # docker/telemetry/grafana/dashboards/transaction-overview.json # docker/telemetry/grafana/provisioning/dashboards/dashboards.yaml # docker/telemetry/grafana/provisioning/datasources/tempo.yaml # docker/telemetry/integration-test.sh # docker/telemetry/otel-collector-config.yaml # docker/telemetry/tempo.yaml # docker/telemetry/xrpld-telemetry.cfg # docs/build/telemetry.md # docs/telemetry-runbook.md # include/xrpl/core/ServiceRegistry.h # include/xrpl/protocol/detail/features.macro # include/xrpl/telemetry/SpanGuard.h # include/xrpl/telemetry/Telemetry.h # include/xrpl/telemetry/TraceContextPropagator.h # src/libxrpl/basics/MallocTrim.cpp # src/libxrpl/nodestore/backend/MemoryFactory.cpp # src/libxrpl/nodestore/backend/NuDBFactory.cpp # src/libxrpl/nodestore/backend/RocksDBFactory.cpp # src/libxrpl/telemetry/NullTelemetry.cpp # src/libxrpl/telemetry/Telemetry.cpp # src/libxrpl/telemetry/TelemetryConfig.cpp # src/tests/libxrpl/basics/MallocTrim.cpp # src/tests/libxrpl/telemetry/TelemetryConfig.cpp # src/xrpld/app/consensus/RCLConsensus.cpp # src/xrpld/app/consensus/RCLConsensus.h # src/xrpld/app/ledger/detail/BuildLedger.cpp # src/xrpld/app/ledger/detail/LedgerMaster.cpp # src/xrpld/app/main/Application.cpp # src/xrpld/app/misc/NetworkOPs.cpp # src/xrpld/consensus/Consensus.h # src/xrpld/overlay/detail/PeerImp.cpp # src/xrpld/rpc/detail/RPCHandler.cpp # src/xrpld/rpc/detail/ServerHandler.cpp
202 lines
5.2 KiB
C++
202 lines
5.2 KiB
C++
#include <xrpl/nodestore/detail/DatabaseRotatingImp.h>
|
|
|
|
#include <xrpl/basics/BasicConfig.h>
|
|
#include <xrpl/basics/Blob.h>
|
|
#include <xrpl/basics/Log.h>
|
|
#include <xrpl/basics/base_uint.h>
|
|
#include <xrpl/basics/contract.h>
|
|
#include <xrpl/beast/utility/Journal.h>
|
|
#include <xrpl/nodestore/Backend.h>
|
|
#include <xrpl/nodestore/Database.h>
|
|
#include <xrpl/nodestore/DatabaseRotating.h>
|
|
#include <xrpl/nodestore/NodeObject.h>
|
|
#include <xrpl/nodestore/Scheduler.h>
|
|
#include <xrpl/nodestore/Types.h>
|
|
|
|
#include <cstdint>
|
|
#include <exception>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace xrpl::NodeStore {
|
|
|
|
DatabaseRotatingImp::DatabaseRotatingImp(
|
|
Scheduler& scheduler,
|
|
int readThreads,
|
|
std::shared_ptr<Backend> writableBackend,
|
|
std::shared_ptr<Backend> archiveBackend,
|
|
Section const& config,
|
|
beast::Journal j)
|
|
: DatabaseRotating(scheduler, readThreads, config, j)
|
|
, writableBackend_(std::move(writableBackend))
|
|
, archiveBackend_(std::move(archiveBackend))
|
|
{
|
|
if (writableBackend_)
|
|
fdRequired_ += writableBackend_->fdRequired();
|
|
if (archiveBackend_)
|
|
fdRequired_ += archiveBackend_->fdRequired();
|
|
}
|
|
|
|
void
|
|
DatabaseRotatingImp::rotate(
|
|
std::unique_ptr<NodeStore::Backend>&& newBackend,
|
|
std::function<void(std::string const& writableName, std::string const& archiveName)> const& f)
|
|
{
|
|
// Pass these two names to the callback function
|
|
std::string const newWritableBackendName = newBackend->getName();
|
|
std::string newArchiveBackendName;
|
|
// Hold on to current archive backend pointer until after the
|
|
// callback finishes. Only then will the archive directory be
|
|
// deleted.
|
|
std::shared_ptr<NodeStore::Backend> oldArchiveBackend;
|
|
{
|
|
std::lock_guard const lock(mutex_);
|
|
|
|
archiveBackend_->setDeletePath();
|
|
oldArchiveBackend = std::move(archiveBackend_);
|
|
|
|
archiveBackend_ = std::move(writableBackend_);
|
|
newArchiveBackendName = archiveBackend_->getName();
|
|
|
|
writableBackend_ = std::move(newBackend);
|
|
}
|
|
|
|
f(newWritableBackendName, newArchiveBackendName);
|
|
}
|
|
|
|
std::string
|
|
DatabaseRotatingImp::getName() const
|
|
{
|
|
std::lock_guard const lock(mutex_);
|
|
return writableBackend_->getName();
|
|
}
|
|
|
|
std::int32_t
|
|
DatabaseRotatingImp::getWriteLoad() const
|
|
{
|
|
std::lock_guard const lock(mutex_);
|
|
return writableBackend_->getWriteLoad();
|
|
}
|
|
|
|
void
|
|
DatabaseRotatingImp::importDatabase(Database& source)
|
|
{
|
|
auto const backend = [&] {
|
|
std::lock_guard const lock(mutex_);
|
|
return writableBackend_;
|
|
}();
|
|
|
|
importInternal(*backend, source);
|
|
}
|
|
|
|
void
|
|
DatabaseRotatingImp::sync()
|
|
{
|
|
std::lock_guard const lock(mutex_);
|
|
writableBackend_->sync();
|
|
}
|
|
|
|
void
|
|
DatabaseRotatingImp::store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t)
|
|
{
|
|
auto nObj = NodeObject::createObject(type, std::move(data), hash);
|
|
|
|
auto const backend = [&] {
|
|
std::lock_guard const lock(mutex_);
|
|
return writableBackend_;
|
|
}();
|
|
|
|
backend->store(nObj);
|
|
storeStats(1, nObj->getData().size());
|
|
}
|
|
|
|
std::shared_ptr<NodeObject>
|
|
DatabaseRotatingImp::fetchNodeObject(
|
|
uint256 const& hash,
|
|
std::uint32_t,
|
|
FetchReport& fetchReport,
|
|
bool duplicate)
|
|
{
|
|
auto fetch = [&](std::shared_ptr<Backend> const& backend) {
|
|
Status status = ok;
|
|
std::shared_ptr<NodeObject> nodeObject;
|
|
try
|
|
{
|
|
status = backend->fetch(hash.data(), &nodeObject);
|
|
}
|
|
catch (std::exception const& e)
|
|
{
|
|
JLOG(j_.fatal()) << "Exception, " << e.what();
|
|
Rethrow();
|
|
}
|
|
|
|
switch (status)
|
|
{
|
|
case ok:
|
|
case notFound:
|
|
break;
|
|
case dataCorrupt:
|
|
JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash;
|
|
break;
|
|
default:
|
|
JLOG(j_.warn()) << "Unknown status=" << status;
|
|
break;
|
|
}
|
|
|
|
return nodeObject;
|
|
};
|
|
|
|
// See if the node object exists in the cache
|
|
std::shared_ptr<NodeObject> nodeObject;
|
|
|
|
auto [writable, archive] = [&] {
|
|
std::lock_guard const lock(mutex_);
|
|
return std::make_pair(writableBackend_, archiveBackend_);
|
|
}();
|
|
|
|
// Try to fetch from the writable backend
|
|
nodeObject = fetch(writable);
|
|
if (!nodeObject)
|
|
{
|
|
// Otherwise try to fetch from the archive backend
|
|
nodeObject = fetch(archive);
|
|
if (nodeObject)
|
|
{
|
|
{
|
|
// Refresh the writable backend pointer
|
|
std::lock_guard const lock(mutex_);
|
|
writable = writableBackend_;
|
|
}
|
|
|
|
// Update writable backend with data from the archive backend
|
|
if (duplicate)
|
|
writable->store(nodeObject);
|
|
}
|
|
}
|
|
|
|
if (nodeObject)
|
|
fetchReport.wasFound = true;
|
|
|
|
return nodeObject;
|
|
}
|
|
|
|
void
|
|
DatabaseRotatingImp::for_each(std::function<void(std::shared_ptr<NodeObject>)> f)
|
|
{
|
|
auto [writable, archive] = [&] {
|
|
std::lock_guard const lock(mutex_);
|
|
return std::make_pair(writableBackend_, archiveBackend_);
|
|
}();
|
|
|
|
// Iterate the writable backend
|
|
writable->for_each(f);
|
|
|
|
// Iterate the archive backend
|
|
archive->for_each(f);
|
|
}
|
|
|
|
} // namespace xrpl::NodeStore
|