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

p2p: add a per-message type send and receive metric #9622

Merged
merged 31 commits into from
Oct 27, 2022

Conversation

williambanfield
Copy link
Contributor

@williambanfield williambanfield commented Oct 24, 2022

This pull request has one primary goal: to add a new metric for measuring the p2p bytes sent and received by message type.

To do this, parts of the p2p.Envelope from v0.35.x were resurrected. This was done to change the p2p.Send and associated methods along with the reactor.Receive method from taking a []byte as an argument to taking an object with an unserialized proto.Message as an argument. Having the actual proto.Message in hand in the p2p code allows for the type of the message to be determined. Otherwise, the p2p code cannot know at all what the type of the message being sent is and the only alternative is to litter p2p message size calculations all over the code, which seems much more awkward.

The p2p.Envelope resurrection required a few steps:

proto.Message must be added to the ChannelDescriptor so that the p2p code knows what to unmarshal into when receiving a message.
All Send and Receive methods must be changed to use the Envelope type. This means that the reactors pass a proto message that has not yet been marshaled to the p2p layer and the p2p layer similarly calls the Receive method with an unmarshaled proto. All code that marshals and unmarshals the protos in the reactors was removed. Finally, many of the reactors have a wrapper type with a Sum field that can contain one of any of the p2p messages that reactor can receive. To actually be able to determine what kind of underlying message was being sent and received for the purposes of the metric, the v0.35 Wrapper was resurrected, albeit with a minor change: instead of Wrap being implemented on the wrapper type, it's implemented on each of the Sum types so that they each know how to Wrap themselves.

With all of these changes in place, the only remaining step was to put the metric into both the send and receive code.

closes: #9599

PR checklist

  • Tests written/updated, or no tests needed
  • CHANGELOG_PENDING.md updated, or no changelog entry needed
  • Updated relevant documentation (docs/) and code comments, or no
    documentation updates needed

