Skip to content
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

[backport] p2p: use decaying tags for fetch peers (#5564) #5568

Merged
merged 1 commit into from
Feb 14, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ See [RELEASE](./RELEASE.md) for workflow instructions.

### Improvements

* [#5564](https://github.com/spacemeshos/go-spacemesh/pull/5564) Use decaying tags for fetch peers. This prevents
libp2p's Connection Manager from breaking sync.
* [#5548](https://github.com/spacemeshos/go-spacemesh/pull/5548) Disable mesh aggremenet sync protocol.

It reduces number of requests for historical activation ids.
Expand Down
8 changes: 8 additions & 0 deletions fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type Config struct {
ServersConfig map[string]ServerConfig `mapstructure:"servers"`
PeersRateThreshold float64 `mapstructure:"peers-rate-threshold"`
GetAtxsConcurrency int64 // The maximum number of concurrent requests to get ATXs.
DecayingTag server.DecayingTagSpec `mapstructure:"decaying-tag"`
}

func (c Config) getServerConfig(protocol string) ServerConfig {
Expand Down Expand Up @@ -149,6 +150,12 @@ func DefaultConfig() Config {
},
PeersRateThreshold: 0.02,
GetAtxsConcurrency: 100,
DecayingTag: server.DecayingTagSpec{
Interval: time.Minute,
Inc: 1000,
Dec: 1000,
Cap: 10000,
},
}
}

Expand Down Expand Up @@ -291,6 +298,7 @@ func (f *Fetch) registerServer(
server.WithTimeout(f.cfg.RequestTimeout),
server.WithHardTimeout(f.cfg.RequestHardTimeout),
server.WithLog(f.logger),
server.WithDecayingTag(f.cfg.DecayingTag),
}
if f.cfg.EnableServerMetrics {
opts = append(opts, server.WithMetrics())
Expand Down
2 changes: 2 additions & 0 deletions p2p/server/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"time"

"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
Expand All @@ -17,6 +18,7 @@ type Host interface {
SetStreamHandler(protocol.ID, network.StreamHandler)
NewStream(context.Context, peer.ID, ...protocol.ID) (network.Stream, error)
Network() network.Network
ConnManager() connmgr.ConnManager
}

type peerStream interface {
Expand Down
39 changes: 39 additions & 0 deletions p2p/server/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions p2p/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"io"
"time"

"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
Expand All @@ -20,6 +21,13 @@
"github.com/spacemeshos/go-spacemesh/log"
)

type DecayingTagSpec struct {
Interval time.Duration `mapstructure:"interval"`
Inc int `mapstructure:"inc"`
Dec int `mapstructure:"dec"`
Cap int `mapstructure:"cap"`
}

// ErrNotConnected is returned when peer is not connected.
var ErrNotConnected = errors.New("peer is not connected")

Expand Down Expand Up @@ -88,6 +96,12 @@
}
}

func WithDecayingTag(tag DecayingTagSpec) Opt {
return func(s *Server) {
s.decayingTagSpec = &tag
}
}

// Handler is the handler to be defined by the application.
type Handler func(context.Context, []byte) ([]byte, error)

Expand All @@ -110,6 +124,8 @@
queueSize int
requestsPerInterval int
interval time.Duration
decayingTagSpec *DecayingTagSpec
decayingTag connmgr.DecayingTag

metrics *tracker // metrics can be nil

Expand All @@ -133,6 +149,23 @@
for _, opt := range opts {
opt(srv)
}

if srv.decayingTagSpec != nil {
decayer, supported := connmgr.SupportsDecay(h.ConnManager())
if supported {
tag, err := decayer.RegisterDecayingTag(
"server:"+proto,
srv.decayingTagSpec.Interval,
connmgr.DecayFixed(srv.decayingTagSpec.Dec),
connmgr.BumpSumBounded(0, srv.decayingTagSpec.Cap))
if err != nil {
srv.logger.Error("error registering decaying tag", log.Err(err))

Check warning on line 162 in p2p/server/server.go

View check run for this annotation

Codecov / codecov/patch

p2p/server/server.go#L162

Added line #L162 was not covered by tests
} else {
srv.decayingTag = tag
}
}
}

return srv
}

Expand Down Expand Up @@ -176,6 +209,9 @@
return nil
}
eg.Go(func() error {
if s.decayingTag != nil {
s.decayingTag.Bump(req.stream.Conn().RemotePeer(), s.decayingTagSpec.Inc)
}

Check warning on line 214 in p2p/server/server.go

View check run for this annotation

Codecov / codecov/patch

p2p/server/server.go#L213-L214

Added lines #L213 - L214 were not covered by tests
ok := s.queueHandler(ctx, req.stream)
if s.metrics != nil {
s.metrics.serverLatency.Observe(time.Since(req.received).Seconds())
Expand Down
Loading