forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBucketApplicator.cpp
341 lines (311 loc) · 10.7 KB
/
BucketApplicator.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2015 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "util/asio.h"
#include "bucket/BucketApplicator.h"
#include "bucket/Bucket.h"
#include "bucket/BucketList.h"
#include "ledger/LedgerTxn.h"
#include "ledger/LedgerTxnEntry.h"
#include "main/Application.h"
#include "util/Logging.h"
#include "util/types.h"
#include <fmt/format.h>
namespace stellar
{
BucketApplicator::BucketApplicator(Application& app,
uint32_t maxProtocolVersion,
uint32_t minProtocolVersionSeen,
uint32_t level,
std::shared_ptr<Bucket const> bucket,
std::function<bool(LedgerEntryType)> filter,
std::unordered_set<LedgerKey>& seenKeys)
: mApp(app)
, mMaxProtocolVersion(maxProtocolVersion)
, mMinProtocolVersionSeen(minProtocolVersionSeen)
, mLevel(level)
, mBucketIter(bucket)
, mEntryTypeFilter(filter)
, mSeenKeys(seenKeys)
{
auto protocolVersion = mBucketIter.getMetadata().ledgerVersion;
if (protocolVersion > mMaxProtocolVersion)
{
throw std::runtime_error(fmt::format(
FMT_STRING(
"bucket protocol version {:d} exceeds maxProtocolVersion {:d}"),
protocolVersion, mMaxProtocolVersion));
}
// Only apply offers if BucketListDB is enabled
if (mApp.getConfig().isUsingBucketListDB() && !bucket->isEmpty())
{
auto offsetOp = bucket->getOfferRange();
if (offsetOp)
{
auto [lowOffset, highOffset] = *offsetOp;
mBucketIter.seek(lowOffset);
mUpperBoundOffset = highOffset;
}
else
{
// No offers in Bucket
mOffersRemaining = false;
}
}
}
BucketApplicator::operator bool() const
{
// There is more work to do (i.e. (bool) *this == true) iff:
// 1. The underlying bucket iterator is not EOF and
// 2. Either BucketListDB is not enabled (so we must apply all entry types)
// or BucketListDB is enabled and we have offers still remaining.
return static_cast<bool>(mBucketIter) &&
(!mApp.getConfig().isUsingBucketListDB() || mOffersRemaining);
}
size_t
BucketApplicator::pos()
{
return mBucketIter.pos();
}
size_t
BucketApplicator::size() const
{
return mBucketIter.size();
}
static bool
shouldApplyEntry(std::function<bool(LedgerEntryType)> const& filter,
BucketEntry const& e)
{
if (e.type() == LIVEENTRY || e.type() == INITENTRY)
{
return filter(e.liveEntry().data.type());
}
if (e.type() != DEADENTRY)
{
throw std::runtime_error(
"Malformed bucket: unexpected non-INIT/LIVE/DEAD entry.");
}
return filter(e.deadEntry().type());
}
size_t
BucketApplicator::advance(BucketApplicator::Counters& counters)
{
size_t count = 0;
auto& root = mApp.getLedgerTxnRoot();
AbstractLedgerTxn* ltx;
std::unique_ptr<LedgerTxn> innerLtx;
// when running in memory mode, make changes to the in memory ledger
// directly instead of creating a temporary inner LedgerTxn
// as "advance" commits changes during each step this does not introduce any
// new failure mode
if (mApp.getConfig().MODE_USES_IN_MEMORY_LEDGER)
{
ltx = static_cast<AbstractLedgerTxn*>(&root);
}
else
{
innerLtx = std::make_unique<LedgerTxn>(root, false);
ltx = innerLtx.get();
ltx->prepareNewObjects(LEDGER_ENTRY_BATCH_COMMIT_SIZE);
}
for (; mBucketIter; ++mBucketIter)
{
// Note: mUpperBoundOffset is not inclusive. However, mBucketIter.pos()
// returns the file offset at the end of the currently loaded entry.
// This means we must read until pos is strictly greater than the upper
// bound so that we don't skip the last offer in the range.
auto isUsingBucketListDB = mApp.getConfig().isUsingBucketListDB();
if (isUsingBucketListDB && mBucketIter.pos() > mUpperBoundOffset)
{
mOffersRemaining = false;
break;
}
BucketEntry const& e = *mBucketIter;
Bucket::checkProtocolLegality(e, mMaxProtocolVersion);
if (shouldApplyEntry(mEntryTypeFilter, e))
{
if (isUsingBucketListDB)
{
if (e.type() == LIVEENTRY || e.type() == INITENTRY)
{
auto [_, wasInserted] =
mSeenKeys.emplace(LedgerEntryKey(e.liveEntry()));
// Skip seen keys
if (!wasInserted)
{
continue;
}
}
else
{
// Only apply INIT and LIVE entries
mSeenKeys.emplace(e.deadEntry());
continue;
}
}
counters.mark(e);
if (e.type() == LIVEENTRY || e.type() == INITENTRY)
{
// The last level can have live entries, but at that point we
// know that they are actually init entries because the earliest
// state of all entries is init, so we mark them as such here
if (mLevel == BucketList::kNumLevels - 1 &&
e.type() == LIVEENTRY)
{
ltx->createWithoutLoading(e.liveEntry());
}
else if (
protocolVersionIsBefore(
mMinProtocolVersionSeen,
Bucket::
FIRST_PROTOCOL_SUPPORTING_INITENTRY_AND_METAENTRY))
{
// Prior to protocol 11, INITENTRY didn't exist, so we need
// to check ltx to see if this is an update or a create
auto key = InternalLedgerEntry(e.liveEntry()).toKey();
if (ltx->getNewestVersion(key))
{
ltx->updateWithoutLoading(e.liveEntry());
}
else
{
ltx->createWithoutLoading(e.liveEntry());
}
}
else
{
if (e.type() == LIVEENTRY)
{
ltx->updateWithoutLoading(e.liveEntry());
}
else
{
ltx->createWithoutLoading(e.liveEntry());
}
}
}
else
{
releaseAssertOrThrow(!isUsingBucketListDB);
if (protocolVersionIsBefore(
mMinProtocolVersionSeen,
Bucket::
FIRST_PROTOCOL_SUPPORTING_INITENTRY_AND_METAENTRY))
{
// Prior to protocol 11, DEAD entries could exist
// without LIVE entries in between
if (ltx->getNewestVersion(e.deadEntry()))
{
ltx->eraseWithoutLoading(e.deadEntry());
}
}
else
{
ltx->eraseWithoutLoading(e.deadEntry());
}
}
if ((++count > LEDGER_ENTRY_BATCH_COMMIT_SIZE))
{
++mBucketIter;
break;
}
}
}
if (innerLtx)
{
ltx->commit();
}
mCount += count;
return count;
}
BucketApplicator::Counters::Counters(VirtualClock::time_point now)
{
reset(now);
}
void
BucketApplicator::Counters::reset(VirtualClock::time_point now)
{
mStarted = now;
for (auto let : xdr::xdr_traits<LedgerEntryType>::enum_values())
{
LedgerEntryType t = static_cast<LedgerEntryType>(let);
mCounters[t] = {0, 0};
}
}
void
BucketApplicator::Counters::getRates(
VirtualClock::time_point now,
std::map<LedgerEntryType, CounterEntry>& sec_counters, uint64_t& T_sec,
uint64_t& total)
{
VirtualClock::duration dur = now - mStarted;
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(dur);
uint64_t usecs = usec.count() + 1;
total = 0;
for (auto const& [t, countPair] : mCounters)
{
sec_counters[t] = {(countPair.numUpserted * 1000000) / usecs,
(countPair.numDeleted * 1000000) / usecs};
total += countPair.numUpserted + countPair.numDeleted;
}
T_sec = (total * 1000000) / usecs;
}
std::string
BucketApplicator::Counters::logStr(
uint64_t total, uint64_t level, std::string const& bucketName,
std::map<LedgerEntryType, CounterEntry> const& counters)
{
auto str =
fmt::format("for {}-entry bucket {}.{}", total, level, bucketName);
for (auto const& [let, countPair] : counters)
{
auto label = xdr::xdr_traits<LedgerEntryType>::enum_name(let);
str += fmt::format(" {} up: {}, del: {}", label, countPair.numUpserted,
countPair.numDeleted);
}
return str;
}
void
BucketApplicator::Counters::logInfo(std::string const& bucketName,
uint32_t level,
VirtualClock::time_point now)
{
uint64_t T_sec, total;
std::map<LedgerEntryType, CounterEntry> sec_counters;
getRates(now, sec_counters, T_sec, total);
CLOG_INFO(Bucket, "Apply-rates {} T:{}",
logStr(total, level, bucketName, sec_counters), T_sec);
CLOG_INFO(Bucket, "Entry-counts {}",
logStr(total, level, bucketName, mCounters));
}
void
BucketApplicator::Counters::logDebug(std::string const& bucketName,
uint32_t level,
VirtualClock::time_point now)
{
uint64_t T_sec, total;
std::map<LedgerEntryType, CounterEntry> sec_counters;
getRates(now, sec_counters, T_sec, total);
CLOG_DEBUG(Bucket, "Apply-rates {} T:{}",
logStr(total, level, bucketName, sec_counters), T_sec);
}
void
BucketApplicator::Counters::mark(BucketEntry const& e)
{
if (e.type() == LIVEENTRY || e.type() == INITENTRY)
{
auto let = e.liveEntry().data.type();
auto iter = mCounters.find(let);
releaseAssert(iter != mCounters.end());
++iter->second.numUpserted;
}
else
{
auto let = e.deadEntry().type();
releaseAssert(let != CONFIG_SETTING);
auto iter = mCounters.find(let);
releaseAssert(iter != mCounters.end());
++iter->second.numDeleted;
}
}
}