Skip to content

Commit

Permalink
Merge pull request #323 from r-c-n/rcn-20241017-resctl-demo-fixes
Browse files Browse the repository at this point in the history
Improve UX for the lambda workflow (`upload` and `lambda`)
  • Loading branch information
htejun authored Oct 29, 2024
2 parents 862e873 + a861c55 commit 47035c5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
49 changes: 38 additions & 11 deletions resctl-bench/src/lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,25 @@ pub fn init_lambda() {
pub async fn run() -> Result<()> {
let handler = |event: LambdaEvent<LambdaFunctionUrlRequest>| async move {
let helper = LambdaHelper::new().await;
let request: Request = serde_json::from_str(event.payload.body.as_ref().unwrap().as_str())?;

let Some(event_body) = event.payload.body else {
error!("No body found in event payload: {:?}", event.payload);
return Ok(Response {
issue: None,
error_type: Some(format!("Custom")),
error_message: Some(format!("No body found in event payload.")),
});
};
let request: Request = match serde_json::from_str(event_body.as_str()) {
Ok(req) => req,
Err(e) => {
error!("Error parsing event body: {}", e);
return Ok(Response {
issue: None,
error_type: Some(format!("Custom")),
error_message: Some(format!("Error parsing event body: {}", e)),
});
}
};
// Unpack the base64 encoded gz-compressed file. This is safe because Lambda has a hard
// limit on the size of the requests (6MB at the moment).
let data = base64::decode(&request.data)?;
Expand All @@ -65,6 +82,7 @@ pub async fn run() -> Result<()> {
let object_name = helper.object_name_from_hash(&data)?;

if helper.s3_object_exists(&object_name).await? {
error!("File {} has already been submitted", object_name);
return Ok(Response {
issue: None,
error_type: Some(format!("Custom")),
Expand All @@ -81,10 +99,18 @@ pub async fn run() -> Result<()> {
&sysinfo,
&format!("{}\n\n{}```\n{}\n```", s3_url, identification, summary),
)
.await?;
.await;
if let Err(e) = issue_url {
error!("Error creating Github issue: {:#}", e);
return Ok(Response {
issue: None,
error_type: Some(format!("Custom")),
error_message: Some(format!("Error creating Github issue: {}", e)),
});
}

Ok::<_, Error>(Response {
issue: Some(issue_url),
issue: Some(issue_url.unwrap()),
error_type: None,
error_message: None,
})
Expand Down Expand Up @@ -158,27 +184,28 @@ impl LambdaHelper {
.set_name(Some("/iocost-bot/appid".to_string()))
.send()
.await
.expect("Failed to query parameter")
.context("Failed to query AWS parameter /iocost-bot/appid")?
.parameter
.expect("Could not find parameter")
.context("Could not find AWS parameter /iocost-bot/appid")?
.value
.expect("Parameter value is None");
.context("Value of parameter AWS /iocost-bot/appid is None")?;

let pem = self
.ssm
.get_parameter()
.set_name(Some("/iocost-bot/privatekey".to_string()))
.send()
.await
.expect("Failed to query parameter")
.context("Failed to query parameter AWS /iocost-bot/privatekey")?
.parameter
.expect("Could not find parameter")
.context("Could not find parameter AWS /iocost-bot/privatekey")?
.value
.expect("Parameter value is None");
.context("Value of parameter AWS /iocost-bot/privatekey is None")?;

let token = octocrab::auth::create_jwt(
app_id.parse::<u64>().unwrap().into(),
&jsonwebtoken::EncodingKey::from_rsa_pem(pem.as_bytes()).unwrap(),
&jsonwebtoken::EncodingKey::from_rsa_pem(pem.as_bytes())
.context("Couldn't create a JWT for Github authentication")?,
)
.unwrap();

Expand Down
21 changes: 10 additions & 11 deletions resctl-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,10 @@ impl Program {

fn do_upload(&mut self) -> Result<()> {
let args = &self.args_file.data;
let path = Path::new(&args.result)
.file_name()
.unwrap()
.to_string_lossy()
.to_string();
let mut data: Vec<u8> = std::fs::read(&args.result)
.context(format!("Error opening or reading {}", &args.result))?;

let mut data = Vec::<u8>::new();
let mut f = std::fs::OpenOptions::new().read(true).open(&path)?;
f.read_to_end(&mut data)?;

if !path.ends_with(".gz") {
if !args.result.ends_with(".gz") {
let deflated = std::mem::take(&mut data);
let mut encoder = libflate::gzip::Encoder::new(&mut data)?;
encoder.write_all(&deflated).context("Compressing file")?;
Expand All @@ -285,7 +278,13 @@ impl Program {
.with_json(&request)?
.send()?;

let response: LambdaResponse = serde_json::from_str(response.as_str()?)?;
let Ok(response) = serde_json::from_str::<LambdaResponse>(response.as_str()?) else {
error!(
"Failed to submit benchmark. Server response: {}",
response.as_str()?
);
std::process::exit(1);
};
if response.issue.is_none() {
if let Some(error_message) = response.error_message {
error!("Failed to submit benchmark: {}", error_message);
Expand Down

0 comments on commit 47035c5

Please sign in to comment.