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

Process batch serialise concurrently #3877

Merged
merged 19 commits into from
Jun 21, 2023
Merged

Conversation

pocockn
Copy link
Contributor

@pocockn pocockn commented Jun 3, 2023

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

process serialization in parallel

Fixes #3760

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [] make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

pocockn and others added 7 commits June 9, 2022 15:18
* upstream/master: (24 commits)
  account type with zero init nonce (iotexproject#3387)
  [api] Separate Server and Server Handler (iotexproject#3485)
  [ioctl] Build hdwallet derive command line into new ioctl (iotexproject#3418)
  [ioctl] Build hdwallet create command line into new ioctl (iotexproject#3470)
  [makefile] add go mod tidy (iotexproject#3471)
  [api] update chain metrics (iotexproject#3484)
  remove config.EVMNetworkID() (iotexproject#3460)
  [filedao] remove checkMasterChainDBFile() (iotexproject#3463)
  [api] add crashlog (iotexproject#3456)
  [api] Move generateBlockMeta to grpcserver.go (iotexproject#3303)
  [ioctl] Build action hash command line into new ioctl (iotexproject#3425)
  [ioctl] Build hdwallet export command line into new ioctl (iotexproject#3423)
  [ioctl] Refactor nodereward command in new ioctl (iotexproject#3416)
  [ioctl] Cleanup TestNewNodeDelegateCmd (iotexproject#3421)
  [blockchain] Remove BoltDBDaoOption (iotexproject#3465)
  remove InMemDaoOption (iotexproject#3464)
  [action] add evm london test (iotexproject#3402)
  [ioctl] create main for ioctl/newcmd (iotexproject#3296)
  [ioctl] Build block bucket command line into new ioctl (iotexproject#3386)
  [ioctl] Build hdwallet import command line into new ioctl (iotexproject#3419)
  ...
* upstream/master: (45 commits)
  Task: Get config cmd (iotexproject#3552)
  [ioctl] fix  Errors unhandled (iotexproject#3567)
  fix dir permission and file inclusion (iotexproject#3566)
  [test] Disable workingset cache in the benchmark test (iotexproject#3558)
  [pkg] fix  deferring unsafe method "Close" on type "*os.File" (iotexproject#3548)
  [action] Refactor handleTransfer() (iotexproject#3557)
  Add MinVersion in tls.Config (iotexproject#3562)
  [ioctl] Modify file permission as 0600 (iotexproject#3563)
  [httputil] add ReadHeaderTimeout (iotexproject#3550)
  [staking] unexport namespace (iotexproject#3551)
  move chanid metrics to chainservice (iotexproject#3544)
  [ioctl] fix log entries created from user input (iotexproject#3546)
  add log in rolldposctx (iotexproject#3553)
  fix uncontrolled data used in path expression (iotexproject#3547)
  [api] impl. TestGrpcServer_GetServerMeta (iotexproject#3559)
  [ioctl] Build action command line into new ioctl (iotexproject#3472)
  fix potential file inclusion via variable (iotexproject#3549)
  [ioctl] Incorrect conversion between integer types (iotexproject#3522)
  [action] fix incorrect conversion between integer types (iotexproject#3545)
  [test] fix TestLoadBlockchainfromDB (iotexproject#3521)
  ...
@pocockn pocockn requested review from envestcc and a team as code owners June 3, 2023 22:01
db/batch/batch_impl.go Show resolved Hide resolved
db/batch/batch_impl_test.go Outdated Show resolved Hide resolved
bytes := make([]byte, 0)
for itemBytes := range bytesChan {
bytes = append(bytes, itemBytes...)
}
Copy link
Member

Choose a reason for hiding this comment

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

in general this is fine, but probably won't work for our case. Because the total bytes will be hashed to serve as a digest of the batch's content, which is used later in validation. So the bytes has to be in fixed order, and working parallel breaks this order

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 see, does this mean the PR is no longer needed? Thanks

Copy link
Member

Choose a reason for hiding this comment

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

I think you should ensure that the order of writing bytes matches the order in the writeQueue. As for the order of concurrent serialization in between does not matter .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@envestcc modified it so the writing of the bytes matches the order 👍

@Liuhaai
Copy link
Member

Liuhaai commented Jun 6, 2023

using a pre-allocated array could enable the processing in parallel (e.g. blks code in https://github.com/iotexproject/iotex-core/blob/master/blockindex/bloomfilterindexer_test.go#L294-L321)

@LuckyPigeon
Copy link
Contributor

LuckyPigeon commented Jun 7, 2023

@pocockn
I've encountered similar problem before, pls check #3180.
IMO, you could use

  1. errgroup to validate if any of your go routine returns error.
  2. Like @Liuhaai mentioned, add a pre-allocated array should fix the order problem.

db/batch/batch_impl.go Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Jun 13, 2023

Codecov Report

Merging #3877 (42add3b) into master (e1f0636) will increase coverage by 0.18%.
The diff coverage is 67.00%.

❗ Current head 42add3b differs from pull request most recent head 5716c32. Consider uploading reports for the commit 5716c32 to get more accurate results

@@            Coverage Diff             @@
##           master    #3877      +/-   ##
==========================================
+ Coverage   75.38%   75.56%   +0.18%     
==========================================
  Files         303      328      +25     
  Lines       25923    27805    +1882     
==========================================
+ Hits        19541    21012    +1471     
- Misses       5360     5717     +357     
- Partials     1022     1076      +54     
Impacted Files Coverage Δ
action/protocol/execution/evm/evm.go 43.52% <0.00%> (-2.95%) ⬇️
action/protocol/execution/evm/evmstatedbadapter.go 66.77% <ø> (ø)
action/protocol/poll/consortium.go 0.00% <0.00%> (ø)
action/protocol/poll/staking_committee.go 43.85% <0.00%> (ø)
...tion/protocol/staking/contractstake_bucket_type.go 0.00% <0.00%> (ø)
api/grpcserver.go 86.40% <0.00%> (ø)
api/loglistener.go 25.00% <0.00%> (ø)
api/web3server_marshal.go 93.21% <ø> (ø)
api/websocket.go 5.17% <0.00%> (ø)
blockchain/config.go 74.54% <ø> (ø)
... and 51 more

... and 3 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

} else {
bytes = append(bytes, wi.Serialize()...)
}
// 1. Digest could be replaced by merkle root if we need proof
Copy link
Member

Choose a reason for hiding this comment

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

b.mutex.Lock()
defer b.mutex.Unlock()

should the mtx be kept?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @Liuhaai , @envestcc said it's not needed as the concurrent writes now happen on different indices within the slice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

could write an unit test to verify the concurrency problem

Copy link
Member

Choose a reason for hiding this comment

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

mtx is for b.writeQueue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the mtx back @Liuhaai

Copy link
Member

Choose a reason for hiding this comment

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

does the Unlock can be called earlier? It seems like that it can be placed just before wg.Wait().

but it seems that this is unrelated to the current issue. It may not improve the serialization process. Perhaps it can be discussed in a new issue.

@sonarcloud
Copy link

sonarcloud bot commented Jun 20, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@Liuhaai
Copy link
Member

Liuhaai commented Jun 21, 2023

lgtm

@Liuhaai Liuhaai merged commit 1abbc44 into iotexproject:master Jun 21, 2023
3 checks passed
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.

Improve baseKVStoreBatch.SerializeQueue
5 participants