@@ -3,17 +3,17 @@ use crate::config_extension_ext::{
33 set_distributed_option_extension, set_distributed_option_extension_from_headers,
44} ;
55use crate :: events:: {
6- DesiredTaskCountHandler , DesiredTaskCountHandlers , RouteTasksHandler , RouteTasksHandlers ,
7- ScaleUpLeafNodeHandler , ScaleUpLeafNodeHandlers , WorkerPlanRewriteHandler ,
8- WorkerPlanRewriteHandlers ,
6+ DesiredTaskCountHandler , DesiredTaskCountHandlers , DynamicStageBuiltHandlers ,
7+ RouteTasksHandler , RouteTasksHandlers , ScaleUpLeafNodeHandler , ScaleUpLeafNodeHandlers ,
8+ WorkerPlanRewriteHandler , WorkerPlanRewriteHandlers ,
99} ;
1010use crate :: passthrough_headers:: set_passthrough_headers;
1111use crate :: protocol:: set_distributed_channel_resolver;
1212use crate :: work_unit_feed:: set_distributed_work_unit_feed;
1313use crate :: worker_resolver:: set_distributed_worker_resolver;
1414use crate :: {
15- ChannelResolver , DistributedConfig , LocalWorkerContext , WorkUnitFeed , WorkUnitFeedProvider ,
16- WorkerResolver , get_distributed_worker_resolver,
15+ ChannelResolver , DistributedConfig , DynamicStageBuiltHandler , LocalWorkerContext , WorkUnitFeed ,
16+ WorkUnitFeedProvider , WorkerResolver , get_distributed_worker_resolver,
1717} ;
1818use datafusion:: common:: DataFusionError ;
1919use datafusion:: config:: ConfigExtension ;
@@ -737,6 +737,39 @@ pub trait DistributedExt: Sized {
737737 & mut self ,
738738 handler : T ,
739739 ) ;
740+
741+ /// Register an event handler that fires every time a new stage is built during dynamic
742+ /// planning.
743+ ///
744+ /// In this handler, users can inspect the plan that is about to be sent to a remote worker,
745+ /// optimize it, or short circuit execution.
746+ ///
747+ /// ```rust
748+ /// # use datafusion::common::{exec_err, Result};
749+ /// # use datafusion::execution::SessionStateBuilder;
750+ /// # use datafusion_distributed::{DynamicStageBuiltEvent, DynamicStageBuiltEventResponse};
751+ ///
752+ /// fn handle_dynamic_stage_built(event: DynamicStageBuiltEvent) -> Option<Result<DynamicStageBuiltEventResponse>> {
753+ /// if event.cost.cpu.get_value().unwrap_or(&0) > 1024 * 1024 * 1024 {
754+ /// return Some(exec_err!("Plan is too expensive to execute"))
755+ /// }
756+ /// None
757+ /// }
758+ ///
759+ /// SessionStateBuilder::new()
760+ /// .with_distributed_dynamic_stage_built_handler(handle_dynamic_stage_built);
761+ /// ```
762+ fn with_distributed_dynamic_stage_built_handler < T : DynamicStageBuiltHandler > (
763+ self ,
764+ handler : T ,
765+ ) -> Self ;
766+
767+ /// Same as [DistributedExt::with_distributed_dynamic_stage_built_handler] but with an
768+ /// in-place mutation.
769+ fn set_distributed_dynamic_stage_built_handler < T : DynamicStageBuiltHandler > (
770+ & mut self ,
771+ handler : T ,
772+ ) ;
740773}
741774
742775/// Trait to have a unified interface for getting structs & properties from SessionConfig that are used in distributed context.
@@ -917,6 +950,10 @@ impl DistributedExt for SessionConfig {
917950 WorkerPlanRewriteHandlers :: push_custom ( self , Arc :: new ( h) ) ;
918951 }
919952
953+ fn set_distributed_dynamic_stage_built_handler < T : DynamicStageBuiltHandler > ( & mut self , h : T ) {
954+ DynamicStageBuiltHandlers :: push_custom ( self , Arc :: new ( h) ) ;
955+ }
956+
920957 delegate ! {
921958 to self {
922959 #[ call( set_distributed_option_extension) ]
@@ -1024,6 +1061,10 @@ impl DistributedExt for SessionConfig {
10241061 #[ call( set_distributed_worker_plan_rewrite_handler) ]
10251062 #[ expr( $; self ) ]
10261063 fn with_distributed_worker_plan_rewrite_handler<H : WorkerPlanRewriteHandler >( mut self , h: H ) -> Self ;
1064+
1065+ #[ call( set_distributed_dynamic_stage_built_handler) ]
1066+ #[ expr( $; self ) ]
1067+ fn with_distributed_dynamic_stage_built_handler<H : DynamicStageBuiltHandler >( mut self , h: H ) -> Self ;
10271068 }
10281069 }
10291070}
@@ -1172,6 +1213,11 @@ impl DistributedExt for SessionStateBuilder {
11721213 #[ call( set_distributed_worker_plan_rewrite_handler) ]
11731214 #[ expr( $; self ) ]
11741215 fn with_distributed_worker_plan_rewrite_handler<H : WorkerPlanRewriteHandler >( mut self , h: H ) -> Self ;
1216+
1217+ fn set_distributed_dynamic_stage_built_handler<H : DynamicStageBuiltHandler >( & mut self , h: H ) ;
1218+ #[ call( set_distributed_dynamic_stage_built_handler) ]
1219+ #[ expr( $; self ) ]
1220+ fn with_distributed_dynamic_stage_built_handler<H : DynamicStageBuiltHandler >( mut self , h: H ) -> Self ;
11751221 }
11761222 }
11771223}
@@ -1322,6 +1368,11 @@ impl DistributedExt for SessionState {
13221368 #[ call( set_distributed_worker_plan_rewrite_handler) ]
13231369 #[ expr( $; self ) ]
13241370 fn with_distributed_worker_plan_rewrite_handler<H : WorkerPlanRewriteHandler >( mut self , h: H ) -> Self ;
1371+
1372+ fn set_distributed_dynamic_stage_built_handler<H : DynamicStageBuiltHandler >( & mut self , h: H ) ;
1373+ #[ call( set_distributed_dynamic_stage_built_handler) ]
1374+ #[ expr( $; self ) ]
1375+ fn with_distributed_dynamic_stage_built_handler<H : DynamicStageBuiltHandler >( mut self , h: H ) -> Self ;
13251376 }
13261377 }
13271378}
@@ -1465,6 +1516,11 @@ impl DistributedExt for SessionContext {
14651516 #[ call( set_distributed_worker_plan_rewrite_handler) ]
14661517 #[ expr( $; self ) ]
14671518 fn with_distributed_worker_plan_rewrite_handler<H : WorkerPlanRewriteHandler >( self , h: H ) -> Self ;
1519+
1520+ fn set_distributed_dynamic_stage_built_handler<H : DynamicStageBuiltHandler >( & mut self , h: H ) ;
1521+ #[ call( set_distributed_dynamic_stage_built_handler) ]
1522+ #[ expr( $; self ) ]
1523+ fn with_distributed_dynamic_stage_built_handler<H : DynamicStageBuiltHandler >( self , h: H ) -> Self ;
14681524 }
14691525 }
14701526}
0 commit comments