Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions .changes/added/3181.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add expensive op extension for full block queries on GraphQL
2 changes: 2 additions & 0 deletions bin/fuel-core/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,8 @@ impl Command {
required_fuel_block_height_timeout: graphql
.required_fuel_block_height_timeout
.into(),
concurrent_full_block_requests: graphql.concurrent_full_block_requests,
full_block_request_timeout: graphql.full_block_request_timeout.into(),
},
combined_db_config,
snapshot_reader,
Expand Down
6 changes: 6 additions & 0 deletions bin/fuel-core/src/cli/run/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ pub struct GraphQLArgs {

#[clap(flatten)]
pub costs: QueryCosts,

#[clap(long = "concurrent-full-block-requests", default_value = "10", env)]
pub concurrent_full_block_requests: usize,

#[clap(long = "full-block-request-timeout", default_value = "3s", env)]
pub full_block_request_timeout: humantime::Duration,
Comment on lines +112 to +113
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice if this was optional for local envs, but just setting a high limit should be fine

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into this, it makes the code a bit more complicated. Still worth considering, but not prioritizing now.

}

/// Costs for individual graphql queries.
Expand Down
3 changes: 3 additions & 0 deletions crates/fuel-core/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub struct ServiceConfig {
pub assemble_tx_estimate_predicates_limit: usize,
/// Configurable cost parameters to limit graphql queries complexity
pub costs: Costs,
/// Configurable parameters to limit number of concurrent requests to the full block API
pub concurrent_full_block_requests: usize,
pub full_block_request_timeout: Duration,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
Expand Down
10 changes: 9 additions & 1 deletion crates/fuel-core/src/graphql_api/api_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{
fuel_core_graphql_api::{
Config,
extensions::unify_response,
extensions::{
expensive_op_guard::ExpensiveOpGuardFactory,
unify_response,
},
ports::{
BlockProducerPort,
ChainStateProvider as ChainStateProviderTrait,
Expand Down Expand Up @@ -297,6 +300,11 @@ where
.extension(MetricsExtension::new(
config.config.query_log_threshold_time,
))
.extension(ExpensiveOpGuardFactory::new(
Arc::new(["FullBlockByHeightQuery".to_string(), "FullBlocksQuery".to_string()]),
config.config.concurrent_full_block_requests,
config.config.full_block_request_timeout,
))
.data(config)
.data(combined_read_database)
.data(txpool)
Expand Down
2 changes: 2 additions & 0 deletions crates/fuel-core/src/graphql_api/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub(crate) mod metrics;
pub(crate) mod required_fuel_block_height;
pub(crate) mod validation;

pub(crate) mod expensive_op_guard;

// In the case of a successful query, we return the information below on
// the `response.extensions` level.
// But in the case of the error, `async_graphql` returns information from extensions
Expand Down
112 changes: 112 additions & 0 deletions crates/fuel-core/src/graphql_api/extensions/expensive_op_guard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use async_graphql::{
Response,
ServerError,
extensions::{
Extension,
ExtensionContext,
ExtensionFactory,
NextExecute,
},
};
use std::{
sync::Arc,
time::Duration,
};
use tokio::sync::Semaphore;

pub struct ExpensiveOpGuardFactory {
expensive_op_names: Arc<[String]>,
semaphore: Arc<Semaphore>,
timeout: Duration,
}

impl ExpensiveOpGuardFactory {
pub fn new(
expensive_op_names: Arc<[String]>,
max_in_flight: usize,
timeout: Duration,
) -> Self {
Self {
expensive_op_names,
semaphore: Arc::new(Semaphore::new(max_in_flight)),
timeout,
}
}
}

impl ExtensionFactory for ExpensiveOpGuardFactory {
fn create(&self) -> Arc<dyn Extension> {
Arc::new(ExpensiveOpGuard {
expensive_op_names: self.expensive_op_names.clone(),
semaphore: self.semaphore.clone(),
timeout: self.timeout,
})
}
}

pub struct ExpensiveOpGuard {
expensive_op_names: Arc<[String]>,
semaphore: Arc<Semaphore>,
timeout: Duration,
}

#[async_trait::async_trait]
impl Extension for ExpensiveOpGuard {
async fn execute(
&self,
ctx: &ExtensionContext<'_>,
operation_name: Option<&str>,
next: NextExecute<'_>,
) -> Response {
let op = operation_name.clone().unwrap_or_default();
let is_expensive = self.expensive_op_names.iter().any(|name| op == name);

tracing::debug!(
"Executing operation: {:?}, and expected one of {:?}, expensive: {:?}, timeout: {:?}, semaphore_size: {:?}",
operation_name,
self.expensive_op_names,
is_expensive,
self.timeout,
self.semaphore.available_permits(),
);

if !is_expensive {
return next.run(ctx, operation_name).await;
}

// Concurrency gate (bulkhead)
let permit = match self.semaphore.clone().try_acquire_owned() {
Ok(p) => p,
Err(_) => {
let mut resp = Response::new(async_graphql::Value::Null);
resp.errors.push(ServerError::new(
"Rate limit exceeded for this operation",
None,
));
return resp;
}
};

// Time bound (avoid request pile-ups)
let fut = next.run(ctx, operation_name);
let starting_time = tokio::time::Instant::now();
let out = tokio::time::timeout(self.timeout, fut).await;
tracing::warn!(
"finished executing in {:?}ns, success: {:?}",
starting_time.elapsed().as_nanos(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could probably just use the Debug impl of Duration here, it gives nice units by default

out.is_ok(),
);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warn-level logging on every expensive request execution

Medium Severity

tracing::warn! is used unconditionally for every expensive operation execution, including successful ones. In production with many legitimate block/blocks queries, this generates a warning-level log entry per request. Warning level is for conditions that may need attention — routine successful completions belong at debug! or info! level. This will create significant log noise and may mask real warnings.

Fix in Cursor Fix in Web

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log message appends spurious "ns" after Duration debug format

Low Severity

The format string "finished executing in {:?}ns, success: {:?}" appends a literal ns after the Debug representation of a Duration, which already includes its own unit. This produces garbled output like "finished executing in 7msns" or "finished executing in 1.234sns". The ns suffix needs to be removed, or starting_time.elapsed().as_nanos() used instead.

Fix in Cursor Fix in Web


drop(permit);

match out {
Ok(resp) => resp,
Err(_) => {
let mut resp = Response::new(async_graphql::Value::Null);
resp.errors
.push(ServerError::new("Operation timed out", None));
resp
}
}
}
}
2 changes: 2 additions & 0 deletions crates/fuel-core/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ impl Config {
costs: Default::default(),
required_fuel_block_height_tolerance: 10,
required_fuel_block_height_timeout: Duration::from_secs(30),
concurrent_full_block_requests: 10,
full_block_request_timeout: Duration::from_secs(3),
},
combined_db_config,
continue_on_error: false,
Expand Down
Loading
Loading