Skip to content

test: Connection eviction logic tests - #16756

Merged
maflcko merged 1 commit into
bitcoin:masterfrom
mzumsande:201908_test_eviction
Jun 12, 2020
Merged

maflcko merged 1 commit into
bitcoin:masterfrom
mzumsande:201908_test_eviction

Conversation

@mzumsande

@mzumsande mzumsande commented Aug 29, 2019 •

Copy link
Copy Markdown
Contributor

This adds a functional test for the eviction logic for inbound peers, which is triggered when the number of maximum connections is exceeded.

The functional test covers eviction protection for peers that have sent us blocks or txns recently, or that have faster pings. I couldn't find a way to test the logic of CConnman::AttemptToEvictConnection that is based on netgroup (see #14210 for related discussion)

Fixes #16660 (at least partially).

[Edit: Earlier, this PR also contained a unit test, which was removed after the discussion]

@mzumsande mzumsande changed the title test: Connection eviction ligc tests test: Connection eviction logic tests Aug 29, 2019
@mzumsande
mzumsande force-pushed the 201908_test_eviction branch from 27c04f4 to b15b0b9 Compare August 29, 2019 14:18
@mzumsande
mzumsande force-pushed the 201908_test_eviction branch from b15b0b9 to 7254563 Compare August 29, 2019 15:36
@DrahtBot

DrahtBot commented Aug 29, 2019 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@mzumsande
mzumsande force-pushed the 201908_test_eviction branch 2 times, most recently from c7f3adc to 42a01bf Compare August 30, 2019 01:19
@instagibbs

Copy link
Copy Markdown
Member

TSan build timing out with last output being:

Remaining jobs: [feature_block.py, p2p_permissions.py]

@mzumsande

Copy link
Copy Markdown
Contributor Author

Hmm, do you think this is caused by my changes? I didn't change the tests that timed out and the changes to net were meant to only expose the eviction helper functions for testing (that shouldn't be called in these tests because the code had no previous test coverage).

@jamesob

jamesob commented Aug 30, 2019

Copy link
Copy Markdown
Contributor

Concept ACK, will review soon

@instagibbs

Copy link
Copy Markdown
Member

@mzumsande I'll let someone else weigh in on that, I just didn't want to blow away that result in case it was interesting. Restarted that build.

@mzumsande

Copy link
Copy Markdown
Contributor Author

@instagibbs Thanks! It happened again, so it does seem to be caused by the changes here. Will look into it and try to reproduce.

@practicalswift

Copy link
Copy Markdown
Contributor

Concept ACK

Thanks for working on this!

@naumenkogs

Copy link
Copy Markdown
Contributor

Tested ACK 42a01bf1c15eeb625abdc8bbc0ef9a7852a2b1af

@mzumsande

Copy link
Copy Markdown
Contributor Author

Thanks for the review!
By the way, the functional test will need a small update once #15759 is merged because of the 2 extra outbound connections:
self.extra_args = [['-maxconnections=32']] instead of 30

@mzumsande
mzumsande force-pushed the 201908_test_eviction branch from 42a01bf to 85e2300 Compare September 7, 2019 21:50
@mzumsande

Copy link
Copy Markdown
Contributor Author

Rebased and adjusted maxconnections after the merge of #15759 as explained above.

@mzumsande

Copy link
Copy Markdown
Contributor Author

The AppVeyor build failed - there seem to be intermittent failures with the new functional test. I can't reproduce so far, but will look into it.

@mzumsande

mzumsande commented Sep 17, 2019 •

Copy link
Copy Markdown
Contributor Author

I think that my assumption that the artificially slowed down peers never have the lowest minimum ping can be wrong, leading to the intermittent failure. I changed the functional test to use getpeerinfo RPC to determine what the 8 nodes with the lowest minimum ping actually are. This should hopefully fix this issue.

@jonatack jonatack left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Concept ACK. Nice. Could you mention/describe the proposed changes in net.h/net.cpp in the PR description, and possibly move the combine log/appveyor changes (if they are still needed) to a separate PR.

@mzumsande

Copy link
Copy Markdown
Contributor Author

I added those commits temporarily, because the AppVeyor build was failing and I couldn't reproduce this locally and wanted look at the debug.log, which AppVeyor currently doesnt' show due to #16894. I have removed the commits and fixed my mistake (not giving enough time for ping after adding a new peer).
Will update the PR text as suggested.

