-
Notifications
You must be signed in to change notification settings - Fork 83
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
Improve autopilot liveness check #2236
Changes from 2 commits
044a337
4dc4e20
385e0b6
39bc5bc
791bd42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,34 +46,42 @@ use { | |
signature_validator, | ||
sources::{ | ||
balancer_v2::{ | ||
pool_fetching::BalancerContracts, | ||
BalancerFactoryKind, | ||
BalancerPoolFetcher, | ||
pool_fetching::BalancerContracts, BalancerFactoryKind, BalancerPoolFetcher, | ||
}, | ||
uniswap_v2::{pool_cache::PoolCache, UniV2BaselineSourceParameters}, | ||
uniswap_v3::pool_fetching::UniswapV3PoolFetcher, | ||
BaselineSource, | ||
PoolAggregator, | ||
BaselineSource, PoolAggregator, | ||
}, | ||
token_info::{CachedTokenInfoFetcher, TokenInfoFetcher}, | ||
token_list::{AutoUpdatingTokenList, TokenListConfiguration}, | ||
zeroex_api::DefaultZeroExApi, | ||
}, | ||
std::{collections::HashSet, sync::Arc, time::Duration}, | ||
std::{ | ||
collections::HashSet, | ||
sync::{Arc, RwLock}, | ||
time::{Duration, Instant}, | ||
}, | ||
tracing::Instrument, | ||
url::Url, | ||
}; | ||
|
||
struct Liveness { | ||
solvable_orders_cache: Arc<SolvableOrdersCache>, | ||
pub struct Liveness { | ||
max_auction_age: Duration, | ||
last_auction_time: RwLock<Instant>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl LivenessChecking for Liveness { | ||
async fn is_alive(&self) -> bool { | ||
let age = self.solvable_orders_cache.last_update_time().elapsed(); | ||
age <= self.max_auction_age | ||
let last_auction_time = self.last_auction_time.read().unwrap(); | ||
let auction_age = last_auction_time.elapsed(); | ||
auction_age <= self.max_auction_age | ||
} | ||
} | ||
|
||
impl Liveness { | ||
pub fn auction(&self) { | ||
*self.last_auction_time.write().unwrap() = Instant::now(); | ||
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. nit: 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. This is the same as |
||
} | ||
} | ||
|
||
|
@@ -583,11 +591,12 @@ pub async fn run(args: Arguments) { | |
.update(block) | ||
.await | ||
.expect("failed to perform initial solvable orders update"); | ||
let liveness = Liveness { | ||
|
||
let liveness = Arc::new(Liveness { | ||
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. nit: Liveness could have a constructor and set last_auction_time itself (given it's not really variable and not needed to be overridden in tests) |
||
max_auction_age: args.max_auction_age, | ||
solvable_orders_cache: solvable_orders_cache.clone(), | ||
}; | ||
shared::metrics::serve_metrics(Arc::new(liveness), args.metrics_address); | ||
last_auction_time: Instant::now().into(), | ||
}); | ||
shared::metrics::serve_metrics(liveness.clone(), args.metrics_address); | ||
|
||
let on_settlement_event_updater = | ||
crate::on_settlement_event_updater::OnSettlementEventUpdater { | ||
|
@@ -644,6 +653,7 @@ pub async fn run(args: Arguments) { | |
in_flight_orders: Default::default(), | ||
fee_policy: args.fee_policy, | ||
persistence: infra::persistence::Persistence::new(args.s3.into().unwrap()).await, | ||
liveness: liveness.clone(), | ||
}; | ||
run.run_forever().await; | ||
unreachable!("run loop exited"); | ||
|
@@ -690,7 +700,11 @@ async fn shadow_mode(args: Arguments) -> ! { | |
.await | ||
}; | ||
|
||
shared::metrics::serve_metrics(Arc::new(shadow::Liveness), args.metrics_address); | ||
let liveness: Arc<Liveness> = Arc::new(Liveness { | ||
KRD-Kai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
max_auction_age: args.max_auction_age, | ||
last_auction_time: Instant::now().into(), | ||
}); | ||
shared::metrics::serve_metrics(liveness.clone(), args.metrics_address); | ||
|
||
let shadow = shadow::RunLoop::new( | ||
orderbook, | ||
|
@@ -699,6 +713,7 @@ async fn shadow_mode(args: Arguments) -> ! { | |
args.score_cap, | ||
args.solve_deadline, | ||
args.fee_policy, | ||
liveness.clone(), | ||
); | ||
shadow.run_forever().await; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ use { | |
solve::{self}, | ||
}, | ||
protocol::{self, fee}, | ||
run::Liveness, | ||
run_loop::{self, observe}, | ||
}, | ||
::observe::metrics, | ||
|
@@ -26,20 +27,11 @@ use { | |
number::nonzero::U256 as NonZeroU256, | ||
primitive_types::{H160, U256}, | ||
rand::seq::SliceRandom, | ||
shared::{metrics::LivenessChecking, token_list::AutoUpdatingTokenList}, | ||
std::{cmp, time::Duration}, | ||
shared::token_list::AutoUpdatingTokenList, | ||
std::{cmp, sync::Arc, time::Duration}, | ||
tracing::Instrument, | ||
}; | ||
|
||
pub struct Liveness; | ||
#[async_trait::async_trait] | ||
impl LivenessChecking for Liveness { | ||
async fn is_alive(&self) -> bool { | ||
// can we somehow check that we keep processing auctions? | ||
true | ||
} | ||
} | ||
|
||
pub struct RunLoop { | ||
orderbook: protocol::Orderbook, | ||
drivers: Vec<Driver>, | ||
|
@@ -49,6 +41,7 @@ pub struct RunLoop { | |
score_cap: U256, | ||
solve_deadline: Duration, | ||
fee_policy: FeePolicy, | ||
liveness: Arc<Liveness>, | ||
} | ||
|
||
impl RunLoop { | ||
|
@@ -59,6 +52,7 @@ impl RunLoop { | |
score_cap: U256, | ||
solve_deadline: Duration, | ||
fee_policy: FeePolicy, | ||
liveness: Arc<Liveness>, | ||
) -> Self { | ||
Self { | ||
orderbook, | ||
|
@@ -69,6 +63,7 @@ impl RunLoop { | |
score_cap, | ||
solve_deadline, | ||
fee_policy, | ||
liveness, | ||
} | ||
} | ||
|
||
|
@@ -81,6 +76,7 @@ impl RunLoop { | |
}; | ||
observe::log_auction_delta(id, &previous, &auction); | ||
previous = Some(auction.clone()); | ||
self.liveness.auction(); | ||
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. I think this is fine at the moment but I think eventually we should consider making the distinction between ready and healthy. Additionally we might run into issues with the current code if we don't have any open order because |
||
|
||
self.single_run(id, auction) | ||
.instrument(tracing::info_span!("auction", id)) | ||
|
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.
ditto, why is it safe to unwrap