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

[3 / 5] Move crypto checks in the approval-distribution #4928

Merged
merged 22 commits into from
Sep 2, 2024

Conversation

alexggh
Copy link
Contributor

@alexggh alexggh commented Jul 2, 2024

Prerequisite

This is part of the work to further optimize the approval subsystems, if you want to understand the full context start with reading #4849 (comment),

Description

This PR contain changes, so that the crypto checks are performed by the approval-distribution subsystem instead of the approval-voting one. The benefit for these, is twofold:

  1. Approval-distribution won't have to wait every single time for the approval-voting to finish its job, so the work gets to be pipelined between approval-distribution and approval-voting.

  2. By running in parallel multiple instances of approval-distribution as described here [5 / 5] Introduce approval-voting-parallel #4849 (comment), this significant body of work gets to run in parallel.

Changes:

  1. When approval-voting send ApprovalDistributionMessage::NewBlocks it needs to pass the core_index and candidate_hash of the candidates.
  2. ApprovalDistribution needs to use RuntimeInfo to be able to fetch the SessionInfo from the runtime.
  3. Move approval-voting logic that checks VRF assignment into approval-distribution
  4. Move approval-voting logic that checks vote is correctly signed into approval-distribution
  5. Plumb approval-distribution and approval-voting tests to support the new logic.

Benefits

Even without parallelisation the gains are significant, for example on my machine if we run approval subsystem bench for 500 validators and 100 cores and trigger all 89 tranches of assignments and approvals, the system won't fall behind anymore because of late processing of messages.

Before change
Chain selection approved  after 11500 ms hash=0x0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a

After change

Chain selection approved  after 5500 ms hash=0x0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a

TODO:

  • Run on versi.
  • Update parachain host documentation.

@alexggh alexggh force-pushed the alexaggh/approval-voting-parallel-1-5 branch from 78708d7 to 406d11c Compare July 3, 2024 12:15
@alexggh alexggh force-pushed the alexaggh/approval-voting-parallel-3-5 branch from 88e15da to 7c968e1 Compare July 3, 2024 12:16
@alexggh alexggh force-pushed the alexaggh/approval-voting-parallel-1-5 branch from 406d11c to 2281330 Compare July 5, 2024 11:36
@alexggh alexggh force-pushed the alexaggh/approval-voting-parallel-3-5 branch from 7c968e1 to 5fe747c Compare July 5, 2024 11:36
@paritytech-cicd-pr
Copy link

The CI pipeline was cancelled due to failure one of the required jobs.
Job name: cargo-clippy
Logs: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/6635844

@alexggh alexggh force-pushed the alexaggh/approval-voting-parallel-3-5 branch from 5fe747c to e4f883e Compare July 5, 2024 13:13
@alexggh alexggh marked this pull request as ready for review July 5, 2024 13:37
@alexggh alexggh force-pushed the alexaggh/approval-voting-parallel-1-5 branch from 2281330 to 8dbb088 Compare July 16, 2024 12:39
@alexggh alexggh force-pushed the alexaggh/approval-voting-parallel-3-5 branch from e4f883e to 14727c5 Compare July 16, 2024 12:40
Signed-off-by: Alexandru Gheorghe <[email protected]>
…l-1-5' into alexaggh/approval-voting-parallel-3-5
- rename check_and_import in import.
- refactor un-needed variable.

Signed-off-by: Alexandru Gheorghe <[email protected]>
@alexggh alexggh added T0-node This PR/Issue is related to the topic “node”. T8-polkadot This PR/Issue is related to/affects the Polkadot network. labels Jul 26, 2024
Signed-off-by: Alexandru Gheorghe <[email protected]>
Signed-off-by: Alexandru Gheorghe <[email protected]>
Base automatically changed from alexaggh/approval-voting-parallel-1-5 to master July 29, 2024 10:49
Copy link
Contributor

@sandreim sandreim left a comment

Choose a reason for hiding this comment

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

Excellent work @alexggh 🚀 . Left some comments and suggestions.

@alexggh
Copy link
Contributor Author

alexggh commented Aug 14, 2024

PR ran in versi for a few days without any issues!

@sandreim
Copy link
Contributor

Anything preventing us from merging it ?

Copy link
Member

@ordian ordian left a comment

Choose a reason for hiding this comment

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

This optimization makes sense and great work 🚀
Left a few minor remarks

@alexggh
Copy link
Contributor Author

alexggh commented Aug 27, 2024

Anything preventing us from merging it ?

The only thing missing now is a security audit, @patriciobcs any thoughts when this PR will get its turn ?

@patriciobcs
Copy link

Anything preventing us from merging it ?

The only thing missing now is a security audit, @patriciobcs any thoughts when this PR will get its turn ?

Will be done by 13th September.

@alexggh alexggh added this pull request to the merge queue Sep 2, 2024
Merged via the queue into master with commit 6b854ac Sep 2, 2024
182 of 185 checks passed
@alexggh alexggh deleted the alexaggh/approval-voting-parallel-3-5 branch September 2, 2024 09:33
github-merge-queue bot pushed a commit that referenced this pull request Sep 26, 2024
This is the implementation of the approach described here:
#1617 (comment)
&
#1617 (comment)
&
#1617 (comment).

## Description of changes

The end goal is to have an architecture where we have single
subsystem(`approval-voting-parallel`) and multiple worker types that
would full-fill the work that currently is fulfilled by the
`approval-distribution` and `approval-voting` subsystems. The main loop
of the new subsystem would do just the distribution of work to the
workers.

