Skip to content

Commit 4d9bfdd

Browse files
authored
tmpnet: Reuse dynamically-allocated API port across restarts (#2857)
1 parent 23e5417 commit 4d9bfdd

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

tests/fixture/tmpnet/network.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,23 @@ func (n *Network) Restart(ctx context.Context, w io.Writer) error {
441441
return err
442442
}
443443
for _, node := range n.Nodes {
444+
// Ensure the node reuses the same API port across restarts to ensure
445+
// consistent labeling of metrics. Otherwise prometheus's automatic
446+
// addition of the `instance` label (host:port) results in
447+
// segmentation of results for a given node every time the port
448+
// changes on restart. This segmentation causes graphs on the grafana
449+
// dashboards to display multiple series per graph for a given node,
450+
// one for each port that the node used.
451+
//
452+
// There is a non-zero chance of the port being allocatted to a
453+
// different process and the node subsequently being unable to start,
454+
// but the alternative is having to update the grafana dashboards
455+
// query-by-query to ensure that node metrics ignore the instance
456+
// label.
457+
if err := node.SaveAPIPort(); err != nil {
458+
return err
459+
}
460+
444461
if err := node.Stop(ctx); err != nil {
445462
return fmt.Errorf("failed to stop node %s: %w", node.NodeID, err)
446463
}

tests/fixture/tmpnet/node.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12+
"net"
1213
"net/http"
1314
"os"
1415
"path/filepath"
@@ -214,13 +215,14 @@ func (n *Node) Stop(ctx context.Context) error {
214215
// Sets networking configuration for the node.
215216
// Convenience method for setting networking flags.
216217
func (n *Node) SetNetworkingConfig(bootstrapIDs []string, bootstrapIPs []string) {
217-
var (
218-
// Use dynamic port allocation.
219-
httpPort uint16 = 0
220-
stakingPort uint16 = 0
221-
)
222-
n.Flags[config.HTTPPortKey] = httpPort
223-
n.Flags[config.StakingPortKey] = stakingPort
218+
if _, ok := n.Flags[config.HTTPPortKey]; !ok {
219+
// Default to dynamic port allocation
220+
n.Flags[config.HTTPPortKey] = 0
221+
}
222+
if _, ok := n.Flags[config.StakingPortKey]; !ok {
223+
// Default to dynamic port allocation
224+
n.Flags[config.StakingPortKey] = 0
225+
}
224226
n.Flags[config.BootstrapIDsKey] = strings.Join(bootstrapIDs, ",")
225227
n.Flags[config.BootstrapIPsKey] = strings.Join(bootstrapIPs, ",")
226228
}
@@ -348,3 +350,16 @@ func (n *Node) EnsureNodeID() error {
348350

349351
return nil
350352
}
353+
354+
// Saves the currently allocated API port to the node's configuration
355+
// for use across restarts. Reusing the port ensures consistent
356+
// labeling of metrics.
357+
func (n *Node) SaveAPIPort() error {
358+
hostPort := strings.TrimPrefix(n.URI, "http://")
359+
_, port, err := net.SplitHostPort(hostPort)
360+
if err != nil {
361+
return err
362+
}
363+
n.Flags[config.HTTPPortKey] = port
364+
return nil
365+
}

0 commit comments

Comments
 (0)