Skip to content

refactor: blocksync module is refactored on using worker-pool - #582

Merged
shotonoff merged 61 commits into
td-115-fix-consensus-deadlockfrom
td-143-fix-flaky-test-reactor-no-block-response
Mar 14, 2023
Merged

shotonoff merged 61 commits into
td-115-fix-consensus-deadlockfrom
td-143-fix-flaky-test-reactor-no-block-response

Conversation

@shotonoff

@shotonoff shotonoff commented Feb 10, 2023 •

Copy link
Copy Markdown
Collaborator

Issue being fixed or feature implemented

Motivation for this refactoring is

  • Replace the original bpRequest implementation on worker-pool approach. Worker pool simplifies the implementation of the requirements, like limit for simultaneous requests for the blocks etc. Not needed to worry how much requests are performed, we just produce a job if a worker is free otherwise wait for worker or peer will be ready for the request.
  • Use the SOLID practices for blocksync codebase, to make the module easier to maintain and develop.

What was done?

Keys design changes

  • WorkerPool is a component that can pre-allocate a number of workers to execute jobs in the background. It will allow us to distribute the workload evenly between the workers with expected limitations.
    The job itself must implement the workerpool.Job interface, and the job instance must be passed into the worker-pool.
    Communication between worker-pool and workers is carried out over the job channel.
    When the execution is complete, the worker sends workerpool.Result over the result channel to the worker pool to report the result of the execution.
  • Promise allows us to write asynchronous code in a synchronous way. We use the execute function to resolve or reject the execution of the result.
  • Channel
  1. Channel implements BlockClient interface
BlockClient interface {
  GetBlock(ctx context.Context, height int64, peerID types.NodeID) (*promise.Promise[*bcproto.BlockResponse], error)
  Send(ctx context.Context, msg any) error
}

The GetBlock method requests a block from a peer and returns promise.Promise which resolve the result if response received in time otherwise reject

  1. it has implemented the Consume method which is designed to consume p2p messages
  • blockFetchJob is an implementation of workerpool.Job. This job is responsible for requesting a peer to get a block.
  • InMemPeerStore is an in-memory key/value store for the peers info. Along with basic features like Put,Get and Remove. The component also provides some methods repository level methods, like Query, FindPeer, FindTimedoutPeers. The stored peer data has the current peer height,base height and a few system fields like time of when peer was added.
  • Synchronizer implements job producer and job’s result consumer logic. Depending on the job' result, the received block can be applied otherwise a peer will be removed from the peer store and the block will request again. Worker-pool uses a fixed number of workers to perform requests for the blocks.

How Has This Been Tested?

  • Unit and E2E tests
  • New code covered by unit tests

Breaking Changes

N/A

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Comment thread libs/promise/promise.go Resolved
Comment thread libs/promise/promise.go Resolved
Comment thread libs/promise/promise.go Resolved
Comment thread libs/promise/promise.go Resolved
Comment thread internal/test/require/require.go Resolved
Comment thread libs/workerpool/worker_pool_test.go Resolved
Comment thread libs/workerpool/worker_pool_test.go Resolved
Comment thread libs/workerpool/worker_pool_test.go Resolved
Comment thread types/mock.go Outdated
@@ -0,0 +1,56 @@
package types

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we have "factory"package that contains similar code

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

opinionated: the package factory (the same as util) doesn't have a certain purpose of usage, it is like a package for everything, which violates the design of the app, in my opinion. I would rather use a more specific package, based on the domain for example.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should not have two different approaches. Either we use factory and you move your code there, or we get rid of it, and move that code to correct places.

I see factory as a 'test helper' package, so maybe just renaming is a solution.

If you decide to get rid of factory, it deserves separate PR.

Comment thread internal/libs/flowrate/flowrate.go Resolved
Comment thread internal/blocksync/channel.go Resolved
Comment thread internal/blocksync/channel.go Resolved
Comment thread internal/blocksync/channel.go Resolved
Comment thread internal/blocksync/channel.go Resolved
Comment thread internal/blocksync/channel.go Resolved
Comment thread internal/blocksync/synchronizer.go Resolved
Comment thread internal/blocksync/synchronizer.go Resolved
Comment thread internal/blocksync/synchronizer.go Resolved
Comment thread internal/blocksync/synchronizer.go Resolved
Comment thread types/mock.go Outdated
@@ -0,0 +1,56 @@
package types

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should not have two different approaches. Either we use factory and you move your code there, or we get rid of it, and move that code to correct places.

I see factory as a 'test helper' package, so maybe just renaming is a solution.

If you decide to get rid of factory, it deserves separate PR.

@shotonoff shotonoff mentioned this pull request Mar 9, 2023
2 of 5 tasks

@shotonoff shotonoff left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

PR is ready for review again

@shotonoff
shotonoff requested a review from lklimek March 9, 2023 13:09
err = s.applyBlock(ctx)
if err != nil {
s.logger.Error("cannot apply a block", "height", resp.Block.Height, "error", err.Error())
s.RemovePeer(resp.PeerID)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When peer responds, we increase its score by 1. Otherwise, we decrease its score by 1 (or whatever "punishment" we want to make). See PeerManager.processPeerEvent().

We select peer to use based on the highest score, descending. See peerStore.ranked().

As a result, best peers (= the ones who responded to most queries) are most likely to get requests.

We can optimize to use latency info when increasing the score, and/or add some weighted random mechanism when selecting peer to use.

Comment thread libs/promise/promise_test.go Outdated
t.Run(fmt.Sprintf("test-case %d", i), func(t *testing.T) {
p := New(tc.exec)
res, err := p.Await()
if tc.wantErr != "" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

let's go with pkg (separate PR)

Comment thread internal/test/require/require.go Resolved
@shotonoff
shotonoff merged commit 5d401c8 into td-115-fix-consensus-deadlock Mar 14, 2023
@shotonoff
shotonoff deleted the td-143-fix-flaky-test-reactor-no-block-response branch March 14, 2023 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants