Skip to content

Promote longest-waiting B session via max_connect_time fold#5825

Merged
renecannao merged 3 commits into
v3.0from
v3.0_partition-fairness
May 27, 2026
Merged

Promote longest-waiting B session via max_connect_time fold#5825
renecannao merged 3 commits into
v3.0from
v3.0_partition-fairness

Conversation

@rahim-kanji

@rahim-kanji rahim-kanji commented May 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-on to the partition gate. Under sustained backend-pool contention, ProcessAllSessions_Partition left the B band (sessions in CONNECTING_SERVER, waiting on the pool) in unordered storage order, so the CONNECTING_SERVER pass could keep serving newer waiters ahead of older ones — letting the unlucky oldest waiters sit until they hit connect_timeout_server_max (default 10s) and abort. This promotes the longest-waiting session to the front of the B band each tick so it's served first.

What changed

  • In the classifier's is_B arm, track the session with the smallest
    max_connect_time (no second pass over B), then swap it to running_end
    after the pass.
  • Gated by |B| > PARTITION_FAIRNESS_MIN_B (=4) and the existing
    partition_active gate — a no-op on small bands / low contention.
  • Logic in lib/Base_Thread.cpp (+25), one constant in
    include/Base_Thread.h (+1). Shared template, so it applies to both MySQL
    and PgSQL.

Why max_connect_time

The is_B test already loads s->mybe->server_myds->max_connect_time, so the min-compare is a register compare with no extra memory access (vs CurrentQuery.start_time, a separate cache line). max_connect_time = connect_start + connect_timeout, so the smallest value is the earliest waiter — the session closest to the abort being prevented — a more direct target than query start, and identical ordering when the connect timeout is uniform.

Behavior / risk

  • Pure reordering within the B band; the A/idle partitions and the gate are
    untouched.
  • One min-track per B session + at most one swap per tick. No allocations, no
    locks (per-thread state only).

Summary by CodeRabbit

  • Improvements
    • Enhanced fairness in session processing by prioritizing connections that have been waiting the longest, ensuring more equitable request handling.

Review Change Stack

…me fold

Under sustained contention ProcessAllSessions_Partition leaves the B
(CONNECTING_SERVER) band unordered, so the pass can serve newer waiters
ahead of older ones until they hit connect_timeout_server_max and abort.

Track the oldest waiter during the classifier's is_B arm (no second pass)
and swap it to running_end. Key on max_connect_time, which the is_B test
already loads -- the min-compare is a register compare with no extra memory
access, and the smallest value is the waiter closest to the timeout abort.
Gated by |B| > PARTITION_FAIRNESS_MIN_B (=4) and partition_active.
@coderabbitai

coderabbitai Bot commented May 26, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 57afdf07-7cc0-4ee5-8286-fa837a3e8732

📥 Commits

Reviewing files that changed from the base of the PR and between a1ca6a0 and 6909011.

📒 Files selected for processing (2)
  • include/Base_Thread.h
  • lib/Base_Thread.cpp
📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{cpp,h,hpp}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{cpp,h,hpp}: Feature tiers are controlled via flags: PROXYSQL31=1 for v3.1.x features (FFTO, TSDB), PROXYSQL40=1 for v4.0.x features (plugin loader). PROXYSQL40=1 implies both PROXYSQL31=1 and PROXYSQLFFTO=1 and PROXYSQLTSDB=1. Use conditional compilation with #ifdef PROXYSQL31, #ifdef PROXYSQL40, #ifdef PROXYSQLFFTO, #ifdef PROXYSQLTSDB, #ifdef PROXYSQLCLICKHOUSE.
Class names use PascalCase with protocol prefixes: MySQL_, PgSQL_, or ProxySQL_ (e.g., MySQL_Protocol, PgSQL_Session).
Member variables use snake_case.
Constants and macros use UPPER_SNAKE_CASE.
Use C++17; conditional compilation for feature tiers via #ifdef PROXYSQL31, #ifdef PROXYSQL40, #ifdef PROXYSQLFFTO, #ifdef PROXYSQLTSDB, #ifdef PROXYSQLCLICKHOUSE.
Use RAII for resource management; use jemalloc for memory allocation.
Use pthread mutexes for synchronization; use std::atomic<> for counters.

Files:

  • include/Base_Thread.h
  • lib/Base_Thread.cpp
include/**/*.{h,hpp}

📄 CodeRabbit inference engine (CLAUDE.md)

Include guards in headers use #ifndef __CLASS_*_H format (e.g., #ifndef __MYSQL_PROTOCOL_H).

Files:

  • include/Base_Thread.h
{lib,src}/**/*.{cpp,h,hpp}

📄 CodeRabbit inference engine (CLAUDE.md)

GenAI/MCP/RAG/LLM features live entirely in plugins/genai/ and load as a .so at runtime via dlopen. Do not guard with PROXYSQLGENAI in core code — that flag no longer guards any core code as of Step 7 of the GenAI plugin carve-out.

Files:

  • lib/Base_Thread.cpp
🔇 Additional comments (5)
include/Base_Thread.h (1)

69-69: LGTM!

lib/Base_Thread.cpp (4)

288-289: LGTM!


295-296: LGTM!

Also applies to: 301-303


310-319: LGTM!


330-339: LGTM!


📝 Walkthrough

Walkthrough

The pull request introduces session fairness promotion to the partition processing logic. A public fairness threshold constant is defined, and the partitioning method now tracks the longest-waiting B-band session by earliest connection time and swaps it to the running boundary when the B-band is sufficiently large, ensuring it is served first.

Changes

Session Fairness Promotion in Partitioning

Layer / File(s) Summary
Fairness constant definition
include/Base_Thread.h
Public constant PARTITION_FAIRNESS_MIN_B = 4 is added to define the minimum B-band session count threshold for fairness promotion eligibility.
Partition fairness tracking and promotion
lib/Base_Thread.cpp
During the partitioning loop, the B-band session with the smallest max_connect_time (oldest/longest-waiting) is tracked via oldest_idx and oldest_mct. After partitioning completes, if the B-band size exceeds the threshold and the tracked session is valid and not already at the boundary, it is swapped into position running_end to ensure the subsequent CONNECTING_SERVER processing pass serves it first.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A fairness hop through sessions long,
The oldest waiter speeds along,
When B-bands swell and patience wanes,
Swap tracks ensure no one complains,
Connect fair quick, no more delays!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and specifically describes the main change: promoting the longest-waiting B session using max_connect_time in the partition fairness logic, which aligns with the primary objective of reordering the B band to prevent timeout.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch v3.0_partition-fairness

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a fairness mechanism in the session partitioning logic. It tracks the longest-waiting B session (the one with the smallest max_connect_time) and promotes it to the front of the B-band (at running_end) so that it is served first during the connection phase, gated by a minimum band size to prevent unnecessary churn. I have no feedback to provide as there are no review comments to assess.

@sonarqubecloud

Copy link
Copy Markdown

@renecannao
renecannao merged commit 97eab1b into v3.0 May 27, 2026
59 checks passed
@renecannao renecannao mentioned this pull request Jul 8, 2026
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