1010import edu .umd .cs .findbugs .annotations .Nullable ;
1111import java .util .HashMap ;
1212import java .util .Iterator ;
13+ import java .util .List ;
1314import java .util .Map ;
1415import java .util .Objects ;
1516import java .util .Queue ;
@@ -83,17 +84,33 @@ public class NetworkMetrics {
8384 /**
8485 * Constructor of {@code NetworkMetrics}
8586 *
86- * @param metrics a reference to the metrics-system
87- * @param selfId this node's id
87+ * @param metrics a reference to the metrics-system
88+ * @param selfId this node's id
89+ * @param peers list of all peers to pre-create dynamic metrics
8890 * @throws IllegalArgumentException if {@code platform} is {@code null}
8991 */
90- public NetworkMetrics (@ NonNull final Metrics metrics , @ NonNull final NodeId selfId ) {
92+ public NetworkMetrics (@ NonNull final Metrics metrics , @ NonNull final NodeId selfId , final List < PeerInfo > peers ) {
9193 this .selfId = Objects .requireNonNull (selfId , "The selfId must not be null." );
9294 this .metrics = Objects .requireNonNull (metrics , "The metrics must not be null." );
9395
9496 avgPing = metrics .getOrCreate (AVG_PING_CONFIG );
9597 bytesPerSecondSent = metrics .getOrCreate (BYTES_PER_SECOND_SENT_CONFIG );
9698 avgConnsCreated = metrics .getOrCreate (AVG_CONNS_CREATED_CONFIG );
99+
100+ precreateDynamicMetrics (peers );
101+ }
102+
103+ /**
104+ * Out metric csv report needs all the metrics upfront to not get confused
105+ * @param peers list of all peers to pre-create dynamic metrics
106+ */
107+ private void precreateDynamicMetrics (final List <PeerInfo > peers ) {
108+ for (final PeerInfo peer : peers ) {
109+ final NodeId nodeId = peer .nodeId ();
110+ recordPingTime (nodeId , 0 );
111+ getDisconnectMetric (nodeId );
112+ getAverageBytesPerSecondSentMetric (nodeId );
113+ }
97114 }
98115
99116 /**
@@ -161,15 +178,7 @@ public void update() {
161178 totalBytesSent += bytesSent ;
162179 final NodeId otherId = conn .getOtherId ();
163180
164- avgBytePerSecSent
165- .computeIfAbsent (
166- otherId ,
167- nodeId -> metrics .getOrCreate (new SpeedometerMetric .Config (
168- BPSS_CATEGORY , String .format ("bytes_per_sec_sent_%02d" , nodeId .id ()))
169- .withDescription (
170- String .format ("bytes per second sent to node %02d" , nodeId .id ()))
171- .withFormat (FloatFormats .FORMAT_16_2 )))
172- .update (bytesSent );
181+ getAverageBytesPerSecondSentMetric (otherId ).update (bytesSent );
173182
174183 if (!conn .connected ()) {
175184 iterator .remove ();
@@ -180,6 +189,15 @@ public void update() {
180189 avgConnsCreated .update (connsCreated .sum ());
181190 }
182191
192+ private SpeedometerMetric getAverageBytesPerSecondSentMetric (final NodeId otherId ) {
193+ return avgBytePerSecSent .computeIfAbsent (
194+ otherId ,
195+ nodeId -> metrics .getOrCreate (new SpeedometerMetric .Config (
196+ BPSS_CATEGORY , String .format ("bytes_per_sec_sent_%02d" , nodeId .id ()))
197+ .withDescription (String .format ("bytes per second sent to node %02d" , nodeId .id ()))
198+ .withFormat (FloatFormats .FORMAT_16_2 )));
199+ }
200+
183201 /**
184202 * Returns the time for a round-trip message to each member (in milliseconds).
185203 * <p>
@@ -204,17 +222,19 @@ public void recordDisconnect(@NonNull final Connection connection) {
204222 final NodeId otherId = Objects .requireNonNull (connection , "connection must not be null." )
205223 .getOtherId ();
206224
207- disconnectFrequency
208- .computeIfAbsent (
209- otherId ,
210- nodeId -> new CountPerSecond (
211- metrics ,
212- new CountPerSecond .Config (
213- Metrics .PLATFORM_CATEGORY ,
214- String .format ("disconnects_per_sec_%02d" , nodeId .id ()))
215- .withDescription (String .format (
216- "number of disconnects per second from node %02d" , nodeId .id ()))
217- .withFormat (FloatFormats .FORMAT_10_0 )))
218- .count ();
225+ getDisconnectMetric (otherId ).count ();
226+ }
227+
228+ private CountPerSecond getDisconnectMetric (final NodeId otherId ) {
229+ return disconnectFrequency .computeIfAbsent (
230+ otherId ,
231+ nodeId -> new CountPerSecond (
232+ metrics ,
233+ new CountPerSecond .Config (
234+ Metrics .PLATFORM_CATEGORY ,
235+ String .format ("disconnects_per_sec_%02d" , nodeId .id ()))
236+ .withDescription (
237+ String .format ("number of disconnects per second from node %02d" , nodeId .id ()))
238+ .withFormat (FloatFormats .FORMAT_10_0 )));
219239 }
220240}
0 commit comments