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

Reconcile dev after merge to main for v1.45.1 #5037

Merged
merged 10 commits into from
Apr 29, 2024
49 changes: 22 additions & 27 deletions apollo-router/src/query_planner/bridge_query_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,25 +621,21 @@ impl BridgeQueryPlanner {
) -> Result<QueryPlannerContent, QueryPlannerError> {
let mut plan_success = self
.planner
.plan(filtered_query.clone(), operation.clone(), plan_options)
.await
.map_err(QueryPlannerError::RouterBridgeError)?
.into_result()
{
Ok(mut plan) => {
plan.data
.query_plan
.hash_subqueries(&self.subgraph_schemas, &self.schema.raw_sdl);
plan.data
.query_plan
.extract_authorization_metadata(&self.schema.definitions, &key);
plan
}
Err(err) => {
let plan_errors: PlanErrors = err.into();
return Err(QueryPlannerError::from(plan_errors));
}
};
.plan(
&self.schema,
filtered_query.clone(),
operation.clone(),
plan_options,
)
.await?;
plan_success
.data
.query_plan
.hash_subqueries(&self.subgraph_schemas, &self.schema.raw_sdl);
plan_success
.data
.query_plan
.extract_authorization_metadata(&self.subgraph_schemas, &key);

// the `statsReportKey` field should match the original query instead of the filtered query, to index them all under the same query
let operation_signature = if matches!(
Expand Down Expand Up @@ -835,21 +831,20 @@ impl Service<QueryPlannerRequest> for BridgeQueryPlanner {
};

let api_schema = this.schema.api_schema();
let api_schema_definitions = &api_schema.definitions;
match add_defer_labels(api_schema_definitions, &doc.ast) {
match add_defer_labels(api_schema, &doc.ast) {
Err(e) => {
return Err(QueryPlannerError::SpecError(SpecError::TransformError(
e.to_string(),
)))
}
Ok(modified_query) => {
let executable_document = modified_query
.to_executable_validate(api_schema_definitions)
.to_executable_validate(api_schema)
// Assume transformation creates a valid document: ignore conversion errors
.map_err(|e| SpecError::ValidationError(e.into()))?;
let hash = QueryHashVisitor::hash_query(
api_schema_definitions,
&api_schema.raw_sdl,
this.schema.supergraph_schema(),
&this.schema.raw_sdl,
Comment on lines +846 to +847
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: here's an additional fix to the query hash I just found: the hashing mechanism has to be done on the supergraph schema, not the API schema. If needed I can split that change in a separate PR, that we would merge immediately after

Copy link
Member

Choose a reason for hiding this comment

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

I just want to clarify that while this is an additional fix necessary for the re-introduction of #5028, it does not constitute a necessary fix right now given that #5028 was exhaustive enough in and of itself.

&executable_document,
operation_name.as_deref(),
)
Expand Down Expand Up @@ -968,7 +963,7 @@ impl BridgeQueryPlanner {
.to_executable_validate(self.schema.api_schema())
.map_err(|e| SpecError::ValidationError(e.into()))?;
let hash = QueryHashVisitor::hash_query(
&self.schema.definitions,
self.schema.supergraph_schema(),
&self.schema.raw_sdl,
&executable_document,
key.operation_name.as_deref(),
Expand Down Expand Up @@ -1064,11 +1059,11 @@ pub(super) struct QueryPlan {
impl QueryPlan {
fn hash_subqueries(
&mut self,
schemas: &HashMap<String, Arc<Valid<apollo_compiler::Schema>>>,
subgraph_schemas: &SubgraphSchemas,
supergraph_schema_hash: &str,
) {
if let Some(node) = self.node.as_mut() {
node.hash_subqueries(schemas, supergraph_schema_hash);
node.hash_subqueries(subgraph_schemas, supergraph_schema_hash);
}
}

Expand Down
8 changes: 4 additions & 4 deletions apollo-router/src/query_planner/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,16 +609,16 @@ impl FetchNode {

pub(crate) fn hash_subquery(
&mut self,
schema: &Valid<apollo_compiler::Schema>,
subgraph_schemas: &SubgraphSchemas,
supergraph_schema_hash: &str,
) {
let doc = ExecutableDocument::parse(schema, &self.operation, "query.graphql")
.expect("subgraph queries should be valid");
let doc = self.parsed_operation(subgraph_schemas);
let schema = &subgraph_schemas[self.service_name.as_str()];

if let Ok(hash) = QueryHashVisitor::hash_query(
schema,
supergraph_schema_hash,
&doc,
doc,
self.operation_name.as_deref(),
) {
self.schema_aware_hash = Arc::new(QueryHash(hash));
Expand Down
22 changes: 10 additions & 12 deletions apollo-router/src/query_planner/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,44 +314,42 @@ impl PlanNode {

pub(crate) fn hash_subqueries(
&mut self,
schemas: &HashMap<String, Arc<Valid<apollo_compiler::Schema>>>,
subgraph_schemas: &SubgraphSchemas,
supergraph_schema_hash: &str,
) {
match self {
PlanNode::Fetch(fetch_node) => {
if let Some(schema) = schemas.get(&fetch_node.service_name) {
fetch_node.hash_subquery(schema, supergraph_schema_hash);
}
fetch_node.hash_subquery(subgraph_schemas, supergraph_schema_hash);
}

PlanNode::Sequence { nodes } => {
for node in nodes {
node.hash_subqueries(schemas, supergraph_schema_hash);
node.hash_subqueries(subgraph_schemas, supergraph_schema_hash);
}
}
PlanNode::Parallel { nodes } => {
for node in nodes {
node.hash_subqueries(schemas, supergraph_schema_hash);
node.hash_subqueries(subgraph_schemas, supergraph_schema_hash);
}
}
PlanNode::Flatten(flatten) => flatten
.node
.hash_subqueries(schemas, supergraph_schema_hash),
.hash_subqueries(subgraph_schemas, supergraph_schema_hash),
PlanNode::Defer { primary, deferred } => {
if let Some(node) = primary.node.as_mut() {
node.hash_subqueries(schemas, supergraph_schema_hash);
node.hash_subqueries(subgraph_schemas, supergraph_schema_hash);
}
for deferred_node in deferred {
if let Some(node) = deferred_node.node.take() {
let mut new_node = (*node).clone();
new_node.hash_subqueries(schemas, supergraph_schema_hash);
new_node.hash_subqueries(subgraph_schemas, supergraph_schema_hash);
deferred_node.node = Some(Arc::new(new_node));
}
}
}
PlanNode::Subscription { primary: _, rest } => {
if let Some(node) = rest.as_mut() {
node.hash_subqueries(schemas, supergraph_schema_hash);
node.hash_subqueries(subgraph_schemas, supergraph_schema_hash);
}
}
PlanNode::Condition {
Expand All @@ -360,10 +358,10 @@ impl PlanNode {
else_clause,
} => {
if let Some(node) = if_clause.as_mut() {
node.hash_subqueries(schemas, supergraph_schema_hash);
node.hash_subqueries(subgraph_schemas, supergraph_schema_hash);
}
if let Some(node) = else_clause.as_mut() {
node.hash_subqueries(schemas, supergraph_schema_hash);
node.hash_subqueries(subgraph_schemas, supergraph_schema_hash);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions apollo-router/src/spec/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ impl Query {
return Err(SpecError::ParseError(errors.into()));
}
};

let api_schema = schema.api_schema();
let api_schema_definitions = &api_schema.definitions;
let executable_document = match ast.to_executable_validate(api_schema_definitions) {
let executable_document = match ast.to_executable_validate(api_schema) {
Ok(doc) => doc,
Err(errors) => {
return Err(SpecError::ValidationError(errors.into()));
Expand All @@ -298,8 +298,8 @@ impl Query {
tracing::trace!(?recursion_limit, "recursion limit data");

let hash = QueryHashVisitor::hash_query(
api_schema_definitions,
&api_schema.raw_sdl,
schema.supergraph_schema(),
&schema.raw_sdl,
Comment on lines +301 to +302
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same as the other note, this can be moved to a separate PR

&executable_document,
operation_name,
)
Expand Down Expand Up @@ -354,7 +354,8 @@ impl Query {
.map(|operation| Operation::from_hir(operation, schema, &mut defer_stats, &fragments))
.collect::<Result<Vec<_>, SpecError>>()?;

let mut visitor = QueryHashVisitor::new(&schema.definitions, &schema.raw_sdl, document);
let mut visitor =
QueryHashVisitor::new(schema.supergraph_schema(), &schema.raw_sdl, document);
traverse::document(&mut visitor, document, operation_name).map_err(|e| {
SpecError::QueryHashing(format!("could not calculate the query hash: {e}"))
})?;
Expand Down
2 changes: 1 addition & 1 deletion apollo-router/tests/integration/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod test {
// 2. run `docker compose up -d` and connect to the redis container by running `docker-compose exec redis /bin/bash`.
// 3. Run the `redis-cli` command from the shell and start the redis `monitor` command.
// 4. Run this test and yank the updated cache key from the redis logs.
let known_cache_key = "plan:v2.7.2:121b9859eba2d8fa6dde0a54b6e3781274cf69f7ffb0af912e92c01c6bfff6ca:3973e022e93220f9212c18d0d0c543ae7c309e46640da93a4a0314de999f5112:5c7a72fa35639949328548d77b56dba2e77d0dfa90c19b69978da119e996bb92";
let known_cache_key = "plan:v2.7.4:121b9859eba2d8fa6dde0a54b6e3781274cf69f7ffb0af912e92c01c6bfff6ca:3973e022e93220f9212c18d0d0c543ae7c309e46640da93a4a0314de999f5112:5c7a72fa35639949328548d77b56dba2e77d0dfa90c19b69978da119e996bb92";

let config = RedisConfig::from_url("redis://127.0.0.1:6379").unwrap();
let client = RedisClient::new(config, None, None, None);
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.