-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Milestone 3 Phase 3.5: Extract hostgroup routing and locking decisions from session #5529
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
Open
renecannao
wants to merge
4
commits into
v3.0
Choose a base branch
from
v3.0-5493
base: v3.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
babac8e
Fix compilation error: add missing #include <cstdint> in MonitorHealt…
renecannao f88e3af
Extract hostgroup routing and locking logic from MySQL and PgSQL sess…
renecannao f01c415
Address review feedback for hostgroup routing extraction
renecannao f3be8cb
Improve routing logic for transaction persistence and fix test assert…
renecannao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #ifndef MYSQL_HOSTGROUP_ROUTING_H | ||
| #define MYSQL_HOSTGROUP_ROUTING_H | ||
|
|
||
| #include <string> | ||
|
|
||
| /** | ||
| * @struct MySQL_Routing_Session_State | ||
| * @brief Represents the session state relevant for hostgroup routing decisions. | ||
| */ | ||
| struct MySQL_Routing_Session_State { | ||
| int current_hostgroup{-1}; | ||
| int default_hostgroup{-1}; | ||
| int locked_on_hostgroup{-1}; | ||
| int transaction_persistent_hostgroup{-1}; | ||
| int last_hg_affected_rows{-1}; | ||
| int warning_in_hg{-1}; | ||
| bool autocommit{true}; | ||
| int autocommit_on_hostgroup{-1}; | ||
| bool mirror{false}; | ||
| }; | ||
|
|
||
| /** | ||
| * @struct MySQL_Routing_QPO_State | ||
| * @brief Represents the Query Processor Output relevant for hostgroup routing decisions. | ||
| */ | ||
| struct MySQL_Routing_QPO_State { | ||
| int destination_hostgroup{-1}; | ||
| bool lock_hostgroup{false}; // Derived from query parsing or QPO | ||
| bool is_show_warnings{false}; // Derived from query parsing | ||
| bool is_last_insert_id{false}; // Derived from query parsing | ||
| bool is_version_query{false}; // Derived from query parsing | ||
| }; | ||
|
|
||
| /** | ||
| * @struct MySQL_Routing_Result | ||
| * @brief Represents the output of the hostgroup routing decision. | ||
| */ | ||
| struct MySQL_Routing_Result { | ||
| int new_current_hostgroup{-1}; | ||
| int new_locked_on_hostgroup{-1}; | ||
| bool lock_hostgroup{false}; | ||
| bool error{false}; | ||
| std::string error_msg; | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * @brief Resolves the target hostgroup and locking decisions based on session and QPO state. | ||
| * | ||
| * This is a pure function designed to be easily testable. | ||
| * | ||
| * @param sess_state Current session state. | ||
| * @param qpo_state Query Processor Output state. | ||
| * @param set_query_lock_on_hostgroup Global configuration (mysql-set_query_lock_on_hostgroup). | ||
| * @return MySQL_Routing_Result The routing decision. | ||
| */ | ||
| MySQL_Routing_Result resolve_hostgroup_routing( | ||
| const MySQL_Routing_Session_State& sess_state, | ||
| const MySQL_Routing_QPO_State& qpo_state, | ||
| int set_query_lock_on_hostgroup | ||
| ); | ||
|
|
||
| #endif // MYSQL_HOSTGROUP_ROUTING_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #ifndef PGSQL_HOSTGROUP_ROUTING_H | ||
| #define PGSQL_HOSTGROUP_ROUTING_H | ||
|
|
||
| #include <string> | ||
|
|
||
| /** | ||
| * @struct PgSQL_Routing_Session_State | ||
| * @brief Represents the session state relevant for hostgroup routing decisions in PostgreSQL. | ||
| */ | ||
| struct PgSQL_Routing_Session_State { | ||
| int current_hostgroup{-1}; | ||
| int default_hostgroup{-1}; | ||
| int locked_on_hostgroup{-1}; | ||
| int transaction_persistent_hostgroup{-1}; | ||
| }; | ||
|
|
||
| /** | ||
| * @struct PgSQL_Routing_QPO_State | ||
| * @brief Represents the Query Processor Output relevant for hostgroup routing decisions in PostgreSQL. | ||
| */ | ||
| struct PgSQL_Routing_QPO_State { | ||
| int destination_hostgroup{-1}; | ||
| bool lock_hostgroup{false}; // Derived from query parsing | ||
| }; | ||
|
|
||
| /** | ||
| * @struct PgSQL_Routing_Result | ||
| * @brief Represents the output of the hostgroup routing decision for PostgreSQL. | ||
| */ | ||
| struct PgSQL_Routing_Result { | ||
| int new_current_hostgroup{-1}; | ||
| int new_locked_on_hostgroup{-1}; | ||
| bool lock_hostgroup{false}; | ||
| bool error{false}; | ||
| std::string error_msg; | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * @brief Resolves the target hostgroup and locking decisions based on session and QPO state. | ||
| * | ||
| * This is a pure function designed to be easily testable. | ||
| * | ||
| * @param sess_state Current session state. | ||
| * @param qpo_state Query Processor Output state. | ||
| * @param set_query_lock_on_hostgroup Global configuration (pgsql-set_query_lock_on_hostgroup). | ||
| * @return PgSQL_Routing_Result The routing decision. | ||
| */ | ||
| PgSQL_Routing_Result resolve_pgsql_hostgroup_routing( | ||
| const PgSQL_Routing_Session_State& sess_state, | ||
| const PgSQL_Routing_QPO_State& qpo_state, | ||
| int set_query_lock_on_hostgroup | ||
| ); | ||
|
|
||
| #endif // PGSQL_HOSTGROUP_ROUTING_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "MySQL_HostGroup_Routing.h" | ||
|
|
||
| MySQL_Routing_Result resolve_hostgroup_routing( | ||
| const MySQL_Routing_Session_State& sess_state, | ||
| const MySQL_Routing_QPO_State& qpo_state, | ||
| int set_query_lock_on_hostgroup | ||
| ) { | ||
| MySQL_Routing_Result result; | ||
| result.new_current_hostgroup = sess_state.current_hostgroup; | ||
| result.new_locked_on_hostgroup = sess_state.locked_on_hostgroup; | ||
| result.lock_hostgroup = false; | ||
| result.error = false; | ||
| result.error_msg = ""; | ||
|
|
||
| // 1. Mirroring (highest priority) | ||
| if (sess_state.mirror) { | ||
| result.new_current_hostgroup = qpo_state.destination_hostgroup; | ||
| return result; | ||
| } | ||
|
|
||
| // 2. SHOW WARNINGS / SHOW COUNT(*) WARNINGS | ||
| if (qpo_state.is_show_warnings) { | ||
| if (sess_state.warning_in_hg > -1) { | ||
| result.new_current_hostgroup = sess_state.warning_in_hg; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // 3. LAST_INSERT_ID / @@IDENTITY | ||
| if (qpo_state.is_last_insert_id) { | ||
| if (sess_state.last_hg_affected_rows >= 0) { | ||
| result.new_current_hostgroup = sess_state.last_hg_affected_rows; | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| // 4. Default routing from QPO or Transaction Persistence | ||
| if (sess_state.transaction_persistent_hostgroup != -1) { | ||
| result.new_current_hostgroup = sess_state.transaction_persistent_hostgroup; | ||
| } else { | ||
| if (qpo_state.destination_hostgroup >= 0) { | ||
| result.new_current_hostgroup = qpo_state.destination_hostgroup; | ||
| } else { | ||
| // qpo_state.destination_hostgroup < 0 means no override from QPO | ||
| result.new_current_hostgroup = sess_state.default_hostgroup; | ||
| } | ||
| } | ||
|
|
||
| // 5. Hostgroup Locking Decisions (mysql-set_query_lock_on_hostgroup) | ||
| if (set_query_lock_on_hostgroup == 1) { | ||
| // Algorithm introduced in ProxySQL 2.0.6 | ||
| if (result.new_locked_on_hostgroup < 0) { | ||
| if (qpo_state.lock_hostgroup) { | ||
| result.lock_hostgroup = true; | ||
| result.new_locked_on_hostgroup = result.new_current_hostgroup; | ||
| } | ||
| } | ||
|
|
||
| if (result.new_locked_on_hostgroup >= 0) { | ||
| if (result.new_current_hostgroup != result.new_locked_on_hostgroup) { | ||
| result.error = true; | ||
| result.error_msg = "ProxySQL Error: connection is locked to hostgroup " + | ||
| std::to_string(result.new_locked_on_hostgroup) + | ||
| " but trying to reach hostgroup " + | ||
| std::to_string(result.new_current_hostgroup); | ||
| return result; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this extracted API,
MySQL_Routing_QPO_State::is_set_statementis used as the “should lock hostgroup” signal (call sites assign it from thelock_hostgroupflag computed by query parsing). The current name reads like a generic query classification, but its semantics are specifically about hostgroup locking (and PgSQL useslock_hostgroupfor the same concept), which makes the API confusing and easy to misuse.Suggested fix: rename this field to something semantics-based like
lock_hostgroup(orshould_lock_hostgroup) to match the actual meaning and align with the PgSQL routing API.