1616
1717package io .grpc .netty ;
1818
19+ import com .google .common .annotations .VisibleForTesting ;
1920import io .grpc .DoubleHistogramMetricInstrument ;
2021import io .grpc .InternalTcpMetrics ;
2122import io .grpc .LongCounterMetricInstrument ;
2930import java .util .Arrays ;
3031import java .util .Collections ;
3132import java .util .List ;
32- import java .util .concurrent .RejectedExecutionException ;
3333import java .util .concurrent .ThreadLocalRandom ;
3434import java .util .concurrent .TimeUnit ;
3535import java .util .logging .Level ;
@@ -58,9 +58,10 @@ final class TcpMetrics {
5858 }
5959 log .log (Level .INFO , "Epoll available during static init of TcpMetrics:"
6060 + "{0}" , epollAvailable );
61- DEFAULT_METRICS = new Metrics (epollAvailable );
61+ DEFAULT_METRICS = new Metrics ();
6262 }
6363
64+ @ VisibleForTesting
6465 static Metrics getDefaultMetrics () {
6566 return DEFAULT_METRICS ;
6667 }
@@ -72,19 +73,12 @@ static final class Metrics {
7273 final LongCounterMetricInstrument recurringRetransmits ;
7374 final DoubleHistogramMetricInstrument minRtt ;
7475
75- Metrics (boolean epollAvailable ) {
76+ Metrics () {
7677 connectionsCreated = InternalTcpMetrics .CONNECTIONS_CREATED_INSTRUMENT ;
7778 connectionCount = InternalTcpMetrics .CONNECTION_COUNT_INSTRUMENT ;
78-
79- if (epollAvailable ) {
80- packetsRetransmitted = InternalTcpMetrics .PACKETS_RETRANSMITTED_INSTRUMENT ;
81- recurringRetransmits = InternalTcpMetrics .RECURRING_RETRANSMITS_INSTRUMENT ;
82- minRtt = InternalTcpMetrics .MIN_RTT_INSTRUMENT ;
83- } else {
84- packetsRetransmitted = null ;
85- recurringRetransmits = null ;
86- minRtt = null ;
87- }
79+ packetsRetransmitted = InternalTcpMetrics .PACKETS_RETRANSMITTED_INSTRUMENT ;
80+ recurringRetransmits = InternalTcpMetrics .RECURRING_RETRANSMITS_INSTRUMENT ;
81+ minRtt = InternalTcpMetrics .MIN_RTT_INSTRUMENT ;
8882 }
8983 }
9084
@@ -171,17 +165,12 @@ private void scheduleNextReport(final Channel channel, boolean isInitial) {
171165 : 0.9 + ThreadLocalRandom .current ().nextDouble () * 0.2 ; // 90% to 110%
172166 long rearmingDelay = (long ) (RECORD_INTERVAL_MILLIS * jitter );
173167
174- try {
175- reportTimer = channel .eventLoop ().schedule (() -> {
176- if (channel .isActive ()) {
177- Tracker .this .recordTcpInfo (channel , false );
178- scheduleNextReport (channel , false ); // Re-arm
179- }
180- }, rearmingDelay , TimeUnit .MILLISECONDS );
181- } catch (RejectedExecutionException e ) {
182- log .log (Level .FINE , "Failed to schedule next TCP metrics report" , e );
183- // The event loop is likely shutting down. We can safely ignore this.
184- }
168+ reportTimer = channel .eventLoop ().schedule (() -> {
169+ if (channel .isActive ()) {
170+ Tracker .this .recordTcpInfo (channel , false );
171+ scheduleNextReport (channel , false ); // Re-arm
172+ }
173+ }, rearmingDelay , TimeUnit .MILLISECONDS );
185174 }
186175
187176 void channelInactive (Channel channel ) {
@@ -226,25 +215,19 @@ private void recordTcpInfo(Channel channel, boolean isClose) {
226215 return ;
227216 }
228217
229- if (metrics .packetsRetransmitted != null ) {
230- long deltaTotal = totalRetrans - lastTotalRetrans ;
231- if (deltaTotal > 0 ) {
232- metricRecorder .addLongCounter (metrics .packetsRetransmitted , deltaTotal ,
233- Collections .emptyList (), labelValues );
234- lastTotalRetrans = totalRetrans ;
235- }
236- }
237- if (metrics .recurringRetransmits != null && isClose ) {
238- if (retransmits > 0 ) {
239- metricRecorder .addLongCounter (metrics .recurringRetransmits , retransmits ,
240- Collections .emptyList (), labelValues );
241- }
218+ long deltaTotal = totalRetrans - lastTotalRetrans ;
219+ if (deltaTotal > 0 ) {
220+ metricRecorder .addLongCounter (metrics .packetsRetransmitted , deltaTotal ,
221+ Collections .emptyList (), labelValues );
222+ lastTotalRetrans = totalRetrans ;
242223 }
243- if (metrics .minRtt != null ) {
244- metricRecorder .recordDoubleHistogram (metrics .minRtt ,
245- rtt / 1000000.0 , // Convert microseconds to seconds
224+ if (isClose && retransmits > 0 ) {
225+ metricRecorder .addLongCounter (metrics .recurringRetransmits , retransmits ,
246226 Collections .emptyList (), labelValues );
247227 }
228+ metricRecorder .recordDoubleHistogram (metrics .minRtt ,
229+ rtt / 1000000.0 , // Convert microseconds to seconds
230+ Collections .emptyList (), labelValues );
248231 }
249232 }
250233
0 commit comments