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

[Access] Upgrade lru cache #4605

Closed
wants to merge 88 commits into from
Closed

Conversation

nozim
Copy link
Contributor

@nozim nozim commented Aug 8, 2023

Following up with #4565, upgrade all the dependencies on lru.Cache with generic version for enhanced performance.

@nozim nozim requested a review from peterargue as a code owner August 8, 2023 10:04
@nozim nozim changed the title Upgrade lru cache [Access] Upgrade lru cache Aug 8, 2023
@codecov-commenter
Copy link

codecov-commenter commented Aug 8, 2023

Codecov Report

Patch coverage: 71.60% and project coverage change: +0.54% 🎉

Comparison is base (ac879c7) 54.57% compared to head (04e8222) 55.11%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4605      +/-   ##
==========================================
+ Coverage   54.57%   55.11%   +0.54%     
==========================================
  Files         917      592     -325     
  Lines       85903    59393   -26510     
==========================================
- Hits        46878    32733   -14145     
+ Misses      35431    24253   -11178     
+ Partials     3594     2407    -1187     
Flag Coverage Δ
unittests 55.11% <71.60%> (+0.54%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
engine/access/rpc/backend/backend_network.go 34.11% <ø> (ø)
engine/access/rpc/backend/backend_block_headers.go 24.61% <20.00%> (ø)
engine/access/rpc/backend/backend_block_details.go 21.42% <22.22%> (ø)
engine/access/rpc/backend/backend.go 66.25% <25.00%> (+0.20%) ⬆️
engine/access/rpc/backend/backend_scripts.go 66.52% <25.00%> (+0.84%) ⬆️
engine/consensus/approvals/approvals_lru_cache.go 49.18% <50.00%> (-0.82%) ⬇️
engine/access/rpc/connection/cache.go 95.55% <100.00%> (ø)
engine/access/rpc/connection/mock_node.go 100.00% <100.00%> (ø)
fvm/storage/derived/derived_chain_data.go 90.47% <100.00%> (ø)

... and 335 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@peterargue peterargue left a comment

Choose a reason for hiding this comment

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

thanks @nozim! This will clean things up nicely

@@ -119,7 +118,7 @@ func New(
retry.Activate()
}

loggedScripts, err := lru.New(DefaultLoggedScriptsCacheSize)
loggedScripts, err := lru.New[[16]byte, time.Time](DefaultLoggedScriptsCacheSize)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
loggedScripts, err := lru.New[[16]byte, time.Time](DefaultLoggedScriptsCacheSize)
loggedScripts, err := lru.New[[md5.Size]byte, time.Time](DefaultLoggedScriptsCacheSize)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

connFactory connection.ConnectionFactory
log zerolog.Logger
metrics module.BackendScriptsMetrics
loggedScripts *lru.Cache[[16]byte, time.Time]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
loggedScripts *lru.Cache[[16]byte, time.Time]
loggedScripts *lru.Cache[[md5.Size]byte, time.Time]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"pgregory.net/rapid"

lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
Copy link
Contributor

Choose a reason for hiding this comment

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

here and all other places, we try to use the following import group ordering

import (
	"std-libs"
	
	"external/packages"
	
	"onflow/other-repo/packages"
	
	"onflow/flow-go/packages"
)

the linter will enforce local mode imports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -129,8 +124,8 @@ func TestProxyAccessAPIConnectionReuse(t *testing.T) {
connectionFactory.CollectionGRPCPort = cn.port
// set the connection pool cache size
cacheSize := 1
cache, _ := lru.NewWithEvict(cacheSize, func(_, evictedValue interface{}) {
evictedValue.(*CachedClient).Close()
cache, _ := lru.NewWithEvict[string, *CachedClient](cacheSize, func(_ string, client *CachedClient) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: how about refactoring a bit to move the duplicated code to a helper?

func getCache(t *testing.T, cacheSize int) *lru.Cache[string, *CachedClient] {
	cache, err := lru.NewWithEvict[string, *CachedClient](cacheSize, func(_ string, client *CachedClient) {
		err := client.Close()
		require.NoError(t, err)
	})
	require.NoError(t, err)
	return cache
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

engine/access/rpc/connection/connection_test.go Outdated Show resolved Hide resolved
engine/access/rpc/connection/connection_test.go Outdated Show resolved Hide resolved
engine/access/rpc/connection/connection_test.go Outdated Show resolved Hide resolved
@jordanschalm
Copy link
Member

@nozim The CI is failing because the generated mocks do not exactly match the committed mocks (the imports are not in the same order).

If you run make generate-mocks from the repository root, it should fix the mock files.

@nozim nozim requested review from jordanschalm and kc1116 August 23, 2023 13:43
@jordanschalm
Copy link
Member

Hey @nozim, since the last review the diff has grown quite a bit. The additions don't seem related this PR. Was there an inadvertent merge of some other branch? It seems like the extra unrelated changes were added in this commit: c2938e4.

@nozim
Copy link
Contributor Author

nozim commented Aug 25, 2023

@jordanschalm yes, I'll revert those changes after the other one is merged. Thanks for bringing this up 🙏

@nozim
Copy link
Contributor Author

nozim commented Aug 28, 2023

@jordanschalm removed irrelevant changes. Kindly re-review.

@nozim
Copy link
Contributor Author

nozim commented Aug 31, 2023

@jordanschalm kindly help to rerun the ci 🙏

engine/access/rpc/backend/backend.go Outdated Show resolved Hide resolved
@nozim
Copy link
Contributor Author

nozim commented Sep 1, 2023

@peterargue kindly run the ci. Fixed the annotation

@nozim
Copy link
Contributor Author

nozim commented Sep 9, 2023

Recreated this PR on latest master here #4700. So closing this one.

@nozim nozim closed this Sep 9, 2023
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.

7 participants