Skip to content

Commit 20a11df

Browse files
authored
fix: Precreate dynamic metrics (#22116)
Signed-off-by: Artur Biesiadowski <[email protected]>
1 parent d6728f7 commit 20a11df

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/gossip/SyncGossipModular.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public SyncGossipModular(
138138
final ProtocolConfig protocolConfig = platformContext.getConfiguration().getConfigData(ProtocolConfig.class);
139139

140140
final int rosterSize = peers.size() + 1;
141-
final SyncMetrics syncMetrics = new SyncMetrics(platformContext.getMetrics(), platformContext.getTime());
141+
final SyncMetrics syncMetrics = new SyncMetrics(platformContext.getMetrics(), platformContext.getTime(), peers);
142142

143143
if (protocolConfig.rpcGossip()) {
144144

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/metrics/SyncMetrics.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.swirlds.platform.gossip.shadowgraph.SyncResult;
2323
import com.swirlds.platform.gossip.shadowgraph.SyncTiming;
2424
import com.swirlds.platform.network.Connection;
25+
import com.swirlds.platform.network.PeerInfo;
2526
import com.swirlds.platform.stats.AverageAndMax;
2627
import com.swirlds.platform.stats.AverageAndMaxTimeStat;
2728
import com.swirlds.platform.stats.AverageStat;
@@ -31,6 +32,7 @@
3132
import edu.umd.cs.findbugs.annotations.NonNull;
3233
import edu.umd.cs.findbugs.annotations.Nullable;
3334
import java.time.temporal.ChronoUnit;
35+
import java.util.List;
3436
import java.util.Objects;
3537
import java.util.concurrent.ConcurrentHashMap;
3638
import org.hiero.consensus.model.hashgraph.EventWindow;
@@ -199,9 +201,10 @@ public class SyncMetrics {
199201
*
200202
* @param metrics a reference to the metrics-system
201203
* @param time time source for the system
204+
* @param peers list of all peers to pre-create dynamic metrics
202205
* @throws IllegalArgumentException if {@code metrics} is {@code null}
203206
*/
204-
public SyncMetrics(final Metrics metrics, final Time time) {
207+
public SyncMetrics(final Metrics metrics, final Time time, final List<PeerInfo> peers) {
205208
this.metrics = Objects.requireNonNull(metrics);
206209
this.time = Objects.requireNonNull(time);
207210
avgBytesPerSecSync = metrics.getOrCreate(AVG_BYTES_PER_SEC_SYNC_CONFIG);
@@ -315,6 +318,21 @@ public SyncMetrics(final Metrics metrics, final Time time) {
315318
"rpc_output_queue_poll_time",
316319
"amount of us spent sleeping waiting for poll to happen or timeout on rpc output queue",
317320
FORMAT_10_0);
321+
322+
precreateDynamicMetrics(peers);
323+
}
324+
325+
/**
326+
* Out metric csv report needs all the metrics upfront to not get confused
327+
* @param peers list of all peers to pre-create dynamic metrics
328+
*/
329+
private void precreateDynamicMetrics(final List<PeerInfo> peers) {
330+
for (final PeerInfo peer : peers) {
331+
final NodeId nodeId = peer.nodeId();
332+
rpcInputQueueSize(nodeId, 0);
333+
rpcOutputQueueSize(nodeId, 0);
334+
reportSyncPhase(nodeId, SyncPhase.OUTSIDE_OF_RPC);
335+
}
318336
}
319337

320338
/**

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/network/NetworkMetrics.java

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import edu.umd.cs.findbugs.annotations.Nullable;
1111
import java.util.HashMap;
1212
import java.util.Iterator;
13+
import java.util.List;
1314
import java.util.Map;
1415
import java.util.Objects;
1516
import 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
}

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/network/PeerCommunication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public PeerCommunication(
8282
this.selfPeer = Objects.requireNonNull(selfPeer);
8383
this.selfId = selfPeer.nodeId();
8484

85-
this.networkMetrics = new NetworkMetrics(platformContext.getMetrics(), selfPeer.nodeId());
85+
this.networkMetrics = new NetworkMetrics(platformContext.getMetrics(), selfPeer.nodeId(), peers);
8686
platformContext.getMetrics().addUpdater(networkMetrics::update);
8787

8888
this.topology = new StaticTopology(peers, selfPeer.nodeId());

0 commit comments

Comments
 (0)