fix(sqlserver): include lock wait in reported query duration

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-15 21:55:03 +08:00
1 parent b6c83838f3
commit 6ec3ff0ccc
1 file changed
+80 -21
+80 -21
View File
@@ -1521,6 +1521,22 @@ where
} }
} }
/// Locks a mutex-guarded shared connection (e.g. SQL Server's single connection
/// per pool key) and reports how long the caller waited for the lock alongside
/// the guard. Callers should fold the returned wait time into any execution-time
/// metric they report, since a driver-level timer that only starts once the lock
/// is held cannot see time spent queued behind another operation on the same
/// connection.
async fn lock_shared_client_with_wait<'a, T>(
client: &'a Arc<tokio::sync::Mutex<T>>,
cancel_token: Option<CancellationToken>,
timeout_duration: Option<Duration>,
) -> Result<(tokio::sync::MutexGuard<'a, T>, u128), String> {
let started_at = std::time::Instant::now();
let guard = wait_for_value_opt(cancel_token, timeout_duration, client.lock()).await?;
Ok((guard, started_at.elapsed().as_millis()))
}
async fn sqlserver_pool_is_current( async fn sqlserver_pool_is_current(
state: &AppState, state: &AppState,
pool_key: &str, pool_key: &str,
@@ -1830,14 +1846,11 @@ async fn do_execute_typed(
let max_rows = options.max_rows; let max_rows = options.max_rows;
let execution_mode = options.execution_mode; let execution_mode = options.execution_mode;
drop(connections); drop(connections);
let mut client = match cancel_token.as_ref() { let (mut client, lock_wait_ms) =
Some(token) => tokio::select! { match lock_shared_client_with_wait(&client, cancel_token.clone(), None).await {
biased; Ok(value) => value,
_ = token.cancelled() => return Err(canceled_error().into()), Err(err) => return Err(err.into()),
guard = client.lock() => guard, };
},
None => client.lock().await,
};
let execution = async { let execution = async {
if execution_mode == QueryExecutionMode::Simple { if execution_mode == QueryExecutionMode::Simple {
let mut results = let mut results =
@@ -1849,7 +1862,11 @@ async fn do_execute_typed(
}; };
let result = wait_for_query_opt(cancel_token, query_timeout, execution) let result = wait_for_query_opt(cancel_token, query_timeout, execution)
.await .await
.map(|result| truncate_result_with_max_rows(result, max_rows)); .map(|result| truncate_result_with_max_rows(result, max_rows))
.map(|mut result| {
result.execution_time_ms += lock_wait_ms;
result
});
drop(client); drop(client);
if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) { if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
state.remove_pool_by_key(pool_key).await; state.remove_pool_by_key(pool_key).await;
@@ -3152,17 +3169,18 @@ async fn execute_multi_sqlserver(
}; };
drop(connections); drop(connections);
let mut client_guard = match wait_for_value_opt(cancel_token.clone(), query_timeout, client.lock()).await { let (mut client_guard, lock_wait_ms) =
Ok(guard) => guard, match lock_shared_client_with_wait(&client, cancel_token.clone(), query_timeout).await {
Err(err) => { Ok(value) => value,
all_results.push(ExecuteMultiResult::execution_error_with_backend( Err(err) => {
error_query_result(err.clone()), all_results.push(ExecuteMultiResult::execution_error_with_backend(
None, error_query_result(err.clone()),
crate::backend_error::BackendError::from_legacy_backend(&err), None,
)); crate::backend_error::BackendError::from_legacy_backend(&err),
break; ));
} break;
}; }
};
if !sqlserver_pool_is_current(state, pool_key, &client).await { if !sqlserver_pool_is_current(state, pool_key, &client).await {
let error = "SQL Server connection was reset while waiting for the query lock; please retry.".to_string(); let error = "SQL Server connection was reset while waiting for the query lock; please retry.".to_string();
@@ -3185,7 +3203,10 @@ async fn execute_multi_sqlserver(
drop(client_guard); drop(client_guard);
match result { match result {
Ok(results) => all_results.extend(sqlserver_batch_results(results)), Ok(results) => all_results.extend(sqlserver_batch_results(results).into_iter().map(|mut item| {
item.result.execution_time_ms += lock_wait_ms;
item
})),
Err(e) => { Err(e) => {
let action = pool_error_action(Some(DatabaseType::SqlServer), &e); let action = pool_error_action(Some(DatabaseType::SqlServer), &e);
all_results.push(ExecuteMultiResult::execution_error_with_backend( all_results.push(ExecuteMultiResult::execution_error_with_backend(
@@ -6184,6 +6205,44 @@ for line in sys.stdin:
assert_eq!(serialized.get("server_message"), Some(&serde_json::Value::Bool(true))); assert_eq!(serialized.get("server_message"), Some(&serde_json::Value::Bool(true)));
} }
// Regression test for #6097: SQL Server queries share a single mutex-guarded
// connection (see PoolKind::SqlServer), so a fast query can queue for seconds
// behind another operation (e.g. autocomplete/schema metadata) holding that
// same connection. Before this fix, `execution_time_ms` was measured only
// from inside db::sqlserver's own timers, which start *after* the lock is
// acquired — so that queueing time was invisible to the user, producing a
// reported duration (e.g. "6-8ms") wildly smaller than what they actually
// waited (e.g. "10s"). `lock_shared_client_with_wait` is the exact helper
// both PoolKind::SqlServer call sites now use to fold that wait back in.
#[tokio::test]
async fn lock_shared_client_with_wait_reports_time_queued_behind_another_holder() {
let client = Arc::new(tokio::sync::Mutex::new(0u8));
let holder_guard = client.lock().await;
let waiter_client = client.clone();
let waiter = tokio::spawn(async move {
let (_guard, wait_ms) = lock_shared_client_with_wait(&waiter_client, None, None).await.unwrap();
wait_ms
});
// Give the spawned task a chance to actually start waiting on the lock
// before the holder releases it, so the measured wait is meaningful.
tokio::time::sleep(Duration::from_millis(150)).await;
drop(holder_guard);
let wait_ms = waiter.await.unwrap();
assert!(wait_ms >= 150, "expected queued wait time to be captured, got {wait_ms}ms");
}
#[tokio::test]
async fn lock_shared_client_with_wait_is_near_zero_when_uncontended() {
let client = Arc::new(tokio::sync::Mutex::new(0u8));
let (_guard, wait_ms) = lock_shared_client_with_wait(&client, None, None).await.unwrap();
assert!(wait_ms < 50, "expected an uncontended lock to report negligible wait, got {wait_ms}ms");
}
#[test] #[test]
fn query_execution_error_preserves_structured_catalog_identity() { fn query_execution_error_preserves_structured_catalog_identity() {
let error = QueryExecutionError::Agent(AgentCallError::Structured { let error = QueryExecutionError::Agent(AgentCallError::Structured {