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

improve generate fragment compression #6651

Merged
merged 20 commits into from
Feb 3, 2025
Merged

improve generate fragment compression #6651

merged 20 commits into from
Feb 3, 2025

Conversation

dariuszkuc
Copy link
Member

@dariuszkuc dariuszkuc commented Jan 27, 2025

Update generate_fragment algorithm to extract named fragments from common sub selections in the given operation. Fragments are now auto generated for each sub selection that occurs at least twice within the final fetch operation.

Algorithm consists of two phases

  • recursively iterate over the selection sets and collect sub selection usages
  • recursively iterate over the selection sets to find which sub selections were used more than once
    • use this sub selection to create new fragment definition and replace current selection set with just a fragment spread

Based on preliminary testing over 2.8 million ops this new algorithm

  • generates better results in 95% of test operations (vs 77% of old one)
  • p99 overhead of running this new compression algorithm results in just 10ms overhead
  • in case of better results, at p99 it shrinks the queries by 50Kb
  • in case it generates worse results, at p99 it only adds an additional 108 bytes to a query

Checklist

Complete the checklist (and note appropriate exceptions) before the PR is marked ready-for-review.

  • Changes are compatible1
  • Documentation2 completed
  • Performance impact assessed and acceptable
  • Tests added and passing3
    • Unit Tests
    • Integration Tests
    • Manual Tests

Exceptions

Note any exceptions here

Notes

Footnotes

  1. It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this.

  2. Configuration is an important part of many changes. Where applicable please try to document configuration examples.

  3. Tick whichever testing boxes are applicable. If you are adding Manual Tests, please document the manual testing (extensively) in the Exceptions.

@dariuszkuc dariuszkuc changed the title improve generate fragment compression [DRAFT] improve generate fragment compression Jan 27, 2025
Copy link
Contributor

@dariuszkuc, please consider creating a changeset entry in /.changesets/. These instructions describe the process and tooling.

@svc-apollo-docs
Copy link
Collaborator

svc-apollo-docs commented Jan 27, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: aabda6874c274ec47673326a

Copy link

@briannafugate408 briannafugate408 left a comment

Choose a reason for hiding this comment

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

Just had one question out of curiosity 🗒️

apollo-router/src/configuration/mod.rs Outdated Show resolved Hide resolved
apollo-federation/src/operation/optimize.rs Outdated Show resolved Hide resolved
apollo-federation/src/operation/optimize.rs Outdated Show resolved Hide resolved
apollo-federation/src/operation/optimize.rs Outdated Show resolved Hide resolved
apollo-federation/src/operation/selection_map.rs Outdated Show resolved Hide resolved
apollo-federation/src/query_plan/query_planner.rs Outdated Show resolved Hide resolved
apollo-federation/src/operation/optimize.rs Outdated Show resolved Hide resolved
@dariuszkuc dariuszkuc force-pushed the gen_fragment_v2 branch 2 times, most recently from 208f22d to ceac413 Compare January 28, 2025 21:49
@dariuszkuc dariuszkuc changed the title [DRAFT] improve generate fragment compression improve generate fragment compression Jan 29, 2025
@dariuszkuc dariuszkuc marked this pull request as ready for review January 29, 2025 06:47
Copy link
Contributor

@TylerBloom TylerBloom left a comment

Choose a reason for hiding this comment

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

We're definitely moving in the first direction. The hash implementation will work. I don't think we should try to make too many changes that try to improve performance without actual numbers, so we have here will work a baseline I think. Making the Hash impl of Selection and SelectionSet a regular method I still think is a good idea just so we don't violate the hash contract sometime in the future.

My only remaining concern is the hash collision of the FragmentGenerator's maps. Of course the chance of the collision is near zero. If you think will be ok, we can leave it. It is just sticking out to me.

apollo-federation/src/operation/mod.rs Show resolved Hide resolved
apollo-federation/src/operation/mod.rs Show resolved Hide resolved
@dariuszkuc
Copy link
Member Author

My only remaining concern is the hash collision of the FragmentGenerator's maps. Of course the chance of the collision is near zero. If you think will be ok, we can leave it. It is just sticking out to me.

In v1 we were comparing the selections of inline fragments so technically there was chance of collision there as well (I guess it was a smaller chance as we were limiting it to the specific type condition selections). Any suggestions in how to improve our hashing to mitigate this risk?

@TylerBloom
Copy link
Contributor

Any suggestions in how to improve our hashing to mitigate this risk?

My thoughts would be to make NamedFragmentCandidateKey the key to the maps. To avoid hashing multiple times, you can cache the hash and just derive PartialEq. That said, it sounds like a non-issue.

