Skip to content

Add support for max batch size for connectors #7274

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

Merged
merged 13 commits into from
Apr 16, 2025
Merged

Conversation

andrewmcgivery
Copy link
Contributor

@andrewmcgivery andrewmcgivery commented Apr 15, 2025

Add support for max batch size for connectors

Screenshot 2025-04-15 at 5 46 11 PM

Screenshot 2025-04-15 at 5 46 28 PM

Screenshot 2025-04-15 at 5 46 38 PM
Screenshot 2025-04-15 at 5 46 47 PM


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.

@svc-apollo-docs
Copy link
Collaborator

svc-apollo-docs commented Apr 15, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: c678b964f6e36d3aeba47100

Copy link
Contributor

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

@router-perf
Copy link

router-perf bot commented Apr 15, 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

Copy link
Contributor

@lennyburdette lennyburdette left a comment

Choose a reason for hiding this comment

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

i think this warrants an integration test next to the existing batch test (which will require a hand-edited supergraph until we release composition changes). nice work tho!

@@ -8,7 +8,7 @@ pub mod expand;
mod header;
mod id;
mod json_selection;
mod models;
pub mod models;
Copy link
Contributor

Choose a reason for hiding this comment

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

let's selectively expose types as pub instead of marking the whole module as pub if possible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

"supplied 'max_size' field in `@connect` directive's `batch` field is not a positive integer"
))?);
// Convert the int to a usize since it is used for chunking an array later.
// Much better to fail here than at run time.
Copy link
Contributor

Choose a reason for hiding this comment

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

this code is part of the runtime!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bad wording on my part 😅

I think what I more meant here is during the request lifecycle? Aka... check this as early as we can (startup?) instead of it failing in the middle of a request.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the comment... let me know if this makes sense 😅

@@ -55,6 +57,15 @@ pub(crate) struct SourceHTTPArguments {
pub(crate) headers: IndexMap<HeaderName, HeaderSource>,
}

/// Settings for the connector when it is doing a $batch entity resolver
#[cfg_attr(test, derive(Debug))]
pub(crate) struct SourceBatchArguments {
Copy link
Contributor

Choose a reason for hiding this comment

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

why is it called Source BatchArguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦 Good call lol

// Because we may have multiple batch entities requests, we should add to ENTITIES as the requests come in so it is additive
let entities = data
.entry(ENTITIES)
.or_insert(Value::Array(Vec::with_capacity(count)));
Copy link
Contributor

Choose a reason for hiding this comment

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

this count is for the current chunk. i dunno if there's a way to get the original count of entity references here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So... it seems like it's the count of....

let count = responses.len();

Which is calculated prior to looping through the responses. 😕 Weird.


// If we've got a max_size set, chunk the batch into smaller batches. Otherwise, we'll default to just a single batch.
let max_size = connector.batch_settings.as_ref().and_then(|bs| bs.max_size);
let batches = max_size.map_or(vec![batch.clone()], |size| {
Copy link
Contributor

Choose a reason for hiding this comment

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

vec![batch.clone()] seems like an unnecessary clone, but i can't solve it without rust-analyzer 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems unnecessary but it complains that it can't be owned by both that line and the closure right below it.... making it borrowed causes all kinds of other errors 😅 I couldn't figure out any alternatives but open to suggestions!

(I tried asking ChatGPT and it said "If you must return owned Vec, then cloning or moving is necessary. 😅 )

Copy link
Contributor

@lennyburdette lennyburdette Apr 16, 2025

Choose a reason for hiding this comment

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

    let batches = if let Some(size) = n {
        v.chunks(size).map(|v| v.to_vec()).collect()  
    } else {
        vec![v]
    };

i guess the borrow checker can't know that the closures passed to map_or are mutually exclusive. but an if/else definitely is!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well I suppose I could do it that way! That's actually what I originally started with and it didn't feel rust-y so I re-factored to what I have now 😆

ResponseKey::BatchEntity {
selection: selection.clone(),
inputs,
keys: keys.clone(),
Copy link
Contributor

Choose a reason for hiding this comment

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

not for this PR, but we should rename this to key_field_set or something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In just this function or on request and BatchEntity too?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, everywhere. it's a very confusing name

@andrewmcgivery andrewmcgivery marked this pull request as ready for review April 16, 2025 16:32
@andrewmcgivery andrewmcgivery requested review from a team as code owners April 16, 2025 16:32
@andrewmcgivery andrewmcgivery changed the title WIP: Add support for max batch size for connectors Add support for max batch size for connectors Apr 16, 2025
@andrewmcgivery andrewmcgivery merged commit cfad283 into dev Apr 16, 2025
15 checks passed
@andrewmcgivery andrewmcgivery deleted the feature/batchmaxsize branch April 16, 2025 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants