Skip to content

Commit 1c22cf6

Browse files
committed
Move SimpleTimer registry ownership to BucketSnapshotManager
1 parent a4b945b commit 1c22cf6

File tree

5 files changed

+37
-54
lines changed

5 files changed

+37
-54
lines changed

src/bucket/BucketListSnapshotBase.cpp

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -170,50 +170,6 @@ BucketLevelSnapshot<BucketT>::BucketLevelSnapshot(
170170
{
171171
}
172172

173-
template <class BucketT>
174-
static UnorderedMap<
175-
medida::MetricsRegistry*,
176-
UnorderedMap<LedgerEntryType, SimpleTimer<std::chrono::microseconds>>>
177-
gPointTimers;
178-
179-
template <class BucketT> static std::mutex gPointTimerLock;
180-
181-
template <class BucketT>
182-
static UnorderedMap<LedgerEntryType, SimpleTimer<std::chrono::microseconds>>&
183-
getPointTimers(medida::MetricsRegistry& registry)
184-
{
185-
std::lock_guard guard{gPointTimerLock<BucketT>};
186-
auto& res = gPointTimers<BucketT>[&registry];
187-
if (res.empty())
188-
{
189-
// Initialize point load timers for each LedgerEntry type
190-
for (auto t : xdr::xdr_traits<LedgerEntryType>::enum_values())
191-
{
192-
auto const& label = xdr::xdr_traits<LedgerEntryType>::enum_name(
193-
static_cast<LedgerEntryType>(t));
194-
res.emplace(std::piecewise_construct,
195-
std::forward_as_tuple(static_cast<LedgerEntryType>(t)),
196-
std::forward_as_tuple(registry, BucketT::METRIC_STRING,
197-
label, "",
198-
std::chrono::seconds(30)));
199-
}
200-
}
201-
return res;
202-
}
203-
204-
void
205-
unregisterSimpleTimers(medida::MetricsRegistry& registry)
206-
{
207-
{
208-
std::lock_guard guard{gPointTimerLock<LiveBucket>};
209-
gPointTimers<LiveBucket>.erase(&registry);
210-
}
211-
{
212-
std::lock_guard guard{gPointTimerLock<HotArchiveBucket>};
213-
gPointTimers<HotArchiveBucket>.erase(&registry);
214-
}
215-
}
216-
217173
template <class BucketT>
218174
SearchableBucketListSnapshotBase<BucketT>::SearchableBucketListSnapshotBase(
219175
BucketSnapshotManager const& snapshotManager, AppConnector const& app,
@@ -223,14 +179,20 @@ SearchableBucketListSnapshotBase<BucketT>::SearchableBucketListSnapshotBase(
223179
, mSnapshot(std::move(snapshot))
224180
, mHistoricalSnapshots(std::move(historicalSnapshots))
225181
, mAppConnector(app)
226-
, mPointTimers{getPointTimers<BucketT>(app.getMetrics())}
227182
, mBulkLoadMeter(app.getMetrics().NewMeter(
228183
{BucketT::METRIC_STRING, "query", "loads"}, "query"))
229184
, mBloomMisses(app.getMetrics().NewMeter(
230185
{BucketT::METRIC_STRING, "bloom", "misses"}, "bloom"))
231186
, mBloomLookups(app.getMetrics().NewMeter(
232187
{BucketT::METRIC_STRING, "bloom", "lookups"}, "bloom"))
233188
{
189+
for (auto t : xdr::xdr_traits<LedgerEntryType>::enum_values())
190+
{
191+
auto const& label = xdr::xdr_traits<LedgerEntryType>::enum_name(
192+
static_cast<LedgerEntryType>(t));
193+
auto& timer = mSnapshotManager.getTimer(BucketT::METRIC_STRING, label);
194+
mPointTimers.emplace(static_cast<LedgerEntryType>(t), timer);
195+
}
234196
}
235197

236198
template <class BucketT>

src/bucket/BucketListSnapshotBase.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ class SearchableBucketListSnapshotBase : public NonMovableOrCopyable
9393
// Tracks the sum of point load times for each LedgerEntryType, in
9494
// microseconds. For point loads, Timers are too expensive to maintain, so
9595
// we use SimpleTimer.
96-
UnorderedMap<LedgerEntryType, SimpleTimer<std::chrono::microseconds>>&
97-
mPointTimers;
96+
UnorderedMap<LedgerEntryType, SimpleTimer<std::chrono::microseconds>&>
97+
mPointTimers{};
9898

9999
// Bulk load timers take significantly longer, so the timer overhead is
100100
// comparatively negligible.
@@ -144,8 +144,4 @@ class SearchableBucketListSnapshotBase : public NonMovableOrCopyable
144144
std::shared_ptr<typename BucketT::LoadT const>
145145
load(LedgerKey const& k) const;
146146
};
147-
148-
// Remove the storage holding the SimpleTimers associated with the given
149-
// registry
150-
void unregisterSimpleTimers(medida::MetricsRegistry& registry);
151147
}

src/bucket/BucketManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ BucketManager::getBucketDir() const
256256
BucketManager::~BucketManager()
257257
{
258258

259-
unregisterSimpleTimers(mApp.getMetrics());
260259
deleteTmpDirAndUnlockBucketDir();
261260
}
262261

src/bucket/BucketSnapshotManager.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,19 @@ BucketSnapshotManager::updateCurrentSnapshot(
192192
updateSnapshot(mCurrHotArchiveSnapshot, mHotArchiveHistoricalSnapshots,
193193
hotArchiveSnapshot);
194194
}
195-
}
195+
196+
SimpleTimer<std::chrono::microseconds>&
197+
BucketSnapshotManager::getTimer(const std::string& domain,
198+
const std::string& type) const
199+
{
200+
auto key = std::make_pair(domain, type);
201+
MutexLocker lock{mSimpleTimerRegistryMutex};
202+
if (mSimpleTimerRegistry.find(key) == mSimpleTimerRegistry.end())
203+
{
204+
mSimpleTimerRegistry.try_emplace(key, mAppConnector.getMetrics(),
205+
domain, type, "",
206+
std::chrono::seconds{30});
207+
}
208+
return mSimpleTimerRegistry.find(key)->second;
209+
}
210+
}

