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] Add caching tx results #4598

Merged
merged 70 commits into from
Sep 5, 2023

Conversation

nozim
Copy link
Contributor

@nozim nozim commented Aug 3, 2023

Re: #4585

@nozim nozim requested a review from peterargue as a code owner August 3, 2023 11:21
@nozim nozim changed the title 4585 add caching tx results [Access] Add caching tx results Aug 3, 2023
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 for picking this up @nozim! added a few comments.

.gitignore Outdated Show resolved Hide resolved
cmd/access/node_builder/access_node_builder.go Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
engine/access/rpc/backend/backend.go Outdated Show resolved Hide resolved
engine/access/rpc/backend/backend.go Outdated Show resolved Hide resolved
engine/access/rpc/backend/backend_transactions_test.go Outdated Show resolved Hide resolved
engine/access/rpc/backend/backend_transactions_test.go Outdated Show resolved Hide resolved
engine/access/rpc/backend/backend_transactions_test.go Outdated Show resolved Hide resolved
engine/access/rpc/backend/backend_transactions_test.go Outdated Show resolved Hide resolved
On("ByBlockID", block.ID()).
Return(l, nil)

suite.historicalAccessClient.
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
suite.historicalAccessClient.
// assert this is called exactly once for the 2 API calls
suite.historicalAccessClient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, added .Once(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

can you also add the comment. it makes it easier for the next person to look at these tests to quickly understand what's happening

@nozim
Copy link
Contributor Author

nozim commented Aug 11, 2023

@peterargue thank you for review, fixes on the way ;)

@kc1116
Copy link
Contributor

kc1116 commented Aug 24, 2023

@nozim please ping me when all feedback has been addressed

@nozim nozim force-pushed the 4585-add-caching-tx-results branch from e2eaf25 to fd03e0f Compare August 24, 2023 21:50
@nozim
Copy link
Contributor Author

nozim commented Aug 28, 2023

@kc1116 could you please rerun ci, fixed last linter error.

@nozim
Copy link
Contributor Author

nozim commented Aug 29, 2023

@kc1116 @peterargue kindly run the ci. Fixed all the errors.

@nozim
Copy link
Contributor Author

nozim commented Aug 31, 2023

flow-go/network/test test are failing on master as well. Kindly let me know when they are fixed and I'll sync with master.
@kc1116 @peterargue

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 looks great. Also thanks for cleaning up the backend inputs. the list was getting pretty long.

Added a few small comments, but I think this is ready to go.

Comment on lines +141 to +146
if params.TxResultCacheSize > 0 {
txResCache, err = lru2.New[flow.Identifier, *access.TransactionResult](int(params.TxResultCacheSize))
if err != nil {
params.Log.Fatal().Err(err).Msg("failed to init cache for transaction results")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

my 2cents is to use the value 0 to indicate that the cache is disabled.

I originally requested that it be disabled by default to start and we can enable it in the future. It's realistically only useful on nodes where users frequently lookup non-existent TXs, so only infrastructure providers will realistically get value from it.

@@ -6,6 +6,7 @@ import (
"fmt"
"time"

lru2 "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.

Let's move this to a separate issue. We're already using this library within the access api, and adding a new herocache type is non-trivial.

}

// Tx not found. If we have historical Sporks setup, lets look through those as well
if b.txResultCache != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

@kc1116 this cache is here to avoid unnecessary lookups on the historic nodes since they are very expensive. the linked issue has some more context

Comment on lines 52 to 53
call func(id *flow.Identity) error,
shouldTerminateOnError func(node *flow.Identity, err error) bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

please use the original types. They help document the callbacks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was added to avoid import cycle due to mock -> backend -> mock dependency.

Copy link
Contributor Author

@nozim nozim Sep 2, 2023

Choose a reason for hiding this comment

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

If i add them back I'll have to amend makefile mockgen which will differ from conventional way 😊

Copy link
Contributor

Choose a reason for hiding this comment

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

ok. then please move the comments from the function types into the CallAvailableNode godocs so devs can easily see the usage details. You can remove those function types if they're unused.

Comment on lines +73 to +85
Params{
State: suite.state,
CollectionRPC: suite.colClient,
Blocks: suite.blocks,
Transactions: suite.transactions,
ExecutionReceipts: suite.receipts,
ChainID: suite.chainID,
AccessMetrics: metrics.NewNoopCollector(),
MaxHeightRange: DefaultMaxHeightRange,
Log: suite.log,
SnapshotHistoryLimit: DefaultSnapshotHistoryLimit,
Communicator: suite.communicator,
})
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you could add a defaultParams() method which returns this default set. That way you don't need to repeat this block of code in each test. If you need to use a specific value for some fields, you can just update the param set before creating the backend

On("ByBlockID", block.ID()).
Return(l, nil)

suite.historicalAccessClient.
Copy link
Contributor

Choose a reason for hiding this comment

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

can you also add the comment. it makes it easier for the next person to look at these tests to quickly understand what's happening

suite.Require().Equal(flow.TransactionStatusExecuted, resp.Status)
suite.Require().Equal(uint(flow.TransactionStatusExecuted), resp.StatusCode)

resp2, err := backend.GetTransactionResult(context.Background(), tx.ID(), block.ID(), coll.ID())
Copy link
Contributor

Choose a reason for hiding this comment

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

also here

Suggested change
resp2, err := backend.GetTransactionResult(context.Background(), tx.ID(), block.ID(), coll.ID())
// the second call should return the correct response without generating a call to the historic node
resp2, err := backend.GetTransactionResult(context.Background(), tx.ID(), block.ID(), coll.ID())

func (suite *Suite) TestGetTransactionResultCacheNonExistent() {
suite.withGetTransactionCachingTestSetup(func(block *flow.Block, tx *flow.Transaction) {

suite.historicalAccessClient.
Copy link
Contributor

Choose a reason for hiding this comment

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

here too

@peterargue
Copy link
Contributor

flow-go/network/test test are failing on master as well. Kindly let me know when they are fixed and I'll sync with master. @kc1116 @peterargue

There's a PR in the works to make that suite more reliable. Once that's merged and you pull the changes, it should pass.

@nozim
Copy link
Contributor Author

nozim commented Sep 1, 2023

@peterargue I'd be more than happy to address all your last inputs in following PR if you don't mind.

@nozim
Copy link
Contributor Author

nozim commented Sep 5, 2023

@kc1116 kindly rerun the ci. should be fixed now.

@peterargue
Copy link
Contributor

@peterargue I'd be more than happy to address all your last inputs in following PR if you don't mind.

feel free to skip the nit, but please do address my notes about adding comments to the tests and node_communicator.go

@nozim
Copy link
Contributor Author

nozim commented Sep 5, 2023

@peterargue kindly check 69b4339 🙏

@franklywatson franklywatson added this pull request to the merge queue Sep 5, 2023
Merged via the queue into onflow:master with commit 3cc4c32 Sep 5, 2023
36 checks passed
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.

5 participants