Skip to content

Commit

Permalink
Add utils.Err helper (#2212)
Browse files Browse the repository at this point in the history
Signed-off-by: Dhruba Basu <7675102+dhrubabasu@users.noreply.github.com>
  • Loading branch information
dhrubabasu committed Oct 27, 2023
1 parent a4cee60 commit b83af9b
Show file tree
Hide file tree
Showing 50 changed files with 181 additions and 267 deletions.
7 changes: 3 additions & 4 deletions api/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/utils"
)

type metrics struct {
Expand Down Expand Up @@ -46,13 +46,12 @@ func newMetrics(namespace string, registerer prometheus.Registerer) (*metrics, e
),
}

errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
registerer.Register(m.numProcessing),
registerer.Register(m.numCalls),
registerer.Register(m.totalDuration),
)
return m, errs.Err
return m, err
}

func (m *metrics) wrapHandler(chainName string, handler http.Handler) http.Handler {
Expand Down
7 changes: 3 additions & 4 deletions database/leveldb/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/syndtr/goleveldb/leveldb"

"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/utils"
)

var levelLabels = []string{"level"}
Expand Down Expand Up @@ -180,8 +180,7 @@ func newMetrics(namespace string, reg prometheus.Registerer) (metrics, error) {
currentStats: &leveldb.DBStats{},
}

errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
reg.Register(m.writesDelayedCount),
reg.Register(m.writesDelayedDuration),
reg.Register(m.writeIsDelayed),
Expand All @@ -206,7 +205,7 @@ func newMetrics(namespace string, reg prometheus.Registerer) (metrics, error) {
reg.Register(m.nonLevel0Compactions),
reg.Register(m.seekCompactions),
)
return m, errs.Err
return m, err
}

