potiuk commented on a change in pull request #6725: [AIRFLOW-6157] Support for 
multiple executors
URL: https://github.com/apache/airflow/pull/6725#discussion_r353706586
 
 

 ##########
 File path: airflow/executors/executor_loader.py
 ##########
 @@ -31,47 +32,108 @@ class ExecutorLoader:
     DASK_EXECUTOR = "DaskExecutor"
     KUBERNETES_EXECUTOR = "KubernetesExecutor"
 
-    _default_executor: Optional[BaseExecutor] = None
+    _default_executor: Optional[BaseExecutorProtocol] = None
+
+    _all_executors: Dict[str, BaseExecutor] = dict()
 
     @classmethod
-    def get_default_executor(cls) -> BaseExecutor:
+    def get_default_executor(cls) -> BaseExecutorProtocol:
         """Creates a new instance of the configured executor if none exists 
and returns it"""
         if cls._default_executor is not None:
             return cls._default_executor
 
         from airflow.configuration import conf
-        executor_name = conf.get('core', 'EXECUTOR')
+        main_executor_name = conf.get('core', 'EXECUTOR')
 
-        cls._default_executor = ExecutorLoader._get_executor(executor_name)
+        main_executor = cls._get_executor(main_executor_name)
 
+        additional_executor_specs = conf.get('core', "ADDITIONAL_EXECUTORS")
+        if additional_executor_specs:
+            executor_array = additional_executor_specs.split(' ')
+            additional_executors_dict: Dict[str, BaseExecutor] = {}
+            for executor_spec in executor_array:
+                executor_name, queue_array_string = executor_spec.split(':')
+                executor = cls._get_executor(executor_name)
+                queue_array = queue_array_string.split(",")
+                for queue_name in queue_array:
+                    additional_executors_dict[queue_name] = executor
+            cls._default_executor = MultipleExecutors(
+                main_executor=main_executor,
+                additional_executors_dict=additional_executors_dict)
+        else:
+            cls._default_executor = main_executor
         from airflow import LoggingMixin
         log = LoggingMixin().log
-        log.info("Using executor %s", executor_name)
+        if additional_executor_specs:
+            log.info("Using MultipleExecutor with main executor %s and 
additional executors %s",
+                     main_executor_name, additional_executor_specs)
+        else:
+            log.info("Using executor %s", main_executor_name)
 
         return cls._default_executor
 
+    @classmethod
+    def get_or_create_executor(cls, executor_name: str,
+                               create_executor: Callable[[], BaseExecutor]) -> 
BaseExecutor:
+        """Retrieves (and creates if needed) an executor with the name 
specified"""
+        if executor_name in cls._all_executors:
+            return cls._all_executors[executor_name]
+        executor = create_executor()
+        cls._all_executors[executor_name] = executor
+        return executor
+
+    @staticmethod
+    def create_local_executor() -> BaseExecutor:
+        """Creates LocalExecutor"""
+        from airflow.executors.local_executor import LocalExecutor
+        return LocalExecutor()
+
     @staticmethod
-    def _get_executor(executor_name: str) -> BaseExecutor:
+    def create_sequential_executor() -> BaseExecutor:
+        """Creates SequentialExecutor"""
+        from airflow.executors.sequential_executor import SequentialExecutor
+        return SequentialExecutor()
+
+    @staticmethod
+    def create_celery_executor() -> BaseExecutor:
+        """Creates CeleryExecutor"""
+        from airflow.executors.celery_executor import CeleryExecutor
+        return CeleryExecutor()
+
+    @staticmethod
+    def create_dask_executor() -> BaseExecutor:
+        """Creates DaskExecutor"""
+        from airflow.executors.dask_executor import DaskExecutor
+        return DaskExecutor()
+
+    @staticmethod
+    def create_kubernetes_executor() -> BaseExecutor:
+        """Creates KubernetesExecutor"""
+        from airflow.executors.kubernetes_executor import KubernetesExecutor
+        return KubernetesExecutor()
+
+    @classmethod
+    def _get_executor(cls, executor_name: str) -> BaseExecutor:
         """
         Creates a new instance of the named executor.
         In case the executor name is unknown in airflow,
-        look for it in the plugins
+        look for it in the plugins.
         """
         if executor_name == ExecutorLoader.LOCAL_EXECUTOR:
-            from airflow.executors.local_executor import LocalExecutor
-            return LocalExecutor()
+            return cls.get_or_create_executor(executor_name=executor_name,
+                                              
create_executor=ExecutorLoader.create_local_executor)
         elif executor_name == ExecutorLoader.SEQUENTIAL_EXECUTOR:
-            from airflow.executors.sequential_executor import 
SequentialExecutor
-            return SequentialExecutor()
+            return cls.get_or_create_executor(executor_name=executor_name,
+                                              
create_executor=ExecutorLoader.create_sequential_executor)
         elif executor_name == ExecutorLoader.CELERY_EXECUTOR:
-            from airflow.executors.celery_executor import CeleryExecutor
-            return CeleryExecutor()
+            return cls.get_or_create_executor(executor_name=executor_name,
+                                              
create_executor=ExecutorLoader.create_celery_executor)
         elif executor_name == ExecutorLoader.DASK_EXECUTOR:
-            from airflow.executors.dask_executor import DaskExecutor
-            return DaskExecutor()
+            return cls.get_or_create_executor(executor_name=executor_name,
+                                              
create_executor=ExecutorLoader.create_dask_executor)
         elif executor_name == ExecutorLoader.KUBERNETES_EXECUTOR:
-            from airflow.executors.kubernetes_executor import 
KubernetesExecutor
-            return KubernetesExecutor()
+            return cls.get_or_create_executor(executor_name=executor_name,
+                                              
create_executor=ExecutorLoader.create_kubernetes_executor)
 
 Review comment:
   sure. We can set variable and make single return :)

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to