rippled
Loading...
Searching...
No Matches
LedgerReplayTask.cpp
1#include <xrpld/app/ledger/InboundLedgers.h>
2#include <xrpld/app/ledger/LedgerReplayTask.h>
3#include <xrpld/app/ledger/LedgerReplayer.h>
4#include <xrpld/app/ledger/detail/LedgerDeltaAcquire.h>
5#include <xrpld/app/ledger/detail/SkipListAcquire.h>
6
7namespace xrpl {
8
11 uint256 const& finishLedgerHash,
12 std::uint32_t totalNumLedgers)
13 : reason_(r), finishHash_(finishLedgerHash), totalLedgers_(totalNumLedgers)
14{
15 XRPL_ASSERT(
16 finishLedgerHash.isNonZero() && totalNumLedgers > 0,
17 "xrpl::LedgerReplayTask::TaskParameter::TaskParameter : valid "
18 "inputs");
19}
20
21bool
23{
24 if (finishHash_ != hash || sList.size() + 1 < totalLedgers_ || full_)
25 return false;
26
27 finishSeq_ = seq;
28 skipList_ = sList;
29 skipList_.emplace_back(finishHash_);
30 startHash_ = skipList_[skipList_.size() - totalLedgers_];
31 XRPL_ASSERT(startHash_.isNonZero(), "xrpl::LedgerReplayTask::TaskParameter::update : nonzero start hash");
32 startSeq_ = finishSeq_ - totalLedgers_ + 1;
33 full_ = true;
34 return true;
35}
36
37bool
39{
40 if (reason_ == existingTask.reason_)
41 {
42 if (finishHash_ == existingTask.finishHash_ && totalLedgers_ <= existingTask.totalLedgers_)
43 {
44 return true;
45 }
46
47 if (existingTask.full_)
48 {
49 auto const& exList = existingTask.skipList_;
50 if (auto i = std::find(exList.begin(), exList.end(), finishHash_); i != exList.end())
51 {
52 return existingTask.totalLedgers_ >= totalLedgers_ + (exList.end() - i) - 1;
53 }
54 }
55 }
56
57 return false;
58}
59
61 Application& app,
62 InboundLedgers& inboundLedgers,
63 LedgerReplayer& replayer,
64 std::shared_ptr<SkipListAcquire>& skipListAcquirer,
65 TaskParameter&& parameter)
67 app,
68 parameter.finishHash_,
69 LedgerReplayParameters::TASK_TIMEOUT,
71 app.journal("LedgerReplayTask"))
72 , inboundLedgers_(inboundLedgers)
73 , replayer_(replayer)
74 , parameter_(parameter)
75 , maxTimeouts_(
79 , skipListAcquirer_(skipListAcquirer)
80{
81 JLOG(journal_.trace()) << "Create " << hash_;
82}
83
85{
86 JLOG(journal_.trace()) << "Destroy " << hash_;
87}
88
89void
91{
92 JLOG(journal_.debug()) << "Task start " << hash_;
93
95 skipListAcquirer_->addDataCallback([wptr](bool good, uint256 const& hash) {
96 if (auto sptr = wptr.lock(); sptr)
97 {
98 if (!good)
99 {
100 sptr->cancel();
101 }
102 else
103 {
104 auto const skipListData = sptr->skipListAcquirer_->getData();
105 sptr->updateSkipList(hash, skipListData->ledgerSeq, skipListData->skipList);
106 }
107 }
108 });
109
111 if (!isDone())
112 {
113 trigger(sl);
114 setTimer(sl);
115 }
116}
117
118void
120{
121 JLOG(journal_.trace()) << "trigger " << hash_;
122 if (!parameter_.full_)
123 return;
124
125 if (!parent_)
126 {
128 if (!parent_)
129 {
130 parent_ =
132 }
133 if (parent_)
134 {
135 JLOG(journal_.trace()) << "Got start ledger " << parameter_.startHash_ << " for task " << hash_;
136 }
137 }
138
139 tryAdvance(sl);
140}
141
142void
144{
145 JLOG(journal_.trace()) << "Delta " << deltaHash << " ready for task " << hash_;
147 if (!isDone())
148 tryAdvance(sl);
149}
150
151void
153{
154 JLOG(journal_.trace()) << "tryAdvance task " << hash_
155 << (parameter_.full_ ? ", full parameter" : ", waiting to fill parameter")
156 << ", deltaIndex=" << deltaToBuild_ << ", totalDeltas=" << deltas_.size() << ", parent "
157 << (parent_ ? parent_->header().hash : uint256());
158
159 bool shouldTry = parent_ && parameter_.full_ && parameter_.totalLedgers_ - 1 == deltas_.size();
160 if (!shouldTry)
161 return;
162
163 try
164 {
165 for (; deltaToBuild_ < deltas_.size(); ++deltaToBuild_)
166 {
167 auto& delta = deltas_[deltaToBuild_];
168 XRPL_ASSERT(
169 parent_->seq() + 1 == delta->ledgerSeq_, "xrpl::LedgerReplayTask::tryAdvance : consecutive sequence");
170 if (auto l = delta->tryBuild(parent_); l)
171 {
172 JLOG(journal_.debug()) << "Task " << hash_ << " got ledger " << l->header().hash
173 << " deltaIndex=" << deltaToBuild_ << " totalDeltas=" << deltas_.size();
174 parent_ = l;
175 }
176 else
177 return;
178 }
179
180 complete_ = true;
181 JLOG(journal_.info()) << "Completed " << hash_;
182 }
183 catch (std::runtime_error const&)
184 {
185 failed_ = true;
186 }
187}
188
189void
191{
192 {
194 if (isDone())
195 return;
196 if (!parameter_.update(hash, seq, sList))
197 {
198 JLOG(journal_.error()) << "Parameter update failed " << hash_;
199 failed_ = true;
200 return;
201 }
202 }
203
206 if (!isDone())
207 trigger(sl);
208}
209
210void
212{
213 JLOG(journal_.trace()) << "mTimeouts=" << timeouts_ << " for " << hash_;
215 {
216 failed_ = true;
217 JLOG(journal_.debug()) << "LedgerReplayTask Failed, too many timeouts " << hash_;
218 }
219 else
220 {
221 trigger(sl);
222 }
223}
224
230
231void
233{
235 delta->addDataCallback(parameter_.reason_, [wptr](bool good, uint256 const& hash) {
236 if (auto sptr = wptr.lock(); sptr)
237 {
238 if (!good)
239 sptr->cancel();
240 else
241 sptr->deltaReady(hash);
242 }
243 });
244
245 ScopedLockType sl(mtx_);
246 if (!isDone())
247 {
248 JLOG(journal_.trace()) << "addDelta task " << hash_ << " deltaIndex=" << deltaToBuild_
249 << " totalDeltas=" << deltas_.size();
250 XRPL_ASSERT(
251 deltas_.empty() || deltas_.back()->ledgerSeq_ + 1 == delta->ledgerSeq_,
252 "xrpl::LedgerReplayTask::addDelta : no deltas or consecutive "
253 "sequence");
254 deltas_.push_back(delta);
255 }
256}
257
258bool
259LedgerReplayTask::finished() const
260{
261 ScopedLockType sl(mtx_);
262 return isDone();
263}
264
265} // namespace xrpl
Stream error() const
Definition Journal.h:318
Stream debug() const
Definition Journal.h:300
Stream info() const
Definition Journal.h:306
Stream trace() const
Severity stream access functions.
Definition Journal.h:294
Manages the lifetime of inbound ledgers.
virtual std::shared_ptr< Ledger const > acquire(uint256 const &hash, std::uint32_t seq, InboundLedger::Reason)=0
std::shared_ptr< Ledger const > getLedgerByHash(uint256 const &hash)
TaskParameter(InboundLedger::Reason r, uint256 const &finishLedgerHash, std::uint32_t totalNumLedgers)
constructor
bool update(uint256 const &hash, std::uint32_t seq, std::vector< uint256 > const &sList)
fill all the fields that was not filled during construction
bool canMergeInto(TaskParameter const &existingTask) const
check if this task can be merged into an existing task
void trigger(ScopedLockType &sl)
Trigger another round.
void onTimer(bool progress, ScopedLockType &sl) override
Hook called from invokeOnTimer().
LedgerReplayer & replayer_
LedgerReplayTask(Application &app, InboundLedgers &inboundLedgers, LedgerReplayer &replayer, std::shared_ptr< SkipListAcquire > &skipListAcquirer, TaskParameter &&parameter)
Constructor.
InboundLedgers & inboundLedgers_
void updateSkipList(uint256 const &hash, std::uint32_t seq, std::vector< uint256 > const &sList)
Update this task (by a SkipListAcquire subtask) when skip list is ready.
void tryAdvance(ScopedLockType &sl)
Try to build more ledgers.
void deltaReady(uint256 const &deltaHash)
Notify this task (by a LedgerDeltaAcquire subtask) that a delta is ready.
std::weak_ptr< TimeoutCounter > pmDowncast() override
Return a weak pointer to this.
std::vector< std::shared_ptr< LedgerDeltaAcquire > > deltas_
void addDelta(std::shared_ptr< LedgerDeltaAcquire > const &delta)
add a new LedgerDeltaAcquire subtask
std::shared_ptr< Ledger const > parent_
std::shared_ptr< SkipListAcquire > skipListAcquirer_
void init()
Start the task.
Manages the lifetime of ledger replay tasks.
void createDeltas(std::shared_ptr< LedgerReplayTask > task)
Create LedgerDeltaAcquire subtasks for the LedgerReplayTask task.
virtual LedgerMaster & getLedgerMaster()=0
This class is an "active" object.
std::recursive_mutex mtx_
uint256 const hash_
The hash of the object (in practice, always a ledger) we are trying to fetch.
beast::Journal journal_
void setTimer(ScopedLockType &)
Schedule a call to queueJob() after mTimerInterval.
static constexpr std::size_t size()
Definition base_uint.h:494
bool isNonZero() const
Definition base_uint.h:513
T emplace_back(T... args)
T find(T... args)
T lock(T... args)
T max(T... args)
std::uint32_t constexpr TASK_MAX_TIMEOUTS_MINIMUM
std::uint32_t constexpr TASK_MAX_TIMEOUTS_MULTIPLIER
std::uint32_t constexpr MAX_QUEUED_TASKS
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
base_uint< 256 > uint256
Definition base_uint.h:526
@ jtREPLAY_TASK
Definition Job.h:40
T size(T... args)