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

fix: budget controller #431

Merged
merged 1 commit into from
Nov 27, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions graph-gateway/src/budgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub struct Feedback {
impl Budgeter {
pub fn new(query_fees_target: USD) -> Self {
let (feedback_tx, feedback_rx) = mpsc::unbounded_channel();
let (budget_limit_tx, budget_limit_rx) = Eventual::new();
let (mut budget_limit_tx, budget_limit_rx) = Eventual::new();
budget_limit_tx.write(query_fees_target);
Actor::create(feedback_rx, budget_limit_tx, query_fees_target);
let absolute_budget_limit = USD(query_fees_target.0 * UDecimal18::from(10));
Self {
Expand Down Expand Up @@ -96,11 +97,13 @@ struct Controller {

impl Controller {
fn new(target_query_fees: USD) -> Self {
let mut error_history = FastDecayBuffer::default();
*error_history.current_mut() = target_query_fees.0.into();
Self {
target_query_fees,
recent_fees: USD(UDecimal18::from(0)),
recent_query_count: 0,
error_history: FastDecayBuffer::default(),
error_history,
}
}

Expand All @@ -123,8 +126,9 @@ impl Controller {
*self.error_history.current_mut() = error;

let i: f64 = self.error_history.frames().iter().sum();
let k_i = 3e4;
UDecimal18::from(1) + UDecimal18::try_from(i * k_i).unwrap_or_default()
let k_i = 1.2;
let correction = UDecimal18::try_from(i * k_i).unwrap_or_default();
self.target_query_fees.0 + correction
}
}

Expand All @@ -143,11 +147,9 @@ mod tests {
) {
let setpoint: f64 = controller.target_query_fees.0.into();
let mut process_variable = 0.0;
for i in 0..30 {
for i in 0..20 {
let control_variable: f64 = controller.control_variable().into();
process_variable = f64::from(controller.target_query_fees.0)
* process_variable_multiplier
* control_variable;
process_variable = control_variable * process_variable_multiplier;
println!(
"{i:02} SP={setpoint:.6}, PV={:.8}, CV={:.8}",
process_variable, control_variable,
Expand All @@ -157,7 +159,7 @@ mod tests {
assert_within(process_variable, setpoint, tolerance);
}

for setpoint in [20e-6, 40e-6] {
for setpoint in [10e-6, 20e-6, 50e-6] {
let setpoint = USD(UDecimal18::try_from(setpoint).unwrap());
let mut controller = Controller::new(setpoint);
test_controller(&mut controller, 0.2, 1e-6);
Expand Down
20 changes: 8 additions & 12 deletions graph-gateway/src/client_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ async fn handle_client_query_inner(
utility_params.latest_block = latest_block.number;

let selection_timer = METRICS.indexer_selection_duration.start_timer();
let (selections, indexer_errors) = ctx
let (mut selections, indexer_errors) = ctx
.isa_state
.latest()
.select_indexers(&mut rng, &utility_params, &candidates)
Expand All @@ -583,23 +583,19 @@ async fn handle_client_query_inner(
.filter(|(_, l)| *l > 0)
.collect::<BTreeMap<&IndexerSelectionError, usize>>();

// Double the budget & retry if there is any indexer requesting a higher fee.
if !last_retry && isa_errors.contains_key(&IndexerSelectionError::FeeTooHigh) {
utility_params.budget = GRT(utility_params.budget.0 * UDecimal18::from(2));
tracing::info!(
target: reports::CLIENT_QUERY_TARGET,
budget_grt = f64::from(budget.0) as f32,
"increase_budget"
);
continue;
}

return Err(Error::NoSuitableIndexer(anyhow!(
"Indexer selection errors: {:?}",
isa_errors
)));
}

for selection in &mut selections {
// TODO: In a future where indexers are expected put more effort into setting cost
// models, we should pay selected indexers the maximum fee of the alternatives
// (where `fee <= budget`).
let min_fee = budget.0 * UDecimal18::try_from(0.75).unwrap();
selection.fee = GRT(selection.fee.0.max(min_fee));
}
total_indexer_fees = GRT(total_indexer_fees.0 + selections.iter().map(|s| s.fee.0).sum());
tracing::info!(
target: reports::CLIENT_QUERY_TARGET,
Expand Down
Loading