Skip to content

Commit 940fbf0

Browse files
Fix stage key extraction (#2472)
1 parent 7c98a43 commit 940fbf0

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

ballista/rust/scheduler/src/state/persistent_state.rs

+40-6
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,21 @@ fn extract_job_id_from_job_key(job_key: &str) -> Result<&str> {
318318

319319
fn extract_stage_id_from_stage_key(stage_key: &str) -> Result<StageKey> {
320320
let splits: Vec<&str> = stage_key.split('/').collect();
321-
if splits.len() < 4 {
321+
if splits.len() > 4 {
322+
Ok((
323+
splits[splits.len() - 2].to_string(),
324+
splits[splits.len() - 1].parse::<u32>().map_err(|e| {
325+
BallistaError::Internal(format!(
326+
"Invalid stage ID in stage key: {}, {:?}",
327+
stage_key, e
328+
))
329+
})?,
330+
))
331+
} else {
322332
Err(BallistaError::Internal(format!(
323333
"Unexpected stage key: {}",
324334
stage_key
325335
)))
326-
} else {
327-
Ok((
328-
splits.get(2).unwrap().to_string(),
329-
splits.get(3).unwrap().parse::<u32>().unwrap(),
330-
))
331336
}
332337
}
333338

@@ -352,3 +357,32 @@ fn encode_protobuf<T: Message + Default>(msg: &T) -> Result<Vec<u8>> {
352357
})?;
353358
Ok(value)
354359
}
360+
361+
#[cfg(test)]
362+
mod test {
363+
use super::extract_stage_id_from_stage_key;
364+
365+
#[test]
366+
fn test_extract_stage_id_from_stage_key() {
367+
let (job_id, stage_id) =
368+
extract_stage_id_from_stage_key("/ballista/default/stages/2Yoyba8/1")
369+
.expect("extracting stage key");
370+
371+
assert_eq!(job_id.as_str(), "2Yoyba8");
372+
assert_eq!(stage_id, 1);
373+
374+
let (job_id, stage_id) =
375+
extract_stage_id_from_stage_key("ballista/default/stages/2Yoyba8/1")
376+
.expect("extracting stage key");
377+
378+
assert_eq!(job_id.as_str(), "2Yoyba8");
379+
assert_eq!(stage_id, 1);
380+
381+
let (job_id, stage_id) =
382+
extract_stage_id_from_stage_key("ballista//stages/2Yoyba8/1")
383+
.expect("extracting stage key");
384+
385+
assert_eq!(job_id.as_str(), "2Yoyba8");
386+
assert_eq!(stage_id, 1);
387+
}
388+
}

0 commit comments

Comments
 (0)