Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Scheduling.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 "etl/Models.hpp"
24#include "etl/SchedulerInterface.hpp"
25
26#include <sys/types.h>
27
28#include <atomic>
29#include <cstdint>
30#include <functional>
31#include <limits>
32#include <memory>
33#include <optional>
34#include <tuple>
35#include <type_traits>
36#include <utility>
37
38namespace etl::impl {
39
40template <typename T>
41concept SomeScheduler = std::is_base_of_v<SchedulerInterface, std::decay_t<T>>;
42
43class ForwardScheduler : public SchedulerInterface {
44 std::reference_wrapper<NetworkValidatedLedgersInterface> ledgers_;
45
46 uint32_t startSeq_;
47 std::optional<uint32_t> maxSeq_;
48 std::atomic_uint32_t seq_;
49
50public:
51 ForwardScheduler(ForwardScheduler const& other)
52 : ledgers_(other.ledgers_)
53 , startSeq_(other.startSeq_)
54 , maxSeq_(other.maxSeq_)
55 , seq_(other.seq_.load())
56 {
57 }
58
59 ForwardScheduler(
60 std::reference_wrapper<NetworkValidatedLedgersInterface> ledgers,
61 uint32_t startSeq,
62 std::optional<uint32_t> maxSeq = std::nullopt
63 )
64 : ledgers_(ledgers), startSeq_(startSeq), maxSeq_(maxSeq), seq_(startSeq)
65 {
66 }
67
68 [[nodiscard]] std::optional<model::Task>
69 next() override
70 {
71 static constexpr auto kMAX = std::numeric_limits<uint32_t>::max();
72 uint32_t currentSeq = seq_;
73
74 if (ledgers_.get().getMostRecent() >= currentSeq) {
75 while (currentSeq < maxSeq_.value_or(kMAX)) {
76 if (seq_.compare_exchange_weak(
77 currentSeq, currentSeq + 1u, std::memory_order_acq_rel
78 )) {
79 return {{.priority = model::Task::Priority::Higher, .seq = currentSeq}};
80 }
81 }
82 }
83
84 return std::nullopt;
85 }
86};
87
88class BackfillScheduler : public SchedulerInterface {
89 uint32_t startSeq_;
90 uint32_t minSeq_ = 0u;
91
92 std::atomic_uint32_t seq_;
93
94public:
95 BackfillScheduler(BackfillScheduler const& other)
96 : startSeq_(other.startSeq_), minSeq_(other.minSeq_), seq_(other.seq_.load())
97 {
98 }
99
100 BackfillScheduler(uint32_t startSeq, std::optional<uint32_t> minSeq = std::nullopt)
101 : startSeq_(startSeq), minSeq_(minSeq.value_or(0)), seq_(startSeq)
102 {
103 }
104
105 [[nodiscard]] std::optional<model::Task>
106 next() override
107 {
108 uint32_t currentSeq = seq_;
109 while (currentSeq > minSeq_) {
110 if (seq_.compare_exchange_weak(
111 currentSeq, currentSeq - 1u, std::memory_order_acq_rel
112 )) {
113 return {{.priority = model::Task::Priority::Lower, .seq = currentSeq}};
114 }
115 }
116
117 return std::nullopt;
118 }
119};
120
121template <SomeScheduler... Schedulers>
122class SchedulerChain : public SchedulerInterface {
123 std::tuple<Schedulers...> schedulers_;
124
125public:
126 template <SomeScheduler... Ts>
127 requires(std::is_same_v<Ts, Schedulers> and ...)
128 SchedulerChain(Ts&&... schedulers) : schedulers_(std::forward<Ts>(schedulers)...)
129 {
130 }
131
132 [[nodiscard]] std::optional<model::Task>
133 next() override
134 {
135 std::optional<model::Task> task;
136 auto const expand = [&](auto& s) {
137 if (task.has_value())
138 return false;
139
140 task = s.next();
141 return task.has_value();
142 };
143
144 std::apply([&expand](auto&&... xs) { (... || expand(xs)); }, schedulers_);
145
146 return task;
147 }
148};
149
150static auto
151makeScheduler(SomeScheduler auto&&... schedulers)
152{
153 return std::make_unique<SchedulerChain<std::decay_t<decltype(schedulers)>...>>(
154 std::forward<decltype(schedulers)>(schedulers)...
155 );
156}
157
158} // namespace etl::impl
std::optional< model::Task > next() override
Attempt to obtain the next task.
Definition Scheduling.hpp:106
std::optional< model::Task > next() override
Attempt to obtain the next task.
Definition Scheduling.hpp:69
std::optional< model::Task > next() override
Attempt to obtain the next task.
Definition Scheduling.hpp:133
Definition Scheduling.hpp:41
The interface of a scheduler for the extraction process.
Definition SchedulerInterface.hpp:31