func (db *Database) updateMetrics() error {
Expand Down
31 changes: 14 additions & 17 deletions genesis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

var (
Expand Down Expand Up @@ -172,30 +171,28 @@ func init() {
unparsedFujiConfig := UnparsedConfig{}
unparsedLocalConfig := UnparsedConfig{}

errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
json.Unmarshal(mainnetGenesisConfigJSON, &unparsedMainnetConfig),
json.Unmarshal(fujiGenesisConfigJSON, &unparsedFujiConfig),
json.Unmarshal(localGenesisConfigJSON, &unparsedLocalConfig),
)
if errs.Errored() {
panic(errs.Err)
if err != nil {
panic(err)
}

mainnetConfig, err := unparsedMainnetConfig.Parse()
errs.Add(err)
MainnetConfig = mainnetConfig

fujiConfig, err := unparsedFujiConfig.Parse()
errs.Add(err)
FujiConfig = fujiConfig
MainnetConfig, err = unparsedMainnetConfig.Parse()
if err != nil {
panic(err)
}

localConfig, err := unparsedLocalConfig.Parse()
errs.Add(err)
LocalConfig = localConfig
FujiConfig, err = unparsedFujiConfig.Parse()
if err != nil {
panic(err)
}

if errs.Errored() {
panic(errs.Err)
LocalConfig, err = unparsedLocalConfig.Parse()
if err != nil {
panic(err)
}
}

Expand Down
6 changes: 2 additions & 4 deletions indexer/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

// Maximum number of containers IDs that can be fetched at a time in a call to
Expand Down Expand Up @@ -114,14 +114,12 @@ func newIndex(

// Close this index
func (i *index) Close() error {
errs := wrappers.Errs{}
errs.Add(
return utils.Err(
i.indexToContainer.Close(),
i.containerToIndex.Close(),
i.vDB.Close(),
i.baseDB.Close(),
)
return errs.Err
}

// Index that the given transaction is accepted
Expand Down
12 changes: 6 additions & 6 deletions ipcs/eventsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/ipcs/socket"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
Expand Down Expand Up @@ -133,12 +134,10 @@ func newEventIPCSocket(
url: url,
socket: socket.NewSocket(url, ctx.log),
unregisterFn: func() error {
errs := wrappers.Errs{}
errs.Add(
return utils.Err(
snowmanAcceptorGroup.DeregisterAcceptor(chainID, ipcName),
avalancheAcceptorGroup.DeregisterAcceptor(chainID, ipcName),
)
return errs.Err
},
}

Expand Down Expand Up @@ -175,9 +174,10 @@ func (eis *eventSocket) Accept(_ *snow.ConsensusContext, _ ids.ID, container []b
// stop unregisters the event handler and closes the eventSocket
func (eis *eventSocket) stop() error {
eis.log.Info("closing Chain IPC")
errs := wrappers.Errs{}
errs.Add(eis.unregisterFn(), eis.socket.Close())
return errs.Err
return utils.Err(
eis.unregisterFn(),
eis.socket.Close(),
)
}

// URL returns the URL of the socket
Expand Down
7 changes: 3 additions & 4 deletions network/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/peer"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

type metrics struct {
Expand Down Expand Up @@ -147,8 +147,7 @@ func newMetrics(namespace string, registerer prometheus.Registerer, initialSubne
peerConnectedStartTimes: make(map[ids.NodeID]float64),
}

errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
registerer.Register(m.numTracked),
registerer.Register(m.numPeers),
registerer.Register(m.numSubnetPeers),
Expand Down Expand Up @@ -182,7 +181,7 @@ func newMetrics(namespace string, registerer prometheus.Registerer, initialSubne
m.nodeSubnetUptimeRewardingStake.WithLabelValues(subnetIDStr).Set(0)
}

return m, errs.Err
return m, err
}

func (m *metrics) markConnected(peer peer.Peer) {
Expand Down
2 changes: 1 addition & 1 deletion network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,6 @@ func (n *network) Peers(peerID ids.NodeID) ([]ips.ClaimedIPPort, error) {
func (n *network) Dispatch() error {
go n.runTimers() // Periodically perform operations
go n.inboundConnUpgradeThrottler.Dispatch()
errs := wrappers.Errs{}
for { // Continuously accept new connections
if n.onCloseCtx.Err() != nil {
break
Expand Down Expand Up @@ -798,6 +797,7 @@ func (n *network) Dispatch() error {
connected := n.connectedPeers.Sample(n.connectedPeers.Len(), peer.NoPrecondition)
n.peersLock.RUnlock()

errs := wrappers.Errs{}
for _, peer := range append(connecting, connected...) {
errs.Add(peer.AwaitClosed(context.TODO()))
}
Expand Down
8 changes: 3 additions & 5 deletions network/p2p/gossip/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/p2p"
"github.com/ava-labs/avalanchego/proto/pb/sdk"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

var (
Expand Down Expand Up @@ -83,13 +83,11 @@ func NewPullGossiper[T any, U GossipableAny[T]](
}),
}

errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
metrics.Register(p.receivedN),
metrics.Register(p.receivedBytes),
)

return p, errs.Err
return p, err
}

type PullGossiper[T any, U GossipableAny[T]] struct {
Expand Down
8 changes: 3 additions & 5 deletions network/p2p/gossip/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/p2p"
"github.com/ava-labs/avalanchego/proto/pb/sdk"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/utils"
)

var (
Expand Down Expand Up @@ -52,13 +52,11 @@ func NewHandler[T Gossipable](
}),
}

errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
metrics.Register(h.sentN),
metrics.Register(h.sentBytes),
)

return h, errs.Err
return h, err
}

type Handler[T Gossipable] struct {
Expand Down
8 changes: 3 additions & 5 deletions network/peer/gossip_tracker_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package peer
import (
"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/utils"
)

type gossipTrackerMetrics struct {
Expand All @@ -32,11 +32,9 @@ func newGossipTrackerMetrics(registerer prometheus.Registerer, namespace string)
),
}

errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
registerer.Register(m.trackedPeersSize),
registerer.Register(m.validatorsSize),
)

return m, errs.Err
return m, err
}
7 changes: 3 additions & 4 deletions network/throttling/inbound_resource_throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/networking/tracker"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

const epsilon = time.Millisecond
Expand Down Expand Up @@ -80,13 +80,12 @@ func newSystemThrottlerMetrics(namespace string, reg prometheus.Registerer) (*sy
Help: "Number of nodes we're waiting to read a message from because their usage is too high",
}),
}
errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
reg.Register(m.totalWaits),
reg.Register(m.totalNoWaits),
reg.Register(m.awaitingAcquire),
)
return m, errs.Err
return m, err
}

func NewSystemThrottler(
Expand Down
6 changes: 2 additions & 4 deletions network/throttling/outbound_msg_throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/message"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/wrappers"
)

var (
Expand Down Expand Up @@ -204,15 +204,13 @@ func (m *outboundMsgThrottlerMetrics) initialize(namespace string, registerer pr
Name: "throttler_outbound_awaiting_release",
Help: "Number of messages waiting to be sent",
})
errs := wrappers.Errs{}
errs.Add(
return utils.Err(
registerer.Register(m.acquireSuccesses),
registerer.Register(m.acquireFailures),
registerer.Register(m.remainingAtLargeBytes),
registerer.Register(m.remainingVdrBytes),
registerer.Register(m.awaitingRelease),
)
return errs.Err
}

func NewNoOutboundThrottler() OutboundMsgThrottler {
Expand Down
8 changes: 3 additions & 5 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ import (
"github.com/ava-labs/avalanchego/utils/resource"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/avm"
Expand Down Expand Up @@ -894,8 +893,7 @@ func (n *Node) initVMs() error {
})

// Register the VMs that Avalanche supports
errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
vmRegisterer.Register(context.TODO(), constants.PlatformVMID, &platformvm.Factory{
Config: platformconfig.Config{
Chains: n.chainManager,
Expand Down Expand Up @@ -940,8 +938,8 @@ func (n *Node) initVMs() error {
n.VMManager.RegisterFactory(context.TODO(), nftfx.ID, &nftfx.Factory{}),
n.VMManager.RegisterFactory(context.TODO(), propertyfx.ID, &propertyfx.Factory{}),
)
if errs.Errored() {
return errs.Err
if err != nil {
return err
}

// initialize vm runtime manager
Expand Down
7 changes: 3 additions & 4 deletions snow/consensus/metrics/polls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package metrics
import (
"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/utils"
)

var _ Polls = (*polls)(nil)
Expand Down Expand Up @@ -38,12 +38,11 @@ func NewPolls(namespace string, reg prometheus.Registerer) (Polls, error) {
Help: "Number of failed polls",
}),
}
errs := wrappers.Errs{}
errs.Add(
err := utils.Err(
reg.Register(p.numFailedPolls),
reg.Register(p.numSuccessfulPolls),
)
return p, errs.Err
return p, err
}

func (p *polls) Failed() {
Expand Down

0 comments on commit b83af9b

Please sign in to comment.