Skip to content

Commit 780ed93

Browse files
committed
[api] use rain_core in rust client
1 parent 7b5f017 commit 780ed93

File tree

36 files changed

+381
-278
lines changed

36 files changed

+381
-278
lines changed

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ version = "0.3.0"
33
[workspace]
44

55
members = [
6+
"rain_client",
7+
"rain_client_test",
68
"rain_core",
79
"rain_server",
810
"rain_task",
911
"rain_task_test",
1012
]
1113

1214
[patch.crates-io]
15+
rain_client = { path = "rain_client" }
1316
rain_core = { path = "rain_core" }
1417
rain_task = { path = "rain_task" }

rain_client/Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "rain_client"
3+
version = "0.3.0"
4+
5+
description = "Distributed computational framework for large-scale task-based pipelines. Client library in Rust."
6+
# documentation = "https://docs.rs/rain_task/" # default docs.rs
7+
homepage = "https://github.com/substantic/rain"
8+
repository = "https://github.com/substantic/rain/"
9+
readme = "README.md"
10+
authors = [
11+
"Stanislav Bohm <[email protected]>",
12+
"Tomas Gavenciak <[email protected]>",
13+
"Vojtech Cima <[email protected]>",
14+
]
15+
license = "MIT"
16+
17+
exclude = ["testing/**/*"]
18+
19+
[badges]
20+
travis-ci = { repository = "substantic/rain", branch = "master" }
21+
maintenance = { status = "actively-developed" }
22+
23+
[dependencies]
24+
capnp-rpc = "0.8"
25+
error-chain = "0.11"
26+
futures = "0.1"
27+
log = "0.4"
28+
rain_core = "0.3.0"
29+
tokio-core = "0.1"
30+
serde = "1"
31+
serde_json = "1"

src/client/client.rs renamed to rain_client/src/client/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::error::Error;
22
use std::net::SocketAddr;
33

4-
use CLIENT_PROTOCOL_VERSION;
5-
use super::session::Session;
64
use super::communicator::Communicator;
5+
use super::session::Session;
6+
use rain_core::CLIENT_PROTOCOL_VERSION;
77
use std::rc::Rc;
88

99
pub struct Client {

src/client/communicator.rs renamed to rain_client/src/client/communicator.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
use tokio_core::reactor::Core;
2-
use std::net::SocketAddr;
3-
use tokio_core::net::TcpStream;
4-
use std::error::Error;
5-
use common::rpc::new_rpc_system;
61
use capnp_rpc::rpc_twoparty_capnp;
72
use futures::Future;
3+
use rain_core::comm::new_rpc_system;
4+
use std::error::Error;
5+
use std::net::SocketAddr;
6+
use tokio_core::net::TcpStream;
7+
use tokio_core::reactor::Core;
88

99
use super::task::Task;
10-
use common::id::{DataObjectId, TaskId};
11-
use common::convert::{FromCapnp, ToCapnp};
1210
use client::dataobject::DataObject;
13-
use std::cell::RefCell;
14-
use std::cell::RefMut;
11+
use rain_core::types::{DataObjectId, TaskId};
12+
use rain_core::utils::{FromCapnp, ToCapnp};
13+
use rain_core::{client_capnp, common_capnp, server_capnp};
14+
use std::cell::{RefCell, RefMut};
1515

1616
pub struct Communicator {
1717
core: RefCell<Core>,
18-
service: ::client_capnp::client_service::Client,
18+
service: client_capnp::client_service::Client,
1919
}
2020

2121
impl Communicator {
@@ -28,7 +28,7 @@ impl Communicator {
2828
debug!("Connection to server {} established", scheduler);
2929

3030
let mut rpc = Box::new(new_rpc_system(stream, None));
31-
let bootstrap: ::server_capnp::server_bootstrap::Client =
31+
let bootstrap: server_capnp::server_bootstrap::Client =
3232
rpc.bootstrap(rpc_twoparty_capnp::Side::Server);
3333
handle.spawn(rpc.map_err(|err| panic!("RPC error: {}", err)));
3434

@@ -116,19 +116,28 @@ impl Communicator {
116116
))
117117
}
118118

119-
pub fn fetch(&self, object_id: DataObjectId) -> Result<Vec<u8>, Box<Error>> {
119+
pub fn fetch(&self, object_id: &DataObjectId) -> Result<Vec<u8>, Box<Error>> {
120120
let mut req = self.service.fetch_request();
121121
object_id.to_capnp(&mut req.get().get_id().unwrap());
122122
req.get().set_size(1024);
123123

124124
let response = self.comm().run(req.send().promise)?;
125125

126+
// TODO: handle error states
126127
let reader = response.get()?;
127128
match reader.get_status().which()? {
128-
::common_capnp::fetch_result::status::Ok(()) => {
129+
common_capnp::fetch_result::status::Ok(()) => {
129130
let data = reader.get_data()?;
130131
Ok(Vec::from(data))
131132
}
133+
common_capnp::fetch_result::status::Removed(()) => {
134+
print!("Removed");
135+
Ok(vec![])
136+
}
137+
common_capnp::fetch_result::status::Error(err) => {
138+
print!("Error: {:?}", err.unwrap().get_message());
139+
Ok(vec![])
140+
}
132141
_ => bail!("Non-ok status"),
133142
}
134143
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
use common::Attributes;
2-
use common::id::DataObjectId;
3-
use common::DataType;
1+
use rain_core::types::{DataObjectId, ObjectSpec};
42
use std::cell::Cell;
53

64
pub struct DataObject {
7-
pub id: DataObjectId,
8-
pub label: String,
95
pub keep: Cell<bool>,
106
pub data: Option<Vec<u8>>,
11-
pub attributes: Attributes,
12-
pub data_type: DataType,
7+
pub spec: ObjectSpec,
138
}
149

1510
impl DataObject {
1611
pub fn keep(&self) {
1712
self.keep.set(true);
1813
}
14+
pub fn id(&self) -> DataObjectId {
15+
self.spec.id
16+
}
1917
}
File renamed without changes.

rain_client/src/client/rpc.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::task::Task;
2+
use client::dataobject::DataObject;
3+
use rain_core::client_capnp;
4+
use rain_core::utils::ToCapnp;
5+
use serde_json;
6+
7+
macro_rules! to_capnp_list {
8+
($builder:expr, $items:expr, $name:ident) => {{
9+
let mut builder = $builder.$name($items.len() as u32);
10+
for (i, obj) in $items.iter().enumerate() {
11+
obj.to_capnp(&mut builder.reborrow().get(i as u32));
12+
}
13+
}};
14+
}
15+
macro_rules! from_capnp_list {
16+
($builder:expr, $items:ident, $obj:ident) => {{
17+
$builder
18+
.$items()?
19+
.iter()
20+
.map(|item| $obj::from_capnp(&item))
21+
.collect()
22+
}};
23+
}
24+
25+
impl<'a> ToCapnp<'a> for Task {
26+
type Builder = client_capnp::task::Builder<'a>;
27+
28+
fn to_capnp(&self, builder: &mut Self::Builder) {
29+
builder.set_spec(&serde_json::to_string(&self.spec).unwrap());
30+
}
31+
}
32+
impl<'a> ToCapnp<'a> for DataObject {
33+
type Builder = client_capnp::data_object::Builder<'a>;
34+
35+
fn to_capnp(&self, builder: &mut Self::Builder) {
36+
builder.set_spec(&serde_json::to_string(&self.spec).unwrap());
37+
builder.set_keep(self.keep.get());
38+
39+
if let &Some(ref data) = &self.data {
40+
builder.set_data(&data);
41+
builder.set_has_data(true);
42+
} else {
43+
builder.set_has_data(false);
44+
}
45+
}
46+
}

src/client/session.rs renamed to rain_client/src/client/session.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
use super::communicator::Communicator;
22
use client::dataobject::DataObject;
33
use client::task::Task;
4+
use rain_core::common_capnp;
5+
use rain_core::types::{DataObjectId, DataType, ObjectSpec, Resources, SId, TaskId, TaskSpec,
6+
TaskSpecInput, UserAttrs};
7+
use serde_json;
8+
use std::cell::Cell;
49
use std::collections::HashMap;
510
use std::error::Error;
6-
use client::task::TaskInput;
7-
use common::Attributes;
8-
use common::id::TaskId;
9-
use common::id::DataObjectId;
10-
use common::id::SId;
11-
use common::DataType;
12-
use std::cell::Cell;
1311
use std::rc::Rc;
1412

1513
pub type DataObjectPtr = Rc<DataObject>;
@@ -45,28 +43,36 @@ impl Session {
4543
}
4644

4745
pub fn unkeep(&mut self, objects: &[DataObjectPtr]) -> Result<(), Box<Error>> {
48-
self.comm
49-
.unkeep(&objects.iter().map(|o| o.id).collect::<Vec<DataObjectId>>())
46+
self.comm.unkeep(&objects
47+
.iter()
48+
.map(|o| o.id())
49+
.collect::<Vec<DataObjectId>>())
5050
}
5151

5252
pub fn wait(&mut self, tasks: &[TaskPtr], objects: &[DataObjectPtr]) -> Result<(), Box<Error>> {
5353
self.comm.wait(
54-
&tasks.iter().map(|t| t.id).collect::<Vec<TaskId>>(),
55-
&objects.iter().map(|o| o.id).collect::<Vec<DataObjectId>>(),
54+
&tasks.iter().map(|t| t.id()).collect::<Vec<TaskId>>(),
55+
&objects
56+
.iter()
57+
.map(|o| o.id())
58+
.collect::<Vec<DataObjectId>>(),
5659
)
5760
}
5861
pub fn wait_some(
5962
&mut self,
6063
tasks: &[TaskPtr],
6164
objects: &[DataObjectPtr],
6265
) -> Result<(Vec<TaskPtr>, Vec<DataObjectPtr>), Box<Error>> {
63-
let task_map: HashMap<TaskId, &TaskPtr> = tasks.iter().map(|t| (t.id, t)).collect();
66+
let task_map: HashMap<TaskId, &TaskPtr> = tasks.iter().map(|t| (t.id(), t)).collect();
6467
let object_map: HashMap<DataObjectId, &DataObjectPtr> =
65-
objects.iter().map(|o| (o.id, o)).collect();
68+
objects.iter().map(|o| (o.id(), o)).collect();
6669

6770
let (task_ids, object_ids) = self.comm.wait_some(
68-
&tasks.iter().map(|t| t.id).collect::<Vec<TaskId>>(),
69-
&objects.iter().map(|o| o.id).collect::<Vec<DataObjectId>>(),
71+
&tasks.iter().map(|t| t.id()).collect::<Vec<TaskId>>(),
72+
&objects
73+
.iter()
74+
.map(|o| o.id())
75+
.collect::<Vec<DataObjectId>>(),
7076
)?;
7177

7278
Ok((
@@ -82,13 +88,13 @@ impl Session {
8288
}
8389
pub fn wait_all(&mut self) -> Result<(), Box<Error>> {
8490
self.comm.wait(
85-
&vec![TaskId::new(self.id, ::common_capnp::ALL_TASKS_ID)],
91+
&vec![TaskId::new(self.id, common_capnp::ALL_TASKS_ID)],
8692
&vec![],
8793
)
8894
}
8995

90-
pub fn fetch(&mut self, object: &DataObject) -> Result<Vec<u8>, Box<Error>> {
91-
self.comm.fetch(object.id)
96+
pub fn fetch(&mut self, o: &DataObjectPtr) -> Result<Vec<u8>, Box<Error>> {
97+
self.comm.fetch(&o.id())
9298
}
9399

94100
pub fn blob(&mut self, data: Vec<u8>) -> DataObjectPtr {
@@ -102,13 +108,17 @@ impl Session {
102108
DataObjectId::new(self.id, id)
103109
}
104110
pub(crate) fn create_object(&mut self, label: String, data: Option<Vec<u8>>) -> DataObjectPtr {
105-
let object = DataObject {
111+
let spec = ObjectSpec {
106112
id: self.create_object_id(),
107-
keep: Cell::new(false),
108113
label,
109-
data,
110-
attributes: Attributes::new(),
111114
data_type: DataType::Blob,
115+
content_type: "".to_owned(),
116+
user: UserAttrs::new(),
117+
};
118+
let object = DataObject {
119+
keep: Cell::new(false),
120+
data,
121+
spec,
112122
};
113123
let rc = Rc::new(object);
114124
self.data_objects.push(rc.clone());
@@ -124,27 +134,24 @@ impl Session {
124134
}
125135
pub fn create_task(
126136
&mut self,
127-
command: String,
128-
inputs: Vec<TaskInput>,
137+
task_type: String,
138+
inputs: Vec<TaskSpecInput>,
129139
outputs: Vec<DataObjectPtr>,
130140
config: HashMap<String, String>,
131-
cpus: i32,
141+
cpus: u32,
132142
) -> TaskPtr {
133-
let mut attributes = Attributes::new();
134-
attributes.set("config", config).unwrap();
135-
136-
let mut resources: HashMap<String, i32> = HashMap::new();
137-
resources.insert("cpus".to_owned(), cpus);
138-
attributes.set("resources", resources).unwrap();
139-
140-
let task = Task {
143+
let spec = TaskSpec {
141144
id: self.create_task_id(),
142-
command,
143145
inputs,
144-
outputs,
145-
attributes,
146+
task_type,
147+
outputs: outputs.iter().map(|o| o.id()).collect(),
148+
config: Some(serde_json::to_value(&config).unwrap()),
149+
resources: Resources { cpus },
150+
user: UserAttrs::new(),
146151
};
147152

153+
let task = Task { spec, outputs };
154+
148155
let rc = Rc::new(task);
149156
self.tasks.push(rc.clone());
150157

0 commit comments

Comments
 (0)