Skip to content

Commit 4e88073

Browse files
committed
feat(router): add a flag to disable the caching for query plans
1 parent 938f130 commit 4e88073

File tree

2 files changed

+62
-24
lines changed

2 files changed

+62
-24
lines changed

bin/router/src/pipeline/query_plan.rs

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::pipeline::normalize::GraphQLNormalizationPayload;
66
use crate::pipeline::progressive_override::{RequestOverrideContext, StableOverrideContext};
77
use crate::shared_state::RouterSharedState;
88
use hive_router_query_planner::planner::plan_nodes::QueryPlan;
9+
use hive_router_query_planner::planner::PlannerError;
910
use hive_router_query_planner::utils::cancellation::CancellationToken;
1011
use ntex::web::HttpRequest;
1112
use xxhash_rust::xxh3::Xxh3;
@@ -22,36 +23,36 @@ pub async fn plan_operation_with_cache(
2223
StableOverrideContext::new(&app_state.planner.supergraph, request_override_context);
2324

2425
let filtered_operation_for_plan = &normalized_operation.operation_for_plan;
25-
let plan_cache_key =
26-
calculate_cache_key(filtered_operation_for_plan.hash(), &stable_override_context);
27-
let is_pure_introspection = filtered_operation_for_plan.selection_set.is_empty()
28-
&& normalized_operation.operation_for_introspection.is_some();
2926

30-
let plan_result = app_state
31-
.plan_cache
32-
.try_get_with(plan_cache_key, async move {
33-
if is_pure_introspection {
34-
return Ok(Arc::new(QueryPlan {
35-
kind: "QueryPlan".to_string(),
36-
node: None,
37-
}));
38-
}
39-
40-
app_state
41-
.planner
42-
.plan_from_normalized_operation(
27+
if !app_state.router_config.query_planner.cache.enabled {
28+
get_plan(
29+
app_state,
30+
filtered_operation_for_plan,
31+
normalized_operation,
32+
request_override_context,
33+
cancellation_token,
34+
)
35+
.map(Arc::new)
36+
.map_err(Arc::new)
37+
} else {
38+
let plan_cache_key =
39+
calculate_cache_key(filtered_operation_for_plan.hash(), &stable_override_context);
40+
41+
app_state
42+
.plan_cache
43+
.try_get_with(plan_cache_key, async move {
44+
get_plan(
45+
app_state,
4346
filtered_operation_for_plan,
44-
(&request_override_context.clone()).into(),
47+
normalized_operation,
48+
request_override_context,
4549
cancellation_token,
4650
)
4751
.map(Arc::new)
48-
})
49-
.await;
50-
51-
match plan_result {
52-
Ok(plan) => Ok(plan),
53-
Err(e) => Err(req.new_pipeline_error(PipelineErrorVariant::PlannerError(e.clone()))),
52+
})
53+
.await
5454
}
55+
.map_err(|err| req.new_pipeline_error(PipelineErrorVariant::PlannerError(err)))
5556
}
5657

5758
#[inline]
@@ -61,3 +62,27 @@ fn calculate_cache_key(operation_hash: u64, context: &StableOverrideContext) ->
6162
context.hash(&mut hasher);
6263
hasher.finish()
6364
}
65+
66+
#[inline]
67+
fn get_plan(
68+
app_state: &Arc<RouterSharedState>,
69+
filtered_operation_for_plan: &hive_router_query_planner::ast::operation::OperationDefinition,
70+
normalized_operation: &Arc<GraphQLNormalizationPayload>,
71+
request_override_context: &RequestOverrideContext,
72+
cancellation_token: &CancellationToken,
73+
) -> Result<QueryPlan, PlannerError> {
74+
let is_pure_introspection = filtered_operation_for_plan.selection_set.is_empty()
75+
&& normalized_operation.operation_for_introspection.is_some();
76+
if is_pure_introspection {
77+
return Ok(QueryPlan {
78+
kind: "QueryPlan".to_string(),
79+
node: None,
80+
});
81+
}
82+
83+
app_state.planner.plan_from_normalized_operation(
84+
filtered_operation_for_plan,
85+
(&request_override_context.clone()).into(),
86+
cancellation_token,
87+
)
88+
}

lib/router-config/src/query_planner.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ pub struct QueryPlannerConfig {
2121
)]
2222
#[schemars(with = "String")]
2323
pub timeout: Duration,
24+
pub cache: QueryPlannerCacheConfig,
2425
}
2526

2627
impl Default for QueryPlannerConfig {
2728
fn default() -> Self {
2829
Self {
2930
allow_expose: default_query_planning_allow_expose(),
3031
timeout: default_query_planning_timeout(),
32+
cache: QueryPlannerCacheConfig::default(),
3133
}
3234
}
3335
}
@@ -39,3 +41,14 @@ fn default_query_planning_allow_expose() -> bool {
3941
fn default_query_planning_timeout() -> Duration {
4042
Duration::from_secs(10)
4143
}
44+
45+
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
46+
pub struct QueryPlannerCacheConfig {
47+
pub enabled: bool,
48+
}
49+
50+
impl Default for QueryPlannerCacheConfig {
51+
fn default() -> Self {
52+
Self { enabled: true }
53+
}
54+
}

0 commit comments

Comments
 (0)