Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExeUnit cache: cleaner cache/tmp, less intermediary files #1340

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions exe-unit/src/service/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ impl Handler<DeployImage> for TransferService {
{
let from_provider = actor_try!(self.provider(&source_url));
let to_provider: FileTransferProvider = Default::default();
let cache_path = self.cache.to_cache_path(&cache_name);
let temp_path = self.cache.to_temp_path(&cache_name);
let temp_url = Url::from_file_path(temp_path.to_path_buf()).unwrap();

Expand All @@ -286,26 +285,29 @@ impl Handler<DeployImage> for TransferService {
let fut = async move {
let final_path = final_path.to_path_buf();
let temp_path = temp_path.to_path_buf();
let cache_path = cache_path.to_path_buf();

let stream_fn = || Self::source(from_provider.clone(), &source_url, &args);
let sink_fn = || to_provider.destination(&temp_url, &args);

if cache_path.exists() {
log::info!("Deploying cached image: {:?}", cache_path);
std::fs::copy(cache_path, &final_path)?;
if final_path.exists() {
log::info!("Deploying cached image: {:?}", final_path);
return Ok(final_path);
}

{
let _guard = AbortHandleGuard::register(address, abort).await?;
Abortable::new(retry_transfer(stream_fn, sink_fn, Retry::default()), reg)
let retry = retry_transfer(stream_fn, sink_fn, Retry::default());
Ok(Abortable::new(retry, reg)
.await
.map_err(TransferError::from)??;
.map_err(TransferError::from)??)
}
.map_err(|e: Error| {
log::warn!("Removing temporary download file: {}", temp_path.display());
let _ = std::fs::remove_file(&temp_path);
e
})?;

std::fs::rename(temp_path, &cache_path)?;
std::fs::copy(cache_path, &final_path)?;
std::fs::rename(temp_path, &final_path)?;

log::info!("Deployment from {:?} finished", source_url.url);
Ok(final_path)
Expand Down Expand Up @@ -461,18 +463,12 @@ impl Cache {
#[inline(always)]
#[cfg(not(feature = "sgx"))]
fn to_temp_path(&self, path: &CachePath) -> ProjectedPath {
ProjectedPath::local(self.tmp_dir.clone(), path.temp_path_buf())
}

#[inline(always)]
#[cfg(not(feature = "sgx"))]
fn to_cache_path(&self, path: &CachePath) -> ProjectedPath {
ProjectedPath::local(self.tmp_dir.clone(), path.cache_path_buf())
ProjectedPath::local(self.tmp_dir.clone(), path.temp_path())
}

#[inline(always)]
fn to_final_path(&self, path: &CachePath) -> ProjectedPath {
ProjectedPath::local(self.dir.clone(), path.final_path_buf())
ProjectedPath::local(self.dir.clone(), path.final_path())
}
}

Expand Down
14 changes: 4 additions & 10 deletions exe-unit/src/util/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl ProjectedPath {

impl From<CachePath> for PathBuf {
fn from(cache_path: CachePath) -> Self {
cache_path.final_path_buf()
cache_path.final_path()
}
}

Expand All @@ -84,22 +84,16 @@ impl CachePath {
pub fn new(path: PathBuf, hash: Vec<u8>, nonce: String) -> Self {
CachePath { path, hash, nonce }
}

/// Creates the long version of path, including hash and the "random" token.
pub fn temp_path_buf(&self) -> PathBuf {
pub fn temp_path(&self) -> PathBuf {
self.to_path_buf(true, true)
}

/// Creates the long version of path, including hash and the "random" token.
pub fn cache_path_buf(&self) -> PathBuf {
/// Creates a shorter version of path, including hash and excluding the "random" token.
pub fn final_path(&self) -> PathBuf {
self.to_path_buf(true, false)
}

/// Creates a shorter version of path, excluding the "random" token.
pub fn final_path_buf(&self) -> PathBuf {
self.to_path_buf(false, false)
}

fn to_path_buf(&self, with_hash: bool, with_nonce: bool) -> PathBuf {
let stem = self.path.file_stem().unwrap();
let extension = self.path.extension();
Expand Down