Skip to content

Commit 9a4efc4

Browse files
committed
Thread safe metrics for BucketsDB
1 parent f0c218b commit 9a4efc4

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/bucket/SearchableBucketListSnapshot.cpp

+46-11
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,20 @@ SearchableBucketListSnapshot::getLedgerEntry(LedgerKey const& k)
4242
ZoneScoped;
4343
mSnapshotManager.maybeUpdateSnapshot(*this);
4444

45-
// TODO: Metrics only on main thread
46-
auto timer = mSnapshotManager.getPointLoadTimer(k.type()).TimeScope();
45+
if (threadIsMain())
46+
{
47+
auto timer = mSnapshotManager.getPointLoadTimer(k.type()).TimeScope();
48+
return getLedgerEntryInternal(k);
49+
}
50+
else
51+
{
52+
return getLedgerEntryInternal(k);
53+
}
54+
}
4755

56+
std::shared_ptr<LedgerEntry>
57+
SearchableBucketListSnapshot::getLedgerEntryInternal(LedgerKey const& k)
58+
{
4859
std::shared_ptr<LedgerEntry> result{};
4960

5061
auto f = [&](SearchableBucketSnapshot const& b) {
@@ -68,16 +79,9 @@ SearchableBucketListSnapshot::getLedgerEntry(LedgerKey const& k)
6879
}
6980

7081
std::vector<LedgerEntry>
71-
SearchableBucketListSnapshot::loadKeys(
82+
SearchableBucketListSnapshot::loadKeysInternal(
7283
std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys)
7384
{
74-
ZoneScoped;
75-
mSnapshotManager.maybeUpdateSnapshot(*this);
76-
77-
auto timer =
78-
mSnapshotManager.recordBulkLoadMetrics("prefetch", inKeys.size())
79-
.TimeScope();
80-
8185
std::vector<LedgerEntry> entries;
8286

8387
// Make a copy of the key set, this loop is destructive
@@ -91,6 +95,26 @@ SearchableBucketListSnapshot::loadKeys(
9195
return entries;
9296
}
9397

98+
std::vector<LedgerEntry>
99+
SearchableBucketListSnapshot::loadKeys(
100+
std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys)
101+
{
102+
ZoneScoped;
103+
mSnapshotManager.maybeUpdateSnapshot(*this);
104+
105+
if (threadIsMain())
106+
{
107+
auto timer =
108+
mSnapshotManager.recordBulkLoadMetrics("prefetch", inKeys.size())
109+
.TimeScope();
110+
return loadKeysInternal(inKeys);
111+
}
112+
else
113+
{
114+
return loadKeysInternal(inKeys);
115+
}
116+
}
117+
94118
// This query has two steps:
95119
// 1. For each bucket, determine what PoolIDs contain the target asset via the
96120
// assetToPoolID index
@@ -101,6 +125,9 @@ SearchableBucketListSnapshot::loadPoolShareTrustLinesByAccountAndAsset(
101125
AccountID const& accountID, Asset const& asset)
102126
{
103127
ZoneScoped;
128+
129+
// This query should only be called during TX apply
130+
releaseAssert(threadIsMain());
104131
mSnapshotManager.maybeUpdateSnapshot(*this);
105132

106133
LedgerKeySet trustlinesToLoad;
@@ -119,7 +146,12 @@ SearchableBucketListSnapshot::loadPoolShareTrustLinesByAccountAndAsset(
119146
};
120147

121148
loopAllBuckets(trustLineLoop);
122-
return loadKeys(trustlinesToLoad);
149+
150+
auto timer = mSnapshotManager
151+
.recordBulkLoadMetrics("poolshareTrustlines",
152+
trustlinesToLoad.size())
153+
.TimeScope();
154+
return loadKeysInternal(trustlinesToLoad);
123155
}
124156

125157
std::vector<InflationWinner>
@@ -129,6 +161,9 @@ SearchableBucketListSnapshot::loadInflationWinners(size_t maxWinners,
129161
ZoneScoped;
130162
mSnapshotManager.maybeUpdateSnapshot(*this);
131163

164+
// This is a legacy query, should only be called by main thread during
165+
// catchup
166+
releaseAssert(threadIsMain());
132167
auto timer = mSnapshotManager.recordBulkLoadMetrics("inflationWinners", 0)
133168
.TimeScope();
134169

src/bucket/SearchableBucketListSnapshot.h

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class SearchableBucketListSnapshot : public NonMovableOrCopyable
4444
SearchableBucketListSnapshot(BucketSnapshotManager const& snapshotManager,
4545
BucketList const& bl);
4646

47+
std::vector<LedgerEntry>
48+
loadKeysInternal(std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys);
49+
50+
std::shared_ptr<LedgerEntry> getLedgerEntryInternal(LedgerKey const& k);
51+
4752
public:
4853
std::vector<LedgerEntry>
4954
loadKeys(std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys);

0 commit comments

Comments
 (0)