-
Notifications
You must be signed in to change notification settings - Fork 15
refactor: improve exclusive liquidity request latency #430
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
Open
louise-poole
wants to merge
2
commits into
main
Choose a base branch
from
fix/exclusive-liquidity-request-latency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−49
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -209,29 +209,61 @@ pub(crate) struct OrderResponses { | |
| } | ||
|
|
||
| impl OrderResponses { | ||
| /// Returns a copy keeping only candidates from public-scoped worker pools. | ||
| /// Borrows every candidate. | ||
| fn view(&self) -> ResponsesView<'_> { | ||
| self.view_filtered(|_| true) | ||
| } | ||
|
|
||
| /// Borrows only the candidates from public-scoped worker pools. | ||
| /// | ||
| /// These form the committed reference and the ranked fallback chain (ranked by `rank_quotes`, | ||
| /// consumed by the price guard); exclusive-access candidates are overlaid separately by | ||
| /// `combine_with_surplus`. `failed_solvers` is retained so placeholder construction is | ||
| /// unchanged. | ||
| fn public_only(&self, pool_scopes: &HashMap<String, LiquidityScope>) -> OrderResponses { | ||
| let quotes = self | ||
| .quotes | ||
| .iter() | ||
| .filter(|wq| { | ||
| pool_scopes.get(&wq.worker_pool) != Some(&LiquidityScope::IncludeExclusive) | ||
| }) | ||
| .cloned() | ||
| .collect(); | ||
| OrderResponses { | ||
| order_id: self.order_id.clone(), | ||
| quotes, | ||
| failed_solvers: self.failed_solvers.clone(), | ||
| fn public_view(&self, pool_scopes: &HashMap<String, LiquidityScope>) -> ResponsesView<'_> { | ||
| self.view_filtered(|pool| pool_scopes.get(pool) != Some(&LiquidityScope::IncludeExclusive)) | ||
| } | ||
|
|
||
| fn view_filtered(&self, keep: impl Fn(&str) -> bool) -> ResponsesView<'_> { | ||
| ResponsesView { | ||
| order_id: &self.order_id, | ||
| quotes: self | ||
| .quotes | ||
| .iter() | ||
| .filter(|worker_pool_quote| keep(&worker_pool_quote.worker_pool)) | ||
| .collect(), | ||
| failed_solvers: &self.failed_solvers, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A borrowed, optionally scope-narrowed view over one order's responses. | ||
| /// | ||
| /// Ranking reads candidates far more often than it keeps them, and every `OrderQuote` owns a | ||
| /// `Route` whose legs each hold a `Box<dyn ProtocolSim>`. Narrowing by borrow rather than by | ||
| /// clone keeps the single deep copy that `rank_quotes` makes of its winners the only one on the | ||
| /// request path. | ||
| struct ResponsesView<'a> { | ||
| order_id: &'a str, | ||
| quotes: Vec<&'a WorkerPoolQuote>, | ||
| failed_solvers: &'a [(String, SolveError)], | ||
| } | ||
|
|
||
| /// Records how long a worker pool took to answer, as observed by the router. | ||
| /// | ||
| /// Unlike `worker_pool_solve_duration_seconds`, which a worker reports for its own successful | ||
| /// solves, this covers everything the request actually waits for — queue wait, readiness wait, | ||
| /// and the solve — and is recorded for failures too (`outcome`). The gap between the two is | ||
| /// what a request pays for a worker pool beyond its algorithm's own working time. | ||
|
Contributor
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. Would it make sense to add that the pools are worker pools? |
||
| fn record_pool_response(worker_pool_name: &str, outcome: &'static str, elapsed: Duration) { | ||
| histogram!( | ||
| "worker_router_pool_response_seconds", | ||
| "pool" => worker_pool_name.to_string(), | ||
| "outcome" => outcome, | ||
| ) | ||
| .record(elapsed.as_secs_f64()); | ||
| } | ||
|
|
||
| /// Orchestrates multiple solver pools to find the best quote. | ||
| pub struct WorkerPoolRouter { | ||
| /// All registered solver pools. | ||
|
|
@@ -363,7 +395,7 @@ impl WorkerPoolRouter { | |
| .map(|(responses, allocation)| { | ||
| if allocation.exclusive_routing_active() { | ||
| let public_ranked = self.rank_quotes( | ||
| &responses.public_only(allocation.scopes()), | ||
| &responses.public_view(allocation.scopes()), | ||
| request.options(), | ||
| ); | ||
| combine_with_surplus( | ||
|
|
@@ -374,7 +406,7 @@ impl WorkerPoolRouter { | |
| *USER_IMPROVEMENT_SHARE_BPS, | ||
| ) | ||
| } else { | ||
| self.rank_quotes(responses, request.options()) | ||
| self.rank_quotes(&responses.view(), request.options()) | ||
| } | ||
| }) | ||
| .collect(); | ||
|
|
@@ -506,8 +538,7 @@ impl WorkerPoolRouter { | |
| // Timeout reached | ||
| _ = tokio::time::sleep_until(deadline_instant) => { | ||
| // Mark all remaining worker pools as timed out | ||
| let elapsed_ms = deadline.saturating_duration_since(Instant::now()) | ||
| .as_millis() as u64; | ||
| let elapsed_ms = start_time.elapsed().as_millis() as u64; | ||
|
Contributor
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. Nice fix! |
||
| for worker_pool_name in remaining_worker_pools.drain() { | ||
| failed_solvers.push(( | ||
| worker_pool_name, | ||
|
|
@@ -523,6 +554,11 @@ impl WorkerPoolRouter { | |
| Some((worker_pool_name, Ok(single_quote))) => { | ||
| // Remove from remaining | ||
| remaining_worker_pools.remove(&worker_pool_name); | ||
| record_pool_response( | ||
| &worker_pool_name, | ||
| "ok", | ||
| start_time.elapsed(), | ||
| ); | ||
|
|
||
| if allocation.is_exclusive(&worker_pool_name) { | ||
| has_exclusive_access_response = true; | ||
|
|
@@ -562,6 +598,11 @@ impl WorkerPoolRouter { | |
| } | ||
| Some((worker_pool_name, Err(e))) => { | ||
| remaining_worker_pools.remove(&worker_pool_name); | ||
| record_pool_response( | ||
| &worker_pool_name, | ||
| "error", | ||
| start_time.elapsed(), | ||
| ); | ||
| // A failed exclusive-access worker pool still counts as "responded" | ||
| // for gating — we know it won't produce a surplus quote, so the | ||
| // public worker pools can early-return without waiting for a result | ||
|
|
@@ -619,7 +660,11 @@ impl WorkerPoolRouter { | |
| /// If no valid quotes exist, returns a single-element vec with a placeholder | ||
| /// (`NoRouteFound` or `Timeout`) so that downstream always has at least one | ||
| /// candidate per order. | ||
| fn rank_quotes(&self, responses: &OrderResponses, options: &QuoteOptions) -> Vec<OrderQuote> { | ||
| fn rank_quotes( | ||
| &self, | ||
| responses: &ResponsesView<'_>, | ||
| options: &QuoteOptions, | ||
| ) -> Vec<OrderQuote> { | ||
| let mut valid_quotes: Vec<_> = responses | ||
| .quotes | ||
| .iter() | ||
|
|
@@ -645,7 +690,7 @@ impl WorkerPoolRouter { | |
| ); | ||
| return valid_quotes | ||
| .into_iter() | ||
| .map(|pq| pq.quote.clone()) | ||
| .map(|worker_pool_quote| worker_pool_quote.quote.clone()) | ||
| .collect(); | ||
| } | ||
|
|
||
|
|
@@ -655,7 +700,7 @@ impl WorkerPoolRouter { | |
| { | ||
| counter!("worker_router_orders_total", "status" => "no_route").increment(1); | ||
| let mut fallback = OrderQuote::new( | ||
| responses.order_id.clone(), | ||
| responses.order_id.to_string(), | ||
| QuoteStatus::NoRouteFound, | ||
| any_q.amount_in().clone(), | ||
| BigUint::ZERO, | ||
|
|
@@ -716,7 +761,7 @@ impl WorkerPoolRouter { | |
| .cloned() | ||
| .unwrap_or_else(|| "0".to_string()); | ||
| let mut fallback = OrderQuote::new( | ||
| responses.order_id.clone(), | ||
| responses.order_id.to_string(), | ||
| status, | ||
| BigUint::ZERO, | ||
| BigUint::ZERO, | ||
|
|
@@ -728,7 +773,7 @@ impl WorkerPoolRouter { | |
| Bytes::default(), | ||
| label, | ||
| ); | ||
| fallback.set_no_route_cause(aggregate_no_route_cause(&responses.failed_solvers)); | ||
| fallback.set_no_route_cause(aggregate_no_route_cause(responses.failed_solvers)); | ||
| fallback | ||
| }; | ||
| vec![fallback] | ||
|
|
@@ -1221,10 +1266,10 @@ mod tests { | |
| ) | ||
| } | ||
|
|
||
| /// `public_only` must carry the order identity across, or the surplus path logs and ranks | ||
| /// `public_view` must carry the order identity across, or the surplus path logs and ranks | ||
| /// against a response set that has lost it. | ||
| #[test] | ||
| fn test_public_only_keeps_order_id_and_failures() { | ||
| fn test_public_view_keeps_order_id_and_failures() { | ||
| let responses = OrderResponses { | ||
| order_id: "o1".to_string(), | ||
| quotes: vec![ | ||
|
|
@@ -1237,7 +1282,7 @@ mod tests { | |
| ("public".to_string(), LiquidityScope::PublicOnly), | ||
| ("excl".to_string(), LiquidityScope::IncludeExclusive), | ||
| ]); | ||
| let public = responses.public_only(&scopes); | ||
| let public = responses.public_view(&scopes); | ||
|
|
||
| assert_eq!(public.order_id, "o1"); | ||
| assert_eq!(public.quotes.len(), 1); | ||
|
|
@@ -1660,7 +1705,7 @@ mod tests { | |
|
|
||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &options); | ||
| let result = worker_router.rank_quotes(&responses.view(), &options); | ||
|
|
||
| if should_pass { | ||
| assert_eq!(result[0].status(), QuoteStatus::Success); | ||
|
|
@@ -1706,7 +1751,7 @@ mod tests { | |
|
|
||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &QuoteOptions::default()); | ||
| let result = worker_router.rank_quotes(&responses.view(), &QuoteOptions::default()); | ||
|
|
||
| assert_eq!(result.len(), 1); | ||
| assert_eq!(result[0].status(), QuoteStatus::Timeout); | ||
|
|
@@ -1725,7 +1770,7 @@ mod tests { | |
|
|
||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &QuoteOptions::default()); | ||
| let result = worker_router.rank_quotes(&responses.view(), &QuoteOptions::default()); | ||
|
|
||
| assert_eq!(result.len(), 1); | ||
| assert_eq!(result[0].status(), QuoteStatus::NoRouteFound); | ||
|
|
@@ -1738,7 +1783,7 @@ mod tests { | |
|
|
||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &QuoteOptions::default()); | ||
| let result = worker_router.rank_quotes(&responses.view(), &QuoteOptions::default()); | ||
|
|
||
| assert_eq!(result.len(), 1); | ||
| assert_eq!(result[0].status(), QuoteStatus::NoRouteFound); | ||
|
|
@@ -1766,7 +1811,7 @@ mod tests { | |
| }; | ||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &QuoteOptions::default()); | ||
| let result = worker_router.rank_quotes(&responses.view(), &QuoteOptions::default()); | ||
| assert_eq!(result.len(), 1); | ||
| assert_eq!(result[0].status(), QuoteStatus::NoRouteFound); | ||
| // Token-not-in-graph wins over no_graph_path regardless of pool order. | ||
|
|
@@ -1786,7 +1831,7 @@ mod tests { | |
| }; | ||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &QuoteOptions::default()); | ||
| let result = worker_router.rank_quotes(&responses.view(), &QuoteOptions::default()); | ||
| assert_eq!(result[0].no_route_reason(), Some(NoPathReason::NoGraphPath)); | ||
| } | ||
|
|
||
|
|
@@ -1880,7 +1925,7 @@ mod tests { | |
| let options = QuoteOptions::default().with_max_gas(BigUint::from(1u64)); | ||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &options); | ||
| let result = worker_router.rank_quotes(&responses.view(), &options); | ||
| assert_eq!(result[0].status(), QuoteStatus::NoRouteFound); | ||
| assert!(matches!(result[0].no_route_cause(), Some(SolveError::MaxGasExceeded))); | ||
| } | ||
|
|
@@ -1910,7 +1955,7 @@ mod tests { | |
| let options = QuoteOptions::default().with_max_gas(BigUint::from(1_000u64)); | ||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &options); | ||
| let result = worker_router.rank_quotes(&responses.view(), &options); | ||
| assert_eq!(result[0].status(), QuoteStatus::NoRouteFound); | ||
| assert!( | ||
| result[0].no_route_cause().is_none(), | ||
|
|
@@ -1928,7 +1973,7 @@ mod tests { | |
| }; | ||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &QuoteOptions::default()); | ||
| let result = worker_router.rank_quotes(&responses.view(), &QuoteOptions::default()); | ||
| assert_eq!(result[0].status(), QuoteStatus::Timeout); | ||
| assert!(matches!(result[0].no_route_cause(), Some(SolveError::Timeout { .. }))); | ||
| } | ||
|
|
@@ -1976,7 +2021,7 @@ mod tests { | |
|
|
||
| let worker_router = | ||
| WorkerPoolRouter::new(vec![], WorkerPoolRouterConfig::default(), default_encoder()); | ||
| let result = worker_router.rank_quotes(&responses, &QuoteOptions::default()); | ||
| let result = worker_router.rank_quotes(&responses.view(), &QuoteOptions::default()); | ||
|
|
||
| assert_eq!(result.len(), 2); | ||
| assert_eq!(*result[0].amount_out_net_gas(), BigUint::from(950u64)); | ||
|
|
||
Oops, something went wrong.
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.
why not to use
solver_error_labelfor the error matching?Is it ok, that we wrap some errors into simple "error" string?