From 28064b682a8645f87d82a1b9bb04915ccfc340e4 Mon Sep 17 00:00:00 2001 From: Cheick Keita Date: Tue, 11 Oct 2022 12:57:05 -0700 Subject: [PATCH 1/3] Update the logic for checking if a blob exist before uploading --- src/agent/onefuzz/src/uploader.rs | 70 +++++++++++++++---------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/src/agent/onefuzz/src/uploader.rs b/src/agent/onefuzz/src/uploader.rs index dcbfc0d2ed..2f21a6abb0 100644 --- a/src/agent/onefuzz/src/uploader.rs +++ b/src/agent/onefuzz/src/uploader.rs @@ -7,7 +7,8 @@ use anyhow::{Context, Result}; use futures::stream::TryStreamExt; use reqwest::{Body, Client, Response, StatusCode, Url}; use reqwest_retry::{ - send_retry_reqwest_default, RetryCheck, SendRetry, DEFAULT_RETRY_PERIOD, MAX_RETRY_ATTEMPTS, + send_retry_reqwest, send_retry_reqwest_default, RetryCheck, SendRetry, DEFAULT_RETRY_PERIOD, + MAX_RETRY_ATTEMPTS, }; use serde::Serialize; use tokio::{fs, io}; @@ -42,44 +43,39 @@ impl BlobUploader { url }; - // Check if the file already exists before uploading - if let Ok(head) = self - .client - .head(url.clone()) - .send_retry( - |code| match code { - StatusCode::NOT_FOUND => RetryCheck::Fail, - _ => RetryCheck::Retry, - }, - DEFAULT_RETRY_PERIOD, - MAX_RETRY_ATTEMPTS, - ) - .await - { - if head.status() == StatusCode::OK { - return Ok(head); - } - } - let content_length = format!("{}", file_len); - let resp = send_retry_reqwest_default(|| { - let file = fs::File::from_std(std::fs::File::open(file_path)?); - let reader = io::BufReader::new(file); - let codec = codec::BytesCodec::new(); - let file_stream = codec::FramedRead::new(reader, codec) - .map_ok(bytes::BytesMut::freeze) - .into_stream(); - - let request_builder = self - .client - .put(url.clone()) - .header("Content-Length", &content_length) - .header("x-ms-blob-type", "BlockBlob") - .body(Body::wrap_stream(file_stream)); - - Ok(request_builder) - }) + let resp = send_retry_reqwest( + || { + let file = fs::File::from_std(std::fs::File::open(file_path)?); + let reader = io::BufReader::new(file); + let codec = codec::BytesCodec::new(); + let file_stream = codec::FramedRead::new(reader, codec) + .map_ok(bytes::BytesMut::freeze) + .into_stream(); + + let request_builder = self + .client + .put(url.clone()) + // https://learn.microsoft.com/en-us/rest/api/storageservices/put-blob-from-url#request-headers + .header("Content-Length", &content_length) + .header("x-ms-blob-type", "BlockBlob") + // upload only if the the destination blob does not exist + .header("If-None-Match", "*") + .body(Body::wrap_stream(file_stream)); + + Ok(request_builder) + }, + |status| { + if status == StatusCode::PRECONDITION_FAILED { + RetryCheck::Succeed + } else { + RetryCheck::Retry + } + }, + DEFAULT_RETRY_PERIOD, + MAX_RETRY_ATTEMPTS, + ) .await .context("BlobUploader.upload")?; From 86c8124a4cb4fab0ddf8de46ceb2783662015533 Mon Sep 17 00:00:00 2001 From: Cheick Keita Date: Tue, 11 Oct 2022 14:19:46 -0700 Subject: [PATCH 2/3] remove unused ref --- src/agent/onefuzz/src/uploader.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/agent/onefuzz/src/uploader.rs b/src/agent/onefuzz/src/uploader.rs index 2f21a6abb0..ea2a0abf7a 100644 --- a/src/agent/onefuzz/src/uploader.rs +++ b/src/agent/onefuzz/src/uploader.rs @@ -7,8 +7,7 @@ use anyhow::{Context, Result}; use futures::stream::TryStreamExt; use reqwest::{Body, Client, Response, StatusCode, Url}; use reqwest_retry::{ - send_retry_reqwest, send_retry_reqwest_default, RetryCheck, SendRetry, DEFAULT_RETRY_PERIOD, - MAX_RETRY_ATTEMPTS, + send_retry_reqwest, RetryCheck, SendRetry, DEFAULT_RETRY_PERIOD, MAX_RETRY_ATTEMPTS, }; use serde::Serialize; use tokio::{fs, io}; From 06c9292fbf858b253b1a90db9257e9b42a44b972 Mon Sep 17 00:00:00 2001 From: Cheick Keita Date: Tue, 11 Oct 2022 15:26:22 -0700 Subject: [PATCH 3/3] update the status code check --- src/agent/onefuzz/src/uploader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent/onefuzz/src/uploader.rs b/src/agent/onefuzz/src/uploader.rs index ea2a0abf7a..cb2d90abf3 100644 --- a/src/agent/onefuzz/src/uploader.rs +++ b/src/agent/onefuzz/src/uploader.rs @@ -66,7 +66,7 @@ impl BlobUploader { Ok(request_builder) }, |status| { - if status == StatusCode::PRECONDITION_FAILED { + if status == StatusCode::PRECONDITION_FAILED || status == StatusCode::CONFLICT { RetryCheck::Succeed } else { RetryCheck::Retry