From 4c30f9d08fae6e7508f7177bbd9805907b1ade5b Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Thu, 7 Mar 2024 05:26:35 +0000 Subject: [PATCH] runtime: make image cache async --- daemon/src/runtime/image/cache.rs | 22 +++++++++++----------- daemon/src/runtime/image/mod.rs | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/daemon/src/runtime/image/cache.rs b/daemon/src/runtime/image/cache.rs index ca59540..d8c1b37 100644 --- a/daemon/src/runtime/image/cache.rs +++ b/daemon/src/runtime/image/cache.rs @@ -2,8 +2,8 @@ use super::ImageInfo; use anyhow::Result; use log::debug; use oci_spec::image::{ImageConfiguration, ImageManifest}; -use std::fs; use std::path::{Path, PathBuf}; +use tokio::fs; pub struct ImageCache { cache_dir: PathBuf, @@ -16,7 +16,7 @@ impl ImageCache { }) } - pub fn recall(&self, digest: &str) -> Result> { + pub async fn recall(&self, digest: &str) -> Result> { let mut squashfs_path = self.cache_dir.clone(); let mut config_path = self.cache_dir.clone(); let mut manifest_path = self.cache_dir.clone(); @@ -25,16 +25,16 @@ impl ImageCache { config_path.push(format!("{}.config.json", digest)); Ok( if squashfs_path.exists() && manifest_path.exists() && config_path.exists() { - let squashfs_metadata = fs::metadata(&squashfs_path)?; - let manifest_metadata = fs::metadata(&manifest_path)?; - let config_metadata = fs::metadata(&config_path)?; + let squashfs_metadata = fs::metadata(&squashfs_path).await?; + let manifest_metadata = fs::metadata(&manifest_path).await?; + let config_metadata = fs::metadata(&config_path).await?; if squashfs_metadata.is_file() && manifest_metadata.is_file() && config_metadata.is_file() { - let manifest_text = fs::read_to_string(&manifest_path)?; + let manifest_text = fs::read_to_string(&manifest_path).await?; let manifest: ImageManifest = serde_json::from_str(&manifest_text)?; - let config_text = fs::read_to_string(&config_path)?; + let config_text = fs::read_to_string(&config_path).await?; let config: ImageConfiguration = serde_json::from_str(&config_text)?; debug!("cache hit digest={}", digest); Some(ImageInfo::new(squashfs_path.clone(), manifest, config)?) @@ -48,7 +48,7 @@ impl ImageCache { ) } - pub fn store(&self, digest: &str, info: &ImageInfo) -> Result { + pub async fn store(&self, digest: &str, info: &ImageInfo) -> Result { debug!("cache store digest={}", digest); let mut squashfs_path = self.cache_dir.clone(); let mut manifest_path = self.cache_dir.clone(); @@ -56,11 +56,11 @@ impl ImageCache { squashfs_path.push(format!("{}.squashfs", digest)); manifest_path.push(format!("{}.manifest.json", digest)); config_path.push(format!("{}.config.json", digest)); - fs::copy(&info.image_squashfs, &squashfs_path)?; + fs::copy(&info.image_squashfs, &squashfs_path).await?; let manifest_text = serde_json::to_string_pretty(&info.manifest)?; - fs::write(&manifest_path, manifest_text)?; + fs::write(&manifest_path, manifest_text).await?; let config_text = serde_json::to_string_pretty(&info.config)?; - fs::write(&config_path, config_text)?; + fs::write(&config_path, config_text).await?; ImageInfo::new( squashfs_path.clone(), info.manifest.clone(), diff --git a/daemon/src/runtime/image/mod.rs b/daemon/src/runtime/image/mod.rs index 202e2a5..9f11929 100644 --- a/daemon/src/runtime/image/mod.rs +++ b/daemon/src/runtime/image/mod.rs @@ -137,7 +137,7 @@ impl ImageCompiler<'_> { ); let cache_digest = sha256::digest(cache_key); - if let Some(cached) = self.cache.recall(&cache_digest)? { + if let Some(cached) = self.cache.recall(&cache_digest).await? { return Ok(cached); } @@ -184,7 +184,7 @@ impl ImageCompiler<'_> { self.squash(image_dir, squash_file)?; let info = ImageInfo::new(squash_file.clone(), manifest.clone(), config)?; - self.cache.store(&cache_digest, &info) + self.cache.store(&cache_digest, &info).await } fn process_whiteout_entry(