diff --git a/mcrouter/ServiceInfo-inl.h b/mcrouter/ServiceInfo-inl.h index 3eb5db965..27692e93d 100644 --- a/mcrouter/ServiceInfo-inl.h +++ b/mcrouter/ServiceInfo-inl.h @@ -51,19 +51,21 @@ class RouteHandlesCommandDispatcher { public: bool dispatch( size_t typeId, + const std::shared_ptr>& ctx, folly::StringPiece key, const ProxyRoute& proxyRoute, std::string& outStr) { - return dispatcher_.dispatch(typeId, *this, key, proxyRoute, outStr); + return dispatcher_.dispatch(typeId, *this, ctx, key, proxyRoute, outStr); } template static void processMsg( RouteHandlesCommandDispatcher& me, + const std::shared_ptr>& ctx, folly::StringPiece key, const ProxyRoute& proxyRoute, std::string& outStr) { - outStr = me.processMsgInternal(key, proxyRoute); + outStr = me.processMsgInternal(ctx, key, proxyRoute); } private: @@ -73,6 +75,7 @@ class RouteHandlesCommandDispatcher { // Dispatcher class detail::RouteHandlesCommandDispatcher, // List of types of args to Dispatcher::processMsg() + const std::shared_ptr>&, folly::StringPiece, const ProxyRoute&, std::string&> @@ -80,6 +83,7 @@ class RouteHandlesCommandDispatcher { template std::string processMsgInternal( + const std::shared_ptr>& ctx, folly::StringPiece key, const ProxyRoute& proxyRoute) { std::string tree; @@ -111,6 +115,7 @@ class RouteHandlesCommandDispatcher { } else { request.key_ref() = key; } + fiber_local::setSharedCtx(ctx); proxyRoute.traverse(request, t); return tree; } @@ -411,8 +416,10 @@ ServiceInfo::ServiceInfoImpl::ServiceInfoImpl( requestName, typename RouterInfo::RoutableRequests()); std::string res; + auto ctx = ProxyRequestContextWithInfo::createRecording( + proxy_, nullptr); if (!routeHandlesCommandDispatcher_.dispatch( - typeId, key, proxyRoute_, res)) { + typeId, ctx, key, proxyRoute_, res)) { throw std::runtime_error( folly::sformat("route: unknown request {}", requestName)); } diff --git a/mcrouter/lib/HashSelector.h b/mcrouter/lib/HashSelector.h index da9432828..5514f2eba 100644 --- a/mcrouter/lib/HashSelector.h +++ b/mcrouter/lib/HashSelector.h @@ -97,7 +97,9 @@ class BucketHashSelector : public HashSelectorBase { auto key = folly::to(*bucketId); if (this->clientFanout_) { auto& ctx = mcrouter::fiber_local::getSharedCtx(); - key += ctx->userIpAddress(); + if (ctx) { + key += ctx->userIpAddress(); + } } return folly::fibers::runInMainContext( [this, size, key]() { return this->selectInternal(key, size); });