refactor SaplingImportRequest to keep ObjectFetchContext

Summary:
## Background
`SaplingBackingStore` is now sending `request` to Sapling with `RemoteOnly` or `LocalOnly`. Therefore `SaplingBackingStore` knows the source of fetching each request. We need to save this info in `ObjectFetchContext` then we can send them to `HgImportTraceEvent::finish` and `traceBus_` can publish this info in `trace hg` command.

## This diff
`ObjectFetchContext` is not attached to `request` and when the `request` is fulfilled we cannot update the context. This diff adds a field `context_` to the `SaplingImportRequest` class then each request can access its own context.

Reviewed By: genevievehelsel

Differential Revision: D56221059

fbshipit-source-id: 2c4f73bfa86912c3189204a3c8c01a528dab0715
This commit is contained in:
Kaveh Ahmadi 2024-05-02 14:02:57 -07:00 committed by Facebook GitHub Bot
parent 26d95a67f2
commit 80411641b4
6 changed files with 62 additions and 110 deletions

View File

@ -1122,12 +1122,9 @@ SaplingBackingStore::getTreeEnqueue(
const HgProxyHash& proxyHash, const HgProxyHash& proxyHash,
const ObjectFetchContextPtr& context) { const ObjectFetchContextPtr& context) {
auto getTreeFuture = makeImmediateFutureWith([&] { auto getTreeFuture = makeImmediateFutureWith([&] {
auto requestContext = context.copy();
auto request = SaplingImportRequest::makeTreeImportRequest( auto request = SaplingImportRequest::makeTreeImportRequest(
id, id, proxyHash, requestContext);
proxyHash,
context->getPriority(),
context->getCause(),
context->getClientPid());
uint64_t unique = request->getUnique(); uint64_t unique = request->getUnique();
auto importTracker = auto importTracker =
@ -1251,13 +1248,9 @@ ImmediateFuture<BackingStore::GetBlobResult> SaplingBackingStore::getBlobImpl(
auto getBlobFuture = makeImmediateFutureWith([&] { auto getBlobFuture = makeImmediateFutureWith([&] {
XLOG(DBG4) << "make blob import request for " << proxyHash.path() XLOG(DBG4) << "make blob import request for " << proxyHash.path()
<< ", hash is:" << id; << ", hash is:" << id;
auto requestContext = context.copy();
auto request = SaplingImportRequest::makeBlobImportRequest( auto request = SaplingImportRequest::makeBlobImportRequest(
id, id, proxyHash, requestContext);
proxyHash,
context->getPriority(),
context->getCause(),
context->getClientPid());
auto unique = request->getUnique(); auto unique = request->getUnique();
auto importTracker = auto importTracker =
@ -1341,13 +1334,9 @@ SaplingBackingStore::getBlobMetadataImpl(
auto getBlobMetaFuture = makeImmediateFutureWith([&] { auto getBlobMetaFuture = makeImmediateFutureWith([&] {
XLOG(DBG4) << "make blob meta import request for " << proxyHash.path() XLOG(DBG4) << "make blob meta import request for " << proxyHash.path()
<< ", hash is:" << id; << ", hash is:" << id;
auto requestContext = context.copy();
auto request = SaplingImportRequest::makeBlobMetaImportRequest( auto request = SaplingImportRequest::makeBlobMetaImportRequest(
id, id, proxyHash, requestContext);
proxyHash,
context->getPriority(),
context->getCause(),
context->getClientPid());
auto unique = request->getUnique(); auto unique = request->getUnique();
auto importTracker = auto importTracker =

View File

@ -15,59 +15,44 @@ namespace facebook::eden {
template <typename RequestType> template <typename RequestType>
SaplingImportRequest::SaplingImportRequest( SaplingImportRequest::SaplingImportRequest(
RequestType request, RequestType request,
ImportPriority priority, const ObjectFetchContextPtr& context,
ObjectFetchContext::Cause cause,
OptionalProcessId pid,
folly::Promise<typename RequestType::Response>&& promise) folly::Promise<typename RequestType::Response>&& promise)
: request_(std::move(request)), : request_(std::move(request)),
priority_(priority), context_(context.copy()),
cause_(cause), priority_(context_->getPriority()),
pid_(pid),
promise_(std::move(promise)) {} promise_(std::move(promise)) {}
template <typename RequestType, typename... Input> template <typename RequestType, typename... Input>
std::shared_ptr<SaplingImportRequest> SaplingImportRequest::makeRequest( std::shared_ptr<SaplingImportRequest> SaplingImportRequest::makeRequest(
ImportPriority priority, const ObjectFetchContextPtr& context,
ObjectFetchContext::Cause cause,
OptionalProcessId pid,
Input&&... input) { Input&&... input) {
auto promise = folly::Promise<typename RequestType::Response>{}; auto promise = folly::Promise<typename RequestType::Response>{};
return std::make_shared<SaplingImportRequest>( return std::make_shared<SaplingImportRequest>(
RequestType{std::forward<Input>(input)...}, RequestType{std::forward<Input>(input)...}, context, std::move(promise));
priority,
cause,
pid,
std::move(promise));
} }
std::shared_ptr<SaplingImportRequest> std::shared_ptr<SaplingImportRequest>
SaplingImportRequest::makeBlobImportRequest( SaplingImportRequest::makeBlobImportRequest(
const ObjectId& hash, const ObjectId& hash,
const HgProxyHash& proxyHash, const HgProxyHash& proxyHash,
ImportPriority priority, const ObjectFetchContextPtr& context) {
ObjectFetchContext::Cause cause, return makeRequest<BlobImport>(context, hash, proxyHash);
OptionalProcessId pid) {
return makeRequest<BlobImport>(priority, cause, pid, hash, proxyHash);
} }
std::shared_ptr<SaplingImportRequest> std::shared_ptr<SaplingImportRequest>
SaplingImportRequest::makeTreeImportRequest( SaplingImportRequest::makeTreeImportRequest(
const ObjectId& hash, const ObjectId& hash,
const HgProxyHash& proxyHash, const HgProxyHash& proxyHash,
ImportPriority priority, const ObjectFetchContextPtr& context) {
ObjectFetchContext::Cause cause, return makeRequest<TreeImport>(context, hash, proxyHash);
OptionalProcessId pid) {
return makeRequest<TreeImport>(priority, cause, pid, hash, proxyHash);
} }
std::shared_ptr<SaplingImportRequest> std::shared_ptr<SaplingImportRequest>
SaplingImportRequest::makeBlobMetaImportRequest( SaplingImportRequest::makeBlobMetaImportRequest(
const ObjectId& hash, const ObjectId& hash,
const HgProxyHash& proxyHash, const HgProxyHash& proxyHash,
ImportPriority priority, const ObjectFetchContextPtr& context) {
ObjectFetchContext::Cause cause, return makeRequest<BlobMetaImport>(context, hash, proxyHash);
OptionalProcessId pid) {
return makeRequest<BlobMetaImport>(priority, cause, pid, hash, proxyHash);
} }
} // namespace facebook::eden } // namespace facebook::eden

View File

@ -55,9 +55,7 @@ class SaplingImportRequest {
static std::shared_ptr<SaplingImportRequest> makeBlobImportRequest( static std::shared_ptr<SaplingImportRequest> makeBlobImportRequest(
const ObjectId& hash, const ObjectId& hash,
const HgProxyHash& proxyHash, const HgProxyHash& proxyHash,
ImportPriority priority, const ObjectFetchContextPtr& context);
ObjectFetchContext::Cause cause,
OptionalProcessId pid);
/** /**
* Allocate a tree request. * Allocate a tree request.
@ -65,16 +63,12 @@ class SaplingImportRequest {
static std::shared_ptr<SaplingImportRequest> makeTreeImportRequest( static std::shared_ptr<SaplingImportRequest> makeTreeImportRequest(
const ObjectId& hash, const ObjectId& hash,
const HgProxyHash& proxyHash, const HgProxyHash& proxyHash,
ImportPriority priority, const ObjectFetchContextPtr& context);
ObjectFetchContext::Cause cause,
OptionalProcessId pid);
static std::shared_ptr<SaplingImportRequest> makeBlobMetaImportRequest( static std::shared_ptr<SaplingImportRequest> makeBlobMetaImportRequest(
const ObjectId& hash, const ObjectId& hash,
const HgProxyHash& proxyHash, const HgProxyHash& proxyHash,
ImportPriority priority, const ObjectFetchContextPtr& context);
ObjectFetchContext::Cause cause,
OptionalProcessId pid);
/** /**
* Implementation detail of the make*Request functions from above. Do not * Implementation detail of the make*Request functions from above. Do not
@ -83,9 +77,7 @@ class SaplingImportRequest {
template <typename RequestType> template <typename RequestType>
SaplingImportRequest( SaplingImportRequest(
RequestType request, RequestType request,
ImportPriority priority, const ObjectFetchContextPtr& context,
ObjectFetchContext::Cause cause,
OptionalProcessId pid,
folly::Promise<typename RequestType::Response>&& promise); folly::Promise<typename RequestType::Response>&& promise);
~SaplingImportRequest() = default; ~SaplingImportRequest() = default;
@ -112,11 +104,11 @@ class SaplingImportRequest {
} }
ObjectFetchContext::Cause getCause() const noexcept { ObjectFetchContext::Cause getCause() const noexcept {
return cause_; return context_->getCause();
} }
OptionalProcessId getPid() const noexcept { OptionalProcessId getPid() const noexcept {
return pid_; return context_->getClientPid();
} }
void setPriority(ImportPriority priority) noexcept { void setPriority(ImportPriority priority) noexcept {
@ -147,9 +139,7 @@ class SaplingImportRequest {
*/ */
template <typename Request, typename... Input> template <typename Request, typename... Input>
static std::shared_ptr<SaplingImportRequest> makeRequest( static std::shared_ptr<SaplingImportRequest> makeRequest(
ImportPriority priority, const ObjectFetchContextPtr& context,
ObjectFetchContext::Cause cause,
OptionalProcessId pid,
Input&&... input); Input&&... input);
SaplingImportRequest(const SaplingImportRequest&) = delete; SaplingImportRequest(const SaplingImportRequest&) = delete;
@ -162,9 +152,16 @@ class SaplingImportRequest {
folly::Promise<BlobMetadataPtr>>; folly::Promise<BlobMetadataPtr>>;
Request request_; Request request_;
ObjectFetchContextPtr context_;
/**
* Priority of the request. The priority_ get initialized with context's
* priority. However, down the line we can update the priority of a request,
* and we don't update priority of the context. For example, before enququing
* a new request, we check the queue and if this request is already in the
* queue, we don't enqueue the request again, instead of that we update the
* priority of the existing request in the queue.
*/
ImportPriority priority_; ImportPriority priority_;
ObjectFetchContext::Cause cause_;
OptionalProcessId pid_;
Response promise_; Response promise_;
uint64_t unique_ = generateUniqueID(); uint64_t unique_ = generateUniqueID();
std::chrono::steady_clock::time_point requestTime_ = std::chrono::steady_clock::time_point requestTime_ =

View File

@ -224,12 +224,9 @@ TEST_F(SaplingBackingStoreWithFaultInjectorIgnoreConfigTest, getTreeBatch) {
HgProxyHash proxyHash = HgProxyHash proxyHash =
HgProxyHash::load(localStore.get(), tree1Hash, "getTree", *stats); HgProxyHash::load(localStore.get(), tree1Hash, "getTree", *stats);
auto requestContext = ObjectFetchContext::getNullContext();
auto request = SaplingImportRequest::makeTreeImportRequest( auto request = SaplingImportRequest::makeTreeImportRequest(
tree1Hash, tree1Hash, proxyHash, requestContext);
proxyHash,
ObjectFetchContext::getNullContext()->getPriority(),
ObjectFetchContext::getNullContext()->getCause(),
ObjectFetchContext::getNullContext()->getClientPid());
auto executor = std::make_shared<folly::CPUThreadPoolExecutor>(1); auto executor = std::make_shared<folly::CPUThreadPoolExecutor>(1);
auto tree1fut = via(executor.get(), [&]() { auto tree1fut = via(executor.get(), [&]() {
@ -263,12 +260,9 @@ TEST_F(SaplingBackingStoreWithFaultInjectorTest, getTreeBatch) {
HgProxyHash proxyHash = HgProxyHash proxyHash =
HgProxyHash::load(localStore.get(), tree1Hash, "getTree", *stats); HgProxyHash::load(localStore.get(), tree1Hash, "getTree", *stats);
auto requestContext = ObjectFetchContext::getNullContext();
auto request = SaplingImportRequest::makeTreeImportRequest( auto request = SaplingImportRequest::makeTreeImportRequest(
tree1Hash, tree1Hash, proxyHash, requestContext);
proxyHash,
ObjectFetchContext::getNullContext()->getPriority(),
ObjectFetchContext::getNullContext()->getCause(),
ObjectFetchContext::getNullContext()->getClientPid());
auto executor = std::make_shared<folly::CPUThreadPoolExecutor>(1); auto executor = std::make_shared<folly::CPUThreadPoolExecutor>(1);
auto tree1fut = via(executor.get(), [&]() { auto tree1fut = via(executor.get(), [&]() {

View File

@ -29,12 +29,11 @@ std::shared_ptr<SaplingImportRequest> makeBlobImportRequest(
auto hgRevHash = uniqueHash(); auto hgRevHash = uniqueHash();
auto proxyHash = HgProxyHash{RelativePath{"some_blob"}, hgRevHash}; auto proxyHash = HgProxyHash{RelativePath{"some_blob"}, hgRevHash};
std::string proxyHashString = proxyHash.getValue(); std::string proxyHashString = proxyHash.getValue();
return SaplingImportRequest::makeBlobImportRequest( auto requestContext = ObjectFetchContext::getNullContext();
ObjectId{proxyHashString}, auto request = SaplingImportRequest::makeBlobImportRequest(
std::move(proxyHash), ObjectId{proxyHashString}, std::move(proxyHash), requestContext);
priority, request->setPriority(priority);
ObjectFetchContext::Cause::Unknown, return request;
std::nullopt);
} }
void enqueue(benchmark::State& state) { void enqueue(benchmark::State& state) {

View File

@ -53,27 +53,21 @@ makeBlobImportRequest(ImportPriority priority) {
auto hgRevHash = uniqueHash(); auto hgRevHash = uniqueHash();
auto proxyHash = HgProxyHash{RelativePath{"some_blob"}, hgRevHash}; auto proxyHash = HgProxyHash{RelativePath{"some_blob"}, hgRevHash};
auto hash = ObjectId{proxyHash.getValue()}; auto hash = ObjectId{proxyHash.getValue()};
return std::make_pair( auto requestContext = ObjectFetchContext::getNullContext();
hash, auto request = SaplingImportRequest::makeBlobImportRequest(
SaplingImportRequest::makeBlobImportRequest( hash, std::move(proxyHash), requestContext);
hash, request->setPriority(priority);
std::move(proxyHash), return std::make_pair(hash, request);
priority,
ObjectFetchContext::Cause::Unknown,
std::nullopt));
} }
std::pair<ObjectId, std::shared_ptr<SaplingImportRequest>> std::pair<ObjectId, std::shared_ptr<SaplingImportRequest>>
makeBlobImportRequestWithHash(ImportPriority priority, HgProxyHash proxyHash) { makeBlobImportRequestWithHash(ImportPriority priority, HgProxyHash proxyHash) {
auto hash = ObjectId{proxyHash.getValue()}; auto hash = ObjectId{proxyHash.getValue()};
return std::make_pair( auto requestContext = ObjectFetchContext::getNullContext();
hash, auto request = SaplingImportRequest::makeBlobImportRequest(
SaplingImportRequest::makeBlobImportRequest( hash, std::move(proxyHash), requestContext);
hash, request->setPriority(priority);
std::move(proxyHash), return std::make_pair(hash, request);
priority,
ObjectFetchContext::Cause::Unknown,
std::nullopt));
} }
std::pair<ObjectId, std::shared_ptr<SaplingImportRequest>> std::pair<ObjectId, std::shared_ptr<SaplingImportRequest>>
@ -81,14 +75,11 @@ makeBlobMetaImportRequestWithHash(
ImportPriority priority, ImportPriority priority,
HgProxyHash proxyHash) { HgProxyHash proxyHash) {
auto hash = ObjectId{proxyHash.getValue()}; auto hash = ObjectId{proxyHash.getValue()};
return std::make_pair( auto requestContext = ObjectFetchContext::getNullContext();
hash, auto request = SaplingImportRequest::makeBlobMetaImportRequest(
SaplingImportRequest::makeBlobMetaImportRequest( hash, std::move(proxyHash), requestContext);
hash, request->setPriority(priority);
std::move(proxyHash), return std::make_pair(hash, request);
priority,
ObjectFetchContext::Cause::Unknown,
std::nullopt));
} }
std::pair<ObjectId, std::shared_ptr<SaplingImportRequest>> std::pair<ObjectId, std::shared_ptr<SaplingImportRequest>>
@ -96,14 +87,11 @@ makeTreeImportRequest(ImportPriority priority) {
auto hgRevHash = uniqueHash(); auto hgRevHash = uniqueHash();
auto proxyHash = HgProxyHash{RelativePath{"some_tree"}, hgRevHash}; auto proxyHash = HgProxyHash{RelativePath{"some_tree"}, hgRevHash};
auto hash = ObjectId{proxyHash.getValue()}; auto hash = ObjectId{proxyHash.getValue()};
return std::make_pair( auto requestContext = ObjectFetchContext::getNullContext();
hash, auto request = SaplingImportRequest::makeTreeImportRequest(
SaplingImportRequest::makeTreeImportRequest( hash, std::move(proxyHash), requestContext);
hash, request->setPriority(priority);
std::move(proxyHash), return std::make_pair(hash, request);
priority,
ObjectFetchContext::Cause::Unknown,
std::nullopt));
} }
ObjectId insertBlobImportRequest( ObjectId insertBlobImportRequest(