Skip to content

Commit

Permalink
editoast: use spawn_blocking for long processing tasks
Browse files Browse the repository at this point in the history
Also add some telemetry to look at the proportion in time.

Signed-off-by: Jean SIMARD <[email protected]>
  • Loading branch information
woshilapin committed Oct 11, 2024
1 parent 2b23321 commit e6614b7
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions editoast/src/views/train_schedule/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ async fn project_path(
routes: path_routes,
blocks: path_blocks,
} = path;
let path_projection = PathProjection::new(&path_track_ranges);
let mut redis_conn = redis_client.get_connection().await?;

let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || {
Expand Down Expand Up @@ -263,7 +262,14 @@ async fn project_path(

// 2 Compute space time curves and signal updates for all miss cache
let (space_time_curves, signal_updates) = join!(
compute_batch_space_time_curves(&miss_cache, &path_projection),
{
let miss_cache = miss_cache.clone();
let path_track_ranges = path_track_ranges.clone();
tokio::task::spawn_blocking(move || {
let path_projection = PathProjection::new(&path_track_ranges);
compute_batch_space_time_curves(&miss_cache, &path_projection)
})
},
compute_batch_signal_updates(
core_client.clone(),
&infra,
Expand All @@ -273,6 +279,7 @@ async fn project_path(
&miss_cache
)
);
let space_time_curves = space_time_curves.expect("spawn_blocking was killed in-flight");
let signal_updates = signal_updates?;

// 3. Store the projection in the cache (using pipeline)
Expand Down Expand Up @@ -339,6 +346,7 @@ struct TrainSimulationDetails {
}

/// Compute the signal updates of a list of train schedules
#[tracing::instrument(level = "info", skip_all)]
async fn compute_batch_signal_updates<'a>(
core: Arc<CoreClient>,
infra: &Infra,
Expand Down Expand Up @@ -375,7 +383,8 @@ async fn compute_batch_signal_updates<'a>(
}

/// Compute space time curves of a list of train schedules
async fn compute_batch_space_time_curves<'a>(
#[tracing::instrument(level = "info", skip_all, fields(train_details_count = trains_details.len()))]
fn compute_batch_space_time_curves<'a>(
trains_details: &HashMap<i64, TrainSimulationDetails>,
path_projection: &PathProjection<'a>,
) -> HashMap<i64, Vec<SpaceTimeCurve>> {
Expand All @@ -391,6 +400,7 @@ async fn compute_batch_space_time_curves<'a>(
}

/// Compute the space time curves of a train schedule on a path
#[tracing::instrument(level = "info", skip_all)]
fn compute_space_time_curves(
project_path_input: &TrainSimulationDetails,
path_projection: &PathProjection,
Expand Down

0 comments on commit e6614b7

Please sign in to comment.