43
43
namespace stellar
44
44
{
45
45
46
+ void
47
+ EvictionStatistics::recordEvictedEntry (uint64_t age)
48
+ {
49
+ std::lock_guard l (mLock );
50
+ ++mNumEntriesEvicted ;
51
+ mEvictedEntriesAgeSum += age;
52
+ }
53
+
54
+ void
55
+ EvictionStatistics::submitMetricsAndRestartCycle (uint32_t currLedgerSeq,
56
+ EvictionCounters& counters)
57
+ {
58
+ std::lock_guard l (mLock );
59
+
60
+ // Only record metrics if we've seen a complete cycle to avoid noise
61
+ if (mCompleteCycle )
62
+ {
63
+ counters.evictionCyclePeriod .set_count (currLedgerSeq -
64
+ mEvictionCycleStartLedger );
65
+
66
+ auto averageAge = mNumEntriesEvicted == 0
67
+ ? 0
68
+ : mEvictedEntriesAgeSum / mNumEntriesEvicted ;
69
+ counters.averageEvictedEntryAge .set_count (averageAge);
70
+ }
71
+
72
+ // Reset to start new cycle
73
+ mCompleteCycle = true ;
74
+ mEvictedEntriesAgeSum = 0 ;
75
+ mNumEntriesEvicted = 0 ;
76
+ mEvictionCycleStartLedger = currLedgerSeq;
77
+ }
78
+
46
79
std::unique_ptr<BucketManager>
47
80
BucketManager::create (Application& app)
48
81
{
@@ -147,6 +180,7 @@ BucketManagerImpl::BucketManagerImpl(Application& app)
147
180
, mBucketListSizeCounter (
148
181
app.getMetrics().NewCounter({" bucketlist" , " size" , " bytes" }))
149
182
, mBucketListEvictionCounters (app)
183
+ , mEvictionStatistics (std::make_shared<EvictionStatistics>())
150
184
// Minimal DB is stored in the buckets dir, so delete it only when
151
185
// mode does not use minimal DB
152
186
, mDeleteEntireBucketDirInDtor (
@@ -958,6 +992,7 @@ BucketManagerImpl::startBackgroundEvictionScan(uint32_t ledgerSeq)
958
992
releaseAssert (mApp .getConfig ().isUsingBucketListDB ());
959
993
releaseAssert (mSnapshotManager );
960
994
releaseAssert (!mEvictionFuture .valid ());
995
+ releaseAssert (mEvictionStatistics );
961
996
962
997
auto searchableBL = mSnapshotManager ->getSearchableBucketListSnapshot ();
963
998
auto const & cfg = mApp .getLedgerManager ().getSorobanNetworkConfig ();
@@ -966,8 +1001,7 @@ BucketManagerImpl::startBackgroundEvictionScan(uint32_t ledgerSeq)
966
1001
using task_t = std::packaged_task<EvictionResult ()>;
967
1002
auto task = std::make_shared<task_t >(
968
1003
[bl = move (searchableBL), iter = cfg.evictionIterator (), ledgerSeq, sas,
969
- &counters = mBucketListEvictionCounters ,
970
- &stats = mEvictionStatistics ] {
1004
+ &counters = mBucketListEvictionCounters , stats = mEvictionStatistics ] {
971
1005
return bl->scanForEviction (ledgerSeq, counters, iter, stats, sas);
972
1006
});
973
1007
@@ -984,6 +1018,7 @@ BucketManagerImpl::resolveBackgroundEvictionScan(
984
1018
{
985
1019
ZoneScoped;
986
1020
releaseAssert (threadIsMain ());
1021
+ releaseAssert (mEvictionStatistics );
987
1022
988
1023
if (!mEvictionFuture .valid ())
989
1024
{
@@ -1032,13 +1067,9 @@ BucketManagerImpl::resolveBackgroundEvictionScan(
1032
1067
ltx.erase (getTTLKey (entryToEvictIter->key ));
1033
1068
--remainingEntriesToEvict;
1034
1069
1070
+ auto age = ledgerSeq - entryToEvictIter->liveUntilLedger ;
1071
+ mEvictionStatistics ->recordEvictedEntry (age);
1035
1072
mBucketListEvictionCounters .entriesEvicted .inc ();
1036
- if (mEvictionStatistics .has_value ())
1037
- {
1038
- ++mEvictionStatistics ->numEntriesEvicted ;
1039
- mEvictionStatistics ->evictedEntriesAgeSum +=
1040
- ledgerSeq - entryToEvictIter->liveUntilLedger ;
1041
- }
1042
1073
1043
1074
newEvictionIterator = entryToEvictIter->iter ;
1044
1075
entryToEvictIter = eligibleKeys.erase (entryToEvictIter);
0 commit comments