Skip to content

Commit 09a9530

Browse files
refactor: in-line network stats package (#4887)
Signed-off-by: Jonathan Oppenheimer <147infiniti@gmail.com> Signed-off-by: Jonathan Oppenheimer <jonathan.oppenheimer@avalabs.org> Co-authored-by: Austin Larson <78000745+alarso16@users.noreply.github.com>
1 parent 56ae1c1 commit 09a9530

File tree

4 files changed

+36
-104
lines changed

4 files changed

+36
-104
lines changed

graft/coreth/network/network.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"time"
1212

1313
"github.com/ava-labs/libevm/log"
14+
"github.com/ava-labs/libevm/metrics"
1415
"github.com/prometheus/client_golang/prometheus"
1516
"golang.org/x/sync/semaphore"
1617

1718
"github.com/ava-labs/avalanchego/codec"
18-
"github.com/ava-labs/avalanchego/graft/coreth/network/stats"
1919
"github.com/ava-labs/avalanchego/graft/evm/message"
2020
"github.com/ava-labs/avalanchego/ids"
2121
"github.com/ava-labs/avalanchego/network/p2p"
@@ -34,13 +34,17 @@ const (
3434
)
3535

3636
var (
37-
errAcquiringSemaphore = errors.New("error acquiring semaphore")
38-
errEmptyNodeID = errors.New("cannot send request to empty nodeID")
39-
errExpiredRequest = errors.New("expired request")
40-
errNoPeersFound = errors.New("no peers found matching version")
41-
_ Network = (*network)(nil)
42-
_ validators.Connector = (*network)(nil)
43-
_ common.AppHandler = (*network)(nil)
37+
_ Network = (*network)(nil)
38+
_ validators.Connector = (*network)(nil)
39+
_ common.AppHandler = (*network)(nil)
40+
41+
errAcquiringSemaphore = errors.New("error acquiring semaphore")
42+
errEmptyNodeID = errors.New("cannot send request to empty nodeID")
43+
errExpiredRequest = errors.New("expired request")
44+
errNoPeersFound = errors.New("no peers found matching version")
45+
46+
timeUntilDeadline = metrics.GetOrRegisterTimer("net_req_time_until_deadline", nil)
47+
droppedRequests = metrics.GetOrRegisterCounter("net_req_deadline_dropped", nil)
4448
)
4549

4650
// SyncedNetworkClient defines ability to send request / response through the Network
@@ -103,7 +107,6 @@ type network struct {
103107
codec codec.Manager // Codec used for parsing messages
104108
appRequestHandler message.RequestHandler // maps request type => handler
105109
peers *peerTracker // tracking of peers & bandwidth
106-
appStats stats.RequestHandlerStats // Provide request handler metrics
107110

108111
// Set to true when Shutdown is called, after which all operations on this
109112
// struct are no-ops.
@@ -150,7 +153,6 @@ func NewNetwork(
150153
sdkNetwork: p2pNetwork,
151154
appRequestHandler: message.NoopRequestHandler{},
152155
peers: NewPeerTracker(),
153-
appStats: stats.NewRequestHandlerStats(),
154156
p2pValidators: p2pValidators,
155157
}, nil
156158
}
@@ -280,8 +282,10 @@ func (n *network) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID u
280282
return n.sdkNetwork.AppRequest(ctx, nodeID, requestID, deadline, request)
281283
}
282284

283-
bufferedDeadline, err := calculateTimeUntilDeadline(deadline, n.appStats)
285+
bufferedDeadline, err := timeUntil(deadline)
284286
if err != nil {
287+
// Drop the request if we already missed the deadline to respond.
288+
droppedRequests.Inc(1)
285289
log.Debug("deadline to process AppRequest has expired, skipping", "nodeID", nodeID, "requestID", requestID, "err", err)
286290
return nil
287291
}
@@ -349,22 +353,20 @@ func (n *network) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, reque
349353
return handler.OnFailure()
350354
}
351355

