From 8d05c0ceaa9bddb7fdaab436730f093eb16374a2 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 8 Jun 2023 23:00:37 +0300 Subject: refactor(tvix/src/nar): drop NARCalculationService There's only one way to calculate NAR files, by walking through them. Things like caching such replies should be done closer to where we use these, composing NARCalculationService doesn't actually give us much. Instead, expose two functions, `nar::calculate_size_and_sha256` and `nar::writer_nar`, the latter writing NAR to a writer, the former using write_nar to only keeping the NAR size and digest. Change-Id: Ie5d2cfea35470fdbb5cbf9da1136b0cdf0250266 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8723 Reviewed-by: tazjin Tested-by: BuildkiteCI Autosubmit: flokli --- .../src/proto/grpc_pathinfoservice_wrapper.rs | 42 ++++++++++++++-------- tvix/store/src/proto/tests/grpc_pathinfoservice.rs | 4 +-- 2 files changed, 30 insertions(+), 16 deletions(-) (limited to 'tvix/store/src/proto') diff --git a/tvix/store/src/proto/grpc_pathinfoservice_wrapper.rs b/tvix/store/src/proto/grpc_pathinfoservice_wrapper.rs index e82557b3a06c..c070b883fa16 100644 --- a/tvix/store/src/proto/grpc_pathinfoservice_wrapper.rs +++ b/tvix/store/src/proto/grpc_pathinfoservice_wrapper.rs @@ -1,19 +1,27 @@ -use crate::nar::RenderError; +use crate::blobservice::BlobService; +use crate::directoryservice::DirectoryService; +use crate::nar::{calculate_size_and_sha256, RenderError}; +use crate::pathinfoservice::PathInfoService; use crate::proto; -use crate::{nar::NARCalculationService, pathinfoservice::PathInfoService}; use tonic::{async_trait, Request, Response, Result, Status}; use tracing::{instrument, warn}; -pub struct GRPCPathInfoServiceWrapper { +pub struct GRPCPathInfoServiceWrapper { path_info_service: PS, - nar_calculation_service: NS, + blob_service: Box, + directory_service: DS, } -impl GRPCPathInfoServiceWrapper { - pub fn new(path_info_service: PS, nar_calculation_service: NS) -> Self { +impl GRPCPathInfoServiceWrapper { + pub fn new( + path_info_service: PS, + blob_service: Box, + directory_service: DS, + ) -> Self { Self { path_info_service, - nar_calculation_service, + blob_service, + directory_service, } } } @@ -21,8 +29,8 @@ impl GRPCPathInfoServiceWrapper< #[async_trait] impl< PS: PathInfoService + Send + Sync + 'static, - NS: NARCalculationService + Send + Sync + 'static, - > proto::path_info_service_server::PathInfoService for GRPCPathInfoServiceWrapper + DS: DirectoryService + Send + Sync + Clone + 'static, + > proto::path_info_service_server::PathInfoService for GRPCPathInfoServiceWrapper { #[instrument(skip(self))] async fn get( @@ -69,13 +77,19 @@ impl< ) -> Result> { match request.into_inner().node { None => Err(Status::invalid_argument("no root node sent")), - Some(root_node) => match self.nar_calculation_service.calculate_nar(&root_node) { - Ok((nar_size, nar_sha256)) => Ok(Response::new(proto::CalculateNarResponse { + Some(root_node) => { + let (nar_size, nar_sha256) = calculate_size_and_sha256( + &root_node, + &self.blob_service, + self.directory_service.clone(), + ) + .expect("error during nar calculation"); // TODO: handle error + + Ok(Response::new(proto::CalculateNarResponse { nar_size, nar_sha256: nar_sha256.to_vec(), - })), - Err(e) => Err(e.into()), - }, + })) + } } } } diff --git a/tvix/store/src/proto/tests/grpc_pathinfoservice.rs b/tvix/store/src/proto/tests/grpc_pathinfoservice.rs index 11cab2c264cc..dbcdc5ced056 100644 --- a/tvix/store/src/proto/tests/grpc_pathinfoservice.rs +++ b/tvix/store/src/proto/tests/grpc_pathinfoservice.rs @@ -1,4 +1,3 @@ -use crate::nar::NonCachingNARCalculationService; use crate::proto::get_path_info_request::ByWhat::ByOutputHash; use crate::proto::node::Node::Symlink; use crate::proto::path_info_service_server::PathInfoService as GRPCPathInfoService; @@ -17,7 +16,8 @@ use tonic::Request; fn gen_grpc_service() -> impl GRPCPathInfoService { GRPCPathInfoServiceWrapper::new( gen_pathinfo_service(), - NonCachingNARCalculationService::new(gen_blob_service(), gen_directory_service()), + gen_blob_service(), + gen_directory_service(), ) } -- cgit 1.4.1