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

View File

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

View File

@ -55,9 +55,7 @@ class SaplingImportRequest {
static std::shared_ptr<SaplingImportRequest> makeBlobImportRequest(
const ObjectId& hash,
const HgProxyHash& proxyHash,
ImportPriority priority,
ObjectFetchContext::Cause cause,
OptionalProcessId pid);
const ObjectFetchContextPtr& context);
/**
* Allocate a tree request.
@ -65,16 +63,12 @@ class SaplingImportRequest {
static std::shared_ptr<SaplingImportRequest> makeTreeImportRequest(
const ObjectId& hash,
const HgProxyHash& proxyHash,
ImportPriority priority,
ObjectFetchContext::Cause cause,
OptionalProcessId pid);
const ObjectFetchContextPtr& context);
static std::shared_ptr<SaplingImportRequest> makeBlobMetaImportRequest(
const ObjectId& hash,
const HgProxyHash& proxyHash,
ImportPriority priority,
ObjectFetchContext::Cause cause,
OptionalProcessId pid);
const ObjectFetchContextPtr& context);
/**
* Implementation detail of the make*Request functions from above. Do not
@ -83,9 +77,7 @@ class SaplingImportRequest {
template <typename RequestType>
SaplingImportRequest(
RequestType request,
ImportPriority priority,
ObjectFetchContext::Cause cause,
OptionalProcessId pid,
const ObjectFetchContextPtr& context,
folly::Promise<typename RequestType::Response>&& promise);
~SaplingImportRequest() = default;
@ -112,11 +104,11 @@ class SaplingImportRequest {
}
ObjectFetchContext::Cause getCause() const noexcept {
return cause_;
return context_->getCause();
}
OptionalProcessId getPid() const noexcept {
return pid_;
return context_->getClientPid();
}
void setPriority(ImportPriority priority) noexcept {
@ -147,9 +139,7 @@ class SaplingImportRequest {
*/
template <typename Request, typename... Input>
static std::shared_ptr<SaplingImportRequest> makeRequest(
ImportPriority priority,
ObjectFetchContext::Cause cause,
OptionalProcessId pid,
const ObjectFetchContextPtr& context,
Input&&... input);
SaplingImportRequest(const SaplingImportRequest&) = delete;
@ -162,9 +152,16 @@ class SaplingImportRequest {
folly::Promise<BlobMetadataPtr>>;
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_;
ObjectFetchContext::Cause cause_;
OptionalProcessId pid_;
Response promise_;
uint64_t unique_ = generateUniqueID();
std::chrono::steady_clock::time_point requestTime_ =

View File

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

View File

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

View File

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