Skip to content

Commit

Permalink
async all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed Aug 26, 2022
1 parent 7d58c2b commit 2467d95
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ services:
POSTGRES_PASSWORD: let-me-in
POSTGRES_DB: graph-node
PGDATA: "/data/postgres"
volumes:
- ./data/postgres:/var/lib/postgresql/data
# volumes:
# - ./data/postgres:/var/lib/postgresql/data
60 changes: 47 additions & 13 deletions graphql/src/execution/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,25 +344,59 @@ pub(crate) async fn execute_root_selection_set<R: Resolver>(
}
}

let run_query = {
let _permit = ctx.resolver.query_permit().await;

let mut query_res = QueryResult::from(
execute_root_selection_set_uncached(&ctx, &selection_set, &root_type).await,
);

// Unwrap: In practice should never fail, but if it does we will catch the panic.
ctx.resolver.post_process(&mut query_res).unwrap();
query_res.deployment = Some(ctx.query.schema.id().clone());
Arc::new(query_res)
let execute_ctx = ctx.cheap_clone();
let execute_selection_set = selection_set.cheap_clone();
let execute_root_type = root_type.cheap_clone();
let run_query = async move {
let _permit = execute_ctx.resolver.query_permit().await;

let logger = execute_ctx.logger.clone();
let query_text = execute_ctx.query.query_text.cheap_clone();
let variables_text = execute_ctx.query.variables_text.cheap_clone();
match graph::spawn_blocking_allow_panic(move || {
let mut query_res =
QueryResult::from(graph::block_on(execute_root_selection_set_uncached(
&execute_ctx,
&execute_selection_set,
&execute_root_type,
)));

// Unwrap: In practice should never fail, but if it does we will catch the panic.
execute_ctx.resolver.post_process(&mut query_res).unwrap();
query_res.deployment = Some(execute_ctx.query.schema.id().clone());
Arc::new(query_res)
})
.await
{
Ok(result) => result,
Err(e) => {
let e = e.into_panic();
let e = match e
.downcast_ref::<String>()
.map(String::as_str)
.or(e.downcast_ref::<&'static str>().copied())
{
Some(e) => e.to_string(),
None => "panic is not a string".to_string(),
};
error!(
logger,
"panic when processing graphql query";
"panic" => e.to_string(),
"query" => query_text,
"variables" => variables_text,
);
Arc::new(QueryResult::from(QueryExecutionError::Panic(e)))
}
}
};

let (result, herd_hit) = if let Some(key) = key {
QUERY_HERD_CACHE
.cached_query(key, async move { run_query }, &ctx.logger)
.cached_query(key, run_query, &ctx.logger)
.await
} else {
(run_query, false)
(run_query.await, false)
};
if herd_hit {
ctx.cache_status.store(CacheStatus::Shared);
Expand Down
5 changes: 2 additions & 3 deletions store/postgres/tests/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! the chain head pointer gets updated in various situations
use futures::executor;
use graph::tokio;
use std::future::Future;
use std::sync::Arc;

Expand Down Expand Up @@ -167,8 +166,8 @@ fn long_chain_with_uncles() {
check_chain_head_update(chain, Some(&*BLOCK_FOUR), None);
}

#[tokio::test]
async fn block_number() {
#[test]
fn test_get_block_number() {
let chain = vec![&*GENESIS_BLOCK, &*BLOCK_ONE, &*BLOCK_TWO];
let subgraph = DeploymentHash::new("nonExistentSubgraph").unwrap();

Expand Down
8 changes: 4 additions & 4 deletions store/postgres/tests/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1992,10 +1992,10 @@ fn cleanup_cached_blocks() {
})
}

#[tokio::test]
#[test]
/// checks if retrieving the timestamp from the data blob works.
/// on ethereum, the block has timestamp as U256 so it will always have a value
async fn parse_timestamp() {
fn parse_timestamp() {
const EXPECTED_TS: &str = "0x62ceae26";

run_test(|store, _, _| async move {
Expand Down Expand Up @@ -2027,10 +2027,10 @@ async fn parse_timestamp() {
})
}

#[tokio::test]
#[test]
/// checks if retrieving the timestamp from the data blob works.
/// on ethereum, the block has timestamp as U256 so it will always have a value
async fn parse_null_timestamp() {
fn parse_null_timestamp() {
run_test(|store, _, _| async move {
use block_store::*;
// The test subgraph is at block 2. Since we don't ever delete
Expand Down

0 comments on commit 2467d95

Please sign in to comment.