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

refactor: make cachekv store thread-safe again #14378

Merged
merged 10 commits into from
Dec 21, 2022

Conversation

yihuang
Copy link
Collaborator

@yihuang yihuang commented Dec 21, 2022

Description

Closes: #14376

Solution:

  • copy isolated sortedCache when starting iterating
  • cleanup deletion logic

go test -run=^$ -bench=. -benchmem ./store/cachekv/... -count=10

name                                 old time/op    new time/op    delta
LargeUnsortedMisses-12                 12.6ms ± 2%    12.7ms ± 2%   +1.11%  (p=0.022 n=9+10)
DeepContextStack1-12                   5.36µs ± 2%    5.42µs ± 1%   +1.09%  (p=0.009 n=10+10)
DeepContextStack3-12                   6.54µs ± 1%    6.97µs ± 3%   +6.55%  (p=0.000 n=10+10)
DeepContextStack10-12                  11.9µs ± 2%    13.0µs ± 0%   +9.26%  (p=0.000 n=10+10)
DeepContextStack13-12                  14.7µs ± 1%    16.1µs ± 1%   +9.51%  (p=0.000 n=8+8)
BlankParentIteratorNextKeySize32-12    1.09µs ± 3%    1.03µs ± 3%   -4.77%  (p=0.000 n=9+10)
BlankParentAppendKeySize32-12           854ns ± 2%     674ns ±12%  -21.09%  (p=0.000 n=9+10)
SetKeySize32-12                        1.60µs ±29%    1.44µs ±14%     ~     (p=0.094 n=9+9)
IteratorOnParentWith1MDeletes-12        1.36s ± 3%     1.14s ± 2%  -15.53%  (p=0.000 n=10+9)
CacheKVStoreGetNoKeyFound-12            493ns ±10%     462ns ± 5%   -6.37%  (p=0.029 n=10+10)
CacheKVStoreGetKeyFound-12              195ns ±22%     168ns ± 2%  -14.01%  (p=0.000 n=10+10)

name                                 old alloc/op   new alloc/op   delta
LargeUnsortedMisses-12                 2.64MB ± 0%    2.64MB ± 0%     ~     (p=0.239 n=10+10)
DeepContextStack1-12                   3.03kB ± 0%    3.12kB ± 0%   +2.90%  (p=0.000 n=10+10)
DeepContextStack3-12                   3.59kB ± 0%    3.86kB ± 0%   +7.35%  (p=0.000 n=10+10)
DeepContextStack10-12                  5.55kB ± 0%    6.43kB ± 0%  +15.85%  (p=0.000 n=10+10)
DeepContextStack13-12                  6.39kB ± 0%    7.54kB ± 0%  +17.90%  (p=0.000 n=10+10)
BlankParentIteratorNextKeySize32-12      205B ± 0%      205B ± 0%     ~     (all equal)
BlankParentAppendKeySize32-12            243B ± 1%      258B ±15%     ~     (p=0.150 n=9+10)
SetKeySize32-12                          298B ±34%      288B ±13%     ~     (p=0.511 n=10+9)
IteratorOnParentWith1MDeletes-12        205MB ± 0%     205MB ± 0%   +0.00%  (p=0.000 n=10+10)
CacheKVStoreGetNoKeyFound-12             181B ± 6%      177B ± 1%     ~     (p=0.396 n=10+7)
CacheKVStoreGetKeyFound-12              3.00B ± 0%     3.00B ± 0%     ~     (all equal)

name                                 old allocs/op  new allocs/op  delta
LargeUnsortedMisses-12                  30.7k ± 0%     30.7k ± 0%     ~     (all equal)
DeepContextStack1-12                     42.0 ± 0%      44.0 ± 0%   +4.76%  (p=0.000 n=10+10)
DeepContextStack3-12                     50.0 ± 0%      56.0 ± 0%  +12.00%  (p=0.000 n=10+10)
DeepContextStack10-12                    78.0 ± 0%      98.0 ± 0%  +25.64%  (p=0.000 n=10+10)
DeepContextStack13-12                    90.0 ± 0%     116.0 ± 0%  +28.89%  (p=0.000 n=10+10)
BlankParentIteratorNextKeySize32-12      2.00 ± 0%      2.00 ± 0%     ~     (all equal)
BlankParentAppendKeySize32-12            1.00 ± 0%      1.00 ± 0%     ~     (all equal)
SetKeySize32-12                          1.30 ±54%      1.00 ± 0%     ~     (p=0.173 n=10+9)
IteratorOnParentWith1MDeletes-12        2.07M ± 0%     2.07M ± 0%   +0.00%  (p=0.000 n=10+10)
CacheKVStoreGetNoKeyFound-12             3.00 ± 0%      3.00 ± 0%     ~     (all equal)
CacheKVStoreGetKeyFound-12               1.00 ± 0%      1.00 ± 0%     ~     (all equal)

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Closes: cosmos#14376

Solution:
- copy isolated sortedCache when starting iterating
- cleanup deletion logic
@yihuang yihuang requested a review from a team as a code owner December 21, 2022 11:27
CHANGELOG.md Outdated Show resolved Hide resolved
store/cachekv/internal/btree.go Outdated Show resolved Hide resolved
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
panic(errKeyEmpty)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we panic over error? Ideally, the caller can handle the error.

Copy link
Collaborator Author

@yihuang yihuang Dec 21, 2022

Choose a reason for hiding this comment

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

I'm just trying to make it similar to KVStore interface, it's usage in the store is side with the parent store, so i try to make the interface similar to the parent store.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but I'd argue those interfaces are bad.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

agree, fixed.

store/cachekv/internal/memiterator.go Show resolved Hide resolved
yihuang and others added 6 commits December 22, 2022 01:46
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
@yihuang yihuang requested a review from alexanderbez December 21, 2022 18:46
@alexanderbez alexanderbez merged commit f1ee974 into cosmos:main Dec 21, 2022
@yihuang yihuang deleted the thread-safe-cachekv branch December 22, 2022 01:00
mmsqe pushed a commit to mmsqe/cosmos-sdk that referenced this pull request Dec 28, 2022
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
yihuang added a commit to yihuang/cosmos-sdk that referenced this pull request Jan 10, 2023
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
@yihuang yihuang mentioned this pull request Jan 27, 2023
19 tasks
@julienrbrt
Copy link
Member

@Mergifyio backport release/v0.47.x

@julienrbrt
Copy link
Member

backporting as per #14798 (comment)

mergify bot pushed a commit that referenced this pull request Jan 27, 2023
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
(cherry picked from commit f1ee974)

# Conflicts:
#	CHANGELOG.md
#	store/cachekv/internal/btree.go
#	store/cachekv/internal/memiterator.go
@mergify
Copy link
Contributor

mergify bot commented Jan 27, 2023

backport release/v0.47.x

✅ Backports have been created

julienrbrt pushed a commit that referenced this pull request Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make cachekv store thread-safe again
3 participants