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

[db]fix counting_index data race #3651

Merged
merged 5 commits into from
Oct 12, 2022
Merged

[db]fix counting_index data race #3651

merged 5 commits into from
Oct 12, 2022

Conversation

millken
Copy link
Contributor

@millken millken commented Sep 29, 2022

Description

When testing the analyser, it was found that db counting_index.go has a data race

==================
WARNING: DATA RACE
Write at 0x00c000127e60 by goroutine 36:
  github.com/iotexproject/iotex-core/db.(*countingIndex).addBatch()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/db/counting_index.go:140 +0x3fa
  github.com/iotexproject/iotex-core/db.(*countingIndex).Add()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/db/counting_index.go:118 +0x77e
  github.com/iotexproject/iotex-core/blockchain/filedao.addOneEntryToBatch()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/filedao/filedao_v2_util.go:126 +0xa4
  github.com/iotexproject/iotex-core/blockchain/filedao.(*fileDAOv2).putBlock()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/filedao/filedao_v2_util.go:107 +0x3ed
  github.com/iotexproject/iotex-core/blockchain/filedao.(*fileDAOv2).PutBlock()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/filedao/filedao_v2.go:236 +0x11d
  github.com/iotexproject/iotex-core/blockchain/filedao.(*fileDAO).PutBlock()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/filedao/filedao.go:312 +0x20e
  github.com/iotexproject/iotex-core/blockchain/blockdao.(*blockDAO).PutBlock()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/blockdao/blockdao.go:244 +0x127


Previous read at 0x00c000127e60 by goroutine 62:
  github.com/iotexproject/iotex-core/db.(*countingIndex).Size()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/db/counting_index.go:112 +0x34
  github.com/iotexproject/iotex-core/blockchain/filedao.(*fileDAOv2).getBlockStore()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/filedao/filedao_v2_util.go:183 +0x151
  github.com/iotexproject/iotex-core/blockchain/filedao.(*fileDAOv2).GetReceipts()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/filedao/filedao_v2.go:197 +0x56
  github.com/iotexproject/iotex-core/blockchain/filedao.(*fileDAO).GetReceipts()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/filedao/filedao.go:270 +0x15c
  github.com/iotexproject/iotex-core/blockchain/blockdao.(*blockDAO).GetReceipts()
      /home/millken/.gvm/pkgsets/go1.18.6/global/pkg/mod/github.com/iotexproject/iotex-core@v1.8.3-rc4/blockchain/blockdao/blockdao.go:229 +0x13e

Fixes #(issue)

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

@millken millken requested a review from a team as a code owner September 29, 2022 08:59
@codecov
Copy link

codecov bot commented Sep 29, 2022

Codecov Report

Merging #3651 (2568949) into master (e9732a1) will decrease coverage by 0.26%.
The diff coverage is 82.72%.

@@            Coverage Diff             @@
##           master    #3651      +/-   ##
==========================================
- Coverage   74.95%   74.69%   -0.27%     
==========================================
  Files         269      266       -3     
  Lines       23819    23759      -60     
==========================================
- Hits        17854    17747     -107     
- Misses       5039     5085      +46     
- Partials      926      927       +1     
Impacted Files Coverage Δ
db/config.go 100.00% <ø> (ø)
db/trie/mptrie/node.go 100.00% <ø> (ø)
ioctl/newcmd/action/action.go 79.62% <ø> (+0.09%) ⬆️
ioctl/newcmd/node/nodedelegate.go 70.19% <0.00%> (-0.95%) ⬇️
ioctl/newcmd/node/nodeprobationlist.go 90.90% <0.00%> (-4.33%) ⬇️
action/protocol/execution/evm/evm.go 44.57% <26.66%> (-0.86%) ⬇️
db/trie/mptrie/hashnode.go 36.36% <41.17%> (-54.55%) ⬇️
db/trie/mptrie/merklepatriciatrie.go 78.73% <46.80%> (-12.70%) ⬇️
db/db_bolt.go 72.13% <50.00%> (-0.39%) ⬇️
db/trie/mptrie/leafiterator.go 62.06% <50.00%> (ø)
... and 21 more

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

@Liuhaai
Copy link
Member

Liuhaai commented Oct 3, 2022

@dustinxie

db/counting_index.go Outdated Show resolved Hide resolved
db/counting_index.go Outdated Show resolved Hide resolved
db/counting_index.go Outdated Show resolved Hide resolved
db/counting_index.go Outdated Show resolved Hide resolved
}

// Size returns the total number of keys so far
func (c *countingIndex) Size() uint64 {
return c.size
return atomic.LoadUint64(&c.size)
Copy link
Member

Choose a reason for hiding this comment

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

add another func

func (c *countingIndex) Add(n uint64) {
atomic.StoreUint64(&c.size, c.Size()+n)
}

read use c.Size(), update use c.Add(). maybe better

@sonarcloud
Copy link

sonarcloud bot commented Oct 12, 2022

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 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@millken millken merged commit 436b7b5 into iotexproject:master Oct 12, 2022
@millken millken deleted the dbrace branch October 12, 2022 09:00
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.

4 participants