perf(query): optimize large-value result diagnostics

This commit is contained in:
zipg authored and GitHub committed 2026-08-14 00:11:28 +08:00
1 parent 9c50cc789b
commit 35eab8a8b1
6 files changed
+429 -33

No files matched your search

+32 -8
View File
@@ -1,7 +1,8 @@
use std::sync::Arc;
use axum::extract::State;
use axum::http::HeaderMap;
use axum::http::{header, HeaderMap, HeaderValue};
use axum::response::Response;
use axum::Json;
use serde::Deserialize;
@@ -389,7 +390,7 @@ pub async fn execute_multi(
State(state): State<Arc<WebState>>,
headers: HeaderMap,
Json(req): Json<ExecuteQueryRequest>,
) -> Result<Json<Vec<dbx_core::query::ExecuteMultiResult>>, AppError> {
) -> Result<Response, AppError> {
let allow_database_switch = req.client_session_id.as_deref().is_some_and(|id| !id.trim().is_empty());
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql, allow_database_switch)
.await?;
@@ -403,6 +404,7 @@ pub async fn execute_multi(
tracing::debug!(connection_id = %req.connection_id, "execute_multi");
let core_started_at = std::time::Instant::now();
let result = dbx_core::query::execute_multi_core_with_options_for_client_typed(
&state.app,
&req.connection_id,
@@ -430,15 +432,30 @@ pub async fn execute_multi(
)
.await
.map_err(|error| AppError::from(error.into_backend_error()))?;
let core_ms = core_started_at.elapsed().as_millis();
drop(registered);
Ok(execute_multi_response(result))
execute_multi_response(result, core_ms)
}
fn execute_multi_response(
result: Vec<dbx_core::query::ExecuteMultiResult>,
) -> Json<Vec<dbx_core::query::ExecuteMultiResult>> {
Json(result)
core_ms: u128,
) -> Result<Response, AppError> {
let serialize_started_at = std::time::Instant::now();
let body = serde_json::to_vec(&result).map_err(|error| AppError::internal(error.to_string()))?;
let serialize_ms = serialize_started_at.elapsed().as_millis();
let mut response = Response::new(axum::body::Body::from(body));
response.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
response.headers_mut().insert(
"x-dbx-core-ms",
HeaderValue::from_str(&core_ms.to_string()).map_err(|error| AppError::internal(error.to_string()))?,
);
response.headers_mut().insert(
"x-dbx-serialize-ms",
HeaderValue::from_str(&serialize_ms.to_string()).map_err(|error| AppError::internal(error.to_string()))?,
);
Ok(response)
}
pub async fn execute_batch(
@@ -1054,8 +1071,8 @@ mod tests {
assert_eq!(log.metadata["blocked"], "destructive_confirmation_required");
}
#[test]
fn execute_multi_response_preserves_nested_original_error_detail() {
#[tokio::test]
async fn execute_multi_response_preserves_nested_original_error_detail() {
let result = dbx_core::query::ExecuteMultiResult {
result: dbx_core::db::QueryResult {
columns: vec!["Error".to_string()],
@@ -1081,7 +1098,14 @@ mod tests {
server_message: false,
};
let payload = serde_json::to_value(execute_multi_response(vec![result]).0).unwrap();
let response = execute_multi_response(vec![result], 17).unwrap();
assert_eq!(response.headers()["x-dbx-core-ms"], "17");
assert!(response.headers()["x-dbx-serialize-ms"].to_str().unwrap().parse::<u128>().is_ok());
assert_eq!(response.headers()[header::CONTENT_TYPE], "application/json");
let body = response.into_body();
let payload =
serde_json::from_slice::<serde_json::Value>(&axum::body::to_bytes(body, usize::MAX).await.unwrap())
.unwrap();
assert_eq!(payload[0]["statement_index"], 1);
assert_eq!(payload[0]["error"]["code"], "DBX-JDBC-4001");