Skip to content

Rip Rayon out of reducer execution and query evaluation #2543

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/core/src/host/wasm_common/module_host_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ impl<T: WasmInstance> WasmModuleInstance<T> {
)
.entered();

// run the call_reducer call in rayon. it's important that we don't acquire a lock inside a rayon task,
// as that can lead to deadlock.
let (mut tx, result) = rayon::scope(|_| tx_slot.set(tx, || self.instance.call_reducer(op, budget)));
// FOR BENCHMARKING: Just run the reducer on whatever thread we're already on,
// instead of bouncing to a Rayon thread.
let (mut tx, result) = tx_slot.set(tx, || self.instance.call_reducer(op, budget));

let ExecuteResult {
energy,
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/subscription/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::sync::Arc;

use anyhow::Result;
use module_subscription_manager::Plan;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use spacetimedb_client_api_messages::websocket::{
ByteListLen, Compression, DatabaseUpdate, QueryUpdate, TableUpdate, WebsocketFormat,
};
Expand Down Expand Up @@ -112,8 +111,9 @@ where
Tx: Datastore + DeltaStore + Sync,
F: WebsocketFormat,
{
// FOR TESTING: Just evaluate sequentially.
plans
.par_iter()
.iter()
.map(|plan| (plan, plan.subscribed_table_id(), plan.subscribed_table_name()))
.map(|(plan, table_id, table_name)| {
plan.physical_plan()
Expand Down
33 changes: 17 additions & 16 deletions crates/core/src/subscription/module_subscription_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::subscription::delta::eval_delta;
use crate::subscription::record_exec_metrics;
use hashbrown::hash_map::OccupiedError;
use hashbrown::{HashMap, HashSet};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use spacetimedb_client_api_messages::websocket::{
BsatnFormat, CompressableQueryUpdate, Compression, FormatSwitch, JsonFormat, QueryId, QueryUpdate, WebsocketFormat,
};
Expand Down Expand Up @@ -463,8 +462,8 @@ impl SubscriptionManager {

let tables = &event.status.database_update().unwrap().tables;

// Put the main work on a rayon compute thread.
rayon::scope(|_| {
// FOR TESTING: Just do all this work on whatever the calling thread is.
{
let span = tracing::info_span!("eval_incr").entered();
let (updates, errs, metrics) = tables
.iter()
Expand All @@ -473,15 +472,16 @@ impl SubscriptionManager {
.filter_map(|table_id| self.tables.get(table_id))
.flatten()
.collect::<HashSet<_>>()
.par_iter()
// FOR TESTING: Sequential execution, rather than parallel.
.iter()
.filter_map(|&hash| {
self.queries
.get(hash)
.map(|state| (hash, &state.query, ExecutionMetrics::default()))
})
// If N clients are subscribed to a query,
// we copy the DatabaseTableUpdate N times,
// which involves cloning BSATN (binary) or product values (json).
// If N clients are subscribed to a query,
// we copy the DatabaseTableUpdate N times,
// which involves cloning BSATN (binary) or product values (json).
.map(|(hash, plan, mut metrics)| {
let table_id = plan.subscribed_table_id();
let table_name: Box<str> = plan.subscribed_table_name().into();
Expand Down Expand Up @@ -561,7 +561,7 @@ impl SubscriptionManager {
(updates, metrics)
})
.fold(
|| (vec![], vec![], ExecutionMetrics::default()),
(vec![], vec![], ExecutionMetrics::default()),
|(mut rows, mut errs, mut agg_metrics), (result, metrics)| {
match result {
Ok(x) => {
Expand All @@ -575,13 +575,14 @@ impl SubscriptionManager {
(rows, errs, agg_metrics)
},
)
.reduce_with(|(mut acc_rows, mut acc_errs, mut acc_metrics), (rows, errs, metrics)| {
acc_rows.extend(rows);
acc_errs.extend(errs);
acc_metrics.merge(metrics);
(acc_rows, acc_errs, acc_metrics)
})
.unwrap_or_default();
// .reduce_with(|(mut acc_rows, mut acc_errs, mut acc_metrics), (rows, errs, metrics)| {
// acc_rows.extend(rows);
// acc_errs.extend(errs);
// acc_metrics.merge(metrics);
// (acc_rows, acc_errs, acc_metrics)
// })
// .unwrap_or_default()
;

record_exec_metrics(&WorkloadType::Update, database_identity, metrics);

Expand Down Expand Up @@ -685,7 +686,7 @@ impl SubscriptionManager {
);
}
}
})
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/subscription/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use crate::sql::ast::SchemaViewer;
use crate::vm::{build_query, TxMode};
use anyhow::Context;
use itertools::Either;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use spacetimedb_client_api_messages::websocket::{Compression, WebsocketFormat};
use spacetimedb_data_structures::map::HashSet;
use spacetimedb_lib::db::auth::{StAccess, StTableType};
Expand Down Expand Up @@ -519,11 +518,11 @@ impl ExecutionSet {
slow_query_threshold: Option<Duration>,
compression: Compression,
) -> ws::DatabaseUpdate<F> {
// evaluate each of the execution units in this ExecutionSet in parallel
// FOR TESTING: Just do sequential execution.
let tables = self
.exec_units
// if you need eval to run single-threaded for debugging, change this to .iter()
.par_iter()
.iter()
.filter_map(|unit| unit.eval(db, tx, &unit.sql, slow_query_threshold, compression))
.collect();
ws::DatabaseUpdate { tables }
Expand Down
Loading