@jonatack

Copy link
Copy Markdown
Member

Thanks, I figured the timing of my review wasn't ideal. Looking forward to reviewing this.

Comment thread test/functional/p2p_eviction.py Resolved
Comment thread src/test/net_tests.cpp Resolved
@mzumsande
mzumsande force-pushed the 201908_test_eviction branch from a0780c5 to 3758af7 Compare May 28, 2020 22:22
@mzumsande

Copy link
Copy Markdown
Contributor Author

@jonatack Thanks again - I took several of your suggestions (some slightly adapted) and pushed an update (just preferred not to split the asserts/logging from the rest)

Yes, protected_peers are the subset of peers that are expected not to be evicted by our node (because they sent us a block, tx, have fast pings) - otherwise the test would fail.

@naumenkogs

Copy link
Copy Markdown
Contributor

Tested ACK 3758af7

@jnewbery

Copy link
Copy Markdown
Contributor

We'll cover this in review club on 2020-06-03: https://bitcoincore.reviews/16756.html

@rajarshimaitra rajarshimaitra left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK. Very nice test framework with elaborate comments to understand the logic. Below are few comments for mostly my own understanding and one probable minor bug.

Comment thread test/functional/p2p_eviction.py Outdated
Comment on lines 105 to 103

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it possible to combine pings initialization with the actual filling of the list with minping data (happening few lines later) together? Any specific reason why they are separated by the add_p2p_connection?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is probably not material, but the reason is that I wanted to trigger the eviction mechanism immediately after the getpeerinfo call, so that the node does not get time to exchange additional pings with its peers while the test does other things.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wondered the same thing. It's confusing that you do some of the processing before adding the new peer (pings = [[...) and some after (for i in range(...). I think you should do all the processing before adding the new connection, since it'll take very little time, and the chance of there being another ping in between is ~zero.

If you do keep this ordering, I think it's worth a comment to say why you're doing it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed the ordering since the probability of furthers pings is extremely low.

Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is coming a little confusing for me, which 4 peers are protected via netgroup? If i understand correctly for functional testing all the peers are from same netgroup. So there's no way to differentiate between them via IP addresses. Is this what this comment is trying to convey?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@mzumsande mzumsande Jun 2, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it is impossible to say which peers are protected via netgroup because it can vary from run to run!
Since A) this is the first check in AttemptToEvictConnection() B) all test nodes have identical netgroup and C) std::sort does not guarantee to preserve the order of identical elements, this step should protect 4 arbitrary peers in the test.
If, for example, all 4 block-sending peers would be protected by chance, then the later EraseLastKElements with CompareNodeBlockTime would operate on identical elements and erase 4 random peers from vEvictionCandidates (protecting them).
The assertion at the end only tests that no peer assumed to be protected will be evicted. Its protection might happen in the EraseLastKElements specific to its unique feature, or earlier by chance.

@jonatack jonatack Jun 3, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, the only certainty we have regarding the evicted peers in this test is that it will be one of the zero-indexed peers in the set of {4, 5, 6, 7, 8} e.g. the slow-pinging ones.

@fjahr fjahr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review ACK 3758af78564e9a0e059f38c11e941d68be192fb6

Nice test, thank you for adding it!

Comment thread test/functional/p2p_eviction.py Resolved

@jonatack jonatack left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Almost-ACK. Thanks for taking some of the suggestions in jonatack/bitcoin@c9e96a1.

Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Resolved
@andrewtoth

Copy link
Copy Markdown
Contributor

Tested ACK 3758af78564e9a0e059f38c11e941d68be192fb6

@mzumsande
mzumsande force-pushed the 201908_test_eviction branch from 3758af7 to 32c6ce0 Compare June 3, 2020 00:07
@rajarshimaitra

Copy link
Copy Markdown
Contributor

Not specific to this PR, but related to the eviction logic code.

bitcoin/src/net.cpp

Lines 932 to 935 in 657b82c

