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

initial stab at making the router prewarm on pqs #5340

Merged
merged 10 commits into from
Jun 11, 2024
5 changes: 5 additions & 0 deletions .changesets/feat_pq_warmup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Warm query plan cache using persisted queries on startup ([Issue #5334](https://github.com/apollographql/router/issues/5334))

This adds support for the router to use [persisted queries](https://www.apollographql.com/docs/graphos/operations/persisted-queries/) to warm the query plan cache upon startup.

By [@lleadbet](https://github.com/lleadbet) in https://github.com/apollographql/router/pull/5340
90 changes: 48 additions & 42 deletions apollo-router/src/query_planner/caching_query_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ where
&mut self,
query_analysis: &QueryAnalysisLayer,
persisted_query_layer: &PersistedQueryLayer,
previous_cache: InMemoryCachePlanner,
previous_cache: Option<InMemoryCachePlanner>,
count: Option<usize>,
experimental_reuse_query_plans: bool,
) {
Expand All @@ -172,38 +172,41 @@ where
}),
);

let mut cache_keys = {
let cache = previous_cache.lock().await;

let count = count.unwrap_or(cache.len() / 3);

cache
.iter()
.map(
|(
CachingQueryKey {
query,
operation,
hash,
metadata,
plan_options,
config_mode: _,
sdl: _,
introspection: _,
let mut cache_keys = match previous_cache {
Some(ref previous_cache) => {
let cache = previous_cache.lock().await;

let count = count.unwrap_or(cache.len() / 3);

cache
.iter()
.map(
|(
CachingQueryKey {
query,
operation,
hash,
metadata,
plan_options,
config_mode: _,
sdl: _,
introspection: _,
},
_,
)| WarmUpCachingQueryKey {
query: query.clone(),
operation: operation.clone(),
hash: Some(hash.clone()),
metadata: metadata.clone(),
plan_options: plan_options.clone(),
config_mode: self.config_mode.clone(),
introspection: self.introspection,
},
_,
)| WarmUpCachingQueryKey {
query: query.clone(),
operation: operation.clone(),
hash: Some(hash.clone()),
metadata: metadata.clone(),
plan_options: plan_options.clone(),
config_mode: self.config_mode.clone(),
introspection: self.introspection,
},
)
.take(count)
.collect::<Vec<_>>()
)
.take(count)
.collect::<Vec<_>>()
}
None => Vec::new(),
};

cache_keys.shuffle(&mut thread_rng());
Expand Down Expand Up @@ -269,19 +272,22 @@ where
};

if experimental_reuse_query_plans {
// if the query hash did not change with the schema update, we can reuse the previously cached entry
if let Some(hash) = hash {
if hash == doc.hash {
if let Some(entry) =
{ previous_cache.lock().await.get(&caching_key).cloned() }
{
self.cache.insert_in_memory(caching_key, entry).await;
reused += 1;
continue;
// check if prewarming via seeing if the previous cache exists (aka a reloaded router); if reloading, try to reuse the
if let Some(ref previous_cache) = previous_cache {
// if the query hash did not change with the schema update, we can reuse the previously cached entry
if let Some(hash) = hash {
if hash == doc.hash {
if let Some(entry) =
{ previous_cache.lock().await.get(&caching_key).cloned() }
{
self.cache.insert_in_memory(caching_key, entry).await;
reused += 1;
continue;
}
}
}
}
}
};

let entry = self
.cache
Expand Down
15 changes: 14 additions & 1 deletion apollo-router/src/router_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,20 @@ impl YamlRouterFactory {
.warm_up_query_planner(
&query_analysis_layer,
&persisted_query_layer,
previous_cache,
Some(previous_cache),
configuration.supergraph.query_planning.warmed_up_queries,
configuration
.supergraph
.query_planning
.experimental_reuse_query_plans,
)
.await;
} else {
supergraph_creator
.warm_up_query_planner(
&query_analysis_layer,
&persisted_query_layer,
None,
configuration.supergraph.query_planning.warmed_up_queries,
configuration
.supergraph
Expand Down
2 changes: 1 addition & 1 deletion apollo-router/src/services/supergraph/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ impl SupergraphCreator {
&mut self,
query_parser: &QueryAnalysisLayer,
persisted_query_layer: &PersistedQueryLayer,
previous_cache: InMemoryCachePlanner,
previous_cache: Option<InMemoryCachePlanner>,
count: Option<usize>,
experimental_reuse_query_plans: bool,
) {
Expand Down
Loading