refactor(tvix/castore): use Directory struct separate from proto one

This uses our own data type to deal with Directories in the castore model.

It makes some undesired states unrepresentable, removing the need for conversions and checking in various places:

 - In the protobuf, blake3 digests could have a wrong length, as proto doesn't know fixed-size fields. We now use `B3Digest`, which makes cloning cheaper, and removes the need to do size-checking everywhere.
 - In the protobuf, we had three different lists for `files`, `symlinks` and `directories`. This was mostly a protobuf size optimization, but made interacting with them a bit awkward. This has now been replaced with a list of enums, and convenience iterators to get various nodes, and add new ones.

Change-Id: I7b92691bb06d77ff3f58a5ccea94a22c16f84f04
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12057
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Yureka 2024-07-29 14:34:50 +02:00 committed by yuka
parent 5d3f3158d6
commit 3ca0b53840
53 changed files with 1429 additions and 1377 deletions

View file

@ -46,25 +46,20 @@ pub async fn get(
}
// parse the proto
let mut root_node: tvix_castore::proto::Node = Message::decode(Bytes::from(root_node_proto))
let root_node: tvix_castore::proto::Node = Message::decode(Bytes::from(root_node_proto))
.map_err(|e| {
warn!(err=%e, "unable to decode root node proto");
StatusCode::NOT_FOUND
})?;
let root_node: tvix_castore::directoryservice::Node = (&root_node).try_into().map_err(|e| {
warn!(err=%e, "root node validation failed");
StatusCode::BAD_REQUEST
})?;
// validate the node, but add a dummy node name, as we only send unnamed
// nodes
if let Some(rn) = root_node.node {
root_node.node = Some(rn.rename("00000000000000000000000000000000-dummy".into()))
}
let root_node = root_node
.validate()
.map_err(|e| {
warn!(err=%e, "root node validation failed");
StatusCode::BAD_REQUEST
})?
.to_owned();
let root_node = root_node.rename("00000000000000000000000000000000-dummy".into());
let (w, r) = tokio::io::duplex(1024 * 8);
@ -130,7 +125,7 @@ pub async fn put(
// store mapping of narhash to root node into root_nodes.
// we need it later to populate the root node when accepting the PathInfo.
root_nodes.write().put(nar_hash_actual, root_node);
root_nodes.write().put(nar_hash_actual, (&root_node).into());
Ok("")
}

View file

@ -61,22 +61,20 @@ pub async fn get(
StatusCode::INTERNAL_SERVER_ERROR
})?;
let mut narinfo = path_info.to_narinfo(store_path).ok_or_else(|| {
let mut narinfo = path_info.to_narinfo(store_path.as_ref()).ok_or_else(|| {
warn!(path_info=?path_info, "PathInfo contained no NAR data");
StatusCode::INTERNAL_SERVER_ERROR
})?;
// encode the (unnamed) root node in the NAR url itself.
let root_node = path_info
.node
.as_ref()
.and_then(|n| n.node.as_ref())
.expect("root node must not be none")
.clone()
.rename("".into());
let root_node = tvix_castore::directoryservice::Node::try_from(
path_info.node.as_ref().expect("root node must not be none"),
)
.unwrap() // PathInfo is validated
.rename("".into());
let mut buf = Vec::new();
Node::encode(&root_node, &mut buf);
Node::encode(&(&root_node).into(), &mut buf);
let url = format!(
"nar/tvix-castore/{}?narsize={}",
@ -128,10 +126,10 @@ pub async fn put(
// Lookup root node with peek, as we don't want to update the LRU list.
// We need to be careful to not hold the RwLock across the await point.
let maybe_root_node = root_nodes
let maybe_root_node: Option<tvix_castore::directoryservice::Node> = root_nodes
.read()
.peek(&narinfo.nar_hash)
.map(|v| v.to_owned());
.and_then(|v| v.try_into().ok());
match maybe_root_node {
Some(root_node) => {
@ -139,7 +137,7 @@ pub async fn put(
// We need to rename the node to the narinfo storepath basename, as
// that's where it's stored in PathInfo.
pathinfo.node = Some(castorepb::Node {
node: Some(root_node.rename(narinfo.store_path.to_string().into())),
node: Some((&root_node.rename(narinfo.store_path.to_string().into())).into()),
});
// Persist the PathInfo.