forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLedgerReplayer.cpp
288 lines (259 loc) · 9.08 KB
/
LedgerReplayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2020 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <xrpld/app/ledger/LedgerReplayer.h>
#include <xrpld/app/ledger/detail/LedgerDeltaAcquire.h>
#include <xrpld/app/ledger/detail/SkipListAcquire.h>
#include <xrpld/core/JobQueue.h>
namespace ripple {
LedgerReplayer::LedgerReplayer(
Application& app,
InboundLedgers& inboundLedgers,
std::unique_ptr<PeerSetBuilder> peerSetBuilder)
: app_(app)
, inboundLedgers_(inboundLedgers)
, peerSetBuilder_(std::move(peerSetBuilder))
, j_(app.journal("LedgerReplayer"))
{
}
LedgerReplayer::~LedgerReplayer()
{
std::lock_guard<std::mutex> lock(mtx_);
tasks_.clear();
}
void
LedgerReplayer::replay(
InboundLedger::Reason r,
uint256 const& finishLedgerHash,
std::uint32_t totalNumLedgers)
{
assert(
finishLedgerHash.isNonZero() && totalNumLedgers > 0 &&
totalNumLedgers <= LedgerReplayParameters::MAX_TASK_SIZE);
LedgerReplayTask::TaskParameter parameter(
r, finishLedgerHash, totalNumLedgers);
std::shared_ptr<LedgerReplayTask> task;
std::shared_ptr<SkipListAcquire> skipList;
bool newSkipList = false;
{
std::lock_guard<std::mutex> lock(mtx_);
if (app_.isStopping())
return;
if (tasks_.size() >= LedgerReplayParameters::MAX_TASKS)
{
JLOG(j_.info()) << "Too many replay tasks, dropping new task "
<< parameter.finishHash_;
return;
}
for (auto const& t : tasks_)
{
if (parameter.canMergeInto(t->getTaskParameter()))
{
JLOG(j_.info()) << "Task " << parameter.finishHash_ << " with "
<< totalNumLedgers
<< " ledgers merged into an existing task.";
return;
}
}
JLOG(j_.info()) << "Replay " << totalNumLedgers
<< " ledgers. Finish ledger hash "
<< parameter.finishHash_;
auto i = skipLists_.find(parameter.finishHash_);
if (i != skipLists_.end())
skipList = i->second.lock();
if (!skipList) // cannot find, or found but cannot lock
{
skipList = std::make_shared<SkipListAcquire>(
app_,
inboundLedgers_,
parameter.finishHash_,
peerSetBuilder_->build());
skipLists_[parameter.finishHash_] = skipList;
newSkipList = true;
}
task = std::make_shared<LedgerReplayTask>(
app_, inboundLedgers_, *this, skipList, std::move(parameter));
tasks_.push_back(task);
}
if (newSkipList)
skipList->init(1);
// task init after skipList init, could save a timeout
task->init();
}
void
LedgerReplayer::createDeltas(std::shared_ptr<LedgerReplayTask> task)
{
{
// TODO for use cases like Consensus (i.e. totalLedgers = 1 or small):
// check if the last closed or validated ledger l the local node has
// is in the skip list and is an ancestor of parameter.startLedger
// that has to be downloaded, if so expand the task to start with l.
}
auto const& parameter = task->getTaskParameter();
JLOG(j_.trace()) << "Creating " << parameter.totalLedgers_ - 1 << " deltas";
if (parameter.totalLedgers_ > 1)
{
auto skipListItem = std::find(
parameter.skipList_.begin(),
parameter.skipList_.end(),
parameter.startHash_);
if (skipListItem == parameter.skipList_.end() ||
++skipListItem == parameter.skipList_.end())
{
JLOG(j_.error()) << "Task parameter error when creating deltas "
<< parameter.finishHash_;
return;
}
for (std::uint32_t seq = parameter.startSeq_ + 1;
seq <= parameter.finishSeq_ &&
skipListItem != parameter.skipList_.end();
++seq, ++skipListItem)
{
std::shared_ptr<LedgerDeltaAcquire> delta;
bool newDelta = false;
{
std::lock_guard<std::mutex> lock(mtx_);
if (app_.isStopping())
return;
auto i = deltas_.find(*skipListItem);
if (i != deltas_.end())
delta = i->second.lock();
if (!delta) // cannot find, or found but cannot lock
{
delta = std::make_shared<LedgerDeltaAcquire>(
app_,
inboundLedgers_,
*skipListItem,
seq,
peerSetBuilder_->build());
deltas_[*skipListItem] = delta;
newDelta = true;
}
}
task->addDelta(delta);
if (newDelta)
delta->init(1);
}
}
}
void
LedgerReplayer::gotSkipList(
LedgerInfo const& info,
boost::intrusive_ptr<SHAMapItem const> const& item)
{
std::shared_ptr<SkipListAcquire> skipList = {};
{
std::lock_guard<std::mutex> lock(mtx_);
auto i = skipLists_.find(info.hash);
if (i == skipLists_.end())
return;
skipList = i->second.lock();
if (!skipList)
{
skipLists_.erase(i);
return;
}
}
if (skipList)
skipList->processData(info.seq, item);
}
void
LedgerReplayer::gotReplayDelta(
LedgerInfo const& info,
std::map<std::uint32_t, std::shared_ptr<STTx const>>&& txns)
{
std::shared_ptr<LedgerDeltaAcquire> delta = {};
{
std::lock_guard<std::mutex> lock(mtx_);
auto i = deltas_.find(info.hash);
if (i == deltas_.end())
return;
delta = i->second.lock();
if (!delta)
{
deltas_.erase(i);
return;
}
}
if (delta)
delta->processData(info, std::move(txns));
}
void
LedgerReplayer::sweep()
{
auto const start = std::chrono::steady_clock::now();
{
std::lock_guard<std::mutex> lock(mtx_);
JLOG(j_.debug()) << "Sweeping, LedgerReplayer has " << tasks_.size()
<< " tasks, " << skipLists_.size()
<< " skipLists, and " << deltas_.size() << " deltas.";
tasks_.erase(
std::remove_if(
tasks_.begin(),
tasks_.end(),
[this](auto const& t) -> bool {
if (t->finished())
{
JLOG(j_.debug()) << "Sweep task "
<< t->getTaskParameter().finishHash_;
return true;
}
return false;
}),
tasks_.end());
auto removeCannotLocked = [](auto& subTasks) {
for (auto it = subTasks.begin(); it != subTasks.end();)
{
if (auto item = it->second.lock(); !item)
{
it = subTasks.erase(it);
}
else
++it;
}
};
removeCannotLocked(skipLists_);
removeCannotLocked(deltas_);
}
JLOG(j_.debug()) << " LedgerReplayer sweep lock duration "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count()
<< "ms";
}
void
LedgerReplayer::stop()
{
JLOG(j_.info()) << "Stopping...";
{
std::lock_guard<std::mutex> lock(mtx_);
std::for_each(
tasks_.begin(), tasks_.end(), [](auto& i) { i->cancel(); });
tasks_.clear();
auto lockAndCancel = [](auto& i) {
if (auto sptr = i.second.lock(); sptr)
{
sptr->cancel();
}
};
std::for_each(skipLists_.begin(), skipLists_.end(), lockAndCancel);
skipLists_.clear();
std::for_each(deltas_.begin(), deltas_.end(), lockAndCancel);
deltas_.clear();
}
JLOG(j_.info()) << "Stopped";
}
} // namespace ripple