mercurial_derived_data: make get_hg_changeset_from_bonsai an extension trait

Summary:
Make the free function in `mercurial_derived_data` into an extension trait that will work
on any kind of repo that supports derived data.

The ordinary way to get a mercurial changeset with facet-based repos would be:
```
   use repo_derived_data::RepoDerivedDataRef;
   use mercurial_derived_data::MappedHgChangesetId;

   let hg_changeset_id = repo
        .repo_derived_data()
        .derive::<MappedHgChangsetId>(ctx, cs_id)
        .await?
        .hg_changeset_id();
```

However, this is a bit of a mouthful for something that is very common in Mononoke.  Instead,
make it similar to how `BlobRepo` works by turning it into an extension trait:
```
   use mercurial_derived_data::DeriveHgChangeset;

   let hg_changeset_id = repo.derive_hg_changeset(ctx, cs_id).await?;
```

The method name is changed to make it clear that derivation may be triggered,
and `BlobRepo::get_hg_from_bonsai_changeset` is changed to use this new
method.

Reviewed By: mitrandir77

Differential Revision: D34971450

fbshipit-source-id: 890d106278e339c2a6deb973eea8ed5122baa4f8
This commit is contained in:
Mark Juggurnauth-Thomas 2022-03-23 07:06:23 -07:00 committed by Facebook GitHub Bot
parent f39ad44fd1
commit ab3be4a673
4 changed files with 42 additions and 23 deletions

View File

@ -39,6 +39,7 @@ use futures::{
stream::{self, BoxStream},
Stream, StreamExt, TryFutureExt, TryStreamExt,
};
use mercurial_derived_data::DeriveHgChangeset;
use mercurial_mutation::ArcHgMutationStore;
use mercurial_types::{HgChangesetId, HgFileNodeId};
use mononoke_types::{ChangesetId, RepoPath};
@ -397,7 +398,7 @@ impl BlobRepoHg for BlobRepo {
ctx: CoreContext,
bcs_id: ChangesetId,
) -> Result<HgChangesetId, Error> {
mercurial_derived_data::get_hg_from_bonsai_changeset(self.clone(), ctx, bcs_id).await
self.derive_hg_changeset(&ctx, bcs_id).await
}
}

View File

@ -13,7 +13,6 @@ path = "lib.rs"
[dependencies]
anyhow = "1.0.51"
async-trait = "0.1.52"
blobrepo = { version = "0.1.0", path = "../../blobrepo" }
blobrepo_common = { version = "0.1.0", path = "../../blobrepo/common" }
blobrepo_errors = { version = "0.1.0", path = "../../blobrepo/errors" }
blobstore = { version = "0.1.0", path = "../../blobstore" }
@ -28,18 +27,19 @@ futures = { version = "0.3.13", features = ["async-await", "compat"] }
manifest = { version = "0.1.0", path = "../../manifest" }
mercurial_types = { version = "0.1.0", path = "../../mercurial/types" }
mononoke_types = { version = "0.1.0", path = "../../mononoke_types" }
repo_derived_data = { version = "0.1.0", path = "../../repo_attributes/repo_derived_data" }
slog = { version = "2.7", features = ["max_level_trace", "nested-values"] }
sorted_vector_map = { version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main" }
stats = { version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main" }
tokio = { version = "1.15", features = ["full", "test-util", "tracing"] }
[dev-dependencies]
blobrepo = { version = "0.1.0", path = "../../blobrepo" }
blobrepo_hg = { version = "0.1.0", path = "../../blobrepo/blobrepo_hg" }
bookmarks = { version = "0.1.0", path = "../../bookmarks" }
fbinit = { version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main" }
fbinit-tokio = { version = "0.1.0", git = "https://github.com/facebookexperimental/rust-shed.git", branch = "main" }
fixtures = { version = "0.1.0", path = "../../tests/fixtures" }
repo_derived_data = { version = "0.1.0", path = "../../repo_attributes/repo_derived_data" }
revset = { version = "0.1.0", path = "../../revset" }
test_repo_factory = { version = "0.1.0", path = "../../repo_factory/test_repo_factory" }
tests_utils = { version = "0.1.0", path = "../../tests/utils" }

View File

@ -10,13 +10,13 @@ use crate::{
mapping::{HgChangesetDeriveOptions, MappedHgChangesetId},
};
use anyhow::{anyhow, bail, Error};
use blobrepo::BlobRepo;
use async_trait::async_trait;
use blobrepo_common::changed_files::compute_changed_files;
use blobstore::{Blobstore, Loadable};
use borrowed::borrowed;
use cloned::cloned;
use context::CoreContext;
use derived_data::{BonsaiDerived, DeriveError};
use derived_data_manager::DerivationError;
use futures::{
future::{self, try_join, try_join_all},
stream, FutureExt, TryStreamExt,
@ -32,6 +32,7 @@ use mercurial_types::{
use mononoke_types::{
BonsaiChangeset, ChangesetId, FileChange, FileType, MPath, TrackedFileChange,
};
use repo_derived_data::RepoDerivedDataRef;
use stats::prelude::*;
use std::sync::Arc;
use std::{collections::HashMap, time::Instant};
@ -474,21 +475,38 @@ async fn generate_hg_changeset(
Ok((csid, cs))
}
pub async fn get_hg_from_bonsai_changeset(
repo: BlobRepo,
ctx: CoreContext,
bcs_id: ChangesetId,
) -> Result<HgChangesetId, Error> {
STATS::get_hg_from_bonsai_changeset.add_value(1);
let start_timestmap = Instant::now();
let result = match MappedHgChangesetId::derive(&ctx, &repo, bcs_id).await {
Ok(id) => Ok(id.hg_changeset_id()),
Err(err) => match err {
DeriveError::Disabled(..) => Err(err.into()),
DeriveError::Error(err) => Err(err),
},
};
STATS::generate_hg_from_bonsai_total_latency_ms
.add_value(start_timestmap.elapsed().as_millis() as i64);
result
#[async_trait]
pub trait DeriveHgChangeset {
async fn derive_hg_changeset(
&self,
ctx: &CoreContext,
cs_id: ChangesetId,
) -> Result<HgChangesetId, Error>;
}
#[async_trait]
impl<Repo> DeriveHgChangeset for Repo
where
Repo: RepoDerivedDataRef + Send + Sync,
{
async fn derive_hg_changeset(
&self,
ctx: &CoreContext,
cs_id: ChangesetId,
) -> Result<HgChangesetId, Error> {
STATS::get_hg_from_bonsai_changeset.add_value(1);
let start_timestamp = Instant::now();
let result = match self
.repo_derived_data()
.derive::<MappedHgChangesetId>(ctx, cs_id)
.await
{
Ok(id) => Ok(id.hg_changeset_id()),
Err(err @ DerivationError::Disabled(..)) => Err(err.into()),
Err(DerivationError::Error(err)) => Err(err),
};
STATS::generate_hg_from_bonsai_total_latency_ms
.add_value(start_timestamp.elapsed().as_millis() as i64);
result
}
}

View File

@ -11,6 +11,6 @@ pub mod derive_hg_changeset;
pub mod derive_hg_manifest;
mod mapping;
pub use derive_hg_changeset::{get_hg_from_bonsai_changeset, get_manifest_from_bonsai};
pub use derive_hg_changeset::{get_manifest_from_bonsai, DeriveHgChangeset};
pub use derive_hg_manifest::derive_hg_manifest;
pub use mapping::MappedHgChangesetId;