Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
WriterState.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2025, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/LedgerCacheInterface.hpp"
23#include "etl/SystemState.hpp"
24#include "util/prometheus/Bool.hpp"
25#include "util/prometheus/Label.hpp"
26#include "util/prometheus/Prometheus.hpp"
27
28#include <functional>
29#include <memory>
30
31namespace etl {
32
41public:
42 virtual ~WriterStateInterface() = default;
43
48 [[nodiscard]] virtual bool
49 isReadOnly() const = 0;
50
55 [[nodiscard]] virtual bool
56 isWriting() const = 0;
57
64 virtual void
66
73 virtual void
75
81 [[nodiscard]] virtual bool
82 isFallback() const = 0;
83
94 [[nodiscard]] virtual bool
95 isFallbackRecovery() const = 0;
96
111 virtual void
112 setFallbackRecovery(bool newValue) = 0;
113
125 virtual void
127
133 [[nodiscard]] virtual bool
134 isEtlStarted() const = 0;
135
141 [[nodiscard]] virtual bool
142 isCacheFull() const = 0;
143
153 [[nodiscard]] virtual std::unique_ptr<WriterStateInterface>
154 clone() const = 0;
155};
156
165private:
166 std::shared_ptr<SystemState>
167 systemState_;
168 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
169
178 util::prometheus::Bool isFallbackRecovery_ = PrometheusService::boolMetric(
179 "etl_writing_deciding_fallback_recovery",
181 "Whether clio is in recovery from the fallback writer decision mechanism"
182 );
183
184public:
191 WriterState(std::shared_ptr<SystemState> state, data::LedgerCacheInterface const& cache);
192
193 bool
194 isReadOnly() const override;
195
200 bool
201 isWriting() const override;
202
209 void
210 startWriting() override;
211
218 void
219 giveUpWriting() override;
220
227 void
228 setWriterDecidingFallback() override;
229
235 bool
236 isFallback() const override;
237
239 bool
240 isFallbackRecovery() const override;
241
243 void
244 setFallbackRecovery(bool newValue) override;
245
247 bool
248 isEtlStarted() const override;
249
251 bool
252 isCacheFull() const override;
253
261 std::unique_ptr<WriterStateInterface>
262 clone() const override;
263};
264
265} // namespace etl
static util::prometheus::Bool boolMetric(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get a bool based metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:220
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:40
Interface for managing writer state in the ETL subsystem.
Definition WriterState.hpp:40
virtual void setWriterDecidingFallback()=0
Switch the cluster to the fallback writer decision mechanism.
virtual bool isEtlStarted() const =0
Whether the ETL monitor has started and the node is ready to become a writer.
virtual bool isFallback() const =0
Check if the cluster is using the fallback writer decision mechanism.
virtual void giveUpWriting()=0
Request to stop writing to the database.
virtual bool isWriting() const =0
Check if the ETL process is currently writing to the database.
virtual std::unique_ptr< WriterStateInterface > clone() const =0
Create a clone of this writer state.
virtual bool isFallbackRecovery() const =0
Check if this node is in fallback recovery mode.
virtual void setFallbackRecovery(bool newValue)=0
Set or clear the fallback recovery flag.
virtual void startWriting()=0
Request to start writing to the database.
virtual bool isReadOnly() const =0
Check if the ETL process is in strict read-only mode.
virtual bool isCacheFull() const =0
Whether the ledger cache is fully loaded.
WriterState(std::shared_ptr< SystemState > state, data::LedgerCacheInterface const &cache)
Construct a WriterState with the given system state and cache.
Definition WriterState.cpp:30
bool isFallbackRecovery() const override
Check if this node is in fallback recovery mode.
Definition WriterState.cpp:82
bool isFallback() const override
Check if the cluster is using the fallback writer decision mechanism.
Definition WriterState.cpp:76
void startWriting() override
Request to start writing to the database.
Definition WriterState.cpp:51
void setFallbackRecovery(bool newValue) override
Set or clear the fallback recovery flag.
Definition WriterState.cpp:88
bool isCacheFull() const override
Whether the ledger cache is fully loaded.
Definition WriterState.cpp:103
bool isEtlStarted() const override
Whether the ETL monitor has started and the node is ready to become a writer.
Definition WriterState.cpp:97
void giveUpWriting() override
Request to stop writing to the database.
Definition WriterState.cpp:60
void setWriterDecidingFallback() override
Switch the cluster to the fallback writer decision mechanism.
Definition WriterState.cpp:69
bool isWriting() const override
Check if the ETL process is currently writing to the database.
Definition WriterState.cpp:45
std::unique_ptr< WriterStateInterface > clone() const override
Create a clone of this writer state.
Definition WriterState.cpp:109
bool isReadOnly() const override
Check if the ETL process is in strict read-only mode.
Definition WriterState.cpp:39
Class representing a collection of Prometheus labels.
Definition Label.hpp:60