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

Kan/clean up secondary result index #1515

Merged
merged 7 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions engine/consensus/approvals/approvals_lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func NewApprovalsLRUCache(limit uint) *LruCache {
lru, _ := simplelru.NewLRU(int(limit), func(key interface{}, value interface{}) {
approval := value.(*flow.ResultApproval)
delete(byResultID[approval.Body.ExecutionResultID], approval.Body.PartialID())
if len(byResultID[approval.Body.ExecutionResultID]) == 0 {
delete(byResultID, approval.Body.ExecutionResultID)
}
})
return &LruCache{
lru: lru,
Expand Down
49 changes: 49 additions & 0 deletions engine/consensus/approvals/approvals_lru_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package approvals

import (
"sync"
"testing"

"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/unittest"
)

// TestApprovalsCache_Get_Put_All tests common use cases for approvals cache.
func TestApprovalsLRUCacheSecondaryIndexPurge(t *testing.T) {
numElements := uint(10)
cache := NewApprovalsLRUCache(numElements)
approvals := make([]*flow.ResultApproval, 2*numElements)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems this approvals field isn't really necessary? We don't actually do anything with the approvals we store inside.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you actually meant to add a check?

for i := range approvals {
approval := unittest.ResultApprovalFixture()
approvals[i] = approval
cache.Put(approval)
require.Equal(t, approval, cache.Get(approval.Body.PartialID()))
}

// LRU kept the last 10 approvals
for i := 10; i < 20; i++ {
approval := approvals[i]
require.Equal(t, approval, cache.Get(approval.Body.PartialID()))
}
require.Len(t, cache.byResultID, int(numElements))
}

func TestApprovalsLRUCacheSecondaryIndexPurgeConcurrently(t *testing.T) {
numElements := 100
cache := NewApprovalsLRUCache(uint(numElements))

var wg sync.WaitGroup

for i := 0; i < 2*numElements; i++ {
wg.Add(1)
go func() {
defer wg.Done()
approval := unittest.ResultApprovalFixture()
cache.Put(approval)
}()
}
wg.Wait()
require.Len(t, cache.byResultID, int(numElements))
}
12 changes: 8 additions & 4 deletions engine/consensus/approvals/caches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,30 @@ import (

// TestApprovalsCache_Get_Put_All tests common use cases for approvals cache.
func TestApprovalsCache_Get_Put_All(t *testing.T) {
cache := NewApprovalsCache(10)
approvals := make([]*flow.ResultApproval, 0, 10)
numElements := uint(10)
cache := NewApprovalsCache(numElements)
approvals := make([]*flow.ResultApproval, numElements)
for i := range approvals {
approval := unittest.ResultApprovalFixture()
approvals[i] = approval
require.True(t, cache.Put(approval.Body.PartialID(), approval))
require.Equal(t, approval, cache.Get(approval.Body.PartialID()))
}
require.ElementsMatch(t, approvals, cache.All())
require.Len(t, cache.All(), int(numElements))
}

// TestIncorporatedResultsCache_Get_Put_All tests common use cases for incorporated results cache.
func TestIncorporatedResultsCache_Get_Put_All(t *testing.T) {
cache := NewIncorporatedResultsCache(10)
results := make([]*flow.IncorporatedResult, 0, 10)
numElements := uint(10)
cache := NewIncorporatedResultsCache(numElements)
results := make([]*flow.IncorporatedResult, numElements)
for i := range results {
result := unittest.IncorporatedResult.Fixture()
results[i] = result
require.True(t, cache.Put(result.ID(), result))
require.Equal(t, result, cache.Get(result.ID()))
}
require.ElementsMatch(t, results, cache.All())
require.Equal(t, int(numElements), len(cache.All()))
Copy link
Contributor

Choose a reason for hiding this comment

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

same comments here as above.

}