if (group.size() > nMostConnections || (group.size() == nMostConnections && grouptime > nMostConnectionsTime)) {
nMostConnections = group.size();
nMostConnectionsTime = grouptime;
naMostConnections = node.nKeyedNetGroup;

The naMostConnections is storing the netgroup key with most number of connections. Why the variable name is like that? First i thought its a typo, but then it seems the a is simply added to distinguish it from nMostConnections, or is there a specific meaning to it? IMO feels like not so appropriate name to give to a variable which is not storing number of connections.

@instagibbs

Copy link
Copy Markdown
Member

@rajarshimaitra feel free to open a PR on that if you have ideas on making the code more self-explanatory 👍

@jonatack

jonatack commented Jun 3, 2020 •

Copy link
Copy Markdown
Member

ACK 3758af78564e9a0e059f38c11e941d68be192fb6 thanks for adding useful test coverage. Reviewed/tested, re-checked the coherence with CConnman::AttemptToEvictConnection(), printed the peers at each step, and added additional assertions. I reckon the code could be shorter, but it's fine for now.

Thanks also for your patience and taking into consideration all the feedback.

@jnewbery

jnewbery commented Jun 3, 2020

Copy link
Copy Markdown
Contributor

I have a few style nits which I won't leave inline in order not to distract this PR from functional review:

Consider installing a python linter like flake8 or black to apply these automatically.

@jonatack

jonatack commented Jun 3, 2020

Copy link
Copy Markdown
Member

Building on John's style points, from test/functional/README.md: When calling RPCs with several arguments, consider using named keyword arguments instead of positional arguments to make the intent of the call clear to readers.

The variables used as positional args in this test are sufficiently well-named that I didn't bring it up, but it's good to know.

@jnewbery jnewbery left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great. A bunch of minor suggestions inline.

Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Outdated
Comment on lines 105 to 103

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wondered the same thing. It's confusing that you do some of the processing before adding the new peer (pings = [[...) and some after (for i in range(...). I think you should do all the processing before adding the new connection, since it'll take very little time, and the chance of there being another ping in between is ~zero.

If you do keep this ordering, I think it's worth a comment to say why you're doing it.

Comment thread test/functional/p2p_eviction.py Resolved
Comment thread test/functional/p2p_eviction.py Resolved
@mzumsande
mzumsande force-pushed the 201908_test_eviction branch from 32c6ce0 to 45eff75 Compare June 6, 2020 20:31
@mzumsande

Copy link
Copy Markdown
Contributor Author

I just pushed an update that should address all outstanding suggestions from review.

@jonatack

jonatack commented Jun 7, 2020

Copy link
Copy Markdown
Member

Looking at it again, this test can be much simpler. One example: the current_peer counter and the protected_peers set building can all be removed in favor of just checking that the evicted peer is one of the slow-pinging peers. (After all this review, I may propose the simplifications as a follow-up.)

ACK 45eff75

@naumenkogs

Copy link
Copy Markdown
Contributor

Tested ACK 45eff75

@fjahr

fjahr commented Jun 12, 2020

Copy link
Copy Markdown
Contributor

re-ACK 45eff75

Changes since my last reviews were minor improvements addressing nit review comments.

@@ -0,0 +1,129 @@
#!/usr/bin/env python3
# Copyright (c) 2019 The Bitcoin Core developers

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit

Suggested change
# Copyright (c) 2019 The Bitcoin Core developers
# Copyright (c) 2020 The Bitcoin Core developers

@andrewtoth

Copy link
Copy Markdown
Contributor

re-ACK 45eff75

@maflcko
maflcko merged commit 5af16a4 into bitcoin:master Jun 12, 2020
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request May 13, 2021
Summary:
45eff751c6d07007dabc365dc4c0e6c63e3fe5cf Add functional test for P2P eviction logic of inbound peers (Martin Zumsande)

Pull request description:

  This adds a functional test for the eviction logic for inbound peers, which is triggered when the number of maximum connections is exceeded.

  The functional test covers eviction protection for peers that  have sent us blocks or txns recently, or that have faster pings. I couldn't find a way to test the logic of `CConnman::AttemptToEvictConnection` that is based on netgroup (see #14210 for related discussion)

  Fixes #16660 (at least partially).

  [Edit: Earlier, this PR also contained a unit test, which was removed after the discussion]

---

Backport of [[bitcoin/bitcoin#16756 | core#16756]]

Test Plan:
  ninja all && ./test/test_runner.py p2p_eviction

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D9510
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
@mzumsande
mzumsande deleted the 201908_test_eviction branch October 13, 2023 15:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection eviction logic (AttemptToEvictConnection) is not covered by our test suite