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

Fix race condition when adding debug data to connector context #5767

Merged
merged 3 commits into from
Aug 7, 2024

Conversation

pubmodmatt
Copy link
Contributor

@pubmodmatt pubmodmatt commented Aug 2, 2024

There was a race condition due to the way debug data was being added to the ConnectorsContext. The connectors context was removed from the request context extensions for each query, updated, then put back. If the query planner executed multiple queries in parallel, they would all try to do this at once. Once one of them successfully removed the connectors context, any others running concurrently would get back None.

The connectors context is now left in place rather than removed, and is accessed through Option<Arc<Mutex<ConnectorsContext>>>. This preserves a single lookup in the Extensions for each fetch. If the debug feature is not enabled, the option will be None. If it is enabled, the mutex is locked and the context is updated with the debug information.


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

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.

@pubmodmatt pubmodmatt self-assigned this Aug 2, 2024
@pubmodmatt pubmodmatt requested review from a team as code owners August 2, 2024 23:56
@router-perf
Copy link

router-perf bot commented Aug 2, 2024

CI performance tests

  • 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

Comment on lines 62 to 66
context.extensions().with_lock(|mut lock| {
if let Some(ref mut debug) = lock.get_mut::<ConnectorContext>() {
debug.push_invalid_response(&parts, body);
}
});
Copy link
Member

Choose a reason for hiding this comment

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

I worry about lock contention—especially since it could slow down all requests even if debug is disabled. Can we get router team eyes on this for a suggestion? Maybe we could make extensions RwLock, or maybe we should be storing an Option<Mutex<ConnectorContext>> and getting a reference to that (so we can still check for debug disabled).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I can reach out for suggestions. For your second proposal, storing Option<Mutex<ConnectorContext>> would still require locking the ExtensionsGuard to get that Option, so there's still potential contention there, or am I misunderstanding?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I think I see - you mean we could get that Option once and then if Some, lock the second Mutex inside the Option. So we'd only lock the extensions guard once instead of several times per request.

Copy link
Member

Choose a reason for hiding this comment

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

And because the Option is on the outside of the second mutex, we wouldn't need to lock it at all if debugging is disabled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this option the best and coded it up.

@pubmodmatt pubmodmatt requested a review from dylan-apollo August 5, 2024 22:32
Copy link
Member

@dylan-apollo dylan-apollo left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@pubmodmatt pubmodmatt merged commit f6c2026 into next Aug 7, 2024
11 of 12 checks passed
@pubmodmatt pubmodmatt deleted the pubmodmatt/connectors/debug_race branch August 7, 2024 23:42
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.

2 participants