datafusion-ballista icon indicating copy to clipboard operation
datafusion-ballista copied to clipboard

Add `ballista.execution.mode=auto` with a pluggable strategy for choosing `pipelined` or `staged` execution

Open milenkovicm opened this issue 3 weeks ago • 6 comments

Is your feature request related to a problem or challenge?

Ballista can run a query in two ways:

  • Staged execution: each stage writes its shuffle output in full before the next stage starts. It recovers well from failures and suits long, heavy jobs that write results somewhere, such as ETL, COPY TO and INSERT INTO.
  • Pipelined execution: stages stream data to each other, so results come back sooner. It suits interactive queries, where the user wants rows back fast and the result goes to the client.

Today users must pick one mode for the whole session. Most sessions run a mix of both kinds of query. Users either accept slower interactive queries or lose fault tolerance on big write jobs. Users also must know about the two modes and the trade-off between them in order to set the option at all.

Describe the solution you'd like

1. Add an auto execution mode

ballista.execution.mode = staged | pipelined | auto

With auto, the scheduler picks the mode for each job when the job is submitted.

2. Default heuristic: does the plan write output?

The built-in strategy treats a query that writes its result somewhere as batch work, and any other query as interactive:

Plan contains Treated as Mode
CopyTo node (COPY ... TO ..., DataFrame::write_*) batch / ETL staged
No CopyTo node interactive pipelined

This is cheap to check (one walk of the plan tree), easy to predict, and easy to explain to users.

3. Make the strategy pluggable

Deployments differ: one might want to choose by estimated data size, number of stages, tenant or job tags. So the decision should sit behind a trait, with the default heuristic as one implementation of it:

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExecutionMode {
    Staged,
    Pipelined,
}

/// Decides how a job runs when `ballista.execution.mode = auto`.
pub trait ExecutionModeStrategy: Send + Sync + std::fmt::Debug {
    fn select_mode(
        &self,
        plan: &LogicalPlan,          // or the physical plan (see open questions)
        config: &SessionConfig,
    ) -> datafusion::error::Result<ExecutionMode>;
}

/// Default: if the plan contains CopyTo, use staged; otherwise use pipelined.
#[derive(Debug, Default)]
pub struct CopyToBasedStrategy;

Users register their own strategy the same way they register other Ballista extensions, for example through the scheduler config or session state builder:

let config = SchedulerConfig::default()
    .with_execution_mode_strategy(Arc::new(MyStrategy::new()));

Behaviour:

  • staged or pipelined set explicitly: the strategy is not called. This is the current behaviour.
  • auto: the scheduler calls the registered strategy, or CopyToBasedStrategy if none is registered.
  • The chosen mode is logged and shown in job status, so users can see why a job ran the way it did.

Describe alternatives you've considered

  • Keep the mode as a session setting only: this is simple, but it pushes the decision onto users and handles mixed workloads poorly.
  • Hard-code the heuristic with no trait: this is easier to build, but many deployments will need different rules, so it would likely have to be reworked later.
  • Choose using cost or statistics: this could pick better, but it is much harder to predict. It can be built later as a custom strategy on top of this proposal.

Open questions

  1. Logical or physical plan: should the strategy look at the LogicalPlan, where CopyTo is easy to spot, or the ExecutionPlan, where DataSinkExec also covers INSERT INTO? Or should it get both?
  2. DML: should INSERT INTO and other DataSinkExec writes count as batch work in the default strategy, as CopyTo does?
  3. Where the decision is made: on the client before submission, or on the scheduler? Deciding on the scheduler keeps the choice in one place and lets operators enforce rules for every client.
  4. Fallback: if a pipelined job fails, should the scheduler retry it in staged mode?
  5. Default value: should auto become the default once it has proved stable?
  6. Will scheduler tolerate mixed workloads?
  7. Should pipelined tasks be prioritised ?

Additional context

This change is additive. Existing configs that set staged or pipelined keep working as before.

milenkovicm avatar Nov 10 '26 21:11 milenkovicm

Love this! Slightly swamped with work right now, will review shortly

villebro avatar Nov 10 '26 21:11 villebro

I suggest keeping the strategy based on the LogicalPlan. The logical plan is available before physical planning, so the strategy can select the execution mode before the ExecutionPlan is generated. COPY is directly represented as LogicalPlan::Copy, and INSERT INTO can be covered through LogicalPlan::Dml.

rizzabhdwaj avatar Nov 11 '26 07:11 rizzabhdwaj

@milenkovicm I really like this, and I feel auto would be a great default execution mode, as it's probably what fits the expectations and preferences typical users have. My quick thoughts:

  • I assume the decision would need to be made from the logical plan as opposed to the physical plan, as currently the physical plan depends on the mode.
  • Routing Copy and DML to staged execution makes sense.
  • The trait can't only take logical plans, as clients can also submit physical plans directly. So when a logical plan is available, use that, otherwise pass in the physical plan.
  • If auto becomes the default, it should fall back to staged on pull-staged schedulers, and we should document that pipelined jobs skip AQE.

Beyond this I feel it would be a great idea to support multiple strategies through some form of registry, so we could add more strategies in the future (both in vanilla Ballista, but also on custom crates). That way one wouldn't have to settle for a single strategy, but could have many, and set one either at startup as the default, or per session or query through config. I'm planning on doing this for scheduler HA strategies, and I feel this type of extensibility would be valuable in all areas of Ballista.

villebro avatar Nov 14 '26 20:11 villebro

The proposal is just to gauge interest for this functionality, nothing is set in stone and your feedback is valuable @villebro, thanks a lot.

We can discuss further, I'd like to implement in due course

milenkovicm avatar Nov 14 '26 23:11 milenkovicm

@milenkovicm that makes sense. The whole extensibility/registry suggestion here is certainly out of scope for this discussion, and should be broken out into a proposal of its own.

villebro avatar Nov 18 '26 17:11 villebro

I won't have time to do this for 56 release, i hope it's ok. I want to put some effort into it

milenkovicm avatar Nov 18 '26 20:11 milenkovicm