refactor(mempool): Add Iterator to replace TxsFront and TxsWaitChan methods#3459
refactor(mempool): Add Iterator to replace TxsFront and TxsWaitChan methods#3459
Iterator to replace TxsFront and TxsWaitChan methods#3459Conversation
.changelog/unreleased/deprecations/3303-mempool-deprecate-txsfront-txswaitchan.md
Show resolved
Hide resolved
Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
melekes
left a comment
There was a problem hiding this comment.
👍 Not sure about channel closing and retrying semantics, otherwise LGTM
| select { | ||
| case entry = <-iter.WaitNextCh(): | ||
| // If the entry we were looking at got garbage collected (removed), try again. | ||
| if entry == nil { |
There was a problem hiding this comment.
If we reach the end of the list, do we also retry here?
There was a problem hiding this comment.
In principle, yes, that's what would happen.
Here, entry is nil either because Clist.Front or CElement.Next returned nil. If Next returns nil because it's at the end of the list and the list is not empty, we shouldn't set the cursor to nil because we will start iterating from the beginning of the list, sending the same transactions more than once. The other reason I see that both Front and Next return nil is when the entry got removed just before calling these functions. I've seen these scenarios happen in unit tests, but they are difficult to reproduce.
I think it's better to investigate these scenarios in another issue; this PR should be just a refactoring.
mempool/clist_mempool.go
Outdated
| if iter.cursor != nil { | ||
| ch <- iter.cursor.Value.(Entry) | ||
| } | ||
| close(ch) |
There was a problem hiding this comment.
Remember that closing a channel also sends a nil value
case entry = <-iter.WaitNextCh():
will fire with nil and we will execute continue
There was a problem hiding this comment.
The receiver will get nil when closing the channel without sending any message on it. This happens when iter.cursor == nil. When iter.cursor != nil, it first send the message on the channel and then it closes it, but the receiver (line 227) already got the message. (I checked this behaviour on the code.)
There was a problem hiding this comment.
To make it more clear I can change it to:
if iter.cursor != nil {
ch <- iter.cursor.Value.(Entry)
} else {
close(ch)
}
In the first case the garbage collector will take care of closing the channel.
… fails (#3647) Fixes a bug introduced in #3459 where instead of retrying the same tx when the peer is lagging, it was skipping it and moving on to the next entry. The same used to happen when sending the transaction failed: instead of retrying to send, it skipped it and proceeded to the next tx. This PR also reverts #3430, which was not working as expected. Note that the first commit introduces a new test `TestMempoolReactorSendLaggingPeer` that initially fails and then it's fixed: https://github.com/cometbft/cometbft/actions/runs/10300181146/job/28509056466?pr=3647 --- #### PR checklist - [X] Tests written/updated - ~~[ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog)~~ - ~~[ ] Updated relevant documentation (`docs/` or `spec/`) and code comments~~ - [X] Title follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
… fails (#3647) Fixes a bug introduced in #3459 where instead of retrying the same tx when the peer is lagging, it was skipping it and moving on to the next entry. The same used to happen when sending the transaction failed: instead of retrying to send, it skipped it and proceeded to the next tx. This PR also reverts #3430, which was not working as expected. Note that the first commit introduces a new test `TestMempoolReactorSendLaggingPeer` that initially fails and then it's fixed: https://github.com/cometbft/cometbft/actions/runs/10300181146/job/28509056466?pr=3647 --- #### PR checklist - [X] Tests written/updated - ~~[ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog)~~ - ~~[ ] Updated relevant documentation (`docs/` or `spec/`) and code comments~~ - [X] Title follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec (cherry picked from commit 1ac8832) # Conflicts: # mempool/reactor.go
… fails (backport #3647) (#3657) Fixes a bug introduced in #3459 where instead of retrying the same tx when the peer is lagging, it was skipping it and moving on to the next entry. The same used to happen when sending the transaction failed: instead of retrying to send, it skipped it and proceeded to the next tx. This PR also reverts #3430, which was not working as expected. Note that the first commit introduces a new test `TestMempoolReactorSendLaggingPeer` that initially fails and then it's fixed: https://github.com/cometbft/cometbft/actions/runs/10300181146/job/28509056466?pr=3647 --- #### PR checklist - [X] Tests written/updated - ~~[ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog)~~ - ~~[ ] Updated relevant documentation (`docs/` or `spec/`) and code comments~~ - [X] Title follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec <hr>This is an automatic backport of pull request #3647 done by [Mergify](https://mergify.com). --------- Co-authored-by: Hernán Vanzetto <15466498+hvanz@users.noreply.github.com> Co-authored-by: hvanz <hernan.vanzetto@gmail.com>
|
@Mergifyio backport v1.x |
✅ Backports have been createdDetails
|
…han` methods (#3459) Closes #3303 This PR adds: - the `Entry` interface, implemented by the existing `mempoolTx` data type, - the `Iterator` interface, with one method `WaitNextCh() <-chan Entry`. On the routine that sends transactions to a peer, we can now use an `Iterator` to select the next mempool entry to send, instead of the methods `TxsFront` and `TxsWaitChan` in `CListMempool`. These two methods leak implementation details about the CList and are now marked as deprecated. Now all the logic needed to wait for and pick the next available entry is implemented in `CListMempool.WaitNextCh`. --- #### PR checklist - [X] Tests written/updated - [x] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog) - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments - [x] Title follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec --------- Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com> (cherry picked from commit d4a82b7) # Conflicts: # mempool/clist_mempool_test.go # mempool/reactor.go
… fails (#3647) Fixes a bug introduced in #3459 where instead of retrying the same tx when the peer is lagging, it was skipping it and moving on to the next entry. The same used to happen when sending the transaction failed: instead of retrying to send, it skipped it and proceeded to the next tx. This PR also reverts #3430, which was not working as expected. Note that the first commit introduces a new test `TestMempoolReactorSendLaggingPeer` that initially fails and then it's fixed: https://github.com/cometbft/cometbft/actions/runs/10300181146/job/28509056466?pr=3647 --- #### PR checklist - [X] Tests written/updated - ~~[ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog)~~ - ~~[ ] Updated relevant documentation (`docs/` or `spec/`) and code comments~~ - [X] Title follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec (cherry picked from commit 1ac8832) # Conflicts: # mempool/reactor_test.go
… fails (backport #3647) (#4109) Fixes a bug introduced in #3459 where instead of retrying the same tx when the peer is lagging, it was skipping it and moving on to the next entry. The same used to happen when sending the transaction failed: instead of retrying to send, it skipped it and proceeded to the next tx. This PR also reverts #3430, which was not working as expected. Note that the first commit introduces a new test `TestMempoolReactorSendLaggingPeer` that initially fails and then it's fixed: https://github.com/cometbft/cometbft/actions/runs/10300181146/job/28509056466?pr=3647 --- #### PR checklist - [X] Tests written/updated - ~~[ ] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog)~~ - ~~[ ] Updated relevant documentation (`docs/` or `spec/`) and code comments~~ - [X] Title follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec <hr>This is an automatic backport of pull request #3647 done by [Mergify](https://mergify.com). --------- Co-authored-by: Hernán Vanzetto <15466498+hvanz@users.noreply.github.com> Co-authored-by: hvanz <hernan.vanzetto@gmail.com>
…han` methods (backport #3459) (#4089) Closes #3303 This PR adds: - the `Entry` interface, implemented by the existing `mempoolTx` data type, - the `Iterator` interface, with one method `WaitNextCh() <-chan Entry`. On the routine that sends transactions to a peer, we can now use an `Iterator` to select the next mempool entry to send, instead of the methods `TxsFront` and `TxsWaitChan` in `CListMempool`. These two methods leak implementation details about the CList and are now marked as deprecated. Now all the logic needed to wait for and pick the next available entry is implemented in `CListMempool.WaitNextCh`. --- #### PR checklist - [X] Tests written/updated - [x] Changelog entry added in `.changelog` (we use [unclog](https://github.com/informalsystems/unclog) to manage our changelog) - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments - [x] Title follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec <hr>This is an automatic backport of pull request #3459 done by [Mergify](https://mergify.com). --------- Co-authored-by: Hernán Vanzetto <15466498+hvanz@users.noreply.github.com> Co-authored-by: hvanz <hernan.vanzetto@gmail.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Closes #3303
This PR adds:
Entryinterface, implemented by the existingmempoolTxdata type,Iteratorinterface, with one methodWaitNextCh() <-chan Entry.On the routine that sends transactions to a peer, we can now use an
Iteratorto select the next mempool entry to send, instead of the methodsTxsFrontandTxsWaitChaninCListMempool. These two methods leak implementation details about the CList and are now marked as deprecated. Now all the logic needed to wait for and pick the next available entry is implemented inCListMempool.WaitNextCh.PR checklist
.changelog(we use unclog to manage our changelog)docs/orspec/) and code comments