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

store: Allow enforcing a timeout for SQL queries #2285

Merged
merged 1 commit into from
Mar 19, 2021
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
3 changes: 3 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ those.
- `GRAPH_GRAPHQL_MAX_OPERATIONS_PER_CONNECTION`: maximum number of GraphQL
operations per WebSocket connection. Any operation created after the limit
will return an error to the client. Default: unlimited.
- `GRAPH_SQL_STATEMENT_TIMEOUT`: the maximum number of seconds an
individual SQL query is allowed to take during GraphQL
execution. Default: unlimited

## Miscellaneous

Expand Down
37 changes: 29 additions & 8 deletions store/postgres/src/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! The pivotal struct in this module is the `Layout` which handles all the
//! information about mapping a GraphQL schema to database tables
use diesel::connection::SimpleConnection;
use diesel::{connection::SimpleConnection, Connection};
use diesel::{debug_query, OptionalExtension, PgConnection, RunQueryDsl};
use graph::prelude::{q, s};
use inflector::Inflector;
Expand Down Expand Up @@ -64,6 +64,20 @@ lazy_static! {
.map(|v| v.split(",").map(|s| s.to_owned()).collect())
.unwrap_or(HashSet::new())
};

/// `GRAPH_SQL_STATEMENT_TIMEOUT` is the timeout for queries in seconds.
/// If it is not set, no statement timeout will be enforced. The statement
/// timeout is local, i.e., can only be used within a transaction and
/// will be cleared at the end of the transaction
static ref STATEMENT_TIMEOUT: Option<String> = {
env::var("GRAPH_SQL_STATEMENT_TIMEOUT")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is important, lets document it in environment_variables.md.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is ;)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nevermind me...

.ok()
.map(|s| {
u64::from_str(&s).unwrap_or_else(|_| {
panic!("GRAPH_SQL_STATEMENT_TIMEOUT must be a number, but is `{}`", s)
})
}).map(|timeout| format!("set local statement_timeout={}", timeout * 1000))
};
}

/// A string we use as a SQL name for a table or column. The important thing
Expand Down Expand Up @@ -622,13 +636,20 @@ impl Layout {
let query_clone = query.clone();

let start = Instant::now();
let values = query.load::<EntityData>(conn).map_err(|e| {
QueryExecutionError::ResolveEntitiesError(format!(
"{}, query = {:?}",
e,
debug_query(&query_clone).to_string()
))
})?;
let values = conn
.transaction(|| {
if let Some(ref timeout_sql) = *STATEMENT_TIMEOUT {
conn.batch_execute(timeout_sql)?;
}
query.load::<EntityData>(conn)
})
.map_err(|e| {
QueryExecutionError::ResolveEntitiesError(format!(
"{}, query = {:?}",
e,
debug_query(&query_clone).to_string()
))
})?;
log_query_timing(logger, &query_clone, start.elapsed(), values.len());
values
.into_iter()
Expand Down