352-
// calculateTimeUntilDeadline calculates the time until deadline and drops it if we missed he deadline to response.
356+
// timeUntil calculates the time until deadline and returns an error if the deadline has passed.
353357
// This function updates metrics for app requests.
354358
// This is called by [AppRequest].
355-
func calculateTimeUntilDeadline(deadline time.Time, stats stats.RequestHandlerStats) (time.Time, error) {
359+
func timeUntil(deadline time.Time) (time.Time, error) {
356360
// calculate how much time is left until the deadline
357361
timeTillDeadline := time.Until(deadline)
358-
stats.UpdateTimeUntilDeadline(timeTillDeadline)
362+
timeUntilDeadline.Update(timeTillDeadline)
359363

360364
// bufferedDeadline is half the time till actual deadline so that the message has a reasonable chance
361365
// of completing its processing and sending the response to the peer.
362366
bufferedDeadline := time.Now().Add(timeTillDeadline / 2)
363367

364368
// check if we have enough time to handle this request
365369
if time.Until(bufferedDeadline) < minRequestHandlingDuration {
366-
// Drop the request if we already missed the deadline to respond.
367-
stats.IncDeadlineDroppedRequest()
368370
return time.Time{}, errExpiredRequest
369371
}
370372

graft/coreth/network/stats/stats.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

graft/subnet-evm/network/network.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"time"
1212

1313
"github.com/ava-labs/libevm/log"
14+
"github.com/ava-labs/libevm/metrics"
1415
"github.com/prometheus/client_golang/prometheus"
1516
"golang.org/x/sync/semaphore"
1617

1718
"github.com/ava-labs/avalanchego/codec"
1819
"github.com/ava-labs/avalanchego/graft/evm/message"
19-
"github.com/ava-labs/avalanchego/graft/subnet-evm/network/stats"
2020
"github.com/ava-labs/avalanchego/ids"
2121
"github.com/ava-labs/avalanchego/network/p2p"
2222
"github.com/ava-labs/avalanchego/snow"
@@ -34,13 +34,17 @@ const (
3434
)
3535

3636
var (
37-
errAcquiringSemaphore = errors.New("error acquiring semaphore")
38-
errEmptyNodeID = errors.New("cannot send request to empty nodeID")
39-
errExpiredRequest = errors.New("expired request")
40-
errNoPeersFound = errors.New("no peers found matching version")
41-
_ Network = (*network)(nil)
42-
_ validators.Connector = (*network)(nil)
43-
_ common.AppHandler = (*network)(nil)
37+
_ Network = (*network)(nil)
38+
_ validators.Connector = (*network)(nil)
39+
_ common.AppHandler = (*network)(nil)
40+
41+
errAcquiringSemaphore = errors.New("error acquiring semaphore")
42+
errEmptyNodeID = errors.New("cannot send request to empty nodeID")
43+
errExpiredRequest = errors.New("expired request")
44+
errNoPeersFound = errors.New("no peers found matching version")
45+
46+
timeUntilDeadline = metrics.GetOrRegisterTimer("net_req_time_until_deadline", nil)
47+
droppedRequests = metrics.GetOrRegisterCounter("net_req_deadline_dropped", nil)
4448
)
4549

4650
// SyncedNetworkClient defines ability to send request / response through the Network
@@ -103,7 +107,6 @@ type network struct {
103107
codec codec.Manager // Codec used for parsing messages
104108
appRequestHandler message.RequestHandler // maps request type => handler
105109
peers *peerTracker // tracking of peers & bandwidth
106-
appStats stats.RequestHandlerStats // Provide request handler metrics
107110

108111
// Set to true when Shutdown is called, after which all operations on this
109112
// struct are no-ops.
@@ -150,7 +153,6 @@ func NewNetwork(
150153
sdkNetwork: p2pNetwork,
151154
appRequestHandler: message.NoopRequestHandler{},
152155
peers: NewPeerTracker(),
153-
appStats: stats.NewRequestHandlerStats(),
154156
p2pValidators: p2pValidators,
155157
}, nil
156158
}
@@ -280,8 +282,10 @@ func (n *network) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID u
280282
return n.sdkNetwork.AppRequest(ctx, nodeID, requestID, deadline, request)
281283
}
282284

283-
bufferedDeadline, err := calculateTimeUntilDeadline(deadline, n.appStats)
285+
bufferedDeadline, err := timeUntil(deadline)
284286
if err != nil {
287+
// Drop the request if we already missed the deadline to respond.
288+
droppedRequests.Inc(1)
285289
log.Debug("deadline to process AppRequest has expired, skipping", "nodeID", nodeID, "requestID", requestID, "err", err)
286290
return nil
287291
}
@@ -349,22 +353,20 @@ func (n *network) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, reque
349353
return handler.OnFailure()
350354
}
351355

352-
// calculateTimeUntilDeadline calculates the time until deadline and drops it if we missed he deadline to response.
356+
// timeUntil calculates the time until deadline and returns an error if the deadline has passed.
353357
// This function updates metrics for app requests.
354358
// This is called by [AppRequest].
355-
func calculateTimeUntilDeadline(deadline time.Time, stats stats.RequestHandlerStats) (time.Time, error) {
359+
func timeUntil(deadline time.Time) (time.Time, error) {
356360
// calculate how much time is left until the deadline
357361
timeTillDeadline := time.Until(deadline)
358-
stats.UpdateTimeUntilDeadline(timeTillDeadline)
362+
timeUntilDeadline.Update(timeTillDeadline)
359363

360364
// bufferedDeadline is half the time till actual deadline so that the message has a reasonable chance
361365
// of completing its processing and sending the response to the peer.
362366
bufferedDeadline := time.Now().Add(timeTillDeadline / 2)
363367

364368
// check if we have enough time to handle this request
365369
if time.Until(bufferedDeadline) < minRequestHandlingDuration {
366-
// Drop the request if we already missed the deadline to respond.
367-
stats.IncDeadlineDroppedRequest()
368370
return time.Time{}, errExpiredRequest
369371
}
370372

graft/subnet-evm/network/stats/stats.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)