Skip to content

Remove usage of timer.Timer in node #2441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions node/beacon_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@
package node

import (
"sync"
"sync/atomic"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/networking/router"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/version"
)

var _ router.Router = (*beaconManager)(nil)

type beaconManager struct {
router.Router
timer *timer.Timer
beacons validators.Manager
requiredConns int64
numConns int64
beacons validators.Manager
requiredConns int64
numConns int64
onSufficientlyConnected chan struct{}
onceOnSufficientlyConnected sync.Once
}

func (b *beaconManager) Connected(nodeID ids.NodeID, nodeVersion *version.Application, subnetID ids.ID) {
_, isBeacon := b.beacons.GetValidator(constants.PrimaryNetworkID, nodeID)
if isBeacon &&
constants.PrimaryNetworkID == subnetID &&
atomic.AddInt64(&b.numConns, 1) >= b.requiredConns {
b.timer.Cancel()
b.onceOnSufficientlyConnected.Do(func() {
close(b.onSufficientlyConnected)
})
}
b.Router.Connected(nodeID, nodeVersion, subnetID)
}
Expand Down
9 changes: 4 additions & 5 deletions node/beacon_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ava-labs/avalanchego/snow/networking/router"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/version"
)

Expand All @@ -41,10 +40,10 @@ func TestBeaconManager_DataRace(t *testing.T) {
mockRouter := router.NewMockRouter(ctrl)

b := beaconManager{
Router: mockRouter,
timer: timer.NewTimer(nil),
beacons: validatorSet,
requiredConns: numValidators,
Router: mockRouter,
beacons: validatorSet,
requiredConns: numValidators,
onSufficientlyConnected: make(chan struct{}),
}

// connect numValidators validators, each with a weight of 1
Expand Down
39 changes: 21 additions & 18 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import (
"github.com/ava-labs/avalanchego/utils/profiler"
"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/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/avm"
Expand Down Expand Up @@ -480,28 +479,32 @@ func (n *Node) initNetworking() error {
requiredConns := (3*numBootstrappers + 3) / 4

if requiredConns > 0 {
// Set a timer that will fire after a given timeout unless we connect
// to a sufficient portion of nodes. If the timeout fires, the node will
// shutdown.
timer := timer.NewTimer(func() {
// If the timeout fires and we're already shutting down, nothing to do.
if !n.shuttingDown.Get() {
onSufficientlyConnected := make(chan struct{})
consensusRouter = &beaconManager{
Router: consensusRouter,
beacons: n.bootstrappers,
requiredConns: int64(requiredConns),
onSufficientlyConnected: onSufficientlyConnected,
}

// Log a warning if we aren't able to connect to a sufficient portion of
// nodes.
go func() {
timer := time.NewTimer(n.Config.BootstrapBeaconConnectionTimeout)
defer timer.Stop()

select {
case <-timer.C:
if n.shuttingDown.Get() {
return
}
n.Log.Warn("failed to connect to bootstrap nodes",
zap.Stringer("bootstrappers", n.bootstrappers),
zap.Duration("duration", n.Config.BootstrapBeaconConnectionTimeout),
)
case <-onSufficientlyConnected:
}
})

go timer.Dispatch()
timer.SetTimeoutIn(n.Config.BootstrapBeaconConnectionTimeout)

consensusRouter = &beaconManager{
Router: consensusRouter,
timer: timer,
beacons: n.bootstrappers,
requiredConns: int64(requiredConns),
}
}()
}

// initialize gossip tracker
Expand Down