-
Notifications
You must be signed in to change notification settings - Fork 273
feat: add admin FORCE_RELOAD command #1502
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
jkaczman
wants to merge
4
commits into
main
Choose a base branch
from
jk-force-reload
base: main
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
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,47 @@ | ||
| use crate::setup::{admin_sqlx, connections_sqlx}; | ||
|
|
||
| /// <https://github.com/pgdogdev/pgdog/issues/1472> | ||
| /// Test the implementation of the command FORCE_RELOAD, which is a normal RELOAD + terminates all in-flight transactions. | ||
| #[tokio::test] | ||
| async fn admin_reload_test() { | ||
| let admin = admin_sqlx().await; | ||
| let connections = connections_sqlx().await; // [pgdog, pgdog_sharded] | ||
|
|
||
| { | ||
| let mut transaction = connections.get(1).unwrap().begin().await.unwrap(); | ||
|
|
||
| // Isn't strictly needed for the functionaltiy of the test; but why not? | ||
| sqlx::raw_sql("SELECT * FROM sharded") | ||
| .fetch_all(&mut *transaction) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| // After we force reload, existing transactions (i.e. this one) are terminated. | ||
| sqlx::raw_sql("FORCE_RELOAD").execute(&admin).await.unwrap(); | ||
|
|
||
| let err = sqlx::raw_sql("SELECT * FROM sharded") | ||
| .fetch_all(&mut *transaction) | ||
| .await | ||
| .err() | ||
| .unwrap(); | ||
|
|
||
| // Standard Postgres error pertaining to pg_terminate_backend; <https://www.postgresql.org/docs/current/functions-admin.html> | ||
| assert!( | ||
| err.as_database_error() | ||
| .unwrap() | ||
| .message() | ||
| .contains("terminating connection due to administrator command") | ||
| ); | ||
|
|
||
| // The transaction drops (allowing another connection in sqlx `Pool`) | ||
| } | ||
|
|
||
| // Does it work with a new Pool? | ||
| let test = connections_sqlx().await; | ||
| let test = test.get(1).unwrap(); | ||
| sqlx::raw_sql("SELECT 1234").fetch_all(test).await.unwrap(); | ||
|
|
||
| // Does it still with the old Pool? | ||
| let conn = connections.get(1).unwrap(); | ||
| sqlx::raw_sql("SELECT 1000").fetch_all(conn).await.unwrap(); | ||
| } |
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub mod admin; | ||
| pub mod admin_reload; | ||
| pub mod admin_termination; | ||
| pub mod auth; | ||
| pub mod auto_id; | ||
|
|
||
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,24 @@ | ||
| //! FORCE RELOAD command. | ||
|
|
||
| use super::prelude::*; | ||
| use crate::backend::databases::{reload, terminate_active_connections}; | ||
|
|
||
| pub(crate) struct ForceReload; | ||
|
|
||
| #[async_trait] | ||
| impl Command for ForceReload { | ||
| fn name(&self) -> String { | ||
| "FORCE_RELOAD".into() | ||
| } | ||
|
|
||
| fn parse(_sql: &str) -> Result<Self, Error> { | ||
| Ok(ForceReload) | ||
| } | ||
|
|
||
| async fn execute(&self) -> Result<Vec<Message>, Error> { | ||
| terminate_active_connections().await?; | ||
| reload()?; | ||
|
|
||
| Ok(vec![]) | ||
| } | ||
| } |
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
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
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
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.
I wanted to get some input on this before implementing a solution for handling errors. I think that if we're guaranteeing all transactions are terminated, then we probably want to exit early with an error to the client and not reload.
However, what if there were some circumstance (e.g. no free connection slots on the server), which prevents us from terminating forever? To handle cases like that, should we have two different variants of this command (or perhaps a parameter) to "override" a potential failure in terminating a transaction?