Skip to content

Commit

Permalink
remove trackers from P2PConfig into Switch struct
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Jun 24, 2024
1 parent 26d7a97 commit 3d867ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
4 changes: 0 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,6 @@ type P2PConfig struct { //nolint: maligned
SameRegion bool `mapstructure:"same_region"`
MaxPercentPeersInSameRegion float64 `mapstructure:"max_percent_peers_in_same_region"`
RegionQueriesPerPeerQueryPeriod int `mapstructure:"region_queries_per_peer_query_period"`

MyRegion string `mapstructure:"my_region"`
CurrentNumOutboundPeersInOtherRegion int `mapstructure:"current_num_outbound_peers_in_other_region"`
CurrentNumInboundPeersInOtherRegion int `mapstructure:"current_num_inbound_peers_in_other_region"`
}

// DefaultP2PConfig returns a default configuration for the peer-to-peer layer
Expand Down
7 changes: 4 additions & 3 deletions p2p/pex/pex_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ func (r *Reactor) ensurePeersCommon(regionAware bool) {
toDialOutOfRegion := make(map[p2p.ID]*p2p.NetAddress)

swConfig := r.Switch.GetConfig()
currentOutboundInOtherRegion := swConfig.CurrentNumOutboundPeersInOtherRegion

currentOutboundInOtherRegion := r.Switch.CurrentNumOutboundPeersInOtherRegion
maxOutboundPeersInOtherRegion := swConfig.MaxNumOutboundPeers - int(swConfig.MaxPercentPeersInSameRegion*float64(swConfig.MaxNumOutboundPeers))

numToDialInOtherRegion := maxOutboundPeersInOtherRegion - currentOutboundInOtherRegion
Expand All @@ -507,7 +508,7 @@ func (r *Reactor) ensurePeersCommon(regionAware bool) {

// First iteration: Dial peers in the same region
for i := 0; i < maxAttempts && len(toDialInRegion) < numToDialInSameRegion+reserveSizeInRegion; i++ {
try := r.book.PickAddressWithRegion(newBias, swConfig.MyRegion)
try := r.book.PickAddressWithRegion(newBias, r.Switch.MyRegion)
if try == nil {
continue
}
Expand All @@ -522,7 +523,7 @@ func (r *Reactor) ensurePeersCommon(regionAware bool) {

// Second iteration: Dial peers in other regions
for i := 0; i < maxAttempts && len(toDialOutOfRegion) < numToDialInOtherRegion+reserveSizeOutOfRegion; i++ {
try := r.book.PickAddressNotInRegion(newBias, swConfig.MyRegion)
try := r.book.PickAddressNotInRegion(newBias, r.Switch.MyRegion)
if try == nil {
continue
}
Expand Down
31 changes: 18 additions & 13 deletions p2p/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ type Switch struct {

metrics *Metrics
mlc *metricsLabelCache

// Trackers used for peering in same region
MyRegion string
CurrentNumOutboundPeersInOtherRegion int
CurrentNumInboundPeersInOtherRegion int
}

// NetAddress returns the address the switch is listening on.
Expand Down Expand Up @@ -147,7 +152,7 @@ func NewSwitch(
if err != nil {
panic(fmt.Sprintf("failed to get own region: %v", err))
}
cfg.MyRegion = myRegion
sw.MyRegion = myRegion

// Make sure that the MaxPercentPeersInSameRegion does not exceed some hard coded value.
// If it does, replace it with the max
Expand Down Expand Up @@ -424,14 +429,14 @@ func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) {
sw.metrics.Peers.Add(float64(-1))
if peer.IsOutbound() {
if sw.config.SameRegion {
if sw.addrBook.GetAddressRegion(peer.SocketAddr()) != sw.config.MyRegion {
sw.config.CurrentNumOutboundPeersInOtherRegion--
if sw.addrBook.GetAddressRegion(peer.SocketAddr()) != sw.MyRegion {
sw.CurrentNumOutboundPeersInOtherRegion--
}
}
} else {
if sw.config.SameRegion {
if sw.addrBook.GetAddressRegion(peer.SocketAddr()) != sw.config.MyRegion {
sw.config.CurrentNumInboundPeersInOtherRegion--
if sw.addrBook.GetAddressRegion(peer.SocketAddr()) != sw.MyRegion {
sw.CurrentNumInboundPeersInOtherRegion--
}
}
}
Expand Down Expand Up @@ -764,23 +769,23 @@ func (sw *Switch) acceptRoutine() {

if sw.config.SameRegion {
// Note if the new peer is in the same region as us
fmt.Println("Checking if peer is same region. My region: ", sw.config.MyRegion, " Peer region: ", sw.addrBook.GetAddressRegion(p.SocketAddr()))
isSameRegion := sw.addrBook.GetAddressRegion(p.SocketAddr()) == sw.config.MyRegion
fmt.Println("Checking if peer is same region. My region: ", sw.MyRegion, " Peer region: ", sw.addrBook.GetAddressRegion(p.SocketAddr()))
isSameRegion := sw.addrBook.GetAddressRegion(p.SocketAddr()) == sw.MyRegion

if !isSameRegion {
// If this peer is not in our same region and we have no room to dial peers outside of our region, return error
// TODO check this formula
if p.IsOutbound() {
fmt.Println("peer is outbound acceptRoutine")
maxOutboundPeersInOtherRegion := sw.config.MaxNumOutboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumOutboundPeers))
if sw.config.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion {
if sw.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion {
sw.Logger.Error("exceeds max percent peers in same region")
continue
}
} else {
fmt.Println("peer is inbound acceptRoutine")
maxInboundPeersInOtherRegion := sw.config.MaxNumInboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumInboundPeers))
if sw.config.CurrentNumInboundPeersInOtherRegion+1 > maxInboundPeersInOtherRegion {
if sw.CurrentNumInboundPeersInOtherRegion+1 > maxInboundPeersInOtherRegion {
sw.Logger.Error("exceeds max percent peers in same region")
continue
}
Expand Down Expand Up @@ -939,12 +944,12 @@ func (sw *Switch) addPeer(p Peer) error {
}
sw.metrics.Peers.Add(float64(1))
if sw.config.SameRegion {
if p.IsOutbound() && sw.addrBook.GetAddressRegion(p.SocketAddr()) != sw.config.MyRegion {
if p.IsOutbound() && sw.addrBook.GetAddressRegion(p.SocketAddr()) != sw.MyRegion {
fmt.Println("adding peer outbound not in region: ", p.SocketAddr().IP.String())
sw.config.CurrentNumOutboundPeersInOtherRegion++
} else if !p.IsOutbound() && sw.addrBook.GetAddressRegion(p.SocketAddr()) != sw.config.MyRegion {
sw.CurrentNumOutboundPeersInOtherRegion++
} else if !p.IsOutbound() && sw.addrBook.GetAddressRegion(p.SocketAddr()) != sw.MyRegion {
fmt.Println("adding peer inbound not in region: ", p.SocketAddr().IP.String())
sw.config.CurrentNumInboundPeersInOtherRegion++
sw.CurrentNumInboundPeersInOtherRegion++
}
}

Expand Down

0 comments on commit 3d867ad

Please sign in to comment.