Skip to content

Commit 59308d3

Browse files
Anton Likhtarovfacebook-github-bot
Anton Likhtarov
authored andcommitted
Make a StatsApi object instance-local
Summary: If we spin up multiple mcrouter instances in a process we want to have separate stats collection for each. This removes the global singleton. Differential Revision: D69613438 fbshipit-source-id: 7defc5714a4cf50a421a43053f15ad1cab1ebda3
1 parent 81067f5 commit 59308d3

8 files changed

+176
-75
lines changed

third-party/mcrouter/src/mcrouter/CarbonRouterInstanceBase.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,8 @@ CarbonRouterInstanceBase::CarbonRouterInstanceBase(McrouterOptions inputOptions)
7676
configApi_(createConfigApi(opts_)),
7777
rtVarsData_(std::make_shared<ObservableRuntimeVars>()),
7878
leaseTokenMap_(globalFunctionScheduler.try_get()),
79-
statsUpdateFunctionHandle_(statsUpdateFunctionName(opts_.router_name)) {
80-
if (gStatsApiInitHook) {
81-
gStatsApiInitHook(*this);
82-
}
83-
79+
statsUpdateFunctionHandle_(statsUpdateFunctionName(opts_.router_name)),
80+
statsApi_(gMakeStatsApiHook ? gMakeStatsApiHook(*this) : nullptr) {
8481
if (auto statsLogger = statsLogWriter()) {
8582
if (opts_.stats_async_queue_length) {
8683
statsLogger->increaseMaxQueueSize(opts_.stats_async_queue_length);

third-party/mcrouter/src/mcrouter/CarbonRouterInstanceBase.h

+6
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ class CarbonRouterInstanceBase {
254254
axonProxyClientFactory_ = std::move(clientFactory);
255255
}
256256

257+
StatsApi* statsApi() {
258+
return statsApi_.get();
259+
}
260+
257261
/**
258262
* Runtime features that can be enabled from runtime_features block
259263
* in routing config
@@ -352,6 +356,8 @@ class CarbonRouterInstanceBase {
352356
std::shared_ptr<void> metadata_;
353357

354358
std::shared_ptr<void> axonProxyClientFactory_;
359+
360+
std::unique_ptr<StatsApi> statsApi_;
355361
};
356362

357363
} // namespace mcrouter

third-party/mcrouter/src/mcrouter/ProxyBase-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ProxyBase::ProxyBase(
3232
std::make_unique<folly::fibers::EventBaseLoopController>(),
3333
getFiberManagerOptions(router_.opts())),
3434
asyncLog_(router_.opts()),
35-
stats_(router_.getStatsEnabledPools()),
35+
stats_(router_.getStatsEnabledPools(), router_.statsApi()),
3636
flushCallback_(*this),
3737
destinationMap_(std::make_unique<ProxyDestinationMap>(this)) {
3838
// Setup a full random seed sequence

third-party/mcrouter/src/mcrouter/ProxyStats.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ namespace facebook {
1111
namespace memcache {
1212
namespace mcrouter {
1313

14-
ProxyStats::ProxyStats(const std::vector<std::string>& statsEnabledPools) {
14+
ProxyStats::ProxyStats(
15+
const std::vector<std::string>& statsEnabledPools,
16+
StatsApi* statsApi)
17+
: statsApi_(statsApi) {
1518
init_stats(stats_);
1619
poolStats_.reserve(statsEnabledPools.size());
1720
for (const auto& curPoolName : statsEnabledPools) {

third-party/mcrouter/src/mcrouter/ProxyStats.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ namespace mcrouter {
2121

2222
class ProxyStats {
2323
public:
24-
explicit ProxyStats(const std::vector<std::string>& statsEnabledPools);
24+
explicit ProxyStats(
25+
const std::vector<std::string>& statsEnabledPools,
26+
StatsApi* statsApi);
2527

2628
/**
2729
* Aggregate proxy stat with the given index.
@@ -107,7 +109,7 @@ class ProxyStats {
107109
* @param amount Amount to increment the stat
108110
*/
109111
void increment(stat_name_t stat, int64_t amount = 1) {
110-
stat_incr(stats_, stat, amount);
112+
stat_incr(stats_, statsApi_, stat, amount);
111113
}
112114

113115
/**
@@ -127,7 +129,7 @@ class ProxyStats {
127129
* @param amount Amount to increment the stat
128130
*/
129131
void incrementSafe(stat_name_t stat, int64_t amount = 1) {
130-
stat_fetch_add(stats_, stat, amount);
132+
stat_fetch_add(stats_, statsApi_, stat, amount);
131133
}
132134

133135
/**
@@ -147,7 +149,7 @@ class ProxyStats {
147149
* @param newValue New value of the stat
148150
*/
149151
void setValue(stat_name_t stat, uint64_t newValue) {
150-
stat_set(stats_, stat, newValue);
152+
stat_set(stats_, statsApi_, stat, newValue);
151153
}
152154

153155
uint64_t getValue(stat_name_t stat) {
@@ -190,6 +192,7 @@ class ProxyStats {
190192
stat_t stats_[num_stats]{};
191193
// vector of the PoolStats
192194
std::vector<PoolStats> poolStats_;
195+
StatsApi* statsApi_{nullptr};
193196

194197
ExponentialSmoothData<64> durationUs_;
195198
// Duration microseconds, broken down by get-like request type

third-party/mcrouter/src/mcrouter/TargetHooks.h

+8-10
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99

1010
#include <folly/Portability.h>
1111
#include <folly/executors/IOThreadPoolExecutor.h>
12-
#include "mcrouter/options.h"
1312

1413
namespace facebook {
1514
namespace memcache {
15+
16+
class McrouterOptions;
17+
1618
namespace mcrouter {
1719

20+
class CarbonRouterInstanceBase;
21+
class StatsApi;
22+
1823
/**
1924
* If linked, initializes and reports utilization to RIM.
2025
*/
@@ -33,19 +38,12 @@ FOLLY_ATTR_WEAK void gAxonInitHook(
3338
CarbonRouterInstanceBase& router,
3439
std::shared_ptr<folly::IOThreadPoolExecutorBase> ioThreadPool);
3540

36-
class StatsApi;
37-
38-
/**
39-
* If linked, returns a reference to a StatsApi implementation allowing
40-
* custom stats handling.
41-
*/
42-
FOLLY_ATTR_WEAK StatsApi& gStatsApiHook();
43-
4441
/**
4542
* If linked, will be called once on router initialization with the intent
4643
* to initialize the custom StatsApi implementation.
4744
*/
48-
FOLLY_ATTR_WEAK void gStatsApiInitHook(const CarbonRouterInstanceBase& router);
45+
FOLLY_ATTR_WEAK std::unique_ptr<StatsApi> gMakeStatsApiHook(
46+
const CarbonRouterInstanceBase& router);
4947

5048
} // namespace mcrouter
5149
} // namespace memcache

0 commit comments

Comments
 (0)