mbutrovich opened a new issue, #2939:
URL: https://github.com/apache/datafusion-ballista/issues/2939

   In a pass where a repeat reads a finished stage, 
`find_runnable_exchanges_reusing_stages` shares every repeat it found and then 
replans 
([planner.rs#L632-L643](https://github.com/apache/datafusion-ballista/blob/b93f86a807b87bbc54cf9a596fec389b2c2dbf5e/ballista/scheduler/src/state/aqe/planner.rs#L632-L643)).
 If the same pass also found two twins that are both about to launch, the 
second becomes a reader of the first while neither has a stage id. 
`replan_stages` then runs the whole chain over both subtrees, including an 
embedder's `replan_rules`, which may rewrite a stage that hasn't started 
([adaptive_rules.rs#L47-L51](https://github.com/apache/datafusion-ballista/blob/b93f86a807b87bbc54cf9a596fec389b2c2dbf5e/ballista/scheduler/src/state/aqe/adaptive_rules.rs#L47-L51)).
   
   When a rule rewrites one twin and not the other, the next pass keys them 
differently and puts both in `launching`. They still share one stage handle, so 
`identify_runnable_stages` launches the first and skips the second through 
`claimed` 
([planner.rs#L710-L716](https://github.com/apache/datafusion-ballista/blob/b93f86a807b87bbc54cf9a596fec389b2c2dbf5e/ballista/scheduler/src/state/aqe/planner.rs#L710-L716)).
 The second exchange's plan never runs, and its consumer reads the first's 
output.
   
   The module doc says matching at launch keys each stage after every rule of 
the replan chain, an embedder's included 
([reuse_exchange.rs#L29-L31](https://github.com/apache/datafusion-ballista/blob/b93f86a807b87bbc54cf9a596fec389b2c2dbf5e/ballista/scheduler/src/state/aqe/reuse_exchange.rs#L29-L31)).
 On this path the twins share before the replan.
   
   ## Reproduction
   
   Append this to `ballista/scheduler/src/state/aqe/test/reuse_exchange.rs` on 
`main` (`b93f86a8`) and run `cargo test -p ballista-scheduler --lib 
twins_found_while_sharing`. Stage 0 finishes, then a repeat of it and a pair of 
twins are released together. The rule stands in for an embedder's replan rule 
that rewrites one twin's pending stage.
   
   ```rust
   #[derive(Debug)]
   struct FilterPendingExchange(usize);
   
   impl PhysicalOptimizerRule for FilterPendingExchange {
       fn optimize(
           &self,
           plan: Arc<dyn ExecutionPlan>,
           _config: &ConfigOptions,
       ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
           use datafusion::physical_expr::expressions::lit;
           use datafusion::physical_plan::filter::FilterExec;
           plan.transform_up(|node| match node.downcast_ref::<ExchangeExec>() {
               Some(exchange)
                   if exchange.plan_id() == self.0
                       && !exchange.is_inactive_stage()
                       && exchange.stage_id().is_none()
                       && !exchange.input().is::<FilterExec>() =>
               {
                   let filter: Arc<dyn ExecutionPlan> = 
Arc::new(FilterExec::try_new(
                       lit(true),
                       Arc::clone(exchange.input()),
                   )?);
                   Ok(Transformed::yes(
                       Arc::clone(&node).with_new_children(vec![filter])?,
                   ))
               }
               _ => Ok(Transformed::no(node)),
           })
           .map(|transformed| transformed.data)
       }
   
       fn name(&self) -> &str {
           "FilterPendingExchange"
       }
   
       fn schema_check(&self) -> bool {
           true
       }
   }
   
   fn twin(plan_id: usize) -> ExchangeExec {
       let schema = Arc::new(Schema::new(vec![Field::new("k", DataType::Int32, 
false)]));
       let key: Arc<dyn PhysicalExpr> = Arc::new(Column::new("k", 0));
       ExchangeExec::new(
           Arc::new(EmptyExec::new(schema)),
           Some(Partitioning::Hash(vec![key], 3)),
           plan_id,
       )
   }
   
   #[test]
   fn twins_found_while_sharing_a_finished_stage_match_after_the_replan() {
       let mut planner = planner_over(
           vec![
               exchange(0),
               exchange(1).with_inactive_stage(true),
               twin(2).with_inactive_stage(true),
               twin(3).with_inactive_stage(true),
           ],
           vec![Arc::new(FilterPendingExchange(3))],
       );
       planner.identify_runnable_stages().unwrap().unwrap();
       planner
           .finalise_stage_internal(0, mock_partitions_with_statistics())
           .unwrap();
   
       release_held_back_exchanges(&mut planner);
       let stages = planner.identify_runnable_stages().unwrap().unwrap();
   
       assert_eq!(stage_ids(&stages), vec![Some(1), Some(2)]);
   }
   ```
   
   It fails with only one stage launched for the pair:
   
   ```text
     left: [Some(1)]
    right: [Some(1), Some(2)]
   ```
   
   ## Suggested fix
   
   Share only repeats of stages in `reusable_stages` in a pass that replans, 
and leave twins among `launching` to the next pass, which shares and launches 
them with no rule in between. Collect the `Entry::Occupied` repeats into a 
second map, pass only the first map to `share_stages` before `replan_stages`, 
and merge the second map into it before the final `share_stages`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to