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_(std::max(
78 , skipListAcquirer_(skipListAcquirer)
79{
80 JLOG(journal_.trace()) << "Create " << hash_;
81}
82
84{
85 JLOG(journal_.trace()) << "Destroy " << hash_;
86}
87
88void
90{
91 JLOG(journal_.debug()) << "Task start " << hash_;
92
94 skipListAcquirer_->addDataCallback([wptr](bool good, uint256 const& hash) {
95 if (auto sptr = wptr.lock(); sptr)
96 {
97 if (!good)
98 {
99 sptr->cancel();
100 }
101 else
102 {
103 auto const skipListData = sptr->skipListAcquirer_->getData();
104 sptr->updateSkipList(hash, skipListData->ledgerSeq, skipListData->skipList);
105 }
106 }
107 });
108
110 if (!isDone())
111 {
112 trigger(sl);
113 setTimer(sl);
114 }
115}
116
117void
119{
120 JLOG(journal_.trace()) << "trigger " << hash_;
121 if (!parameter_.full_)
122 return;
123
124 if (!parent_)
125 {
127 if (!parent_)
128 {
129 parent_ =
131 }
132 if (parent_)
133 {
134 JLOG(journal_.trace()) << "Got start ledger " << parameter_.startHash_ << " for task " << hash_;
135 }
136 }
137
138 tryAdvance(sl);
139}
140
141void
143{
144 JLOG(journal_.trace()) << "Delta " << deltaHash << " ready for task " << hash_;
146 if (!isDone())
147 tryAdvance(sl);
148}
149
150void
152{
153 JLOG(journal_.trace()) << "tryAdvance task " << hash_
154 << (parameter_.full_ ? ", full parameter" : ", waiting to fill parameter")
155 << ", deltaIndex=" << deltaToBuild_ << ", totalDeltas=" << deltas_.size() << ", parent "
156 << (parent_ ? parent_->header().hash : uint256());
157
158 bool shouldTry = parent_ && parameter_.full_ && parameter_.totalLedgers_ - 1 == deltas_.size();
159 if (!shouldTry)
160 return;
161
162 try
163 {
164 for (; deltaToBuild_ < deltas_.size(); ++deltaToBuild_)
165 {
166 auto& delta = deltas_[deltaToBuild_];
167 XRPL_ASSERT(
168 parent_->seq() + 1 == delta->ledgerSeq_, "xrpl::LedgerReplayTask::tryAdvance : consecutive sequence");
169 if (auto l = delta->tryBuild(parent_); l)
170 {
171 JLOG(journal_.debug()) << "Task " << hash_ << " got ledger " << l->header().hash
172 << " deltaIndex=" << deltaToBuild_ << " totalDeltas=" << deltas_.size();
173 parent_ = l;
174 }
175 else
176 return;
177 }
178
179 complete_ = true;
180 JLOG(journal_.info()) << "Completed " << hash_;
181 }
182 catch (std::runtime_error const&)
183 {
184 failed_ = true;
185 }
186}
187
188void
190{
191 {
193 if (isDone())
194 return;
195 if (!parameter_.update(hash, seq, sList))
196 {
197 JLOG(journal_.error()) << "Parameter update failed " << hash_;
198 failed_ = true;
199 return;
200 }
201 }
202
205 if (!isDone())
206 trigger(sl);
207}
208
209void
211{
212 JLOG(journal_.trace()) << "mTimeouts=" << timeouts_ << " for " << hash_;
214 {
215 failed_ = true;
216 JLOG(journal_.debug()) << "LedgerReplayTask Failed, too many timeouts " << hash_;
217 }
218 else
219 {
220 trigger(sl);
221 }
222}
223
229
230void
232{
234 delta->addDataCallback(parameter_.reason_, [wptr](bool good, uint256 const& hash) {
235 if (auto sptr = wptr.lock(); sptr)
236 {
237 if (!good)
238 sptr->cancel();
239 else
240 sptr->deltaReady(hash);
241 }
242 });
243
244 ScopedLockType sl(mtx_);
245 if (!isDone())
246 {
247 JLOG(journal_.trace()) << "addDelta task " << hash_ << " deltaIndex=" << deltaToBuild_
248 << " totalDeltas=" << deltas_.size();
249 XRPL_ASSERT(
250 deltas_.empty() || deltas_.back()->ledgerSeq_ + 1 == delta->ledgerSeq_,
251 "xrpl::LedgerReplayTask::addDelta : no deltas or consecutive "
252 "sequence");
253 deltas_.push_back(delta);
254 }
255}
256
257bool
258LedgerReplayTask::finished() const
259{
260 ScopedLockType sl(mtx_);
261 return isDone();
262}
263
264} // 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)