-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBucketListSnapshot.cpp
More file actions
1105 lines (986 loc) · 35 KB
/
Copy pathBucketListSnapshot.cpp
File metadata and controls
1105 lines (986 loc) · 35 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2025 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/BucketIndexUtils.h"
#include "bucket/BucketInputIterator.h"
#include "bucket/BucketListBase.h"
#include "bucket/LiveBucketList.h"
#include "ledger/LedgerTxn.h"
#include "ledger/LedgerTypeUtils.h"
#include "util/GlobalChecks.h"
#include "util/MetricsRegistry.h"
#include "util/ProtocolVersion.h"
#include <medida/counter.h>
#include <medida/meter.h>
#include <medida/timer.h>
namespace stellar
{
//
// BucketListSnapshotData
//
template <class BucketT>
BucketListSnapshotData<BucketT>::Level::Level(BucketLevel<BucketT> const& level)
: curr(level.getCurr()), snap(level.getSnap())
{
}
template <class BucketT>
BucketListSnapshotData<BucketT>::Level::Level(
std::shared_ptr<BucketT const> currBucket,
std::shared_ptr<BucketT const> snapBucket)
: curr(std::move(currBucket)), snap(std::move(snapBucket))
{
}
template <class BucketT>
BucketListSnapshotData<BucketT>::BucketListSnapshotData(
BucketListBase<BucketT> const& bl)
: levels([&bl]() {
std::vector<Level> v;
v.reserve(BucketListBase<BucketT>::kNumLevels);
for (uint32_t i = 0; i < BucketListBase<BucketT>::kNumLevels; ++i)
{
v.emplace_back(bl.getLevel(i));
}
return v;
}())
{
}
//
// BucketSnapshotMetrics
//
template <class BucketT>
BucketSnapshotMetrics<BucketT>::BucketSnapshotMetrics(MetricsRegistry& metrics)
: mPointTimers([&metrics]() {
UnorderedMap<LedgerEntryType, std::reference_wrapper<SimpleTimer>>
timers;
for (auto t : xdr::xdr_traits<LedgerEntryType>::enum_values())
{
auto const& label = xdr::xdr_traits<LedgerEntryType>::enum_name(
static_cast<LedgerEntryType>(t));
auto& metric = metrics.NewSimpleTimer(
{BucketT::METRIC_STRING, label}, std::chrono::microseconds{1});
timers.emplace(static_cast<LedgerEntryType>(t), metric);
}
return timers;
}())
, mBulkLoadMeter(
metrics.NewMeter({BucketT::METRIC_STRING, "query", "loads"}, "query"))
{
}
//
// SearchableBucketListSnapshot
//
template <class BucketT>
SearchableBucketListSnapshot<BucketT>::SearchableBucketListSnapshot(
MetricsRegistry& metrics,
std::shared_ptr<BucketSnapshotMetrics<BucketT> const> snapshotMetrics,
std::shared_ptr<BucketListSnapshotData<BucketT> const> data)
: mData(std::move(data))
, mMetrics(metrics)
, mSnapshotMetrics(std::move(snapshotMetrics))
{
releaseAssert(mSnapshotMetrics);
}
template <class BucketT>
SearchableBucketListSnapshot<BucketT>::SearchableBucketListSnapshot(
SearchableBucketListSnapshot const& other)
: mData(other.mData)
// mStreams intentionally left empty — each copy gets its own stream cache
, mMetrics(other.mMetrics)
, mSnapshotMetrics(other.mSnapshotMetrics)
, mBulkTimers(other.mBulkTimers)
{
}
template <class BucketT>
SearchableBucketListSnapshot<BucketT>&
SearchableBucketListSnapshot<BucketT>::operator=(
SearchableBucketListSnapshot const& other)
{
if (this != &other)
{
mData = other.mData;
mStreams.clear();
mMetrics = other.mMetrics;
mSnapshotMetrics = other.mSnapshotMetrics;
mBulkTimers = other.mBulkTimers;
#ifdef BUILD_TESTS
// Reset thread ownership so the copy can be claimed by another thread.
mThreadId.store(std::thread::id{});
#endif
}
return *this;
}
// Bucket loads are not thread safe and a single snapshot instance should only
// be queried by one thread. We cache the initial caller's thread id and assert
// following queries are from the same thread. Note: this only guards the
// bucket-loading query entry points; access to the immutable underlying
// snapshot data is thread safe.
template <class BucketT>
void
SearchableBucketListSnapshot<BucketT>::threadInvariant() const
{
#ifdef BUILD_TESTS
auto const current = std::this_thread::get_id();
std::thread::id unclaimed{};
// Atomically claim ownership on first use, so any concurrent claimant sees
// the CAS fail with `unclaimed` set to the owner's id and asserts.
if (!mThreadId.compare_exchange_strong(unclaimed, current))
{
releaseAssert(unclaimed == current);
}
#endif
}
// File streams are fairly expensive to create, so they are lazily created and
// stored in mStreams.
template <class BucketT>
XDRInputFileStream&
SearchableBucketListSnapshot<BucketT>::getStream(
std::shared_ptr<BucketT const> const& bucket) const
{
threadInvariant();
BucketT const* key = bucket.get();
auto it = mStreams.find(key);
if (it == mStreams.end())
{
auto stream = std::make_unique<XDRInputFileStream>();
stream->open(bucket->getFilename());
it = mStreams.emplace(key, std::move(stream)).first;
}
return *it->second;
}
// Loads an entry from the bucket file at the given offset. Returns a pair of
// (entry, bloomMiss) where bloomMiss is true if the bloom filter indicated the
// key might exist but it wasn't actually found (a false positive).
template <class BucketT>
std::pair<std::shared_ptr<typename BucketT::EntryT const>, bool>
SearchableBucketListSnapshot<BucketT>::getEntryAtOffset(
std::shared_ptr<BucketT const> const& bucket, LedgerKey const& k,
std::streamoff pos, size_t pageSize) const
{
ZoneScoped;
releaseAssertOrThrow(pageSize > 0);
if (bucket->isEmpty())
{
return {nullptr, false};
}
auto& stream = getStream(bucket);
stream.seek(pos);
typename BucketT::EntryT be;
if (stream.readPage(be, k, pageSize))
{
auto entry = std::make_shared<typename BucketT::EntryT const>(be);
bucket->getIndex().maybeAddToCache(entry);
return {entry, false};
}
bucket->getIndex().markBloomMiss();
return {nullptr, true};
}
// Looks up a single key in a bucket using its index. Returns (entry, bloomMiss)
// where entry is nullptr if not found, and bloomMiss indicates a bloom filter
// false positive (key appeared to exist but wasn't actually in the bucket).
template <class BucketT>
std::pair<std::shared_ptr<typename BucketT::EntryT const>, bool>
SearchableBucketListSnapshot<BucketT>::getBucketEntry(
std::shared_ptr<BucketT const> const& bucket, LedgerKey const& k) const
{
ZoneScoped;
if (bucket->isEmpty())
{
return {nullptr, false};
}
auto indexRes = bucket->getIndex().lookup(k);
switch (indexRes.getState())
{
// Index had entry in cache
case IndexReturnState::CACHE_HIT:
if constexpr (std::is_same_v<BucketT, LiveBucket>)
{
return {indexRes.cacheHit(), false};
}
else
{
throw std::runtime_error("Hot Archive reported cache hit");
}
// Index found file offset, load entry from disk
case IndexReturnState::FILE_OFFSET:
return getEntryAtOffset(bucket, k, indexRes.fileOffset(),
bucket->getIndex().getPageSize());
// Key not in this bucket
case IndexReturnState::NOT_FOUND:
return {nullptr, false};
}
}
// Bulk load multiple keys from a single bucket. Since the input keys are
// sorted, we do a binary search for the first key. If we find an entry, we
// remove it from keys so that later buckets do not load shadowed entries. If we
// don't find the entry, we keep it in keys so it will be searched for in lower
// levels.
template <class BucketT>
void
SearchableBucketListSnapshot<BucketT>::loadKeysFromBucket(
std::shared_ptr<BucketT const> const& bucket,
std::set<LedgerKey, LedgerEntryIdCmp>& keys,
std::vector<typename BucketT::LoadT>& result) const
{
ZoneScoped;
if (bucket->isEmpty())
{
return;
}
auto currKeyIt = keys.begin();
auto const& index = bucket->getIndex();
auto indexIter = index.begin();
while (currKeyIt != keys.end() && indexIter != index.end())
{
// Scan for current key. Iterator returned is the lower_bound of the
// key which will be our starting point for the next key search.
auto [indexRes, newIndexIter] = index.scan(indexIter, *currKeyIt);
indexIter = newIndexIter;
std::shared_ptr<typename BucketT::EntryT const> entryOp;
switch (indexRes.getState())
{
// Index had entry in cache
case IndexReturnState::CACHE_HIT:
if constexpr (std::is_same_v<BucketT, LiveBucket>)
{
entryOp = indexRes.cacheHit();
}
else
{
throw std::runtime_error("Hot Archive reported cache hit");
}
break;
// Index found file offset, load entry from disk
case IndexReturnState::FILE_OFFSET:
std::tie(entryOp, std::ignore) =
getEntryAtOffset(bucket, *currKeyIt, indexRes.fileOffset(),
bucket->getIndex().getPageSize());
break;
// Key not in this bucket, try next key
case IndexReturnState::NOT_FOUND:
++currKeyIt;
continue;
}
if (entryOp)
{
if (!BucketT::isTombstoneEntry(*entryOp))
{
if constexpr (std::is_same_v<BucketT, LiveBucket>)
{
result.push_back(entryOp->liveEntry());
}
else
{
static_assert(std::is_same_v<BucketT, HotArchiveBucket>,
"unexpected bucket type");
result.push_back(*entryOp);
}
}
currKeyIt = keys.erase(currKeyIt);
continue;
}
++currKeyIt;
}
}
template <class BucketT>
template <typename Func>
void
SearchableBucketListSnapshot<BucketT>::loopAllBuckets(
Func&& f, BucketListSnapshotData<BucketT> const& snapshot) const
{
for (auto const& level : snapshot.levels)
{
if (level.curr && !level.curr->isEmpty())
{
if (f(level.curr) == Loop::COMPLETE)
{
return;
}
}
if (level.snap && !level.snap->isEmpty())
{
if (f(level.snap) == Loop::COMPLETE)
{
return;
}
}
}
}
template <class BucketT>
template <typename Func>
void
SearchableBucketListSnapshot<BucketT>::loopAllBuckets(Func&& f) const
{
releaseAssert(mData);
loopAllBuckets(std::forward<Func>(f), *mData);
}
template <class BucketT>
std::shared_ptr<typename BucketT::LoadT const>
SearchableBucketListSnapshot<BucketT>::load(LedgerKey const& k) const
{
ZoneScoped;
releaseAssert(mData);
threadInvariant();
auto timerIter = mSnapshotMetrics->mPointTimers.find(k.type());
releaseAssert(timerIter != mSnapshotMetrics->mPointTimers.end());
auto timer = timerIter->second.get().TimeScope();
std::shared_ptr<typename BucketT::LoadT const> result{};
// Search function called on each Bucket in BucketList until we find the key
auto loadKeyBucketLoop = [&](std::shared_ptr<BucketT const> const& bucket) {
auto [be, bloomMiss] = getBucketEntry(bucket, k);
if (bloomMiss)
{
// Reset timer on bloom miss to avoid outlier metrics, since we
// really only want to measure disk performance
timer.Reset();
}
if (be)
{
result = BucketT::bucketEntryToLoadResult(be);
return Loop::COMPLETE;
}
return Loop::INCOMPLETE;
};
loopAllBuckets(loadKeyBucketLoop);
return result;
}
template <class BucketT>
medida::Timer&
SearchableBucketListSnapshot<BucketT>::getBulkLoadTimer(
std::string const& label, size_t numEntries) const
{
// mBulkTimers is per-snapshot mutable state lazily populated here, so this
// must be single-threaded. Enforced here as well as at the public query
// entry points.
threadInvariant();
if (numEntries != 0)
{
mSnapshotMetrics->mBulkLoadMeter.get().Mark(numEntries);
}
auto iter = mBulkTimers.find(label);
if (iter == mBulkTimers.end())
{
auto& metric =
mMetrics.get().NewTimer({BucketT::METRIC_STRING, "bulk", label});
iter = mBulkTimers.emplace(label, metric).first;
}
return iter->second.get();
}
template <class BucketT>
std::shared_ptr<BucketListSnapshotData<BucketT> const> const&
SearchableBucketListSnapshot<BucketT>::getSnapshotData() const
{
return mData;
}
//
// SearchableLiveBucketListSnapshot
//
SearchableLiveBucketListSnapshot::SearchableLiveBucketListSnapshot(
MetricsRegistry& metrics,
std::shared_ptr<BucketSnapshotMetrics<LiveBucket> const> snapshotMetrics,
std::shared_ptr<BucketListSnapshotData<LiveBucket> const> data)
: SearchableBucketListSnapshot<LiveBucket>(
metrics, std::move(snapshotMetrics), std::move(data))
{
}
template <class BucketT>
std::vector<typename BucketT::LoadT>
SearchableBucketListSnapshot<BucketT>::loadKeys(
std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys,
std::string const& label) const
{
ZoneScoped;
releaseAssert(mData);
threadInvariant();
auto timer = getBulkLoadTimer(label, inKeys.size()).TimeScope();
auto keys = inKeys;
std::vector<typename BucketT::LoadT> entries;
auto loadKeysLoop = [&](std::shared_ptr<BucketT const> const& bucket) {
loadKeysFromBucket(bucket, keys, entries);
return keys.empty() ? Loop::COMPLETE : Loop::INCOMPLETE;
};
loopAllBuckets(loadKeysLoop, *mData);
return entries;
}
// 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>
SearchableLiveBucketListSnapshot::loadPoolShareTrustLinesByAccountAndAsset(
AccountID const& accountID, Asset const& asset) const
{
ZoneScoped;
releaseAssert(mData);
threadInvariant();
LedgerKeySet trustlinesToLoad;
auto trustLineLoop = [&](std::shared_ptr<LiveBucket const> const& bucket) {
static std::vector<PoolID> const emptyVec = {};
auto const& poolIDs = bucket->isEmpty()
? emptyVec
: bucket->getIndex().getPoolIDsByAsset(asset);
for (auto const& poolID : poolIDs)
{
LedgerKey trustlineKey(TRUSTLINE);
trustlineKey.trustLine().accountID = accountID;
trustlineKey.trustLine().asset.type(ASSET_TYPE_POOL_SHARE);
trustlineKey.trustLine().asset.liquidityPoolID() = poolID;
trustlinesToLoad.emplace(trustlineKey);
}
return Loop::INCOMPLETE;
};
loopAllBuckets(trustLineLoop);
auto timer =
getBulkLoadTimer("poolshareTrustlines", trustlinesToLoad.size())
.TimeScope();
std::vector<LedgerEntry> result;
auto loadKeysLoop = [&](std::shared_ptr<LiveBucket const> const& bucket) {
loadKeysFromBucket(bucket, trustlinesToLoad, result);
return trustlinesToLoad.empty() ? Loop::COMPLETE : Loop::INCOMPLETE;
};
loopAllBuckets(loadKeysLoop);
return result;
}
// This is a legacy query, should only be called by main thread during catchup.
std::vector<InflationWinner>
SearchableLiveBucketListSnapshot::loadInflationWinners(size_t maxWinners,
int64_t minBalance) const
{
ZoneScoped;
releaseAssert(mData);
threadInvariant();
auto timer = getBulkLoadTimer("inflationWinners", 0).TimeScope();
UnorderedMap<AccountID, int64_t> voteCount;
UnorderedSet<AccountID> seen;
auto countVotesInBucket =
[&](std::shared_ptr<LiveBucket const> const& bucket) {
for (LiveBucketInputIterator in(bucket); in; ++in)
{
BucketEntry const& be = *in;
if (be.type() == DEADENTRY)
{
if (be.deadEntry().type() == ACCOUNT)
{
seen.insert(be.deadEntry().account().accountID);
}
continue;
}
// Accounts 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 Loop::INCOMPLETE;
};
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;
}
for (auto iter = voteCountSortedByCount.cbegin();
winners.size() < maxWinners && iter->first >= minBalance; ++iter)
{
winners.push_back(
InflationWinner{iter->second->first, iter->first});
}
}
else
{
for (auto const& [id, count] : voteCount)
{
if (count >= minBalance)
{
winners.push_back({id, count});
}
}
}
return winners;
}
// Scans the BucketList for entries eligible for eviction. This runs in the
// background and returns candidates that may be invalidated during TX apply.
//
// We track evicted keys in two ways:
// 1. result linked list - maintains order of eviction candidates. Since some
// candidates may be invalidated during TX apply, we track order so the main
// thread knows the cutoff point for what actually gets evicted.
// 2. keysToEvict set - prevents evicting the same key twice. It's possible for
// an entry to be expired with two different versions in different buckets.
// We scan both versions but should only evict once.
std::unique_ptr<EvictionResultCandidates>
SearchableLiveBucketListSnapshot::scanForEviction(
uint32_t ledgerSeq, EvictionMetrics& metrics, EvictionIterator evictionIter,
std::shared_ptr<EvictionStatistics> stats, StateArchivalSettings const& sas,
uint32_t ledgerVers) const
{
ZoneScoped;
releaseAssert(mData);
releaseAssert(stats);
threadInvariant();
auto getBucketFromIter =
[&levels = mData->levels](
EvictionIterator const& iter) -> std::shared_ptr<LiveBucket const> {
auto& level = levels.at(iter.bucketListLevel);
return iter.isCurrBucket ? level.curr : level.snap;
};
LiveBucketList::updateStartingEvictionIterator(
evictionIter, sas.startingEvictionScanLevel, ledgerSeq);
std::unique_ptr<EvictionResultCandidates> result =
std::make_unique<EvictionResultCandidates>(sas, ledgerSeq, ledgerVers);
UnorderedSet<LedgerKey> keysToEvict;
auto startIter = evictionIter;
auto scanSize = sas.evictionScanSize;
for (;;)
{
auto bucket = getBucketFromIter(evictionIter);
LiveBucketList::checkIfEvictionScanIsStuck(
evictionIter, sas.evictionScanSize, bucket, metrics);
// If we scan scanSize bytes before hitting bucket EOF, exit early
if (scanForEvictionInBucket(bucket, evictionIter, scanSize, ledgerSeq,
result->eligibleEntries, ledgerVers,
keysToEvict) == Loop::COMPLETE)
{
break;
}
// If we return back to the Bucket we started at, exit
if (LiveBucketList::updateEvictionIterAndRecordStats(
evictionIter, startIter, sas.startingEvictionScanLevel,
ledgerSeq, stats, metrics))
{
break;
}
}
result->endOfRegionIterator = evictionIter;
return result;
}
void
SearchableLiveBucketListSnapshot::scanForEntriesOfType(
LedgerEntryType type,
std::function<Loop(BucketEntry const&)> callback) const
{
ZoneScoped;
releaseAssert(mData);
threadInvariant();
auto scanBucket = [&](std::shared_ptr<LiveBucket const> const& bucket) {
if (bucket->isEmpty())
{
return Loop::INCOMPLETE;
}
auto range = bucket->getRangeForType(type);
if (!range)
{
return Loop::INCOMPLETE;
}
auto& stream = getStream(bucket);
stream.seek(range->first);
BucketEntry be;
while (stream.readOne(be))
{
if (!isBucketMetaEntry<LiveBucket>(be))
{
if (LedgerKey key = getBucketLedgerKey(be); key.type() > type)
{
break;
}
}
bool matchesType = false;
if (be.type() == LIVEENTRY || be.type() == INITENTRY)
{
matchesType = be.liveEntry().data.type() == type;
}
else if (be.type() == DEADENTRY)
{
matchesType = be.deadEntry().type() == type;
}
if (matchesType)
{
if (callback(be) == Loop::COMPLETE)
{
return Loop::COMPLETE;
}
}
}
return Loop::INCOMPLETE;
};
loopAllBuckets(scanBucket);
}
namespace
{
// Iterator for `BucketEntry`s of a given type in a bucket. Expects the stream
// to be positioned at the start of the type range. This is basically the same
// as SearchableLiveBucketListSnapshot::scanForEntriesOfType's scanBucket except
// with more control over when iteration happens.
class BucketEntryIterator
{
BucketEntry mEntry;
LedgerKey mKey;
XDRInputFileStream& mStream;
LedgerEntryType const mType;
public:
BucketEntryIterator(XDRInputFileStream& stream, LedgerEntryType type)
: mStream(stream), mType(type)
{
}
BucketEntry const&
getEntry() const
{
return mEntry;
}
LedgerKey const&
getKey() const
{
return mKey;
}
bool
advance()
{
while (mStream.readOne(mEntry))
{
if (isBucketMetaEntry<LiveBucket>(mEntry))
{
continue;
}
mKey = getBucketLedgerKey(mEntry);
if (mKey.type() > mType)
{
break;
}
if (mKey.type() == mType)
{
return true;
}
}
return false;
}
};
} // namespace
void
SearchableLiveBucketListSnapshot::scanForLiveEntriesOfType(
LedgerEntryType type,
std::function<void(LedgerEntry const&, LedgerKey const&)> callback) const
{
ZoneScoped;
// We implement this as a k-way merge over all buckets. We use a loser tree
// for this. The benefit over a heap is ~2x fewer comparisons. A loser tree
// is like a single-elimination tournament. The leaves of the tree are the
// iterators, and the internal nodes represent the loser of the comparison
// between the two children. This implementation represents the binary tree
// in an array, where the tournament tree is from indices [1, 2n) (leaves
// are [n, 2n)). Index 0 is used for keeping track of the overall winner. To
// update, we just need to advance the iterator for the winning node and
// then do the log(k) comparisons upward along the path to the root to
// update the losers. While loser trees often store the whole node value at
// intermediate nodes, we just store an index, since copying the XDR types
// is probably more expensive than the extra indirection.
std::vector<BucketEntryIterator> iterators;
loopAllBuckets([&iterators, type,
this](std::shared_ptr<LiveBucket const> const& bucket) {
if (bucket->isEmpty())
{
return Loop::INCOMPLETE;
}
auto range = bucket->getRangeForType(type);
if (!range)
{
return Loop::INCOMPLETE;
}
auto& stream = getStream(bucket);
stream.seek(range->first);
iterators.emplace_back(stream, type);
return Loop::INCOMPLETE;
});
if (iterators.empty())
{
return;
}
size_t const numIterators = iterators.size();
constexpr int exhausted = -1;
std::vector<int> tree;
tree.resize(numIterators * 2);
for (size_t i = 0; i < numIterators; ++i)
{
if (iterators[i].advance())
{
tree[numIterators + i] = i;
}
else
{
tree[numIterators + i] = exhausted;
}
}
// The leftIndex wins if it should come before the rightIndex. This happens
// when the left key is less than the right key, or if they are equal and
// the left index is less than the right index (newer buckets shadow older
// buckets).
auto leftWins = [&iterators](int leftIndex, int rightIndex) -> bool {
if (leftIndex == exhausted)
{
return false;
}
if (rightIndex == exhausted)
{
return true;
}
if (std::strong_ordering cmp = LedgerEntryIdCmp::compare(
iterators[leftIndex].getKey(), iterators[rightIndex].getKey());
cmp != std::strong_ordering::equal)
{
return cmp == std::strong_ordering::less;
}
return leftIndex < rightIndex;
};
// Play the match at index i; store the loser, return the winner
auto play = [&tree, &leftWins](auto& play, size_t index) -> int {
if (2 * index >= tree.size())
{
return tree[index];
}
int left = play(play, 2 * index);
int right = play(play, 2 * index + 1);
if (leftWins(left, right))
{
tree[index] = right;
return left;
}
else
{
tree[index] = left;
return right;
}
};
tree[0] = play(play, 1);
bool first = true;
LedgerKey last;
while (tree[0] != exhausted)
{
int index = tree[0];
auto& iter = iterators[index];
// Only call the callback if this is the first time we've seen the key
if (auto& key = iter.getKey(); first || key != last)
{
last = key;
auto& entry = iter.getEntry();
if (entry.type() == LIVEENTRY || entry.type() == INITENTRY)
{
callback(entry.liveEntry(), key);
}
}
first = false;
if (!iter.advance())
{
tree[index + numIterators] = exhausted;
}
// Update tournament up the tree to the root. As before, we store the
// loser at each node and keep track of the winner in `winner` and at
// tree[0].
int winner = tree[index + numIterators];
for (int i = (index + numIterators) / 2; i > 0; i /= 2)
{
if (leftWins(tree[i], winner))
{
std::swap(tree[i], winner);
}
}
tree[0] = winner;
}
}
// Helper function to handle scan logic in a single bucket.
Loop
SearchableLiveBucketListSnapshot::scanForEvictionInBucket(
std::shared_ptr<LiveBucket const> const& bucket, EvictionIterator& iter,
uint32_t& bytesToScan, uint32_t ledgerSeq,
std::list<EvictionResultEntry>& evictableEntries, uint32_t ledgerVers,
UnorderedSet<LedgerKey>& keysInEvictableEntries) const
{
ZoneScoped;
if (bucket->isEmpty() || protocolVersionIsBefore(bucket->getBucketVersion(),
SOROBAN_PROTOCOL_VERSION))
{
// EOF, skip to next bucket
return Loop::INCOMPLETE;
}
if (bytesToScan == 0)
{
// Reached end of scan region
return Loop::COMPLETE;
}
std::list<EvictionResultEntry> maybeEvictQueue;
LedgerKeySet keysToSearch;
auto processQueue = [&]() {
// Note: load from a stale snapshot here. Expired entries should
// never be modified by the ltx, unless they're getting restored.
// `resolveBackgroundEviction` checks that entries loaded from the
// snapshot are indeed not touched by the ltx, so it should be safe
// to evict these candidates.
auto loadResult = populateLoadedEntries(
keysToSearch, loadKeys(keysToSearch, "eviction"));
for (auto& e : maybeEvictQueue)
{
// If TTL entry has not yet been deleted
if (auto ttl = loadResult.find(getTTLKey(e.entry))->second;
ttl != nullptr)
{
// If TTL of entry is expired
if (!isLive(*ttl, ledgerSeq))
{
// Note: There was a bug in protocol 23 where we would
// not check if an entry was the newest version and would
// evict whatever version was scanned.
if (protocolVersionStartsFrom(ledgerVers,
ProtocolVersion::V_24) &&
isPersistentEntry(e.entry.data))
{
// Make sure we only ever evict the most recent version
// of persistent entries. Make sure we use the entry
// from loadKeys, as they are guaranteed to be the
// newest version. We could have scanned and populated
// `e` with an older version.
auto newestVersionIter =
loadResult.find(LedgerEntryKey(e.entry));
releaseAssertOrThrow(newestVersionIter !=
loadResult.end());
e.entry = *newestVersionIter->second;
}
e.liveUntilLedger = ttl->data.ttl().liveUntilLedgerSeq;
evictableEntries.emplace_back(e);
keysInEvictableEntries.insert(LedgerEntryKey(e.entry));
releaseAssertOrThrow(evictableEntries.size() ==
keysInEvictableEntries.size());
}
}
}
};
// Start evicting persistent entries in p23
auto isEvictableType = [ledgerVers](auto const& le) {
if (protocolVersionIsBefore(
ledgerVers,
LiveBucket::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION))
{
return isTemporaryEntry(le);
}
else
{
return isSorobanEntry(le);
}
};
// Open new stream for eviction scan to not interfere with BucketListDB load
// streams
XDRInputFileStream stream{};
stream.open(bucket->getFilename().string());
stream.seek(iter.bucketFileOffset);
BucketEntry be;
// First, scan the bucket region and record all temp entry keys in
// maybeEvictQueue. After scanning, we will load all the TTL keys for these
// entries in a single bulk load to determine