forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBucketListSnapshot.cpp
341 lines (289 loc) · 9.54 KB
/
BucketListSnapshot.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 2024 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 "bucket/BucketListSnapshot.h"
#include "bucket/BucketInputIterator.h"
#include "crypto/SecretKey.h" // IWYU pragma: keep
#include "ledger/LedgerTxn.h"
#include "medida/timer.h"
namespace stellar
{
BucketListSnapshot::BucketListSnapshot(BucketList const& bl, uint32_t ledgerSeq)
: mLedgerSeq(ledgerSeq)
{
releaseAssert(threadIsMain());
for (uint32_t i = 0; i < BucketList::kNumLevels; ++i)
{
auto const& level = bl.getLevel(i);
mLevels.emplace_back(BucketLevelSnapshot(level));
}
}
BucketListSnapshot::BucketListSnapshot(BucketListSnapshot const& snapshot)
: mLevels(snapshot.mLevels), mLedgerSeq(snapshot.mLedgerSeq)
{
}
std::vector<BucketLevelSnapshot> const&
BucketListSnapshot::getLevels() const
{
return mLevels;
}
uint32_t
BucketListSnapshot::getLedgerSeq() const
{
return mLedgerSeq;
}
void
SearchableBucketListSnapshot::loopAllBuckets(
std::function<bool(BucketSnapshot const&)> f) const
{
releaseAssert(mSnapshot);
for (auto const& lev : mSnapshot->getLevels())
{
// Return true if we should exit loop early
auto processBucket = [f](BucketSnapshot const& b) {
if (b.isEmpty())
{
return false;
}
return f(b);
};
if (processBucket(lev.curr) || processBucket(lev.snap))
{
return;
}
}
}
EvictionResult
SearchableBucketListSnapshot::scanForEviction(
uint32_t ledgerSeq, EvictionCounters& counters,
EvictionIterator evictionIter, std::shared_ptr<EvictionStatistics> stats,
StateArchivalSettings const& sas)
{
releaseAssert(mSnapshot);
releaseAssert(stats);
auto getBucketFromIter =
[&levels = mSnapshot->getLevels()](
EvictionIterator const& iter) -> BucketSnapshot const& {
auto& level = levels.at(iter.bucketListLevel);
return iter.isCurrBucket ? level.curr : level.snap;
};
BucketList::updateStartingEvictionIterator(
evictionIter, sas.startingEvictionScanLevel, ledgerSeq);
EvictionResult result(sas);
auto startIter = evictionIter;
auto scanSize = sas.evictionScanSize;
for (;;)
{
auto const& b = getBucketFromIter(evictionIter);
BucketList::checkIfEvictionScanIsStuck(
evictionIter, sas.evictionScanSize, b.getRawBucket(), counters);
// If we scan scanSize before hitting bucket EOF, exit early
if (b.scanForEviction(evictionIter, scanSize, ledgerSeq,
result.eligibleKeys, *this))
{
break;
}
// If we return back to the Bucket we started at, exit
if (BucketList::updateEvictionIterAndRecordStats(
evictionIter, startIter, sas.startingEvictionScanLevel,
ledgerSeq, stats, counters))
{
break;
}
}
result.endOfRegionIterator = evictionIter;
result.initialLedger = ledgerSeq;
return result;
}
std::shared_ptr<LedgerEntry>
SearchableBucketListSnapshot::getLedgerEntry(LedgerKey const& k)
{
ZoneScoped;
mSnapshotManager.maybeUpdateSnapshot(mSnapshot);
if (threadIsMain())
{
auto timer = mSnapshotManager.getPointLoadTimer(k.type()).TimeScope();
return getLedgerEntryInternal(k);
}
else
{
return getLedgerEntryInternal(k);
}
}
std::shared_ptr<LedgerEntry>
SearchableBucketListSnapshot::getLedgerEntryInternal(LedgerKey const& k)
{
std::shared_ptr<LedgerEntry> result{};
auto f = [&](BucketSnapshot const& b) {
auto be = b.getBucketEntry(k);
if (be.has_value())
{
result =
be.value().type() == DEADENTRY
? nullptr
: std::make_shared<LedgerEntry>(be.value().liveEntry());
return true;
}
else
{
return false;
}
};
loopAllBuckets(f);
return result;
}
std::vector<LedgerEntry>
SearchableBucketListSnapshot::loadKeysInternal(
std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys)
{
std::vector<LedgerEntry> entries;
// Make a copy of the key set, this loop is destructive
auto keys = inKeys;
auto f = [&](BucketSnapshot const& b) {
b.loadKeys(keys, entries);
return keys.empty();
};
loopAllBuckets(f);
return entries;
}
std::vector<LedgerEntry>
SearchableBucketListSnapshot::loadKeys(
std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys)
{
ZoneScoped;
mSnapshotManager.maybeUpdateSnapshot(mSnapshot);
if (threadIsMain())
{
auto timer =
mSnapshotManager.recordBulkLoadMetrics("prefetch", inKeys.size())
.TimeScope();
return loadKeysInternal(inKeys);
}
else
{
return loadKeysInternal(inKeys);
}
}
// This query has two steps:
// 1. For each bucket, determine what PoolIDs contain the target asset via the
// assetToPoolID index
// 2. Perform a bulk lookup for all possible trustline keys, that is, all
// trustlines with the given accountID and poolID from step 1
std::vector<LedgerEntry>
SearchableBucketListSnapshot::loadPoolShareTrustLinesByAccountAndAsset(
AccountID const& accountID, Asset const& asset)
{
ZoneScoped;
// This query should only be called during TX apply
releaseAssert(threadIsMain());
mSnapshotManager.maybeUpdateSnapshot(mSnapshot);
LedgerKeySet trustlinesToLoad;
auto trustLineLoop = [&](BucketSnapshot const& b) {
for (auto const& poolID : b.getPoolIDsByAsset(asset))
{
LedgerKey trustlineKey(TRUSTLINE);
trustlineKey.trustLine().accountID = accountID;
trustlineKey.trustLine().asset.type(ASSET_TYPE_POOL_SHARE);
trustlineKey.trustLine().asset.liquidityPoolID() = poolID;
trustlinesToLoad.emplace(trustlineKey);
}
return false; // continue
};
loopAllBuckets(trustLineLoop);
auto timer = mSnapshotManager
.recordBulkLoadMetrics("poolshareTrustlines",
trustlinesToLoad.size())
.TimeScope();
return loadKeysInternal(trustlinesToLoad);
}
std::vector<InflationWinner>
SearchableBucketListSnapshot::loadInflationWinners(size_t maxWinners,
int64_t minBalance)
{
ZoneScoped;
mSnapshotManager.maybeUpdateSnapshot(mSnapshot);
// This is a legacy query, should only be called by main thread during
// catchup
releaseAssert(threadIsMain());
auto timer = mSnapshotManager.recordBulkLoadMetrics("inflationWinners", 0)
.TimeScope();
UnorderedMap<AccountID, int64_t> voteCount;
UnorderedSet<AccountID> seen;
auto countVotesInBucket = [&](BucketSnapshot const& b) {
for (BucketInputIterator in(b.getRawBucket()); in; ++in)
{
BucketEntry const& be = *in;
if (be.type() == DEADENTRY)
{
if (be.deadEntry().type() == ACCOUNT)
{
seen.insert(be.deadEntry().account().accountID);
}
continue;
}
// Account are ordered first, so once we see a non-account entry, no
// other accounts are left in the bucket
LedgerEntry const& le = be.liveEntry();
if (le.data.type() != ACCOUNT)
{
break;
}
// Don't double count AccountEntry's seen in earlier levels
AccountEntry const& ae = le.data.account();
AccountID const& id = ae.accountID;
if (!seen.insert(id).second)
{
continue;
}
if (ae.inflationDest && ae.balance >= 1000000000)
{
voteCount[*ae.inflationDest] += ae.balance;
}
}
return false;
};
loopAllBuckets(countVotesInBucket);
std::vector<InflationWinner> winners;
// Check if we need to sort the voteCount by number of votes
if (voteCount.size() > maxWinners)
{
// Sort Inflation winners by vote count in descending order
std::map<int64_t, UnorderedMap<AccountID, int64_t>::const_iterator,
std::greater<int64_t>>
voteCountSortedByCount;
for (auto iter = voteCount.cbegin(); iter != voteCount.cend(); ++iter)
{
voteCountSortedByCount[iter->second] = iter;
}
// Insert first maxWinners entries that are larger thanminBalance
for (auto iter = voteCountSortedByCount.cbegin();
winners.size() < maxWinners && iter->first >= minBalance; ++iter)
{
// push back {AccountID, voteCount}
winners.push_back({iter->second->first, iter->first});
}
}
else
{
for (auto const& [id, count] : voteCount)
{
if (count >= minBalance)
{
winners.push_back({id, count});
}
}
}
return winners;
}
BucketLevelSnapshot::BucketLevelSnapshot(BucketLevel const& level)
: curr(level.getCurr()), snap(level.getSnap())
{
}
SearchableBucketListSnapshot::SearchableBucketListSnapshot(
BucketSnapshotManager const& snapshotManager)
: mSnapshotManager(snapshotManager)
{
// Initialize snapshot from SnapshotManager
mSnapshotManager.maybeUpdateSnapshot(mSnapshot);
}
}