src/bucket/BucketSnapshotManager.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "bucket/LiveBucket.h"
1010
#include "main/AppConnector.h"
1111
#include "util/NonCopyable.h"
12+
#include "util/SimpleTimer.h"
1213
#include "util/ThreadAnnotations.h"
1314

1415
#include <map>
@@ -49,6 +50,11 @@ class BucketSnapshotManager : NonMovableOrCopyable
4950
// Lock must be held when accessing any member variables holding snapshots
5051
mutable SharedMutex mSnapshotMutex;
5152

53+
mutable Mutex mSimpleTimerRegistryMutex;
54+
mutable std::map<std::pair<std::string, std::string>,
55+
SimpleTimer<std::chrono::microseconds>>
56+
mSimpleTimerRegistry;
57+
5258
// Snapshot that is maintained and periodically updated by BucketManager on
5359
// the main thread. When background threads need to generate or refresh a
5460
// snapshot, they will copy this snapshot.
@@ -117,5 +123,10 @@ class BucketSnapshotManager : NonMovableOrCopyable
117123
SearchableSnapshotConstPtr& liveSnapshot,
118124
SearchableHotArchiveSnapshotConstPtr& hotArchiveSnapshot)
119125
LOCKS_EXCLUDED(mSnapshotMutex);
126+
127+
// Called in SearchableBucketListSnapshotBase to get access to the same
128+
// SimpleTimers across snapshots
129+
SimpleTimer<std::chrono::microseconds>&
130+
getTimer(const std::string& domain, const std::string& type) const;
120131
};
121-
}
132+
}

0 commit comments

Comments
 (0)