refactor(tvix/[ca]store): use auto_impl

This implements BS, DS, PS for Box'ed or Arc'ed variants of it with less
code, and less potential to accidentially forget to proxy default trait
methods for blanked impls, as fixed in cl/12658.

Change-Id: If2cdbb563a73792038ebe7bff45d6f880214855b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12661
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: edef <edef@edef.eu>
This commit is contained in:
Florian Klink 2024-10-18 14:41:14 +02:00 committed by clbot
parent 47efebfc6f
commit 9c22345019
16 changed files with 85 additions and 99 deletions

View file

@ -88,8 +88,8 @@ impl ServiceBuilder for CacheConfig {
context: &CompositionContext,
) -> Result<Arc<dyn PathInfoService>, Box<dyn std::error::Error + Send + Sync + 'static>> {
let (near, far) = futures::join!(
context.resolve(self.near.clone()),
context.resolve(self.far.clone())
context.resolve::<Self::Output>(self.near.clone()),
context.resolve::<Self::Output>(self.far.clone())
);
Ok(Arc::new(Cache {
near: near?,

View file

@ -20,9 +20,9 @@ pub fn make_fs<BS, DS, PS>(
show_xattr: bool,
) -> TvixStoreFs<BS, DS, RootNodesWrapper<PS>>
where
BS: AsRef<dyn BlobService> + Send + Clone + 'static,
DS: AsRef<dyn DirectoryService> + Send + Clone + 'static,
PS: AsRef<dyn PathInfoService> + Send + Sync + Clone + 'static,
BS: BlobService + Send + Clone + 'static,
DS: DirectoryService + Send + Clone + 'static,
PS: PathInfoService + Send + Sync + Clone + 'static,
{
TvixStoreFs::new(
blob_service,
@ -46,7 +46,7 @@ pub struct RootNodesWrapper<T>(pub(crate) T);
#[async_trait]
impl<T> RootNodes for RootNodesWrapper<T>
where
T: AsRef<dyn PathInfoService> + Send + Sync,
T: PathInfoService + Send + Sync,
{
async fn get_by_basename(&self, name: &PathComponent) -> Result<Option<Node>, Error> {
let Ok(store_path) = StorePathRef::from_bytes(name.as_ref()) else {
@ -55,14 +55,13 @@ where
Ok(self
.0
.as_ref()
.get(*store_path.digest())
.await?
.map(|path_info| path_info.node))
}
fn list(&self) -> BoxStream<Result<(PathComponent, Node), Error>> {
Box::pin(self.0.as_ref().list().map(|result| {
Box::pin(self.0.list().map(|result| {
result.map(|path_info| {
let basename = path_info.store_path.to_string();
(

View file

@ -13,6 +13,7 @@ mod fs;
#[cfg(test)]
mod tests;
use auto_impl::auto_impl;
use futures::stream::BoxStream;
use tonic::async_trait;
use tvix_castore::composition::{Registry, ServiceBuilder};
@ -45,6 +46,7 @@ pub use self::fs::make_fs;
/// The base trait all PathInfo services need to implement.
#[async_trait]
#[auto_impl(&, &mut, Arc, Box)]
pub trait PathInfoService: Send + Sync {
/// Retrieve a PathInfo message by the output digest.
async fn get(&self, digest: [u8; 20]) -> Result<Option<PathInfo>, Error>;
@ -69,28 +71,6 @@ pub trait PathInfoService: Send + Sync {
}
}
#[async_trait]
impl<A> PathInfoService for A
where
A: AsRef<dyn PathInfoService> + Send + Sync + 'static,
{
async fn get(&self, digest: [u8; 20]) -> Result<Option<PathInfo>, Error> {
self.as_ref().get(digest).await
}
async fn put(&self, path_info: PathInfo) -> Result<PathInfo, Error> {
self.as_ref().put(path_info).await
}
fn list(&self) -> BoxStream<'static, Result<PathInfo, Error>> {
self.as_ref().list()
}
fn nar_calculation_service(&self) -> Option<Box<dyn NarCalculationService>> {
self.as_ref().nar_calculation_service()
}
}
/// Registers the builtin PathInfoService implementations with the registry
pub(crate) fn register_pathinfo_services(reg: &mut Registry) {
reg.register::<Box<dyn ServiceBuilder<Output = dyn PathInfoService>>, CachePathInfoServiceConfig>("cache");

View file

@ -66,8 +66,8 @@ impl<BS, DS> NixHTTPPathInfoService<BS, DS> {
#[async_trait]
impl<BS, DS> PathInfoService for NixHTTPPathInfoService<BS, DS>
where
BS: AsRef<dyn BlobService> + Send + Sync + Clone + 'static,
DS: AsRef<dyn DirectoryService> + Send + Sync + Clone + 'static,
BS: BlobService + Send + Sync + Clone + 'static,
DS: DirectoryService + Send + Sync + Clone + 'static,
{
#[instrument(skip_all, err, fields(path.digest=nixbase32::encode(&digest)))]
async fn get(&self, digest: [u8; 20]) -> Result<Option<PathInfo>, Error> {
@ -316,10 +316,10 @@ impl ServiceBuilder for NixHTTPPathInfoServiceConfig {
&'a self,
_instance_name: &str,
context: &CompositionContext,
) -> Result<Arc<dyn PathInfoService>, Box<dyn std::error::Error + Send + Sync + 'static>> {
) -> Result<Arc<Self::Output>, Box<dyn std::error::Error + Send + Sync + 'static>> {
let (blob_service, directory_service) = futures::join!(
context.resolve(self.blob_service.clone()),
context.resolve(self.directory_service.clone())
context.resolve::<dyn BlobService>(self.blob_service.clone()),
context.resolve::<dyn DirectoryService>(self.directory_service.clone())
);
let mut svc = NixHTTPPathInfoService::new(
Url::parse(&self.base_url)?,

View file

@ -96,7 +96,7 @@ impl ServiceBuilder for KeyFileSigningPathInfoServiceConfig {
_instance_name: &str,
context: &CompositionContext,
) -> Result<Arc<dyn PathInfoService>, Box<dyn std::error::Error + Send + Sync + 'static>> {
let inner = context.resolve(self.inner.clone()).await?;
let inner = context.resolve::<Self::Output>(self.inner.clone()).await?;
let signing_key = Arc::new(
parse_keypair(tokio::fs::read_to_string(&self.keyfile).await?.trim())
.map_err(|e| Error::StorageError(e.to_string()))?