forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBucketManagerImpl.cpp
1362 lines (1236 loc) · 44.4 KB
/
BucketManagerImpl.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
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 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 "bucket/BucketManagerImpl.h"
#include "bucket/Bucket.h"
#include "bucket/BucketInputIterator.h"
#include "bucket/BucketList.h"
#include "bucket/BucketOutputIterator.h"
#include "crypto/Hex.h"
#include "history/HistoryManager.h"
#include "historywork/VerifyBucketWork.h"
#include "ledger/LedgerManager.h"
#include "ledger/LedgerTxn.h"
#include "main/Application.h"
#include "main/Config.h"
#include "overlay/StellarXDR.h"
#include "util/Fs.h"
#include "util/GlobalChecks.h"
#include "util/LogSlowExecution.h"
#include "util/Logging.h"
#include "util/TmpDir.h"
#include "util/types.h"
#include <filesystem>
#include <fmt/chrono.h>
#include <fmt/format.h>
#include <fstream>
#include <map>
#include <regex>
#include <set>
#include <thread>
#include "medida/counter.h"
#include "medida/meter.h"
#include "medida/metrics_registry.h"
#include "medida/timer.h"
#include "work/WorkScheduler.h"
#include "xdrpp/printer.h"
#include <Tracy.hpp>
namespace stellar
{
std::unique_ptr<BucketManager>
BucketManager::create(Application& app)
{
auto bucketManagerPtr = std::make_unique<BucketManagerImpl>(app);
bucketManagerPtr->initialize();
return bucketManagerPtr;
}
void
BucketManagerImpl::initialize()
{
ZoneScoped;
std::string d = mApp.getConfig().BUCKET_DIR_PATH;
if (!fs::exists(d))
{
if (!fs::mkpath(d))
{
throw std::runtime_error("Unable to create bucket directory: " + d);
}
}
// Acquire exclusive lock on `buckets` folder
std::string lock = d + "/" + kLockFilename;
// there are many reasons the lock can fail so let lockFile throw
// directly for more clear error messages since we end up just raising
// a runtime exception anyway
try
{
fs::lockFile(lock);
}
catch (std::exception const& e)
{
throw std::runtime_error(fmt::format(
FMT_STRING("{}. This can be caused by access rights issues or "
"another stellar-core process already running"),
e.what()));
}
mLockedBucketDir = std::make_unique<std::string>(d);
mTmpDirManager = std::make_unique<TmpDirManager>(d + "/tmp");
if (mApp.getConfig().MODE_ENABLES_BUCKETLIST)
{
mBucketList = std::make_unique<BucketList>();
}
}
void
BucketManagerImpl::dropAll()
{
ZoneScoped;
deleteEntireBucketDir();
initialize();
}
TmpDirManager&
BucketManagerImpl::getTmpDirManager()
{
return *mTmpDirManager;
}
BucketListEvictionCounters::BucketListEvictionCounters(Application& app)
: entriesEvicted(app.getMetrics().NewCounter(
{"state-archival", "eviction", "entries-evicted"}))
, bytesScannedForEviction(app.getMetrics().NewCounter(
{"state-archival", "eviction", "bytes-scanned"}))
, incompleteBucketScan(app.getMetrics().NewCounter(
{"state-archival", "eviction", "incomplete-scan"}))
, evictionCyclePeriod(
app.getMetrics().NewCounter({"state-archival", "eviction", "period"}))
, averageEvictedEntryAge(
app.getMetrics().NewCounter({"state-archival", "eviction", "age"}))
{
}
BucketManagerImpl::BucketManagerImpl(Application& app)
: mApp(app)
, mBucketList(nullptr)
, mTmpDirManager(nullptr)
, mWorkDir(nullptr)
, mLockedBucketDir(nullptr)
, mBucketObjectInsertBatch(app.getMetrics().NewMeter(
{"bucket", "batch", "objectsadded"}, "object"))
, mBucketAddBatch(app.getMetrics().NewTimer({"bucket", "batch", "addtime"}))
, mBucketSnapMerge(app.getMetrics().NewTimer({"bucket", "snap", "merge"}))
, mSharedBucketsSize(
app.getMetrics().NewCounter({"bucket", "memory", "shared"}))
, mBucketListDBBulkLoadMeter(app.getMetrics().NewMeter(
{"bucketlistDB", "query", "loads"}, "query"))
, mBucketListDBBloomMisses(app.getMetrics().NewMeter(
{"bucketlistDB", "bloom", "misses"}, "bloom"))
, mBucketListDBBloomLookups(app.getMetrics().NewMeter(
{"bucketlistDB", "bloom", "lookups"}, "bloom"))
, mBucketListSizeCounter(
app.getMetrics().NewCounter({"bucketlist", "size", "bytes"}))
, mBucketListEvictionCounters(app)
// Minimal DB is stored in the buckets dir, so delete it only when
// mode does not use minimal DB
, mDeleteEntireBucketDirInDtor(
app.getConfig().isInMemoryModeWithoutMinimalDB())
{
}
const std::string BucketManagerImpl::kLockFilename = "stellar-core.lock";
namespace
{
std::string
bucketBasename(std::string const& bucketHexHash)
{
return "bucket-" + bucketHexHash + ".xdr";
}
bool
isBucketFile(std::string const& name)
{
static std::regex re("^bucket-[a-z0-9]{64}\\.xdr(\\.gz)?$");
return std::regex_match(name, re);
};
uint256
extractFromFilename(std::string const& name)
{
return hexToBin256(name.substr(7, 64));
};
}
std::string
BucketManagerImpl::bucketFilename(std::string const& bucketHexHash)
{
std::string basename = bucketBasename(bucketHexHash);
return getBucketDir() + "/" + basename;
}
std::string
BucketManagerImpl::bucketFilename(Hash const& hash)
{
return bucketFilename(binToHex(hash));
}
std::string
BucketManagerImpl::bucketIndexFilename(Hash const& hash) const
{
auto hashStr = binToHex(hash);
auto basename = "bucket-" + hashStr + ".index";
return getBucketDir() + "/" + basename;
}
std::string const&
BucketManagerImpl::getTmpDir()
{
ZoneScoped;
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
if (!mWorkDir)
{
TmpDir t = mTmpDirManager->tmpDir("bucket");
mWorkDir = std::make_unique<TmpDir>(std::move(t));
}
return mWorkDir->getName();
}
std::string const&
BucketManagerImpl::getBucketDir() const
{
return *(mLockedBucketDir);
}
BucketManagerImpl::~BucketManagerImpl()
{
ZoneScoped;
if (mDeleteEntireBucketDirInDtor)
{
deleteEntireBucketDir();
}
else
{
deleteTmpDirAndUnlockBucketDir();
}
}
void
BucketManagerImpl::deleteEntireBucketDir()
{
ZoneScoped;
std::string d = mApp.getConfig().BUCKET_DIR_PATH;
if (fs::exists(d))
{
// First clean out the contents of the tmpdir, as usual.
deleteTmpDirAndUnlockBucketDir();
// Then more seriously delete _all the buckets_, even live
// ones that represent the canonical state of the ledger.
//
// Should only happen on new-db or in-memory-replay shutdown.
CLOG_DEBUG(Bucket, "Deleting bucket directory: {}", d);
fs::deltree(d);
}
}
void
BucketManagerImpl::deleteTmpDirAndUnlockBucketDir()
{
ZoneScoped;
// First do fs::deltree on $BUCKET_DIR_PATH/tmp/bucket
//
// (which should just be bucket merges-in-progress and such)
mWorkDir.reset();
// Then do fs::deltree on $BUCKET_DIR_PATH/tmp
//
// (which also contains files from other subsystems, like history)
mTmpDirManager.reset();
// Then delete the lockfile $BUCKET_DIR_PATH/stellar-core.lock
if (mLockedBucketDir)
{
std::string d = mApp.getConfig().BUCKET_DIR_PATH;
std::string lock = d + "/" + kLockFilename;
releaseAssert(fs::exists(lock));
fs::unlockFile(lock);
mLockedBucketDir.reset();
}
}
BucketList&
BucketManagerImpl::getBucketList()
{
releaseAssertOrThrow(mApp.getConfig().MODE_ENABLES_BUCKETLIST);
return *mBucketList;
}
medida::Timer&
BucketManagerImpl::getMergeTimer()
{
return mBucketSnapMerge;
}
MergeCounters
BucketManagerImpl::readMergeCounters()
{
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
return mMergeCounters;
}
MergeCounters&
MergeCounters::operator+=(MergeCounters const& delta)
{
mPreInitEntryProtocolMerges += delta.mPreInitEntryProtocolMerges;
mPostInitEntryProtocolMerges += delta.mPostInitEntryProtocolMerges;
mRunningMergeReattachments += delta.mRunningMergeReattachments;
mFinishedMergeReattachments += delta.mFinishedMergeReattachments;
mPreShadowRemovalProtocolMerges += delta.mPreShadowRemovalProtocolMerges;
mPostShadowRemovalProtocolMerges += delta.mPostShadowRemovalProtocolMerges;
mNewMetaEntries += delta.mNewMetaEntries;
mNewInitEntries += delta.mNewInitEntries;
mNewLiveEntries += delta.mNewLiveEntries;
mNewDeadEntries += delta.mNewDeadEntries;
mOldMetaEntries += delta.mOldMetaEntries;
mOldInitEntries += delta.mOldInitEntries;
mOldLiveEntries += delta.mOldLiveEntries;
mOldDeadEntries += delta.mOldDeadEntries;
mOldEntriesDefaultAccepted += delta.mOldEntriesDefaultAccepted;
mNewEntriesDefaultAccepted += delta.mNewEntriesDefaultAccepted;
mNewInitEntriesMergedWithOldDead += delta.mNewInitEntriesMergedWithOldDead;
mOldInitEntriesMergedWithNewLive += delta.mOldInitEntriesMergedWithNewLive;
mOldInitEntriesMergedWithNewDead += delta.mOldInitEntriesMergedWithNewDead;
mNewEntriesMergedWithOldNeitherInit +=
delta.mNewEntriesMergedWithOldNeitherInit;
mShadowScanSteps += delta.mShadowScanSteps;
mMetaEntryShadowElisions += delta.mMetaEntryShadowElisions;
mLiveEntryShadowElisions += delta.mLiveEntryShadowElisions;
mInitEntryShadowElisions += delta.mInitEntryShadowElisions;
mDeadEntryShadowElisions += delta.mDeadEntryShadowElisions;
mOutputIteratorTombstoneElisions += delta.mOutputIteratorTombstoneElisions;
mOutputIteratorBufferUpdates += delta.mOutputIteratorBufferUpdates;
mOutputIteratorActualWrites += delta.mOutputIteratorActualWrites;
return *this;
}
bool
MergeCounters::operator==(MergeCounters const& other) const
{
return (
mPreInitEntryProtocolMerges == other.mPreInitEntryProtocolMerges &&
mPostInitEntryProtocolMerges == other.mPostInitEntryProtocolMerges &&
mRunningMergeReattachments == other.mRunningMergeReattachments &&
mFinishedMergeReattachments == other.mFinishedMergeReattachments &&
mNewMetaEntries == other.mNewMetaEntries &&
mNewInitEntries == other.mNewInitEntries &&
mNewLiveEntries == other.mNewLiveEntries &&
mNewDeadEntries == other.mNewDeadEntries &&
mOldMetaEntries == other.mOldMetaEntries &&
mOldInitEntries == other.mOldInitEntries &&
mOldLiveEntries == other.mOldLiveEntries &&
mOldDeadEntries == other.mOldDeadEntries &&
mOldEntriesDefaultAccepted == other.mOldEntriesDefaultAccepted &&
mNewEntriesDefaultAccepted == other.mNewEntriesDefaultAccepted &&
mNewInitEntriesMergedWithOldDead ==
other.mNewInitEntriesMergedWithOldDead &&
mOldInitEntriesMergedWithNewLive ==
other.mOldInitEntriesMergedWithNewLive &&
mOldInitEntriesMergedWithNewDead ==
other.mOldInitEntriesMergedWithNewDead &&
mNewEntriesMergedWithOldNeitherInit ==
other.mNewEntriesMergedWithOldNeitherInit &&
mShadowScanSteps == other.mShadowScanSteps &&
mMetaEntryShadowElisions == other.mMetaEntryShadowElisions &&
mLiveEntryShadowElisions == other.mLiveEntryShadowElisions &&
mInitEntryShadowElisions == other.mInitEntryShadowElisions &&
mDeadEntryShadowElisions == other.mDeadEntryShadowElisions &&
mOutputIteratorTombstoneElisions ==
other.mOutputIteratorTombstoneElisions &&
mOutputIteratorBufferUpdates == other.mOutputIteratorBufferUpdates &&
mOutputIteratorActualWrites == other.mOutputIteratorActualWrites);
}
void
BucketManagerImpl::incrMergeCounters(MergeCounters const& delta)
{
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
mMergeCounters += delta;
}
bool
BucketManagerImpl::renameBucketDirFile(std::filesystem::path const& src,
std::filesystem::path const& dst)
{
ZoneScoped;
if (mApp.getConfig().DISABLE_XDR_FSYNC)
{
return rename(src.string().c_str(), dst.string().c_str()) == 0;
}
else
{
return fs::durableRename(src.string(), dst.string(), getBucketDir());
}
}
std::shared_ptr<Bucket>
BucketManagerImpl::adoptFileAsBucket(std::string const& filename,
uint256 const& hash, MergeKey* mergeKey,
std::unique_ptr<BucketIndex const> index)
{
ZoneScoped;
releaseAssertOrThrow(mApp.getConfig().MODE_ENABLES_BUCKETLIST);
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
if (mergeKey)
{
// If this adoption was a merge, drop any strong reference we were
// retaining pointing to the std::shared_future it was being produced
// within (so that we can accurately track references to the bucket via
// its refcount) and if the adoption succeeds (see below) _retain_ a
// weak record of the input/output mapping, so we can reconstruct the
// future if anyone wants to restart the same merge before the bucket
// expires.
CLOG_TRACE(Bucket,
"BucketManager::adoptFileAsBucket switching merge {} from "
"live to finished for output={}",
*mergeKey, hexAbbrev(hash));
mLiveFutures.erase(*mergeKey);
}
// Check to see if we have an existing bucket (either in-memory or on-disk)
std::shared_ptr<Bucket> b = getBucketByHash(hash);
if (b)
{
CLOG_DEBUG(
Bucket,
"Deleting bucket file {} that is redundant with existing bucket",
filename);
{
auto timer = LogSlowExecution("Delete redundant bucket");
std::remove(filename.c_str());
// race condition: two buckets + indexes were produced in parallel
// only setIndex if there is no index already.
maybeSetIndex(b, std::move(index));
}
}
else
{
std::string canonicalName = bucketFilename(hash);
CLOG_DEBUG(Bucket, "Adopting bucket file {} as {}", filename,
canonicalName);
if (!renameBucketDirFile(filename, canonicalName))
{
std::string err("Failed to rename bucket :");
err += strerror(errno);
// it seems there is a race condition with external systems
// retry after sleeping for a second works around the problem
std::this_thread::sleep_for(std::chrono::seconds(1));
if (!renameBucketDirFile(filename, canonicalName))
{
// if rename fails again, surface the original error
throw std::runtime_error(err);
}
}
b = std::make_shared<Bucket>(canonicalName, hash, std::move(index));
{
mSharedBuckets.emplace(hash, b);
mSharedBucketsSize.set_count(mSharedBuckets.size());
}
}
releaseAssert(b);
if (mergeKey)
{
// Second half of the mergeKey record-keeping, above: if we successfully
// adopted (no throw), then (weakly) record the preimage of the hash.
mFinishedMerges.recordMerge(*mergeKey, hash);
}
return b;
}
void
BucketManagerImpl::noteEmptyMergeOutput(MergeKey const& mergeKey)
{
releaseAssertOrThrow(mApp.getConfig().MODE_ENABLES_BUCKETLIST);
// We _do_ want to remove the mergeKey from mLiveFutures, both so that that
// map does not grow without bound and more importantly so that we drop the
// refcount on the input buckets so they get GC'ed from the bucket dir.
//
// But: we do _not_ want to store the empty merge in mFinishedMerges,
// despite it being a theoretically meaningful place to record empty merges,
// because it'd over-identify multiple individual inputs with the empty
// output, potentially retaining far too many inputs, as lots of different
// mergeKeys result in an empty output.
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
CLOG_TRACE(Bucket, "BucketManager::noteEmptyMergeOutput({})", mergeKey);
mLiveFutures.erase(mergeKey);
}
std::shared_ptr<Bucket>
BucketManagerImpl::getBucketIfExists(uint256 const& hash)
{
ZoneScoped;
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
auto i = mSharedBuckets.find(hash);
if (i != mSharedBuckets.end())
{
CLOG_TRACE(Bucket,
"BucketManager::getBucketIfExists({}) found bucket {}",
binToHex(hash), i->second->getFilename());
return i->second;
}
return nullptr;
}
std::shared_ptr<Bucket>
BucketManagerImpl::getBucketByHash(uint256 const& hash)
{
ZoneScoped;
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
if (isZero(hash))
{
return std::make_shared<Bucket>();
}
auto i = mSharedBuckets.find(hash);
if (i != mSharedBuckets.end())
{
CLOG_TRACE(Bucket, "BucketManager::getBucketByHash({}) found bucket {}",
binToHex(hash), i->second->getFilename());
return i->second;
}
std::string canonicalName = bucketFilename(hash);
if (fs::exists(canonicalName))
{
CLOG_TRACE(Bucket,
"BucketManager::getBucketByHash({}) found no bucket, making "
"new one",
binToHex(hash));
auto p =
std::make_shared<Bucket>(canonicalName, hash, /*index=*/nullptr);
mSharedBuckets.emplace(hash, p);
mSharedBucketsSize.set_count(mSharedBuckets.size());
return p;
}
return std::shared_ptr<Bucket>();
}
std::shared_future<std::shared_ptr<Bucket>>
BucketManagerImpl::getMergeFuture(MergeKey const& key)
{
ZoneScoped;
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
MergeCounters mc;
auto i = mLiveFutures.find(key);
if (i == mLiveFutures.end())
{
// If there's no live (running) future, we might be able to _make_ one
// for a retained bucket, if we still know its inputs.
Hash bucketHash;
if (mFinishedMerges.findMergeFor(key, bucketHash))
{
auto bucket = getBucketByHash(bucketHash);
if (bucket)
{
CLOG_TRACE(Bucket,
"BucketManager::getMergeFuture returning new future "
"for finished merge {} with output={}",
key, hexAbbrev(bucketHash));
std::promise<std::shared_ptr<Bucket>> promise;
auto future = promise.get_future().share();
promise.set_value(bucket);
mc.mFinishedMergeReattachments++;
incrMergeCounters(mc);
return future;
}
}
CLOG_TRACE(
Bucket,
"BucketManager::getMergeFuture returning empty future for merge {}",
key);
return std::shared_future<std::shared_ptr<Bucket>>();
}
CLOG_TRACE(
Bucket,
"BucketManager::getMergeFuture returning running future for merge {}",
key);
mc.mRunningMergeReattachments++;
incrMergeCounters(mc);
return i->second;
}
void
BucketManagerImpl::putMergeFuture(
MergeKey const& key, std::shared_future<std::shared_ptr<Bucket>> wp)
{
ZoneScoped;
releaseAssertOrThrow(mApp.getConfig().MODE_ENABLES_BUCKETLIST);
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
CLOG_TRACE(
Bucket,
"BucketManager::putMergeFuture storing future for running merge {}",
key);
mLiveFutures.emplace(key, wp);
}
#ifdef BUILD_TESTS
void
BucketManagerImpl::clearMergeFuturesForTesting()
{
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
mLiveFutures.clear();
}
#endif
std::set<Hash>
BucketManagerImpl::getBucketListReferencedBuckets() const
{
ZoneScoped;
std::set<Hash> referenced;
if (!mApp.getConfig().MODE_ENABLES_BUCKETLIST)
{
return referenced;
}
// retain current bucket list
for (uint32_t i = 0; i < BucketList::kNumLevels; ++i)
{
auto const& level = mBucketList->getLevel(i);
auto rit = referenced.emplace(level.getCurr()->getHash());
if (rit.second)
{
CLOG_TRACE(Bucket, "{} referenced by bucket list",
binToHex(*rit.first));
}
rit = referenced.emplace(level.getSnap()->getHash());
if (rit.second)
{
CLOG_TRACE(Bucket, "{} referenced by bucket list",
binToHex(*rit.first));
}
for (auto const& h : level.getNext().getHashes())
{
rit = referenced.emplace(hexToBin256(h));
if (rit.second)
{
CLOG_TRACE(Bucket, "{} referenced by bucket list", h);
}
}
}
return referenced;
}
std::set<Hash>
BucketManagerImpl::getAllReferencedBuckets() const
{
ZoneScoped;
auto referenced = getBucketListReferencedBuckets();
if (!mApp.getConfig().MODE_ENABLES_BUCKETLIST)
{
return referenced;
}
// retain any bucket referenced by the last closed ledger as recorded in the
// database (as merges complete, the bucket list drifts from that state)
auto lclHas = mApp.getLedgerManager().getLastClosedLedgerHAS();
auto lclBuckets = lclHas.allBuckets();
for (auto const& h : lclBuckets)
{
auto rit = referenced.emplace(hexToBin256(h));
if (rit.second)
{
CLOG_TRACE(Bucket, "{} referenced by LCL", h);
}
}
// retain buckets that are referenced by a state in the publish queue.
auto pub = mApp.getHistoryManager().getBucketsReferencedByPublishQueue();
{
for (auto const& h : pub)
{
auto rhash = hexToBin256(h);
auto rit = referenced.emplace(rhash);
if (rit.second)
{
CLOG_TRACE(Bucket, "{} referenced by publish queue", h);
// Project referenced bucket `rhash` -- which might be a merge
// input captured before a merge finished -- through our weak
// map of merge input/output relationships, to find any outputs
// we'll want to retain in order to resynthesize the merge in
// the future, rather than re-run it.
mFinishedMerges.getOutputsUsingInput(rhash, referenced);
}
}
}
return referenced;
}
void
BucketManagerImpl::cleanupStaleFiles()
{
ZoneScoped;
if (mApp.getConfig().DISABLE_BUCKET_GC)
{
return;
}
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
auto referenced = getAllReferencedBuckets();
std::transform(std::begin(mSharedBuckets), std::end(mSharedBuckets),
std::inserter(referenced, std::end(referenced)),
[](std::pair<Hash, std::shared_ptr<Bucket>> const& p) {
return p.first;
});
for (auto f : fs::findfiles(getBucketDir(), isBucketFile))
{
auto hash = extractFromFilename(f);
if (referenced.find(hash) == std::end(referenced))
{
// we don't care about failure here
// if removing file failed one time, it may not fail when this is
// called again
auto fullName = getBucketDir() + "/" + f;
std::remove(fullName.c_str());
// GC index as well
auto indexFilename = bucketIndexFilename(hash);
std::remove(indexFilename.c_str());
}
}
}
void
BucketManagerImpl::forgetUnreferencedBuckets()
{
ZoneScoped;
std::lock_guard<std::recursive_mutex> lock(mBucketMutex);
auto referenced = getAllReferencedBuckets();
auto blReferenced = getBucketListReferencedBuckets();
for (auto i = mSharedBuckets.begin(); i != mSharedBuckets.end();)
{
// Standard says map iterators other than the one you're erasing
// remain valid.
auto j = i;
++i;
// Delete indexes for buckets no longer in bucketlist. There is a race
// condition on startup where future buckets for a level will be
// finished and have an index but will not yet be referred to by the
// bucket level's next pointer. Checking use_count == 1 makes sure no
// other in-progress structures will add bucket to bucket list after
// deleting index
if (j->second->isIndexed() && j->second.use_count() == 1 &&
blReferenced.find(j->first) == blReferenced.end())
{
CLOG_TRACE(Bucket,
"BucketManager::forgetUnreferencedBuckets deleting "
"index for {}",
j->second->getFilename());
j->second->freeIndex();
}
// Only drop buckets if the bucketlist has forgotten them _and_
// no other in-progress structures (worker threads, shadow lists)
// have references to them, just us. It's ok to retain a few too
// many buckets, a little longer than necessary.
//
// This conservatism is important because we want to enforce that
// only one bucket ever exists in memory with a given filename, and
// that we're the first and last to know about it. Otherwise buckets
// might race on deleting the underlying file from one another.
if (referenced.find(j->first) == referenced.end() &&
j->second.use_count() == 1)
{
auto filename = j->second->getFilename();
CLOG_TRACE(Bucket,
"BucketManager::forgetUnreferencedBuckets dropping {}",
filename);
if (!filename.empty() && !mApp.getConfig().DISABLE_BUCKET_GC)
{
CLOG_TRACE(Bucket, "removing bucket file: {}", filename);
std::filesystem::remove(filename);
auto gzfilename = filename.string() + ".gz";
std::remove(gzfilename.c_str());
auto indexFilename = bucketIndexFilename(j->second->getHash());
std::remove(indexFilename.c_str());
}
// Dropping this bucket means we'll no longer be able to
// resynthesize a std::shared_future pointing directly to it
// as a short-cut to performing a merge we've already seen.
// Therefore we should forget it from the weak map we use
// for that resynthesis.
for (auto const& forgottenMergeKey :
mFinishedMerges.forgetAllMergesProducing(j->first))
{
// There should be no futures alive with this output: we
// switched to storing only weak input/output mappings
// when any merge producing the bucket completed (in
// adoptFileAsBucket), and we believe there's only one
// reference to the bucket anyways -- our own in
// mSharedBuckets. But there might be a race we missed,
// so double check & mop up here. Worst case we prevent
// a slow memory leak at the cost of redoing merges we
// might have been able to reattach to.
auto f = mLiveFutures.find(forgottenMergeKey);
if (f != mLiveFutures.end())
{
CLOG_WARNING(Bucket,
"Unexpected live future for unreferenced "
"bucket: {}",
binToHex(i->first));
mLiveFutures.erase(f);
}
}
// All done, delete the bucket from the shared map.
mSharedBuckets.erase(j);
}
}
mSharedBucketsSize.set_count(mSharedBuckets.size());
}
void
BucketManagerImpl::addBatch(Application& app, uint32_t currLedger,
uint32_t currLedgerProtocol,
std::vector<LedgerEntry> const& initEntries,
std::vector<LedgerEntry> const& liveEntries,
std::vector<LedgerKey> const& deadEntries)
{
ZoneScoped;
releaseAssertOrThrow(app.getConfig().MODE_ENABLES_BUCKETLIST);
#ifdef BUILD_TESTS
if (mUseFakeTestValuesForNextClose)
{
currLedgerProtocol = mFakeTestProtocolVersion;
}
#endif
auto timer = mBucketAddBatch.TimeScope();
mBucketObjectInsertBatch.Mark(initEntries.size() + liveEntries.size() +
deadEntries.size());
mBucketList->addBatch(app, currLedger, currLedgerProtocol, initEntries,
liveEntries, deadEntries);
mBucketListSizeCounter.set_count(mBucketList->getSize());
}
#ifdef BUILD_TESTS
void
BucketManagerImpl::setNextCloseVersionAndHashForTesting(uint32_t protocolVers,
uint256 const& hash)
{
mUseFakeTestValuesForNextClose = true;
mFakeTestProtocolVersion = protocolVers;
mFakeTestBucketListHash = hash;
}
std::set<Hash>
BucketManagerImpl::getBucketHashesInBucketDirForTesting() const
{
std::set<Hash> hashes;
for (auto f : fs::findfiles(getBucketDir(), isBucketFile))
{
hashes.emplace(extractFromFilename(f));
}
return hashes;
}
medida::Counter&
BucketManagerImpl::getEntriesEvictedCounter() const
{
return mBucketListEvictionCounters.entriesEvicted;
}
#endif
// updates the given LedgerHeader to reflect the current state of the bucket
// list
void
BucketManagerImpl::snapshotLedger(LedgerHeader& currentHeader)
{
ZoneScoped;
Hash hash;
if (mApp.getConfig().MODE_ENABLES_BUCKETLIST)
{
hash = mBucketList->getHash();
}
currentHeader.bucketListHash = hash;
#ifdef BUILD_TESTS
if (mUseFakeTestValuesForNextClose)
{
// Copy fake value and disarm for next close.
currentHeader.bucketListHash = mFakeTestBucketListHash;
mUseFakeTestValuesForNextClose = false;
}
#endif
calculateSkipValues(currentHeader);
}
void
BucketManagerImpl::maybeSetIndex(std::shared_ptr<Bucket> b,
std::unique_ptr<BucketIndex const>&& index)
{
ZoneScoped;
if (!isShutdown() && index && !b->isIndexed())
{
b->setIndex(std::move(index));
}
}
void
BucketManagerImpl::scanForEviction(AbstractLedgerTxn& ltx, uint32_t ledgerSeq)
{
ZoneScoped;
if (protocolVersionStartsFrom(ltx.getHeader().ledgerVersion,
SOROBAN_PROTOCOL_VERSION))
{
mBucketList->scanForEviction(mApp, ltx, ledgerSeq,
mBucketListEvictionCounters);
}
}
medida::Timer&
BucketManagerImpl::recordBulkLoadMetrics(std::string const& label,
size_t numEntries) const
{
if (numEntries != 0)
{
mBucketListDBBulkLoadMeter.Mark(numEntries);
}
auto iter = mBucketListDBBulkTimers.find(label);
if (iter == mBucketListDBBulkTimers.end())
{
auto& metric =
mApp.getMetrics().NewTimer({"bucketlistDB", "bulk", label});
iter = mBucketListDBBulkTimers.emplace(label, metric).first;
}
return iter->second;
}
medida::Timer&
BucketManagerImpl::getPointLoadTimer(LedgerEntryType t) const
{
auto iter = mBucketListDBPointTimers.find(t);
if (iter == mBucketListDBPointTimers.end())
{
auto const& label = xdr::xdr_traits<LedgerEntryType>::enum_name(t);
auto& metric =
mApp.getMetrics().NewTimer({"bucketlistDB", "point", label});
iter = mBucketListDBPointTimers.emplace(t, metric).first;
}
return iter->second;
}
std::shared_ptr<LedgerEntry>
BucketManagerImpl::getLedgerEntry(LedgerKey const& k) const
{
releaseAssertOrThrow(getConfig().isUsingBucketListDB());
auto timer = getPointLoadTimer(k.type()).TimeScope();
return mBucketList->getLedgerEntry(k);
}
std::vector<LedgerEntry>
BucketManagerImpl::loadKeys(
std::set<LedgerKey, LedgerEntryIdCmp> const& keys) const
{
releaseAssertOrThrow(getConfig().isUsingBucketListDB());
auto timer = recordBulkLoadMetrics("prefetch", keys.size()).TimeScope();
return mBucketList->loadKeys(keys);
}
std::vector<LedgerEntry>
BucketManagerImpl::loadPoolShareTrustLinesByAccountAndAsset(
AccountID const& accountID, Asset const& asset) const
{
releaseAssertOrThrow(getConfig().isUsingBucketListDB());
// This query needs to do a linear scan of certain regions of the
// BucketList, so the number of entries loaded is meaningless
auto timer = recordBulkLoadMetrics("poolshareTrustlines", 0).TimeScope();
return mBucketList->loadPoolShareTrustLinesByAccountAndAsset(accountID,
asset);
}
std::vector<InflationWinner>
BucketManagerImpl::loadInflationWinners(size_t maxWinners,
int64_t minBalance) const
{
releaseAssertOrThrow(getConfig().isUsingBucketListDB());
// This query needs to do a linear scan of certain regions of the
// BucketList, so the number of entries loaded is meaningless
auto timer = recordBulkLoadMetrics("inflationWinners", 0).TimeScope();
return mBucketList->loadInflationWinners(maxWinners, minBalance);
}
medida::Meter&
BucketManagerImpl::getBloomMissMeter() const
{
return mBucketListDBBloomMisses;
}