Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Task executor extension for runtime host #5249

Merged
merged 1 commit into from
Mar 16, 2020

Conversation

NikVolf
Copy link
Contributor

@NikVolf NikVolf commented Mar 13, 2020

This will essentially allow spawning tasks when handling host functions, using the main scheduler.

It is immediately needed for #5023, also for variant of #5023 for block production

And is generally useful in a sense that it will allow parallelise runtime execution (but should be used carefully and join spawned tasks deterministically - one-by-one / or in well-defined order if multiple are joined)

@NikVolf NikVolf added the A0-please_review Pull request needs code review. label Mar 13, 2020
/// Return handle that can spawn parallel tasks.
///
/// Can be none if host does not support parallel tasks.
fn spawn_handle(&self) -> Option<&dyn ClonableSpawn>;
Copy link
Member

Choose a reason for hiding this comment

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

I really don't want to add anymore functionality to Externalities. This was the reason Extension's where introduced. I know that extension handling is currently a bit shitty, but please make this an extension.

Copy link
Contributor Author

@NikVolf NikVolf Mar 13, 2020

Choose a reason for hiding this comment

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

Fine with me as Extension as well, but just to note and as discussed: this Externalities ability will be quite essential (modulo test implementations of it), while Extension-s are optional and generally not required for code execution.

Copy link
Contributor Author

@NikVolf NikVolf Mar 13, 2020

Choose a reason for hiding this comment

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

Ok after some more disussion with @kianenigma there is a strong indication that this functionality will need to be rather configurable, and Extension will definitely help to achieve configurability in much more convenient way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Mot notably, if we can configure the child threads (or whatever we call them in this context) to not be able to write to storage and instead return the buffer changes, this will give us a big chunk of what we need for transaction parallelism.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bkchr updated!

@kianenigma
Copy link
Contributor

kianenigma commented Mar 13, 2020

CC @tomusdrw ^^ / #1459

@NikVolf NikVolf changed the title Pass task scheduler to Externalities, LocalCallExecutor, Client Host parallelization infrastructure Mar 13, 2020
@NikVolf NikVolf force-pushed the nv-externalities-scheduler branch from 7dfa665 to 2fa2411 Compare March 14, 2020 06:26
@NikVolf
Copy link
Contributor Author

NikVolf commented Mar 14, 2020

I will squash / force push this pr, because it is used in another pr, so sorry about that

@NikVolf NikVolf changed the title Host parallelization infrastructure Task executor extension for runtime host Mar 14, 2020
@NikVolf NikVolf force-pushed the nv-externalities-scheduler branch 7 times, most recently from 2c8716f to a11fa28 Compare March 14, 2020 16:17
Copy link
Contributor

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

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

lgtm, few minor grumbles that I'm not super strong on.

@@ -212,3 +212,21 @@ impl CallInWasmExt {
Self(Box::new(inner))
}
}

/// Something that can spawn tasks and also can be cloned.
pub trait CloneableSpawn: futures::task::Spawn + Send + Sync {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not a fan of traits that just describe the expected programatic behavior instead of some more high-level code idea.
Also this causes Box<dyn CloneableSpawn> to be quite widespread.

Can't we have a struct that will just wrap this? Like so:

#[derive(Clone)]
struct TaskExecutor(Arc<dyn futures::Task::Spawn + Send + Sync>);
impl TaskExecutor {
  pub fn spawn(...) {}
}

We don't even need this extra trait cause it's in Arc and I feel it avoids much of code repetition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've considered something like that. The problem is we won't be able to have special implementation of clone in task executor this way. It might require little more than just shared reference clone in use cases I think about.

@@ -70,6 +70,8 @@ mod changes_trie;
#[cfg(feature = "std")]
pub mod traits;
pub mod testing;
#[cfg(feature = "std")]
pub mod tasks;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not 100% sure this fits into sp-core, but at the same time we don't really have a better place currently, so I'm fine with this (unless someone has a better idea where to put it).

Copy link
Member

Choose a reason for hiding this comment

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

We probably could put everything tasks related (Also the CloneableSpawn trait) in its own crate, but the current amount of code isn't also that much.

@NikVolf NikVolf force-pushed the nv-externalities-scheduler branch 2 times, most recently from ebb37e7 to 2f61e79 Compare March 16, 2020 10:30
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Some nitpicks, otherwise looks good.

client/service/src/task_manager.rs Outdated Show resolved Hide resolved
primitives/core/Cargo.toml Outdated Show resolved Hide resolved
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Module for low-level asynchronous processing.
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this just providing a single threaded executor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, at the moment, yes

@@ -70,6 +70,8 @@ mod changes_trie;
#[cfg(feature = "std")]
pub mod traits;
pub mod testing;
#[cfg(feature = "std")]
pub mod tasks;
Copy link
Member

Choose a reason for hiding this comment

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

We probably could put everything tasks related (Also the CloneableSpawn trait) in its own crate, but the current amount of code isn't also that much.

primitives/state-machine/Cargo.toml Outdated Show resolved Hide resolved
primitives/externalities/Cargo.toml Outdated Show resolved Hide resolved
@NikVolf NikVolf force-pushed the nv-externalities-scheduler branch from 2f61e79 to ecee163 Compare March 16, 2020 11:16
@NikVolf NikVolf force-pushed the nv-externalities-scheduler branch from ecee163 to a6a9e5c Compare March 16, 2020 12:14
@NikVolf NikVolf added B0-silent Changes should not be mentioned in any release notes A8-looksgood and removed A0-please_review Pull request needs code review. labels Mar 16, 2020
@NikVolf NikVolf added this to the 2.0 milestone Mar 16, 2020
@gavofyork gavofyork merged commit 0bd9ffa into master Mar 16, 2020
@gavofyork gavofyork deleted the nv-externalities-scheduler branch March 16, 2020 15:30
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
B0-silent Changes should not be mentioned in any release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants