Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #15836: Fix async & keep_output behavior #2503

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 24 additions & 23 deletions relay/sources/relayd/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 42 additions & 20 deletions relay/sources/relayd/src/remote_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,22 @@ impl RemoteRun {
self.run_parameters.asynchronous,
self.run_parameters.keep_output,
) {
// There are actually three cases:
// * sync with output
// * sync without output
// * async
//
// Coded by two booleans, hence the impossible case.
(true, true) => Err(warp::reject::custom(
"keep_output and asynchronous cannot be true simultaneously",
)),
// Async and output -> spawn in background and stream output
(true, true) => Ok(warp::reply::html(Body::wrap_stream(
self.run_parameters
.remote_run(
&job_config.cfg.remote_run,
self.target.neighbors(job_config.clone()),
self.run_parameters.asynchronous,
)
.select(select_all(
self.target
.next_hops(job_config.clone())
.iter()
.map(|relay| self.forward_call(job_config.clone(), relay.clone())),
)),
))),
// Async and no output -> spawn in background and return early
(true, false) => {
for relay in self.target.next_hops(job_config.clone()) {
tokio::spawn(RemoteRun::consume(
Expand All @@ -153,31 +160,34 @@ impl RemoteRun {
tokio::spawn(RemoteRun::consume(self.run_parameters.remote_run(
&job_config.cfg.remote_run,
self.target.neighbors(job_config.clone()),
self.run_parameters.asynchronous,
)));

Ok(warp::reply::html(Body::wrap_stream(
futures::stream::empty::<Chunk, Error>(),
)))
Ok(warp::reply::html(Body::empty()))
}
// Sync and no output -> wait until the send and return empty output
(false, false) => Ok(warp::reply::html(Body::wrap_stream(
self.run_parameters
.remote_run(
&job_config.cfg.remote_run,
self.target.neighbors(job_config.clone()),
self.run_parameters.asynchronous,
)
.map(|_| Chunk::from(""))
.select(select_all(
self.target
.next_hops(job_config.clone())
.iter()
.map(|relay| self.forward_call(job_config.clone(), relay.clone())),
))
.map(|_| Chunk::from("")),
)),
))),
// Sync and output -> wait until the end and return output
(false, true) => Ok(warp::reply::html(Body::wrap_stream(
self.run_parameters
.remote_run(
&job_config.cfg.remote_run,
self.target.neighbors(job_config.clone()),
self.run_parameters.asynchronous,
)
.select(select_all(
self.target
Expand Down Expand Up @@ -349,26 +359,38 @@ impl RunParameters {
);
}
cmd.arg(nodes.join(","));
debug!("Remote run command: '{:#?}'", cmd);
cmd
}

fn remote_run(
&self,
cfg: &RemoteRunCfg,
nodes: Vec<String>,
) -> Box<dyn Stream<Item = hyper::Chunk, Error = Error> + Send + 'static> {
asynchronous: bool,
) -> Box<dyn Stream<Item = Chunk, Error = Error> + Send + 'static> {
trace!("Starting local remote run on {:#?} with {:#?}", nodes, cfg);
let mut cmd = self.command(cfg, nodes);
cmd.stdout(Stdio::piped());
if asynchronous {
cmd.stdout(Stdio::piped());
}

match cmd.spawn_async() {
Ok(mut c) => {
match (asynchronous, cmd.spawn_async()) {
(false, Ok(c)) => Box::new(
// send output at once
c.wait_with_output()
.map(|o| o.stdout)
.map(Chunk::from)
.map_err(|e| e.into())
.into_stream(),
),
(true, Ok(mut c)) => {
// stream lines
let lines = RunParameters::lines_stream(&mut c);
tokio::spawn(c.map(|_| ()).map_err(|_| ()));
debug!("Spawned remote run command: '{:#?}'", cmd);
Box::new(lines)
}
Err(e) => {
(_, Err(e)) => {
error!("Remote run error while running '{:#?}': {}", cmd, e);
Box::new(futures::stream::once(Err(e.into())))
}
Expand Down
2 changes: 1 addition & 1 deletion relay/sources/relayd/tests/remote_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod tests {

let _ = remove_file("target/tmp/api_test.txt");
let params_sync = [
("asynchronous", "false"),
("asynchronous", "true"),
("keep_output", "true"),
("classes", "class2,class4"),
("nodes", "root"),
Expand Down