mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-19 22:40:40 +00:00
feat: handle Ctrl-C command in MySQL client (#6320)
* feat/answer-ctrl-c-in-mysql: ## Implement Connection ID-based Query Killing ### Key Changes: - **Connection ID Management:** - Added `connection_id` to `Session` and `QueryContext` in `src/session/src/lib.rs` and `src/session/src/context.rs`. - Updated `MysqlInstanceShim` and `MysqlServer` to handle `connection_id` in `src/servers/src/mysql/handler.rs` and `src/servers/src/mysql/server.rs`. - **KILL Statement Enhancements:** - Introduced `Kill` enum to handle both `ProcessId` and `ConnectionId` in `src/sql/src/statements/kill.rs`. - Updated `ParserContext` to parse `KILL QUERY <connection_id>` in `src/sql/src/parser.rs`. - Modified `StatementExecutor` to support killing queries by `connection_id` in `src/operator/src/statement/kill.rs`. - **Process Management:** - Refactored `ProcessManager` to include `connection_id` in `src/catalog/src/process_manager.rs`. - Added `kill_local_process` method for local query termination. - **Testing:** - Added tests for `KILL` statement parsing and execution in `src/sql/src/parser.rs`. ### Affected Files: - `Cargo.lock`, `Cargo.toml` - `src/catalog/src/process_manager.rs` - `src/frontend/src/instance.rs` - `src/frontend/src/stream_wrapper.rs` - `src/operator/src/statement.rs` - `src/operator/src/statement/kill.rs` - `src/servers/src/mysql/federated.rs` - `src/servers/src/mysql/handler.rs` - `src/servers/src/mysql/server.rs` - `src/servers/src/postgres.rs` - `src/session/src/context.rs` - `src/session/src/lib.rs` - `src/sql/src/parser.rs` - `src/sql/src/statements.rs` - `src/sql/src/statements/kill.rs` - `src/sql/src/statements/statement.rs` Signed-off-by: Lei, HUANG <mrsatangel@gmail.com> Conflicts: Cargo.lock Cargo.toml Signed-off-by: Lei, HUANG <mrsatangel@gmail.com> * feat/answer-ctrl-c-in-mysql: ### Enhance Process Management and Execution - **`process_manager.rs`**: Added a new method `find_processes_by_connection_id` to filter processes by connection ID, improving process management capabilities. - **`kill.rs`**: Refactored the process killing logic to utilize the new `find_processes_by_connection_id` method, streamlining the execution flow and reducing redundant checks. Signed-off-by: Lei, HUANG <mrsatangel@gmail.com> * feat/answer-ctrl-c-in-mysql: ## Commit Message ### Update Process ID Type and Refactor Code - **Change Process ID Type**: Updated the process ID type from `u64` to `u32` across multiple files to optimize memory usage. Affected files include `process_manager.rs`, `lib.rs`, `database.rs`, `instance.rs`, `server.rs`, `stream_wrapper.rs`, `kill.rs`, `federated.rs`, `handler.rs`, `server.rs`, `postgres.rs`, `mysql_server_test.rs`, `context.rs`, `lib.rs`, and `test_util.rs`. - **Remove Connection ID**: Removed the `connection_id` field and related logic from `process_manager.rs`, `lib.rs`, `instance.rs`, `server.rs`, `stream_wrapper.rs`, `kill.rs`, `federated.rs`, `handler.rs`, `server.rs`, `postgres.rs`, `mysql_server_test.rs`, `context.rs`, `lib.rs`, and `test_util.rs` to simplify the codebase. - **Refactor Process Management**: Refactored process management logic to improve clarity and maintainability in `process_manager.rs`, `kill.rs`, and `handler.rs`. - **Enhance MySQL Server Handling**: Improved MySQL server handling by integrating process management in `server.rs` and `mysql_server_test.rs`. Signed-off-by: Lei, HUANG <mrsatangel@gmail.com> * feat/answer-ctrl-c-in-mysql: ### Add Process Manager to Postgres Server - **`src/frontend/src/server.rs`**: Updated server initialization to include `process_manager`. - **`src/servers/src/postgres.rs`**: Modified `MakePostgresServerHandler` to accept `process_id` for session creation. - **`src/servers/src/postgres/server.rs`**: Integrated `process_manager` into `PostgresServer` for generating `process_id` during connection handling. - **`src/servers/tests/postgres/mod.rs`** and **`tests-integration/src/test_util.rs`**: Adjusted test server setup to accommodate optional `process_manager`. Signed-off-by: Lei, HUANG <mrsatangel@gmail.com> * feat/answer-ctrl-c-in-mysql: Update `greptime-proto` Dependency - Updated the `greptime-proto` dependency to a new revision in both `Cargo.lock` and `Cargo.toml`. - `Cargo.lock`: Changed source revision from `d75a56e05a87594fe31ad5c48525e9b2124149ba` to `fdcbe5f1c7c467634c90a1fd1a00a784b92a4e80`. - `Cargo.toml`: Updated the `greptime-proto` git revision to match the new commit. Signed-off-by: Lei, HUANG <mrsatangel@gmail.com> --------- Signed-off-by: Lei, HUANG <mrsatangel@gmail.com>
This commit is contained in:
@@ -369,7 +369,7 @@ impl StatementExecutor {
|
||||
Statement::ShowSearchPath(_) => self.show_search_path(query_ctx).await,
|
||||
Statement::Use(db) => self.use_database(db, query_ctx).await,
|
||||
Statement::Admin(admin) => self.execute_admin_command(admin, query_ctx).await,
|
||||
Statement::Kill(id) => self.execute_kill(query_ctx, id).await,
|
||||
Statement::Kill(kill) => self.execute_kill(query_ctx, kill).await,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,11 +12,13 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use catalog::process_manager::ProcessManagerRef;
|
||||
use common_frontend::DisplayProcessId;
|
||||
use common_query::Output;
|
||||
use common_telemetry::error;
|
||||
use session::context::QueryContextRef;
|
||||
use snafu::ResultExt;
|
||||
use sql::statements::kill::Kill;
|
||||
|
||||
use crate::error;
|
||||
use crate::statement::StatementExecutor;
|
||||
@@ -25,22 +27,51 @@ impl StatementExecutor {
|
||||
pub async fn execute_kill(
|
||||
&self,
|
||||
query_ctx: QueryContextRef,
|
||||
process_id: String,
|
||||
) -> crate::error::Result<Output> {
|
||||
kill: Kill,
|
||||
) -> error::Result<Output> {
|
||||
let Some(process_manager) = self.process_manager.as_ref() else {
|
||||
error!("Process manager is not initialized");
|
||||
return error::ProcessManagerMissingSnafu.fail();
|
||||
};
|
||||
|
||||
let succ = match kill {
|
||||
Kill::ProcessId(process_id) => {
|
||||
self.kill_process_id(process_manager, query_ctx, process_id)
|
||||
.await?
|
||||
}
|
||||
Kill::ConnectionId(conn_id) => {
|
||||
self.kill_connection_id(process_manager, query_ctx, conn_id)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
Ok(Output::new_with_affected_rows(if succ { 1 } else { 0 }))
|
||||
}
|
||||
|
||||
/// Handles `KILL <PROCESS_ID>` statements.
|
||||
async fn kill_process_id(
|
||||
&self,
|
||||
pm: &ProcessManagerRef,
|
||||
query_ctx: QueryContextRef,
|
||||
process_id: String,
|
||||
) -> error::Result<bool> {
|
||||
let display_id = DisplayProcessId::try_from(process_id.as_str())
|
||||
.map_err(|_| error::InvalidProcessIdSnafu { id: process_id }.build())?;
|
||||
|
||||
let current_user_catalog = query_ctx.current_catalog().to_string();
|
||||
process_manager
|
||||
.kill_process(display_id.server_addr, current_user_catalog, display_id.id)
|
||||
pm.kill_process(display_id.server_addr, current_user_catalog, display_id.id)
|
||||
.await
|
||||
.context(error::CatalogSnafu)?;
|
||||
.context(error::CatalogSnafu)
|
||||
}
|
||||
|
||||
Ok(Output::new_with_affected_rows(0))
|
||||
/// Handles MySQL `KILL QUERY <CONNECTION_ID>` statements.
|
||||
pub async fn kill_connection_id(
|
||||
&self,
|
||||
pm: &ProcessManagerRef,
|
||||
query_ctx: QueryContextRef,
|
||||
connection_id: u32,
|
||||
) -> error::Result<bool> {
|
||||
pm.kill_local_process(query_ctx.current_catalog().to_string(), connection_id)
|
||||
.await
|
||||
.context(error::CatalogSnafu)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user