@@ -351,13 +351,19 @@ public static Server get() {
351
351
* after the call returns.
352
352
*/
353
353
private static final ThreadLocal <Call > CurCall = new ThreadLocal <Call >();
354
-
354
+
355
+ private static final ThreadLocal <Long > CurCallStartNanos = new ThreadLocal <Long >();
356
+
355
357
/** @return Get the current call. */
356
358
@ VisibleForTesting
357
359
public static ThreadLocal <Call > getCurCall () {
358
360
return CurCall ;
359
361
}
360
-
362
+
363
+ public static ThreadLocal <Long > getCurCallStartNanos () {
364
+ return CurCallStartNanos ;
365
+ }
366
+
361
367
/**
362
368
* Returns the currently active RPC call's sequential ID number. A negative
363
369
* call ID indicates an invalid value, such as if there is no currently active
@@ -638,7 +644,8 @@ void updateMetrics(Call call, long processingStartTimeNanos, boolean connDropped
638
644
rpcMetrics .addRpcQueueTime (queueTime );
639
645
640
646
if (call .isResponseDeferred () || connDropped ) {
641
- // call was skipped; don't include it in processing metrics
647
+ // The call was skipped; don't include it in processing metrics.
648
+ // Will update metrics in method updateDeferredMetrics.
642
649
return ;
643
650
}
644
651
@@ -668,9 +675,41 @@ void updateMetrics(Call call, long processingStartTimeNanos, boolean connDropped
668
675
}
669
676
}
670
677
671
- void updateDeferredMetrics (String name , long processingTime ) {
678
+ /**
679
+ * Update rpc metrics for defered calls.
680
+ * @param call The Rpc Call
681
+ * @param name Rpc method name
682
+ * @param processingTime processing call in ms unit.
683
+ */
684
+ void updateDeferredMetrics (Call call , String name , long processingTime ) {
685
+ long completionTimeNanos = Time .monotonicNowNanos ();
686
+ long arrivalTimeNanos = call .timestampNanos ;
687
+
688
+ ProcessingDetails details = call .getProcessingDetails ();
689
+ long waitTime =
690
+ details .get (Timing .LOCKWAIT , rpcMetrics .getMetricsTimeUnit ());
691
+ long responseTime =
692
+ details .get (Timing .RESPONSE , rpcMetrics .getMetricsTimeUnit ());
693
+ rpcMetrics .addRpcLockWaitTime (waitTime );
694
+ rpcMetrics .addRpcProcessingTime (processingTime );
695
+ rpcMetrics .addRpcResponseTime (responseTime );
672
696
rpcMetrics .addDeferredRpcProcessingTime (processingTime );
673
697
rpcDetailedMetrics .addDeferredProcessingTime (name , processingTime );
698
+ // don't include lock wait for detailed metrics.
699
+ processingTime -= waitTime ;
700
+ rpcDetailedMetrics .addProcessingTime (name , processingTime );
701
+
702
+ // Overall processing time is from arrival to completion.
703
+ long overallProcessingTime = rpcMetrics .getMetricsTimeUnit ()
704
+ .convert (completionTimeNanos - arrivalTimeNanos , TimeUnit .NANOSECONDS );
705
+ rpcDetailedMetrics .addOverallProcessingTime (name , overallProcessingTime );
706
+ callQueue .addResponseTime (name , call , details );
707
+ if (isLogSlowRPC ()) {
708
+ logSlowRpcCalls (name , call , details );
709
+ }
710
+ if (details .getReturnStatus () == RpcStatusProto .SUCCESS ) {
711
+ rpcMetrics .incrRpcCallSuccesses ();
712
+ }
674
713
}
675
714
676
715
/**
@@ -1243,6 +1282,8 @@ public Void run() throws Exception {
1243
1282
}
1244
1283
1245
1284
long startNanos = Time .monotonicNowNanos ();
1285
+ // TODO ZHB 这个用来统计processing耗时
1286
+ CurCallStartNanos .set (startNanos );
1246
1287
Writable value = null ;
1247
1288
ResponseParams responseParams = new ResponseParams ();
1248
1289
@@ -1331,6 +1372,7 @@ void doResponse(Throwable t, RpcStatusProto status) throws IOException {
1331
1372
* Send a deferred response, ignoring errors.
1332
1373
*/
1333
1374
private void sendDeferedResponse () {
1375
+ long startNanos = Time .monotonicNowNanos ();
1334
1376
try {
1335
1377
connection .sendResponse (this );
1336
1378
} catch (Exception e ) {
@@ -1342,6 +1384,8 @@ private void sendDeferedResponse() {
1342
1384
.currentThread ().getName () + ", CallId="
1343
1385
+ callId + ", hostname=" + getHostAddress ());
1344
1386
}
1387
+ getProcessingDetails ().set (Timing .RESPONSE ,
1388
+ Time .monotonicNowNanos () - startNanos , TimeUnit .NANOSECONDS );
1345
1389
}
1346
1390
1347
1391
@ Override
@@ -3220,6 +3264,7 @@ public void run() {
3220
3264
}
3221
3265
} finally {
3222
3266
CurCall .set (null );
3267
+ CurCallStartNanos .set (null );
3223
3268
numInProcessHandler .decrementAndGet ();
3224
3269
IOUtils .cleanupWithLogger (LOG , traceScope );
3225
3270
if (call != null ) {
0 commit comments