refactor(tvix/store/pathinfosvc): drop ByWhat, use digest directly

We currently only support querying by the output hash digest.
This makes the interface a bit simpler.

Change-Id: I80b285373f1923e85cb0e404c4b15d51a7f259ef
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8570
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
Florian Klink 2023-05-14 18:10:23 +03:00 committed by flokli
parent 71c29d0f4c
commit e815b680c0
5 changed files with 46 additions and 72 deletions

View file

@ -28,10 +28,7 @@ impl GRPCPathInfoService {
} }
impl PathInfoService for GRPCPathInfoService { impl PathInfoService for GRPCPathInfoService {
fn get( fn get(&self, digest: [u8; 20]) -> Result<Option<proto::PathInfo>, crate::Error> {
&self,
by_what: proto::get_path_info_request::ByWhat,
) -> Result<Option<proto::PathInfo>, crate::Error> {
// Get a new handle to the gRPC client. // Get a new handle to the gRPC client.
let mut grpc_client = self.grpc_client.clone(); let mut grpc_client = self.grpc_client.clone();
@ -39,7 +36,9 @@ impl PathInfoService for GRPCPathInfoService {
self.tokio_handle.spawn(async move { self.tokio_handle.spawn(async move {
let path_info = grpc_client let path_info = grpc_client
.get(proto::GetPathInfoRequest { .get(proto::GetPathInfoRequest {
by_what: Some(by_what), by_what: Some(proto::get_path_info_request::ByWhat::ByOutputHash(
digest.to_vec(),
)),
}) })
.await? .await?
.into_inner(); .into_inner();

View file

@ -1,36 +1,22 @@
use super::PathInfoService;
use crate::{proto, Error};
use std::{ use std::{
collections::HashMap, collections::HashMap,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
use crate::{proto, Error};
use nix_compat::store_path::DIGEST_SIZE;
use super::PathInfoService;
#[derive(Default)] #[derive(Default)]
pub struct MemoryPathInfoService { pub struct MemoryPathInfoService {
db: Arc<RwLock<HashMap<Vec<u8>, proto::PathInfo>>>, db: Arc<RwLock<HashMap<[u8; 20], proto::PathInfo>>>,
} }
impl PathInfoService for MemoryPathInfoService { impl PathInfoService for MemoryPathInfoService {
fn get( fn get(&self, digest: [u8; 20]) -> Result<Option<proto::PathInfo>, Error> {
&self, let db = self.db.read().unwrap();
by_what: proto::get_path_info_request::ByWhat,
) -> Result<Option<proto::PathInfo>, Error> {
match by_what {
proto::get_path_info_request::ByWhat::ByOutputHash(digest) => {
if digest.len() != DIGEST_SIZE {
return Err(Error::InvalidRequest("invalid digest length".to_string()));
}
let db = self.db.read().unwrap(); match db.get(&digest) {
None => Ok(None),
match db.get(&digest) { Some(path_info) => Ok(Some(path_info.clone())),
None => Ok(None),
Some(path_info) => Ok(Some(path_info.clone())),
}
}
} }
} }
@ -46,7 +32,7 @@ impl PathInfoService for MemoryPathInfoService {
// This overwrites existing PathInfo objects. // This overwrites existing PathInfo objects.
Ok(nix_path) => { Ok(nix_path) => {
let mut db = self.db.write().unwrap(); let mut db = self.db.write().unwrap();
db.insert(nix_path.digest.to_vec(), path_info.clone()); db.insert(nix_path.digest, path_info.clone());
Ok(path_info) Ok(path_info)
} }

View file

@ -11,11 +11,8 @@ pub use self::sled::SledPathInfoService;
/// The base trait all PathInfo services need to implement. /// The base trait all PathInfo services need to implement.
/// This is a simple get and put of [proto::Directory], returning their digest. /// This is a simple get and put of [proto::Directory], returning their digest.
pub trait PathInfoService { pub trait PathInfoService {
/// Retrieve a PathInfo message. /// Retrieve a PathInfo message by the output digest.
fn get( fn get(&self, digest: [u8; 20]) -> Result<Option<proto::PathInfo>, Error>;
&self,
by_what: proto::get_path_info_request::ByWhat,
) -> Result<Option<proto::PathInfo>, Error>;
/// Store a PathInfo message. Implementations MUST call validate and reject /// Store a PathInfo message. Implementations MUST call validate and reject
/// invalid messages. /// invalid messages.

View file

@ -1,11 +1,9 @@
use super::PathInfoService;
use crate::{proto, Error}; use crate::{proto, Error};
use nix_compat::store_path::DIGEST_SIZE;
use prost::Message; use prost::Message;
use std::path::PathBuf; use std::path::PathBuf;
use tracing::warn; use tracing::warn;
use super::PathInfoService;
/// SledPathInfoService stores PathInfo in a [sled](https://github.com/spacejam/sled). /// SledPathInfoService stores PathInfo in a [sled](https://github.com/spacejam/sled).
/// ///
/// The PathInfo messages are stored as encoded protos, and keyed by their output hash, /// The PathInfo messages are stored as encoded protos, and keyed by their output hash,
@ -31,36 +29,25 @@ impl SledPathInfoService {
} }
impl PathInfoService for SledPathInfoService { impl PathInfoService for SledPathInfoService {
fn get( fn get(&self, digest: [u8; 20]) -> Result<Option<proto::PathInfo>, Error> {
&self, match self.db.get(digest) {
by_what: proto::get_path_info_request::ByWhat, Ok(None) => Ok(None),
) -> Result<Option<proto::PathInfo>, Error> { Ok(Some(data)) => match proto::PathInfo::decode(&*data) {
match by_what { Ok(path_info) => Ok(Some(path_info)),
proto::get_path_info_request::ByWhat::ByOutputHash(digest) => { Err(e) => {
if digest.len() != DIGEST_SIZE { warn!("failed to decode stored PathInfo: {}", e);
return Err(Error::InvalidRequest("invalid digest length".to_string())); Err(Error::StorageError(format!(
} "failed to decode stored PathInfo: {}",
e
match self.db.get(digest) { )))
Ok(None) => Ok(None),
Ok(Some(data)) => match proto::PathInfo::decode(&*data) {
Ok(path_info) => Ok(Some(path_info)),
Err(e) => {
warn!("failed to decode stored PathInfo: {}", e);
Err(Error::StorageError(format!(
"failed to decode stored PathInfo: {}",
e
)))
}
},
Err(e) => {
warn!("failed to retrieve PathInfo: {}", e);
Err(Error::StorageError(format!(
"failed to retrieve PathInfo: {}",
e
)))
}
} }
},
Err(e) => {
warn!("failed to retrieve PathInfo: {}", e);
Err(Error::StorageError(format!(
"failed to retrieve PathInfo: {}",
e
)))
} }
} }
} }

View file

@ -31,14 +31,19 @@ impl<
) -> Result<Response<proto::PathInfo>> { ) -> Result<Response<proto::PathInfo>> {
match request.into_inner().by_what { match request.into_inner().by_what {
None => Err(Status::unimplemented("by_what needs to be specified")), None => Err(Status::unimplemented("by_what needs to be specified")),
Some(by_what) => match self.path_info_service.get(by_what) { Some(proto::get_path_info_request::ByWhat::ByOutputHash(digest)) => {
Ok(None) => Err(Status::not_found("PathInfo not found")), let digest: [u8; 20] = digest
Ok(Some(path_info)) => Ok(Response::new(path_info)), .try_into()
Err(e) => { .map_err(|_e| Status::invalid_argument("invalid digest length"))?;
warn!("failed to retrieve PathInfo: {}", e); match self.path_info_service.get(digest) {
Err(e.into()) Ok(None) => Err(Status::not_found("PathInfo not found")),
Ok(Some(path_info)) => Ok(Response::new(path_info)),
Err(e) => {
warn!("failed to retrieve PathInfo: {}", e);
Err(e.into())
}
} }
}, }
} }
} }