refactor(castore/directory): separate order logic from ClosureValidator

ClosureValidator was previously only suitable for a very narrow use case:
Validating incoming uploads, which are in leaves-to-root order.
This is because the ordering validation was hard-wired into the add()
function.
This
- Re-name ClosureValidator to DirectoryGraph, which is more suitable
  since it actually stores the Directory structs and is drained in the end.
- Move the ordering-related logic to a separate OrderValidator, which
  can be used independently.
- re-write DirectoryGraph to be a general purpose
  validator which can accept the input in both orders
  and can be drained in both orders as well.

This means the DirectoryGraph and OrderValidator can now serve
multiple new purposes:
- Validating the incoming closure on the client while downloading.
- Validating the incoming closure downloaded in a caching layer from the
  `far` cache, and re-order it for insertion into the `near` cache.

Change-Id: I2b4b226348416912d7a31935bec050e53d911b70
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11708
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Autosubmit: yuka <yuka@yuka.dev>
This commit is contained in:
Yureka 2024-06-15 18:24:42 +02:00 committed by clbot
parent c2a9ad3583
commit daada1b2fa
8 changed files with 640 additions and 328 deletions

View file

@ -16,7 +16,7 @@ use tonic::async_trait;
use tracing::{instrument, trace, warn, Level};
use url::Url;
use super::{ClosureValidator, DirectoryPutter, DirectoryService};
use super::{DirectoryGraph, DirectoryPutter, DirectoryService, LeavesToRootValidator};
use crate::{proto, B3Digest, Error};
/// Stores directory closures in an object store.
@ -177,7 +177,7 @@ struct ObjectStoreDirectoryPutter {
object_store: Arc<dyn ObjectStore>,
base_path: Path,
directory_validator: Option<ClosureValidator>,
directory_validator: Option<DirectoryGraph<LeavesToRootValidator>>,
}
impl ObjectStoreDirectoryPutter {
@ -197,7 +197,9 @@ impl DirectoryPutter for ObjectStoreDirectoryPutter {
match self.directory_validator {
None => return Err(Error::StorageError("already closed".to_string())),
Some(ref mut validator) => {
validator.add(directory)?;
validator
.add(directory)
.map_err(|e| Error::StorageError(e.to_string()))?;
}
}
@ -214,7 +216,11 @@ impl DirectoryPutter for ObjectStoreDirectoryPutter {
// retrieve the validated directories.
// It is important that they are in topological order (root first),
// since that's how we want to retrieve them from the object store in the end.
let directories = validator.finalize_root_to_leaves()?;
let directories = validator
.validate()
.map_err(|e| Error::StorageError(e.to_string()))?
.drain_root_to_leaves()
.collect::<Vec<_>>();
// Get the root digest
let root_digest = directories