@williambanfield williambanfield changed the title Wb/final metric impl p2p: add a per-message type metric Oct 24, 2022
@williambanfield williambanfield changed the title p2p: add a per-message type metric p2p: add a per-message type send and receive metric Oct 24, 2022
rs, err := tmmath.SafeConvertUint8(int64(msg.NewRoundStep.Step))
switch msg := p.(type) {
case *tmcons.NewRoundStep:
rs, err := tmmath.SafeConvertUint8(int64(msg.Step))

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
p2p/switch.go Outdated
@@ -3,12 +3,13 @@
import (
"fmt"
"math"
"reflect"

Check failure

Code scanning / gosec

Blocklisted import runtime

Blocklisted import reflect
@@ -1,6 +1,11 @@
package p2p

import (
"fmt"
"reflect"

Check failure

Code scanning / gosec

Blocklisted import runtime

Blocklisted import reflect
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't at all see why we're linting for this. Sometimes reflect is useful to use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I couldn't find what the reasoning behind blocking this import is

func (mp *Peer) FlushStop() { mp.Stop() } //nolint:errcheck //ignore error
func (mp *Peer) TrySend(chID byte, msgBytes []byte) bool { return true }
func (mp *Peer) Send(chID byte, msgBytes []byte) bool { return true }
func (mp *Peer) FlushStop() { mp.Stop() } //nolint:errcheck //ignore error

Check warning

Code scanning / gosec

Errors unhandled.

Errors unhandled.
@williambanfield williambanfield marked this pull request as ready for review October 24, 2022 20:43
@williambanfield williambanfield requested a review from a team October 24, 2022 20:43
Copy link
Contributor

@cmwaters cmwaters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems all in order

p2p/base_reactor.go Outdated Show resolved Hide resolved
p2p/peer.go Outdated Show resolved Hide resolved
p2p/metrics.go Show resolved Hide resolved
@@ -3,8 +3,11 @@
import (
"fmt"
"net"
"reflect"

Check failure

Code scanning / gosec

Blocklisted import runtime

Blocklisted import reflect
@williambanfield williambanfield added S:automerge Automatically merge PR when requirements pass S:backport-to-v0.34.x Tell mergify to backport the PR to v0.34.x S:backport-to-v0.37.x Tell mergify to backport the PR to v0.37.x labels Oct 26, 2022
@williambanfield williambanfield removed the S:automerge Automatically merge PR when requirements pass label Oct 27, 2022
@williambanfield williambanfield merged commit 09b8708 into main Oct 27, 2022
@williambanfield williambanfield deleted the wb/final-metric-impl branch October 27, 2022 19:46
mergify bot pushed a commit that referenced this pull request Oct 27, 2022
* p2p: ressurrect the p2p envelope and use to calculate message metric

Co-authored-by: Callum Waters <cmwaters19@gmail.com>
(cherry picked from commit 09b8708)

# Conflicts:
#	cmd/tendermint/commands/rollback.go
#	go.mod
#	go.sum
#	p2p/peer.go
#	p2p/peer_set_test.go
mergify bot pushed a commit that referenced this pull request Oct 27, 2022
* p2p: ressurrect the p2p envelope and use to calculate message metric

Co-authored-by: Callum Waters <cmwaters19@gmail.com>
(cherry picked from commit 09b8708)

# Conflicts:
#	.github/workflows/lint.yml
#	blockchain/v0/reactor.go
#	cmd/tendermint/commands/rollback.go
#	consensus/msgs.go
#	consensus/reactor.go
#	go.mod
#	go.sum
#	libs/rand/random.go
#	p2p/metrics.gen.go
#	p2p/metrics.go
#	p2p/pex/pex_reactor.go
#	proto/tendermint/blockchain/message.go
williambanfield added a commit that referenced this pull request Nov 1, 2022
…9641)

* p2p: add a per-message type send and receive metric (#9622)

* p2p: ressurrect the p2p envelope and use to calculate message metric

Add new SendEnvelope, TrySendEnvelope, BroadcastEnvelope, and ReceiveEnvelope methods in the p2p package to work with the new envelope type.

Care was taken to ensure this was performed in a non-breaking manner.

Co-authored-by: William Banfield <4561443+williambanfield@users.noreply.github.com>
Co-authored-by: William Banfield <wbanfield@gmail.com>
adrianbrink pushed a commit to heliaxdev/tendermint that referenced this pull request May 23, 2023
…int#9622) (#230)

* p2p: add a per-message type send and receive metric (tendermint#9622)

* p2p: ressurrect the p2p envelope and use to calculate message metric

Co-authored-by: Callum Waters <cmwaters19@gmail.com>
(cherry picked from commit 09b8708)

# Conflicts:
#	cmd/tendermint/commands/rollback.go
#	go.mod
#	go.sum
#	p2p/peer.go
#	p2p/peer_set_test.go

* all builds except fuzz

* all builds except fuzz

* New methods call the old methods

* Receive called from OnReceive

* tests build after reintroducing send and receive

* fix fuzz peer

* nolint gosec

* mocks use the 'New' calls

* move nolint

* NewReceive called conditionally from switch

* add receives to all with NewReceive

* all reactors have receive with empty new receive call

* reactors have channel and peer in new receive

* add proto message unmarshal to reactors

* unwrap messages in receive calls

* add channel types where absent

* fix mempool reactor to include Receive

* unwrap messages in receive calls

* remove erroneously added blockchain reactors

* remove erroneously added maverick reactor

* re-add broadcast method

* Rename new methods to *Envelope instead of New*

* add deprecation notices

* fix typos

* document new metrics

* format after sed changes

* remove erroneous broadcast method name update

* remove old receives

* comment out old print statement

* Resolved conflicts and fixed renaming clashes

* Reverted renaming of functions to match original backport PR

* reverted accidental renaming due to search/replace

* Added changelog entry

---------

Co-authored-by: William Banfield <4561443+williambanfield@users.noreply.github.com>
Co-authored-by: William Banfield <wbanfield@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S:backport-to-v0.34.x Tell mergify to backport the PR to v0.34.x S:backport-to-v0.37.x Tell mergify to backport the PR to v0.37.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants