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

Update to tokio 0.2 and futures 0.3 #1448

Merged
merged 26 commits into from
Jan 20, 2020
Merged

Update to tokio 0.2 and futures 0.3 #1448

merged 26 commits into from
Jan 20, 2020

Conversation

leoyvens
Copy link
Collaborator

@leoyvens leoyvens commented Jan 13, 2020

This brings into the future of futures by upgrading to tokio 0.2, futures 0.3, hyper 0.13 and various other dependency upgrades. This not only allows us to use async/.await but also pushes us towards that by having updated dependencies. Some parts of the code were updated to use async/.await to serve as examples. This is working fine as far as I have tested it.

Using std::future and async

There's now a Future trait that lives in std, though helpers are still in the futures crate. The major difference from the one in futures 0.1 is that it has a single Output associated type, instead of Item and Error (same for streams). It is easy to convert back and forth between a std future that has Output = Result<_, _> and the old futures by calling .compat().

The async syntax can be thought of as a future literal, and works on fns and blocks (but not closures, yet). To use async in trait fns the async-trait crate is required, I haven't tried that yet. In blocks you usually want async move. Inside an async fn or block, you can use .await on futures as it were the old and_then, but more flexible. Using future combinators is still useful, so checkout the docs for FutureExt and TryFutureExt to catch up on them. There is no new syntax for streams, yet. Sometimes the compiler will complain about Unpin, I haven't totally understood the pinning stuff but wrapping the future in a Box::pin usually fixes that.

Spawning tasks and panic handling

Usually we want a tokio task panic to abort the process so that the node has a chance to recover by restarting. Except in some situations such as in the runtime or in queries. So instead of using tokio::spawn directly, we should choose between:

  • graph::spawn which runs on the default threadpool and aborts on panic.
  • graph::spawn_blocking which is run on the blocking threadpool and aborts on panic.
  • graph::spawn_blocking_allow_panic which is run on the blocking threadpool and allows panics. A panic will result in an error in the JoinHandle.

Note that all of these now return a JoinHandle for joining the task, which can be very useful.

Resolves #937, since we're now careful to use the blocking thread pool when called for, and we're also back to the default thread pool size.

Resolves #805, panics in query processing are no longer fatal.

@leoyvens leoyvens force-pushed the leo/update-tokio branch 3 times, most recently from 39f35c4 to 2b237ca Compare January 15, 2020 00:01
@leoyvens leoyvens marked this pull request as ready for review January 15, 2020 16:17
@That3Percent That3Percent self-assigned this Jan 15, 2020
@leoyvens leoyvens requested a review from a team January 15, 2020 16:25
@leoyvens leoyvens force-pushed the leo/update-tokio branch 2 times, most recently from 00a9a48 to 75d0dd0 Compare January 15, 2020 20:08
Copy link
Contributor

@That3Percent That3Percent left a comment

Choose a reason for hiding this comment

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

Mostly suggestions which I leave to your judgment, but there are a couple blocking issues here that need be addressed, particularly tests not running and futures not being polled.

core/src/subgraph/instance_manager.rs Show resolved Hide resolved
core/src/subgraph/instance_manager.rs Show resolved Hide resolved
core/src/subgraph/registrar.rs Show resolved Hide resolved
core/tests/tests.rs Show resolved Hide resolved
runtime/wasm/src/module/test.rs Outdated Show resolved Hide resolved
runtime/wasm/src/module/test.rs Show resolved Hide resolved
graph/src/task_spawn.rs Show resolved Hide resolved
graph/src/log/elastic.rs Outdated Show resolved Hide resolved
node/src/main.rs Show resolved Hide resolved
@leoyvens
Copy link
Collaborator Author

@That3Percent Thank you for the review, I believe I've addressed all comments.

@Jannis
Copy link
Contributor

Jannis commented Jan 17, 2020

There's a wip commit in this PR. Can we change that? 😉

Copy link
Contributor

@That3Percent That3Percent left a comment

Choose a reason for hiding this comment

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

Nice. LGTM

@leoyvens
Copy link
Collaborator Author

@Jannis You've caught me, I squashed that and rebased. After tests pass, I'll merge.

@Jannis
Copy link
Contributor

Jannis commented Jan 17, 2020

I'm doing a review pass myself. Can you wait for that? I'll make it quick. 😄

@leoyvens
Copy link
Collaborator Author

@Jannis np at all, thanks for also reviewing.

Copy link
Contributor

@Jannis Jannis left a comment

Choose a reason for hiding this comment

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

Looking good! I merely have a few questions.

chain/ethereum/src/block_ingestor.rs Show resolved Hide resolved
core/src/link_resolver.rs Show resolved Hide resolved
graph/src/components/store.rs Outdated Show resolved Hide resolved
graph/src/components/store.rs Outdated Show resolved Hide resolved
graph/src/util/futures.rs Outdated Show resolved Hide resolved
server/index-node/src/service.rs Show resolved Hide resolved
server/index-node/src/service.rs Show resolved Hide resolved
store/postgres/src/chain_head_listener.rs Outdated Show resolved Hide resolved
@leoyvens
Copy link
Collaborator Author

@Jannis I've addressed your review.

@leoyvens leoyvens merged commit 6d41cc9 into master Jan 20, 2020
@leoyvens leoyvens deleted the leo/update-tokio branch January 20, 2020 15:38
@leoyvens leoyvens restored the leo/update-tokio branch February 28, 2020 19:26
@leoyvens leoyvens deleted the leo/update-tokio branch February 28, 2020 19:27
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.

Reduce thread contention in subgraph indexing Catch panics in query processing
3 participants