1515use std:: fmt;
1616use std:: fmt:: Display ;
1717use std:: time:: Duration ;
18+ use std:: time:: Instant ;
1819
1920use anyerror:: AnyError ;
2021use databend_common_base:: future:: TimedFutureExt ;
2122use databend_common_meta_types:: ConnectionError ;
2223use databend_common_meta_types:: MetaClientError ;
2324use databend_common_meta_types:: MetaNetworkError ;
24- use display_more:: DisplayOptionExt ;
2525use log:: debug;
2626use log:: info;
2727use log:: warn;
@@ -70,9 +70,9 @@ pub(crate) struct RpcHandler<'a> {
7070 /// Used to validate compatibility before executing RPCs.
7171 pub ( crate ) required_feature : FeatureSpec ,
7272
73- /// Currently established connection to the meta-service, if any .
74- /// This is populated after a successful connection is made and validated .
75- pub ( crate ) established_client : Option < EstablishedClient > ,
73+ /// Currently established connection and its start time .
74+ /// The client and start time are paired together to ensure consistent timing tracking .
75+ pub ( crate ) established_client : Option < ( EstablishedClient , Instant ) > ,
7676}
7777
7878impl < ' a > RpcHandler < ' a > {
@@ -115,9 +115,9 @@ impl<'a> RpcHandler<'a> {
115115
116116 client. ensure_feature_spec ( & self . required_feature ) ?;
117117
118- self . established_client = Some ( client) ;
118+ self . established_client = Some ( ( client, Instant :: now ( ) ) ) ;
119119
120- Ok ( self . established_client . as_mut ( ) . unwrap ( ) )
120+ Ok ( & mut self . established_client . as_mut ( ) . unwrap ( ) . 0 )
121121 }
122122
123123 /// Processes an RPC response and determines the appropriate action.
@@ -132,6 +132,9 @@ impl<'a> RpcHandler<'a> {
132132 /// - Record the failure for error reporting
133133 /// - Switch to the next endpoint for retry
134134 ///
135+ /// The established client is consumed by this method to prevent accidental reuse.
136+ /// For retry attempts, a new client must be established via `new_established_client()`.
137+ ///
135138 /// # Returns
136139 /// - `Ok(ResponseAction::ShouldRetry)` for retryable errors
137140 /// - `Ok(ResponseAction::Success(response))` for successful responses
@@ -145,44 +148,60 @@ impl<'a> RpcHandler<'a> {
145148 R : fmt:: Debug ,
146149 T : fmt:: Debug ,
147150 {
151+ let ( established_client, start_time) = self
152+ . established_client
153+ . take ( )
154+ . expect ( "established client should be set before processing response" ) ;
155+
156+ let elapsed = start_time. elapsed ( ) ;
157+
148158 debug ! (
149- "MetaGrpcClient::{} {}-th try: result: {:?}" ,
159+ "MetaGrpcClient::{} {}-th try: elapsed: {:?}; with {}; result: {:?}" ,
150160 self . required_feature. 0 ,
151161 self . rpc_failures. len( ) ,
162+ elapsed,
163+ established_client,
152164 result
153165 ) ;
154166
155167 let status = match result {
156168 Err ( e) => e,
157- Ok ( x) => return Ok ( ResponseAction :: Success ( x) ) ,
169+ Ok ( x) => {
170+ // Log slow requests even on success
171+ if elapsed > default_timing_threshold ( ) {
172+ warn ! (
173+ "MetaGrpcClient::{}: done slowly: elapsed: {:?}; with {}; request: {:?}" ,
174+ self . required_feature. 0 , elapsed, established_client, request
175+ ) ;
176+ }
177+ return Ok ( ResponseAction :: Success ( x) ) ;
178+ }
158179 } ;
159180
160181 if is_status_retryable ( & status) {
182+ let client_display = established_client. to_string ( ) ;
183+
161184 warn ! (
162- "MetaGrpcClient::{} retryable error: {:?}; with {}: request: {:?}" ,
185+ "MetaGrpcClient::{} retryable error: elapsed: {:?}; error: {:?}; with {}; request: {:?}" ,
163186 self . required_feature. 0 ,
187+ elapsed,
164188 status,
165- self . established_client . display ( ) ,
189+ client_display ,
166190 request
167191 ) ;
168192
169- let established_client = self
170- . established_client
171- . as_mut ( )
172- . expect ( "established client should be set before processing response" ) ;
173-
174- self . rpc_failures
175- . push ( ( established_client. to_string ( ) , status) ) ;
193+ self . rpc_failures . push ( ( client_display, status) ) ;
176194
177195 established_client. rotate_failing_target ( ) ;
178196
179197 Ok ( ResponseAction :: ShouldRetry )
180198 } else {
181199 warn ! (
182- "MetaGrpcClient::{} non-retryable error: {:?}; with {}: request: {:?}" ,
200+ "MetaGrpcClient::{} non-retryable error: elapsed: {:?}; error: {:?}; with {}; request: {:?}" ,
183201 self . required_feature. 0 ,
202+ elapsed,
184203 status,
185- self . established_client. display ( ) ,
204+ established_client,
186205 request
187206 ) ;
188207
0 commit comments