-
Notifications
You must be signed in to change notification settings - Fork 215
Proofs pool improvements #6878
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
Merged
Merged
Proofs pool improvements #6878
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
35f8e8a
remove sort operation from proofs pool insert
ssd04 91fd46e
added proofs cache buckets
ssd04 7066189
add parallel cleanup
ssd04 147c0d3
proofs cache benchmarks
ssd04 c21c78c
added bucket size to config
ssd04 5704d4e
Merge branch 'feat/andromeda-fixes' into fix-proofs-pool-insert
ssd04 3a1a9e2
fix after review
ssd04 e03e7a1
remove concurrency from cleanup
ssd04 b2fc09a
fix after review: keep only one cache
ssd04 ac7f4ab
Merge branch 'feat/andromeda-fixes' into fix-proofs-pool-insert
sstanculeanu b759ac8
Merge branch 'feat/andromeda-fixes' into fix-proofs-pool-insert
sstanculeanu 3cb4bce
Merge branch 'feat/andromeda-fixes' into fix-proofs-pool-insert
sstanculeanu 57532ba
Merge branch 'feat/andromeda-fixes' into fix-proofs-pool-insert
sstanculeanu 36ae586
Merge branch 'feat/andromeda-fixes' into fix-proofs-pool-insert
sstanculeanu 4ea5d8e
use range buckets instead of stack buckets
ssd04 21fd3ea
fixes after review
ssd04 210f52b
Merge branch 'feat/andromeda-fixes' into fix-proofs-pool-insert
sstanculeanu acf47c7
use simple map instead of sync.Map
ssd04 ecd4e54
delete map entry while iterating
ssd04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package proofscache | ||
|
|
||
| import "github.com/multiversx/mx-chain-core-go/data" | ||
|
|
||
| // NewProofsCache - | ||
| func NewProofsCache(bucketSize int) *proofsCache { | ||
| return newProofsCache(bucketSize) | ||
| } | ||
|
|
||
| // HeadBucketSize - | ||
| func (pc *proofsCache) FullProofsByNonceSize() int { | ||
| size := 0 | ||
|
|
||
| for _, bucket := range pc.proofsByNonceBuckets { | ||
| size += bucket.size() | ||
| } | ||
|
|
||
| return size | ||
| } | ||
|
|
||
| // ProofsByHashSize - | ||
| func (pc *proofsCache) ProofsByHashSize() int { | ||
| return len(pc.proofsByHash) | ||
| } | ||
|
|
||
| // AddProof - | ||
| func (pc *proofsCache) AddProof(proof data.HeaderProofHandler) { | ||
| pc.addProof(proof) | ||
| } | ||
|
|
||
| // CleanupProofsBehindNonce - | ||
| func (pc *proofsCache) CleanupProofsBehindNonce(nonce uint64) { | ||
| pc.cleanupProofsBehindNonce(nonce) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package proofscache | ||
|
|
||
| import "github.com/multiversx/mx-chain-core-go/data" | ||
|
|
||
| type proofNonceBucket struct { | ||
| maxNonce uint64 | ||
| proofsByNonce map[uint64]string | ||
| } | ||
|
|
||
| func newProofBucket() *proofNonceBucket { | ||
| return &proofNonceBucket{ | ||
| proofsByNonce: make(map[uint64]string), | ||
| } | ||
| } | ||
|
|
||
| func (p *proofNonceBucket) size() int { | ||
| return len(p.proofsByNonce) | ||
| } | ||
|
|
||
| func (p *proofNonceBucket) insert(proof data.HeaderProofHandler) { | ||
| p.proofsByNonce[proof.GetHeaderNonce()] = string(proof.GetHeaderHash()) | ||
|
|
||
| if proof.GetHeaderNonce() > p.maxNonce { | ||
| p.maxNonce = proof.GetHeaderNonce() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
dataRetriever/dataPool/proofsCache/proofsCache_bench_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package proofscache_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| proofscache "github.com/multiversx/mx-chain-go/dataRetriever/dataPool/proofsCache" | ||
| ) | ||
|
|
||
| func Benchmark_AddProof_Bucket10_Pool1000(b *testing.B) { | ||
| benchmarkAddProof(b, 10, 1000) | ||
| } | ||
|
|
||
| func Benchmark_AddProof_Bucket100_Pool10000(b *testing.B) { | ||
| benchmarkAddProof(b, 100, 10000) | ||
| } | ||
|
|
||
| func Benchmark_AddProof_Bucket1000_Pool100000(b *testing.B) { | ||
| benchmarkAddProof(b, 1000, 100000) | ||
| } | ||
|
Comment on lines
+10
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also add some output / statistics in the PR description. |
||
|
|
||
| func benchmarkAddProof(b *testing.B, bucketSize int, nonceRange int) { | ||
| pc := proofscache.NewProofsCache(bucketSize) | ||
|
|
||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| b.StopTimer() | ||
| proof := generateProof() | ||
| nonce := generateRandomNonce(int64(nonceRange)) | ||
|
|
||
| proof.HeaderNonce = nonce | ||
| proof.HeaderHash = []byte("hash_" + fmt.Sprintf("%d", nonce)) | ||
| b.StartTimer() | ||
|
|
||
| pc.AddProof(proof) | ||
| } | ||
| } | ||
|
|
||
| func Benchmark_CleanupProofs_Bucket10_Pool1000(b *testing.B) { | ||
| benchmarkCleanupProofs(b, 10, 1000) | ||
| } | ||
|
|
||
| func Benchmark_CleanupProofs_Bucket100_Pool10000(b *testing.B) { | ||
| benchmarkCleanupProofs(b, 100, 10000) | ||
| } | ||
|
|
||
| func Benchmark_CleanupProofs_Bucket1000_Pool100000(b *testing.B) { | ||
| benchmarkCleanupProofs(b, 1000, 100000) | ||
| } | ||
|
|
||
| func benchmarkCleanupProofs(b *testing.B, bucketSize int, nonceRange int) { | ||
| pc := proofscache.NewProofsCache(bucketSize) | ||
|
|
||
| for i := uint64(0); i < uint64(nonceRange); i++ { | ||
| proof := generateProof() | ||
| proof.HeaderNonce = i | ||
| proof.HeaderHash = []byte("hash_" + fmt.Sprintf("%d", i)) | ||
|
|
||
| pc.AddProof(proof) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| pc.CleanupProofsBehindNonce(uint64(nonceRange)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package proofscache_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/rand" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/multiversx/mx-chain-core-go/data/block" | ||
| proofscache "github.com/multiversx/mx-chain-go/dataRetriever/dataPool/proofsCache" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestProofsCache(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("incremental nonces, should cleanup all caches", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| proof0 := &block.HeaderProof{HeaderHash: []byte{0}, HeaderNonce: 0} | ||
| proof1 := &block.HeaderProof{HeaderHash: []byte{1}, HeaderNonce: 1} | ||
| proof2 := &block.HeaderProof{HeaderHash: []byte{2}, HeaderNonce: 2} | ||
| proof3 := &block.HeaderProof{HeaderHash: []byte{3}, HeaderNonce: 3} | ||
| proof4 := &block.HeaderProof{HeaderHash: []byte{4}, HeaderNonce: 4} | ||
|
|
||
| pc := proofscache.NewProofsCache(4) | ||
|
|
||
| pc.AddProof(proof0) | ||
| pc.AddProof(proof1) | ||
| pc.AddProof(proof2) | ||
| pc.AddProof(proof3) | ||
|
|
||
| require.Equal(t, 4, pc.FullProofsByNonceSize()) | ||
| require.Equal(t, 4, pc.ProofsByHashSize()) | ||
|
|
||
| pc.AddProof(proof4) // added to new head bucket | ||
|
|
||
| require.Equal(t, 5, pc.ProofsByHashSize()) | ||
|
|
||
| pc.CleanupProofsBehindNonce(4) | ||
| require.Equal(t, 1, pc.ProofsByHashSize()) | ||
|
|
||
| pc.CleanupProofsBehindNonce(10) | ||
| require.Equal(t, 0, pc.ProofsByHashSize()) | ||
| }) | ||
|
|
||
| t.Run("non incremental nonces", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| proof0 := &block.HeaderProof{HeaderHash: []byte{0}, HeaderNonce: 0} | ||
| proof1 := &block.HeaderProof{HeaderHash: []byte{1}, HeaderNonce: 1} | ||
| proof2 := &block.HeaderProof{HeaderHash: []byte{2}, HeaderNonce: 2} | ||
| proof3 := &block.HeaderProof{HeaderHash: []byte{3}, HeaderNonce: 3} | ||
| proof4 := &block.HeaderProof{HeaderHash: []byte{4}, HeaderNonce: 4} | ||
| proof5 := &block.HeaderProof{HeaderHash: []byte{5}, HeaderNonce: 5} | ||
|
|
||
| pc := proofscache.NewProofsCache(4) | ||
|
|
||
| pc.AddProof(proof4) | ||
| pc.AddProof(proof1) | ||
| pc.AddProof(proof2) | ||
| pc.AddProof(proof3) | ||
|
|
||
| require.Equal(t, 4, pc.FullProofsByNonceSize()) | ||
| require.Equal(t, 4, pc.ProofsByHashSize()) | ||
|
|
||
| pc.AddProof(proof0) // added to new head bucket | ||
|
|
||
| require.Equal(t, 5, pc.FullProofsByNonceSize()) | ||
| require.Equal(t, 5, pc.ProofsByHashSize()) | ||
|
|
||
| pc.CleanupProofsBehindNonce(4) | ||
|
|
||
| // cleanup up head bucket with only one proof | ||
| require.Equal(t, 1, pc.ProofsByHashSize()) | ||
|
|
||
| pc.AddProof(proof5) // added to new head bucket | ||
|
|
||
| require.Equal(t, 2, pc.ProofsByHashSize()) | ||
|
|
||
| pc.CleanupProofsBehindNonce(5) // will not remove any bucket | ||
| require.Equal(t, 2, pc.FullProofsByNonceSize()) | ||
| require.Equal(t, 2, pc.ProofsByHashSize()) | ||
|
|
||
| pc.CleanupProofsBehindNonce(10) | ||
| require.Equal(t, 0, pc.ProofsByHashSize()) | ||
| }) | ||
|
|
||
| t.Run("shuffled nonces, should cleanup all caches", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| pc := proofscache.NewProofsCache(10) | ||
|
|
||
| nonces := generateShuffledNonces(100) | ||
| for _, nonce := range nonces { | ||
| proof := generateProof() | ||
| proof.HeaderNonce = nonce | ||
| proof.HeaderHash = []byte("hash_" + fmt.Sprintf("%d", nonce)) | ||
|
|
||
| pc.AddProof(proof) | ||
| } | ||
|
|
||
| require.Equal(t, 100, pc.FullProofsByNonceSize()) | ||
| require.Equal(t, 100, pc.ProofsByHashSize()) | ||
|
|
||
| pc.CleanupProofsBehindNonce(100) | ||
| require.Equal(t, 0, pc.FullProofsByNonceSize()) | ||
| require.Equal(t, 0, pc.ProofsByHashSize()) | ||
| }) | ||
| } | ||
|
|
||
| func TestProofsCache_Concurrency(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| pc := proofscache.NewProofsCache(100) | ||
|
|
||
| numOperations := 1000 | ||
|
|
||
| wg := sync.WaitGroup{} | ||
| wg.Add(numOperations) | ||
|
|
||
| for i := 0; i < numOperations; i++ { | ||
| go func(idx int) { | ||
| switch idx % 2 { | ||
| case 0: | ||
| pc.AddProof(generateProof()) | ||
| case 1: | ||
| pc.CleanupProofsBehindNonce(generateRandomNonce(100)) | ||
| default: | ||
| assert.Fail(t, "should have not beed called") | ||
| } | ||
|
|
||
| wg.Done() | ||
| }(i) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| } | ||
|
|
||
| func generateShuffledNonces(n int) []uint64 { | ||
| nonces := make([]uint64, n) | ||
| for i := uint64(0); i < uint64(n); i++ { | ||
| nonces[i] = i | ||
| } | ||
|
|
||
| rand.Shuffle(len(nonces), func(i, j int) { | ||
| nonces[i], nonces[j] = nonces[j], nonces[i] | ||
| }) | ||
|
|
||
| return nonces | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we move the test to package
proofscache, maybe we can drop some code from theexport_testfile. Can also stay as it is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would keep it in _test package since there are other useful functions from test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and we plan to make this component more generic and to export it in other packages as well (for headers pool)