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
Minimum viable speculating command runner #7992
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
65e0b2d
Boiler plate for speculating command runner
bae3c01
it's boxes all the way down
57ebff6
add speculation with hardcoded speculation timeout of 1s
d404b37
fixup rebase conflicts
ddaafc7
better future combinators on delay
4558982
revert context changes
0a39bbc
grad test helper directly from remote tests. move TODO comment
fc95039
Merge branch 'master' into hfuller/speculation-7949
a0b583e
add call counter, add missing trait back. formatting
5a5323c
Merge branch 'master' into hfuller/speculation-7949
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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,218 @@ | ||
use super::{CommandRunner, ExecuteProcessRequest, FallibleExecuteProcessResult}; | ||
use boxfuture::{BoxFuture, Boxable}; | ||
use futures::future::{err, ok, Future}; | ||
use std::sync::Arc; | ||
use std::time::{Duration, Instant}; | ||
use tokio_timer::Delay; | ||
use workunit_store::WorkUnitStore; | ||
|
||
#[derive(Clone)] | ||
pub struct SpeculatingCommandRunner { | ||
primary: Arc<dyn CommandRunner>, | ||
secondary: Arc<dyn CommandRunner>, | ||
speculation_timeout: Duration, | ||
} | ||
|
||
impl SpeculatingCommandRunner { | ||
pub fn new( | ||
primary: Box<dyn CommandRunner>, | ||
secondary: Box<dyn CommandRunner>, | ||
speculation_timeout: Duration, | ||
) -> SpeculatingCommandRunner { | ||
SpeculatingCommandRunner { | ||
primary: primary.into(), | ||
secondary: secondary.into(), | ||
speculation_timeout: speculation_timeout, | ||
} | ||
} | ||
} | ||
|
||
impl CommandRunner for SpeculatingCommandRunner { | ||
fn run( | ||
&self, | ||
req: ExecuteProcessRequest, | ||
workunit_store: WorkUnitStore, | ||
) -> BoxFuture<FallibleExecuteProcessResult, String> { | ||
let command_runner = self.clone(); | ||
let workunit_store_clone = workunit_store.clone(); | ||
let req_2 = req.clone(); | ||
let delay = Delay::new(Instant::now() + self.speculation_timeout); | ||
self | ||
.primary | ||
.run(req, workunit_store) | ||
.select(delay.then(move |_| command_runner.secondary.run(req_2, workunit_store_clone))) | ||
.then(|raced_result| match raced_result { | ||
Ok((successful_res, _outstanding_req)) => { | ||
ok::<FallibleExecuteProcessResult, String>(successful_res).to_boxed() | ||
} | ||
Err((failed_res, _outstanding_req)) => { | ||
err::<FallibleExecuteProcessResult, String>(failed_res).to_boxed() | ||
} | ||
}) | ||
.to_boxed() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::remote::tests::echo_foo_request; | ||
use boxfuture::{BoxFuture, Boxable}; | ||
use bytes::Bytes; | ||
use futures::future::Future; | ||
use hashing::EMPTY_DIGEST; | ||
use std::sync::{Arc, Mutex}; | ||
use std::time::{Duration, Instant}; | ||
use tokio; | ||
use tokio_timer::Delay; | ||
use workunit_store::WorkUnitStore; | ||
|
||
use super::{ | ||
CommandRunner, ExecuteProcessRequest, FallibleExecuteProcessResult, SpeculatingCommandRunner, | ||
}; | ||
|
||
#[test] | ||
fn test_no_speculation() { | ||
let (result, call_counter) = run_speculation_test(0, 0, 100, false, false); | ||
assert_eq![1, *call_counter.lock().unwrap()]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the no speculation case, it may actually be nice to have two counters; a "started" counter and a "finished" counter, to show that we didn't even start the second attempt. But also understand if you don't think it's worthwhile :) |
||
assert_eq![result.unwrap().stdout, Bytes::from("m1")]; | ||
} | ||
|
||
#[test] | ||
fn test_speculate() { | ||
let (result, call_counter) = run_speculation_test(100, 0, 10, false, false); | ||
assert_eq![1, *call_counter.lock().unwrap()]; | ||
assert_eq![result.unwrap().stdout, Bytes::from("m2")] | ||
} | ||
|
||
#[test] | ||
fn first_req_slow_success() { | ||
let (result, call_counter) = run_speculation_test(15, 10, 10, false, false); | ||
assert_eq![1, *call_counter.lock().unwrap()]; | ||
assert_eq![result.unwrap().stdout, Bytes::from("m1")] | ||
} | ||
|
||
#[test] | ||
fn first_req_slow_fail() { | ||
let (result, call_counter) = run_speculation_test(100, 0, 10, true, false); | ||
assert_eq![1, *call_counter.lock().unwrap()]; | ||
assert_eq![result.unwrap().stdout, Bytes::from("m2")] | ||
} | ||
|
||
#[test] | ||
fn first_req_fast_fail() { | ||
let (result, call_counter) = run_speculation_test(15, 10, 10, true, false); | ||
assert_eq![1, *call_counter.lock().unwrap()]; | ||
assert_eq![result.unwrap_err(), Bytes::from("m1")] | ||
} | ||
|
||
#[test] | ||
fn second_req_fast_fail() { | ||
let (result, call_counter) = run_speculation_test(100, 0, 10, true, true); | ||
assert_eq![1, *call_counter.lock().unwrap()]; | ||
assert_eq![result.unwrap_err(), Bytes::from("m2")] | ||
} | ||
|
||
fn run_speculation_test( | ||
r1_latency_ms: u64, | ||
r2_latency_ms: u64, | ||
speculation_delay_ms: u64, | ||
r1_is_err: bool, | ||
r2_is_err: bool, | ||
) -> ( | ||
Result<FallibleExecuteProcessResult, String>, | ||
Arc<Mutex<u32>>, | ||
) { | ||
let mut runtime = tokio::runtime::Runtime::new().unwrap(); | ||
let execute_request = echo_foo_request(); | ||
let msg1: String = "m1".into(); | ||
let msg2: String = "m2".into(); | ||
let workunit_store = WorkUnitStore::new(); | ||
let call_counter = Arc::new(Mutex::new(0)); | ||
let runner = SpeculatingCommandRunner::new( | ||
Box::new(make_delayed_command_runner( | ||
msg1.clone(), | ||
r1_latency_ms, | ||
r1_is_err, | ||
call_counter.clone(), | ||
)), | ||
Box::new(make_delayed_command_runner( | ||
msg2.clone(), | ||
r2_latency_ms, | ||
r2_is_err, | ||
call_counter.clone(), | ||
)), | ||
Duration::from_millis(speculation_delay_ms), | ||
); | ||
( | ||
runtime.block_on(runner.run(execute_request, workunit_store)), | ||
call_counter, | ||
) | ||
} | ||
|
||
fn make_delayed_command_runner( | ||
msg: String, | ||
delay: u64, | ||
is_err: bool, | ||
call_counter: Arc<Mutex<u32>>, | ||
) -> DelayedCommandRunner { | ||
let mut result; | ||
if is_err { | ||
result = Err(msg.into()); | ||
} else { | ||
result = Ok(FallibleExecuteProcessResult { | ||
stdout: msg.into(), | ||
stderr: "".into(), | ||
exit_code: 0, | ||
output_directory: EMPTY_DIGEST, | ||
execution_attempts: vec![], | ||
}) | ||
} | ||
DelayedCommandRunner::new(Duration::from_millis(delay), result, call_counter) | ||
} | ||
|
||
#[derive(Clone)] | ||
struct DelayedCommandRunner { | ||
delay: Duration, | ||
result: Result<FallibleExecuteProcessResult, String>, | ||
call_counter: Arc<Mutex<u32>>, | ||
} | ||
|
||
impl DelayedCommandRunner { | ||
pub fn new( | ||
delay: Duration, | ||
result: Result<FallibleExecuteProcessResult, String>, | ||
call_counter: Arc<Mutex<u32>>, | ||
) -> DelayedCommandRunner { | ||
DelayedCommandRunner { | ||
delay: delay, | ||
result: result, | ||
call_counter: call_counter, | ||
} | ||
} | ||
fn incr_call_counter(&self) { | ||
let mut calls = self.call_counter.lock().unwrap(); | ||
*calls += 1; | ||
} | ||
} | ||
|
||
impl CommandRunner for DelayedCommandRunner { | ||
fn run( | ||
&self, | ||
_req: ExecuteProcessRequest, | ||
_workunit_store: WorkUnitStore, | ||
) -> BoxFuture<FallibleExecuteProcessResult, String> { | ||
let delay = Delay::new(Instant::now() + self.delay); | ||
let exec_result = self.result.clone(); | ||
let command_runner = self.clone(); | ||
delay | ||
.then(move |delay_res| match delay_res { | ||
Ok(_) => { | ||
command_runner.incr_call_counter(); | ||
exec_result | ||
} | ||
Err(_) => Err(String::from("Timer failed during testing")), | ||
}) | ||
.to_boxed() | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
Result
implementsIntoFuture
, you should actually be able to just:Ok(successful_res)
and
Err(failed_res)
in this closure :) Not particularly important, but good to know about :)