Skip to content

fix(gateway): reuse existing user pool on settings-key miss in get_data_pool - #721

Closed
morch23mj wants to merge 1 commit into
documentdb:mainfrom
wetransform:fix/get-data-pool-fallback-on-miss
Closed

morch23mj wants to merge 1 commit into
documentdb:mainfrom
wetransform:fix/get-data-pool-fallback-on-miss

Conversation

@morch23mj

@morch23mj morch23mj commented Aug 13, 2026 •

Copy link
Copy Markdown

Description

Fixes #720.

get_data_pool (postgres/conn_mgmt/pool_manager.rs) keys the per-user pool cache on (username, PgPoolSettings). PgPoolSettings includes max_connections, which is re-read from a Postgres GUC on every dynamic-config refresh (and silently defaults to 25 on a bad read). When it changes, the exact-key lookup misses and the gateway hard-fails every request for that user — permanently — even though their pool is still present and healthy. This PR makes get_data_pool reuse the user's existing pool on a settings-key miss instead of erroring. Root cause and evidence: #720.

What changed

get_data_pool keeps the exact-key fast path, and on a miss reuses any existing pool for that username before erroring (illustrative):

if let Some(p) = self.user_data_pools.get(&(username.to_owned(), settings)) {
    p.touch(); return Ok(Arc::clone(p.value()));
}
// settings drifted (e.g. max_connections re-read) — pool is healthy but filed
// under a stale key. Reuse it; allocate_data_pool re-files under the current
// key on the next auth.
if let Some(p) = self.user_data_pools.iter()
    .find(|e| e.key().0 == username).map(|e| Arc::clone(e.value())) {
    p.touch(); return Ok(p);
}
Err(DocumentDBError::internal_error("Connection pool missing for user.".to_owned()))

Plus a regression test (test_get_data_pool_falls_back_to_existing_pool_on_settings_change): allocate a pool, flip max_connections, assert get_data_pool returns the same pool (Arc::ptr_eq) with no new pool created.

Why it's correct

  • The settings delta isn't a real topology change — role/password/live connections are unaffected; only the pool's sizing may lag by one refresh.
  • Convergence is preserved: allocate_data_pool re-files the pool under the current key on the next authentication, so this only bridges the gap for already-authenticated users — it isn't a permanent bypass. The exact-key hit path is untouched, so the deliberate rebuild-on-max_connections-change (validate_max_conn_change) still works.

Why not mirror get_system_shared_pool

That path self-heals by building a new pool from system credentials it always holds. get_data_pool has no user password at request time (only allocate_data_pool has it, at auth), so reusing the existing pool is the only option here.

Alternatives considered

  • Drop max_connections from the key — rejected: rebuilding pools on a real max_connections change is intentional/tested (validate_max_conn_change).
  • Harden max_connections() to not fabricate 25 — good complementary defense-in-depth, but a legitimate max_connections change flips the same key and hits the same miss, so it's not a substitute. Happy to add as a follow-up.

Testing

cargo test -p documentdb_gateway_core pool_manager → 13 passed, 0 failed (the new test, the no-pool-still-errors case, and validate_max_conn_change). rustfmt + clippy --workspace --all-targets --all-features -- -D warnings clean on the pinned toolchain.

Type of Change

  • Bug fix

AI Disclosure

  • AI tools were used. Tool: Claude (Anthropic). Root-cause tracing, the fix, and the test were prepared with substantial AI assistance, then reviewed and verified against the source by the author.

Checklist

  • I can explain every change if asked
  • Tested locally (cargo test — 13 passed)
  • Tests added/updated
  • Commits signed off (DCO)
  • Read CONTRIBUTING.md

Signed-off-by: Morchid Chellali [email protected]

@morch23mj
morch23mj requested a review from a team as a code owner August 13, 2026 04:07
@documentdb-triage-tool documentdb-triage-tool Bot added bug gateway for gateway related issues performance labels Aug 13, 2026
@documentdb-triage-tool

Copy link
Copy Markdown

🤖 Auto-triaged by documentdb-triage-tool.

Applied: gateway, bug, performance
Project fields suggested: Component gateway · Priority P1 · Effort S · Status Needs Review
Confidence: 0.90 (mixed)

Reasoning

component from path globs (gateway); effort from diff stats (72+9 LOC, 1 files); LLM: Fixes a permanent hard-failure for all requests per user when a settings-key miss occurs in get_data_pool due to GUC re-read drift — an active blocker in production gateway scenarios.

If a label is wrong, remove it manually and ping @patty-chow so the rules can be tuned. The bot will not re-label items that already have component labels.

.user_data_pools
.iter()
.find(|entry| entry.key().0 == username)
.map(|entry| Arc::clone(entry.value()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but the pool you've received will not govern the gw of using the 25 connections as in your test case, but will let to go the whole 100 connections (which it doesn't have).

the whole rationale of why we don't have this logic for the user data pools is because the max_connections is the startup GUC, so I would expect the whole docker restart so the pg can pick up the new param too; if your use case requires the dynamic change I highly recommend changing the

user_data_pools: DashMap<ClientKey, Arc<ConnectionPool>> to something like
user_data_pools: DashMap<(ClientKey, PgPoolSettings), Arc<ConnectionPool>>

so in case any pg pool settings changes you will use the "fresh" pool and the old pool will be cleaned up on retention

@morch23mj

morch23mj commented Aug 15, 2026 •

Copy link
Copy Markdown
Author

Thanks @AndrewKhoma — agreed on both points. Closing this in favor of #723, which takes that feedback directly — instead of reusing pools it fixes the root cause: the transient bad max_connections read that silently substitutes 25 and flips the pool-cache key mid-run with no real change behind it. The intentional hard-fail in get_data_pool stays untouched, and there's no pool reuse / over-subscription. A genuine (restart-driven) change still flips the key as designed.

@morch23mj morch23mj closed this Aug 15, 2026
@github-project-automation github-project-automation Bot moved this from Todo to Done in Issue triage Aug 15, 2026
@morch23mj
morch23mj deleted the fix/get-data-pool-fallback-on-miss branch August 18, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug gateway for gateway related issues performance

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Gateway permanently fails all requests for a user after a dynamic-config refresh flips the connection-pool key ("Connection pool missing for user.")

3 participants