-
Notifications
You must be signed in to change notification settings - Fork 65
apollo_gateway: move get_state_reader_from_latest_block to be async #10119
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
apollo_gateway: move get_state_reader_from_latest_block to be async #10119
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
3813fb6 to
3d0e7ae
Compare
TzahiTaub
left a comment
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.
@TzahiTaub reviewed 4 of 6 files at r1, all commit messages.
Reviewable status: 4 of 6 files reviewed, all discussions resolved (waiting on @ArniStarkware)
TzahiTaub
left a comment
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.
@TzahiTaub reviewed 1 of 6 files at r1.
Reviewable status: 5 of 6 files reviewed, 1 unresolved discussion (waiting on @ArniStarkware)
crates/apollo_gateway/src/stateful_transaction_validator.rs line 61 at r1 (raw file):
// TODO(yael 6/5/2024): consider storing the block_info as part of the // StatefulTransactionValidator and update it only once a new block is created. let state_reader = tokio::runtime::Handle::current()
I'm not sure what happens here, as we're not in an async context. See this. Does it work at all? If so, is because we assume the instantiate_validator function was called using a tokio::task::spawn_blocking somewhere in the stack?
Code quote:
tokio::runtime::Handle::current()|
Previously, TzahiTaub (Tzahi) wrote…
I will insert runtime as arguemnt to the function |
|
Cause (runtime error):
Cause (compilation error):
This tokio wrapper will be removed in the next PR when instantiate_validator will become himself an async function. |
3d0e7ae to
bd540e1
Compare
TzahiTaub
left a comment
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.
@TzahiTaub reviewed 8 of 8 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @ArniStarkware and @itamar-starkware)
crates/apollo_gateway/src/stateful_transaction_validator_test.rs line 179 at r1 (raw file):
Previously, itamar-starkware wrote…
- Why I don't run instantiate_validator without any tokio wrapper:
let validator = stateful_validator_factory .instantiate_validator(&mock_state_reader_factory, tokio::runtime::Handle::current());Cause (runtime error):
Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.
- Why I don't use
spawn_blocking:let validator = tokio::task::spawn_blocking(|| { stateful_validator_factory .instantiate_validator(&mock_state_reader_factory, tokio::runtime::Handle::current()) })Cause (compilation error):
`dyn stateful_transaction_validator::StatefulTransactionValidatorTrait` cannot be sent between threads safely the trait `std::marker::Send` is not implemented for `dyn stateful_transaction_validator::StatefulTransactionValidatorTrait` required for `std::ptr::Unique<dyn stateful_transaction_validator::StatefulTransactionValidatorTrait>` to implement `std::marker::Send`
- Why I use multi-threaded tokio and not single threaded tokio:
#[tokio::test]
Cause (runtime error):
can call blocking only when running on the multi-threaded runtimeThis tokio wrapper will be removed in the next PR when instantiate_validator will become himself an async function.
- is what I wanted as it is very similar to what we do in the code, but we don't return the validator as done here in the test, so it compiles. This is OK, but should be reverted in the following PRs when possible.
bd540e1 to
4f323c8
Compare
ArniStarkware
left a comment
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.
@ArniStarkware reviewed 1 of 2 files at r3.
Reviewable status: 7 of 8 files reviewed, 1 unresolved discussion (waiting on @itamar-starkware and @TzahiTaub)
ArniStarkware
left a comment
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.
@ArniStarkware reviewed 5 of 8 files at r2, 1 of 2 files at r3, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @itamar-starkware and @TzahiTaub)
ArniStarkware
left a comment
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.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @TzahiTaub)
crates/apollo_gateway/src/sync_state_reader.rs line 362 at r3 (raw file):
let latest_block_number = self .runtime // TODO(guy.f): Do we want to count this as well?
I wonder what that means...
(This is a joke about how I don't understand this comment - I am in favor of deleting this line. This comment is non-blocking. Still - it would be nice to know.)
Code quote:
// TODO(guy.f): Do we want to count this as well?
ArniStarkware
left a comment
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.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @itamar-starkware and @TzahiTaub)
crates/apollo_gateway/src/stateful_transaction_validator_test.rs line 179 at r1 (raw file):
Previously, TzahiTaub (Tzahi) wrote…
- is what I wanted as it is very similar to what we do in the code, but we don't return the validator as done here in the test, so it compiles. This is OK, but should be reverted in the following PRs when possible.
Add a todo to delete this.
IIUC, it was deleted in #10122, but it would be nice to confirm.
TzahiTaub
left a comment
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.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @ArniStarkware)
crates/apollo_gateway/src/sync_state_reader.rs line 362 at r3 (raw file):
Previously, ArniStarkware (Arnon Hod) wrote…
I wonder what that means...
(This is a joke about how I don't understand this comment - I am in favor of deleting this line. This comment is non-blocking. Still - it would be nice to know.)
Asked what it means. Please revive it with the following (Sahaks wants it to stay).
// TODO(guy.f): the call to `get_latest_block_number()` is not counted in the storage metrics as it is done prior to the creation of
SharedStateSyncClientMetricWrapper, directly via the SharedStateSyncClient. Fix this and add this call to the metrics.
Also, please ask the TODO creator before removing one when possible.
TzahiTaub
left a comment
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.
@TzahiTaub reviewed 2 of 2 files at r3.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @ArniStarkware)
4f323c8 to
2aecae9
Compare
|
Previously, ArniStarkware (Arnon Hod) wrote…
It is indeed deleted. |
|
Previously, TzahiTaub (Tzahi) wrote…
Done. |
TzahiTaub
left a comment
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.
@TzahiTaub reviewed 2 of 2 files at r4, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @itamar-starkware)

TL;DR
Made
get_state_reader_from_latest_blockan async function to improve performance and resource utilization.What changed?
#[async_trait]attribute to theStateReaderFactorytrait and its implementationsget_state_reader_from_latest_blockfrom a synchronous to an async functionSyncStateReaderFactoryimplementation to useawaitinstead ofblock_onStatefulTransactionValidatorFactoryto handle the async state readerHow to test?
cargo testtest_instantiate_validatorpasses with the new async implementationStateReaderFactorycorrectly handle the async behaviorWhy make this change?
Converting the state reader interface to async improves performance by avoiding blocking operations. This prevents thread pool exhaustion and allows for better resource utilization, especially during high-load scenarios. The change is part of a larger effort to make the codebase more asynchronous and efficient.