Skip to content

Commit

Permalink
Merge pull request #522 from cgwalters/max-layer-one
Browse files Browse the repository at this point in the history
chunking: Add const for minimum, change to regular error
  • Loading branch information
cgwalters committed Aug 28, 2023
2 parents c5addba + 449b400 commit 6ad2608
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/src/chunking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ use serde::{Deserialize, Serialize};
// We take half the limit of 128.
// https://github.com/ostreedev/ostree-rs-ext/issues/69
pub(crate) const MAX_CHUNKS: u32 = 64;
/// Minimum number of layers we can create in a "chunked" flow; otherwise
/// we will just drop down to one.
const MIN_CHUNKED_LAYERS: u32 = 4;

type RcStr = Rc<str>;
pub(crate) type ChunkMapping = BTreeMap<RcStr, (u64, Vec<Utf8PathBuf>)>;
Expand Down Expand Up @@ -638,6 +641,8 @@ fn basic_packing<'a>(
const HIGH_SIZE_CUTOFF: f32 = 0.6;
let before_processing_pkgs_len = components.len();

anyhow::ensure!(bin_size.get() >= MIN_CHUNKED_LAYERS);

// If we have a prior build, then use that
if let Some(prior_build) = prior_build_metadata {
return basic_packing_with_prior_build(components, bin_size, prior_build);
Expand Down Expand Up @@ -687,7 +692,7 @@ fn basic_packing<'a>(
// Approximate number of components we should have per medium-size bin.
let pkg_per_bin_ms: usize = (components.len() - limit_hs_bins - low_sized_component_count)
.checked_div(limit_ms_bins)
.expect("number of bins should be >= 4");
.ok_or_else(|| anyhow::anyhow!("number of bins should be >= {}", MIN_CHUNKED_LAYERS))?;

// Bins assignment
for (partition, pkgs) in partitions.iter() {
Expand Down Expand Up @@ -772,7 +777,7 @@ mod test {
#[test]
fn test_packing_basics() -> Result<()> {
// null cases
for v in [1u32, 7].map(|v| NonZeroU32::new(v).unwrap()) {
for v in [4, 7].map(|v| NonZeroU32::new(v).unwrap()) {
assert_eq!(basic_packing(&[], v, None).unwrap().len(), 0);
}
Ok(())
Expand All @@ -795,6 +800,15 @@ mod test {
Ok(())
}

#[test]
fn test_packing_one_layer() -> Result<()> {
let contentmeta: Vec<ObjectSourceMetaSized> =
serde_json::from_reader(flate2::read::GzDecoder::new(FCOS_CONTENTMETA))?;
let r = basic_packing(&contentmeta, NonZeroU32::new(1).unwrap(), None);
assert!(r.is_err());
Ok(())
}

fn create_manifest(prev_expected_structure: Vec<Vec<&str>>) -> oci_spec::image::ImageManifest {
let mut p = prev_expected_structure
.iter()
Expand Down

0 comments on commit 6ad2608

Please sign in to comment.