-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[subworker] WIP on rust SW: mosly finished rust API
- Loading branch information
Showing
7 changed files
with
186 additions
and
114 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use super::*; | ||
use std::{fs, env}; | ||
|
||
/// | ||
#[derive(Debug)] | ||
pub struct Context<'a> { | ||
spec: &'a CallMsg, | ||
pub(crate) inputs: Vec<DataInstance<'a>>, | ||
pub(crate) outputs: Vec<Output<'a>>, | ||
/// Task attributes | ||
pub(crate) attributes: Attributes, | ||
/// Absolute path to task working dir | ||
pub(crate) work_dir: PathBuf, | ||
/// Absolute path to staging dir with input and output objects | ||
stage_dir: PathBuf, | ||
pub(crate) success: bool, | ||
} | ||
|
||
impl<'a> Context<'a> { | ||
pub(crate) fn for_call_msg(cm: &'a CallMsg, work_dir: &Path) -> Result<Self> { | ||
assert!(work_dir.is_absolute()); | ||
let stage_dir = work_dir.join("stage"); | ||
fs::create_dir_all(&stage_dir)?; | ||
let inputs = cm.inputs.iter().enumerate().map(|(order, inp)| { | ||
DataInstance::new(inp, &stage_dir, order) | ||
}).collect(); | ||
let outputs = cm.outputs.iter().enumerate().map(|(order, outp)| { | ||
Output::new(outp, &stage_dir, order) | ||
}).collect(); | ||
Ok(Context { | ||
spec: cm, | ||
inputs: inputs, | ||
outputs: outputs, | ||
attributes: Attributes::new(), | ||
work_dir: work_dir.into(), | ||
stage_dir: stage_dir, | ||
success: true, | ||
}) | ||
} | ||
|
||
pub(crate) fn into_result_msg(self) -> ResultMsg { | ||
ResultMsg { | ||
task: self.spec.task, | ||
success: self.success, | ||
attributes: self.attributes, | ||
outputs: self.outputs.into_iter().map(|o| { | ||
let (os, _cached) = o.into_output_spec(); | ||
os | ||
}).collect(), | ||
cached_objects: Vec::new(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::sync::{Mutex, MutexGuard}; | ||
use librain::worker::rpc::subworker_serde::*; | ||
use memmap::Mmap; | ||
use std::mem; | ||
use super::*; | ||
|
||
#[derive(Debug)] | ||
enum InputState { | ||
SpecMem, | ||
SpecMemAndFile, | ||
NotOpen, | ||
MMap(File, Mmap), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct DataInstance<'a> { | ||
spec: &'a DataObjectSpec, | ||
state: Mutex<InputState>, | ||
/// The absolute path to the existing (or potential) file or dir. | ||
/// NB: Must NOT be modified after DataInstance creation! | ||
path: PathBuf, | ||
order: usize, | ||
} | ||
|
||
impl<'a> DataInstance<'a> { | ||
pub(crate) fn new(spec: &'a DataObjectSpec, stage_path: &Path, order: usize) -> Self { | ||
let istate = match spec.location.as_ref().expect("bug: input needs a data location") { | ||
DataLocation::Cached => panic!("bug: cached object requested"), | ||
DataLocation::OtherObject(_) => panic!("bug: `OtherObject` location in input"), | ||
DataLocation::Memory(_) => InputState::SpecMem, | ||
DataLocation::Path(_) => InputState::NotOpen, | ||
}; | ||
let path = if let DataLocation::Path(p) = spec.location.as_ref().unwrap() { | ||
stage_path.join(p) | ||
} else { | ||
stage_path.join(format!("input-{}-{}", spec.id.get_session_id(), spec.id.get_id())) | ||
}; | ||
DataInstance { | ||
spec: spec, | ||
state: Mutex::new(istate), | ||
path: path, | ||
order: order, | ||
} | ||
} | ||
|
||
pub fn get_bytes(&self) -> Result<&'a[u8]> { | ||
// TODO: Check this is not a dir | ||
let mut guard = self.state.lock().unwrap(); | ||
if matchvar!(*guard, InputState::SpecMem) | ||
|| matchvar!(*guard, InputState::SpecMemAndFile) { | ||
if let Some(DataLocation::Memory(d)) = self.spec.location.as_ref() { | ||
return Ok(&d) | ||
} else { | ||
panic!("bug: spec suddenly does not contain memory location"); | ||
} | ||
} | ||
if matchvar!(*guard, InputState::NotOpen) { | ||
let f = File::open(&self.path)?; | ||
let mmap = unsafe { Mmap::map(&f)? }; | ||
*guard = InputState::MMap(f, mmap); | ||
} | ||
if let InputState::MMap(_, ref mmap) = *guard { | ||
// This is safe since the Mmap is not dealocated before the | ||
// containing Input<'a>. | ||
return Ok( unsafe { mem::transmute::<&[u8], &'a [u8]>(&*mmap) }); | ||
} | ||
unreachable!(); | ||
} | ||
|
||
pub fn get_path(&self) -> Result<&'a Path> { | ||
{ | ||
let guard = self.state.lock().unwrap(); | ||
if matchvar!(*guard, InputState::SpecMem) { | ||
unimplemented!(); // TODO: Save the file to disk | ||
} | ||
} | ||
// This is safe since the PathBuf is never modified after creation. | ||
return Ok( unsafe { mem::transmute::<&Path, &'a Path>(&self.path) }); | ||
} | ||
|
||
pub fn get_str(&self) -> Result<&'a str> { | ||
unimplemented!() | ||
} | ||
|
||
pub fn get_content_type(&self) -> Result<&'a[u8]> { | ||
unimplemented!() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.