@dariuszkuc dariuszkuc requested review from a team as code owners January 30, 2025 00:04
@dariuszkuc dariuszkuc requested a review from a team as a code owner January 30, 2025 02:59
Copy link
Member

@goto-bus-stop goto-bus-stop left a comment

Choose a reason for hiding this comment

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

This looks really promising! I left a few comments that are probably not critically important. The perf tradeoffs you've made seem reasonable.

I do think that we should avoid the hash collision risk. It's unlikely, yes, but collision avoidance isn't the top priority of hashers for hashmaps, so it's a lot more likely we get a collision on accident than with a sha256 or whatever.

apollo-federation/src/operation/optimize.rs Outdated Show resolved Hide resolved
@router-perf
Copy link

router-perf bot commented Jan 31, 2025

CI performance tests

  • connectors-const - Connectors stress test that runs with a constant number of users
  • const - Basic stress test that runs with a constant number of users
  • demand-control-instrumented - A copy of the step test, but with demand control monitoring and metrics enabled
  • demand-control-uninstrumented - A copy of the step test, but with demand control monitoring enabled
  • enhanced-signature - Enhanced signature enabled
  • events - Stress test for events with a lot of users and deduplication ENABLED
  • events_big_cap_high_rate - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity
  • events_big_cap_high_rate_callback - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity using callback mode
  • events_callback - Stress test for events with a lot of users and deduplication ENABLED in callback mode
  • events_without_dedup - Stress test for events with a lot of users and deduplication DISABLED
  • events_without_dedup_callback - Stress test for events with a lot of users and deduplication DISABLED using callback mode
  • extended-reference-mode - Extended reference mode enabled
  • large-request - Stress test with a 1 MB request payload
  • no-tracing - Basic stress test, no tracing
  • reload - Reload test over a long period of time at a constant rate of users
  • step-jemalloc-tuning - Clone of the basic stress test for jemalloc tuning
  • step-local-metrics - Field stats that are generated from the router rather than FTV1
  • step-with-prometheus - A copy of the step test with the Prometheus metrics exporter enabled
  • step - Basic stress test that steps up the number of users over time
  • xlarge-request - Stress test with 10 MB request payload
  • xxlarge-request - Stress test with 100 MB request payload

@goto-bus-stop goto-bus-stop self-requested a review February 3, 2025 09:01
@dariuszkuc dariuszkuc added the backport-1.x Backport this PR to 1.x label Feb 3, 2025
@dariuszkuc dariuszkuc merged commit 273ee25 into dev Feb 3, 2025
16 checks passed
@dariuszkuc dariuszkuc deleted the gen_fragment_v2 branch February 3, 2025 20:49
@dariuszkuc
Copy link
Member Author

@Mergifyio backport 1.x

Copy link
Contributor

mergify bot commented Feb 3, 2025

backport 1.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Feb 3, 2025
Update `generate_fragment` algorithm to extract named fragments from common sub selections in the given operation. Fragments are now auto generated for each sub selection that occurs at least twice within the final fetch operation.

Algorithm consists of two phases
- recursively iterate over the selection sets and collect sub selection usages
- recursively iterate over the selection sets to find which sub selections were used more than once
  - use this sub selection to create new fragment definition and replace current selection set with just a fragment spread

Based on preliminary testing over 2.8 million ops this new algorithm
* generates better results in 95% of test operations (vs 77% of old one)
* p99 overhead of running this new compression algorithm results in just 10ms overhead
* in case of better results, at p99 it shrinks the queries by 50Kb
* in case it generates worse results, at p99 it only adds an additional 108 bytes to a query

(cherry picked from commit 273ee25)

# Conflicts:
#	apollo-router/src/plugins/connectors/tests/mod.rs
dariuszkuc added a commit that referenced this pull request Feb 4, 2025
Update `generate_fragment` algorithm to extract named fragments from common sub selections in the given operation. Fragments are now auto generated for each sub selection that occurs at least twice within the final fetch operation.

Algorithm consists of two phases
- recursively iterate over the selection sets and collect sub selection usages
- recursively iterate over the selection sets to find which sub selections were used more than once
  - use this sub selection to create new fragment definition and replace current selection set with just a fragment spread

Based on preliminary testing over 2.8 million ops this new algorithm
* generates better results in 95% of test operations (vs 77% of old one)
* p99 overhead of running this new compression algorithm results in just 10ms overhead
* in case of better results, at p99 it shrinks the queries by 50Kb
* in case it generates worse results, at p99 it only adds an additional 108 bytes to a query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-1.x Backport this PR to 1.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants