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

adapter: refactor max connection handling #30624

Merged

Conversation

ParkMyCar
Copy link
Member

This PR refactors our max connection handling in a few ways:

  1. It pulls the logic out of the sql crate and moves it into the pgwire_common crate. AFAICT this logic lived in the sql crate so it could be updated via SystemVars.
  2. Refactors SystemVars to contain a generic set of callbacks so anyone can register to be notified about changes to a system variable, not just connection handling.

It also fixes a bug that has occurred in production. Previously if the max connection limit dropped below the number of current connections (e.g. reducing the LaunchDarkly flag) we could panic.

Motivation

Fixes a known panic and refactors the code.

Tips for reviewer

I would start by reviewing the changes in sql/src/session/vars.rs to see how SystemVars changed, then look at the changes in pgwire-common/src/conn.rs.

Checklist

  • This PR has adequate test coverage / QA involvement has been duly considered. (trigger-ci for additional test/nightly runs)
  • This PR has an associated up-to-date design doc, is a design doc (template), or is sufficiently small to not require a design.
  • If this PR evolves an existing $T ⇔ Proto$T mapping (possibly in a backwards-incompatible way), then it is tagged with a T-proto label.
  • If this PR will require changes to cloud orchestration or tests, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label (example).
  • If this PR includes major user-facing behavior changes, I have pinged the relevant PM to schedule a changelog post.

* pull connection handling out of the sql crate and into pgwire_common
* refactor SystemVars to have a set of callbacks to notify interested folks in changes to variables
* refactor max connection handling so we don't panic if the limit drops to less than current number of connections
Copy link
Contributor

@jkosh44 jkosh44 left a comment

Choose a reason for hiding this comment

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

LGTM, had a couple of small questions and nits. It also might be good to add a test where we lower the connection limit with a bunch of connections open to repro the panic.

Comment on lines 3943 to 3944
// If superuser_reserved > max_connections, prefer max_connections.
let superuser_reserved = std::cmp::min(limit, superuser_reserved);
Copy link
Contributor

Choose a reason for hiding this comment

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

If superuser_reserved > max_connections is true, then that means only super users could connect to the database right? i.e., all connections are reserved for superusers and normal users will be locked out. That feels like a bad enough situation that we should add an error log here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great call, added a soft assert here!

* add some extra asserts
* update comments
* add test case to make sure we don't panic when dropping below number of open connections
@ParkMyCar ParkMyCar enabled auto-merge (squash) November 25, 2024 23:53
@ParkMyCar
Copy link
Member Author

Added a test that previously repro'd the panic!

@ParkMyCar ParkMyCar merged commit 518c273 into MaterializeInc:main Nov 26, 2024
83 checks passed
@ParkMyCar ParkMyCar deleted the adapter/refactor-max-connections branch February 7, 2025 16:52
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