The new subsystem will have:
- N approval-distribution workers: This would do the work that is
currently being done by the approval-distribution subsystem and in
addition to that will also perform the crypto-checks that an assignment
is valid and that a vote is correctly signed. Work is assigned via the
following formula: `worker_index = msg.validator % WORKER_COUNT`, this
guarantees that all assignments and approvals from the same validator
reach the same worker.
- 1 approval-voting worker: This would receive an already valid message
and do everything the approval-voting currently does, except the
crypto-checking that has been moved already to the approval-distribution
worker.

On the hot path of processing messages **no** synchronisation and
waiting is needed between approval-distribution and approval-voting
workers.

<img width="1431" alt="Screenshot 2024-06-07 at 11 28 08"
src="https://github.com/paritytech/polkadot-sdk/assets/49718502/a196199b-b705-4140-87d4-c6900ba8595e">



## Guidelines for reading

The full implementation is broken in 5 PRs and all of them are
self-contained and improve things incrementally even without the
parallelisation being implemented/enabled, the reason this approach was
taken instead of a big-bang PR, is to make things easier to review and
reduced the risk of breaking this critical subsystems.

After reading the full description of this PR, the changes should be
read in the following order:
1. #4848, some other
micro-optimizations for networks with a high number of validators. This
change gives us a speed up by itself without any other changes.
2. #4845 , this contains
only interface changes to decouple the subsystem from the `Context` and
be able to run multiple instances of the subsystem on different threads.
**No functional changes**
3. #4928, moving of the
crypto checks from approval-voting in approval-distribution, so that the
approval-distribution has no reason to wait after approval-voting
anymore. This change gives us a speed up by itself without any other
changes.
4. #4846, interface
changes to make approval-voting runnable on a separate thread. **No
functional changes**
5. This PR, where we instantiate an `approval-voting-parallel` subsystem
that runs on different workers the logic currently in
`approval-distribution` and `approval-voting`.
6. The next step after this changes get merged and deploy would be to
bring all the files from approval-distribution, approval-voting,
approval-voting-parallel into a single rust crate, to make it easier to
maintain and understand the structure.

## Results
Running subsystem-benchmarks with 1000 validators 100 fully ocuppied
cores and triggering all assignments and approvals for all tranches

#### Approval does not lags behind. 
 Master
```
Chain selection approved  after 72500 ms hash=0x0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a
```
With this PoC
```
Chain selection approved  after 3500 ms hash=0x0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a
```

#### Gathering enough assignments
 
Enough assignments are gathered in less than 500ms, so that gives un a
guarantee that un-necessary work does not get triggered, on master on
the same benchmark because the subsystems fall behind on work, that
number goes above 32 seconds on master.
 
<img width="2240" alt="Screenshot 2024-06-20 at 15 48 22"
src="https://github.com/paritytech/polkadot-sdk/assets/49718502/d2f2b29c-5ff6-44b4-a245-5b37ab8e58bc">


#### Cpu usage:
Master
```
CPU usage, seconds                     total   per block
approval-distribution                96.9436      9.6944
approval-voting                     117.4676     11.7468
test-environment                     44.0092      4.4009
```
With this PoC
```
CPU usage, seconds                     total   per block
approval-distribution                 0.0014      0.0001 --- unused
approval-voting                       0.0437      0.0044.  --- unused
approval-voting-parallel              5.9560      0.5956
approval-voting-parallel-0           22.9073      2.2907
approval-voting-parallel-1           23.0417      2.3042
approval-voting-parallel-2           22.0445      2.2045
approval-voting-parallel-3           22.7234      2.2723
approval-voting-parallel-4           21.9788      2.1979
approval-voting-parallel-5           23.0601      2.3060
approval-voting-parallel-6           22.4805      2.2481
approval-voting-parallel-7           21.8330      2.1833
approval-voting-parallel-db          37.1954      3.7195.  --- the approval-voting thread.
```

# Enablement strategy

Because just some trivial plumbing is needed in approval-distribution
and approval-voting to be able to run things in parallel and because
this subsystems plays a critical part in the system this PR proposes
that we keep both ways of running the approval work, as separated
subsystems and just a single subsystem(`approval-voting-parallel`) which
has multiple workers for the distribution work and one worker for the
approval-voting work and switch between them with a comandline flag.

The benefits for this is twofold.
1. With the same polkadot binary we can easily switch just a few
validators to use the parallel approach and gradually make this the
default way of running, if now issues arise.
2. In the worst case scenario were it becomes the default way of running
things, but we discover there are critical issues with it we have the
path to quickly disable it by asking validators to adjust their command
line flags.


# Next steps
- [x] Make sure through various testing we are not missing anything 
- [x] Polish the implementations to make them production ready
- [x] Add Unittest Tests for approval-voting-parallel.
- [x] Define and implement the strategy for rolling this change, so that
the blast radius is minimal(single validator) in case there are problems
with the implementation.
- [x]  Versi long running tests.
- [x] Add relevant metrics.

@ordian @eskimor @sandreim @AndreiEres, let me know what you think.

---------

Signed-off-by: Alexandru Gheorghe <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T0-node This PR/Issue is related to the topic “node”. T8-polkadot This PR/Issue is related to/affects the Polkadot network.
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

7 participants