kaxil commented on code in PR #67127:
URL: https://github.com/apache/airflow/pull/67127#discussion_r3475540805


##########
providers/celery/src/airflow/providers/celery/executors/celery_executor_utils.py:
##########
@@ -389,24 +408,12 @@ def send_workload_to_executor(
     Send workload to executor (serialized and executed as a Celery task).
 
     This function is called in ProcessPoolExecutor subprocesses. To avoid 
pickling issues with
-    team-specific Celery apps, we pass the team_name and reconstruct the 
Celery app here.
+    team-specific Celery apps, we pass the team_name and create the app in the 
subprocess. The app
+    is cached per subprocess and team so Celery backend resolution is 
amortized across publishes.

Review Comment:
   This docstring says the app cache amortizes backend resolution "across 
publishes", but the `ProcessPoolExecutor` in `_send_workloads_to_celery` is 
recreated and torn down on every publish, so in the subprocess path the cache 
only lives for one batch. The durable win is the inline path (`len==1` or 
`sync_parallelism==1`), which runs in the long-lived scheduler process and is 
exactly the 1-vCPU repro from #67123. Might be worth tightening the wording so 
a future reader doesn't assume the pool-path cache survives across publishes.



##########
providers/celery/tests/unit/celery/executors/test_celery_executor.py:
##########
@@ -551,6 +558,100 @@ def 
test_send_workload_uses_external_executor_id_as_celery_task_id():
     assert result.task_id == pre_assigned_id
 
 
[email protected]("team_name", [None, "team-a"])
+def test_get_celery_app_for_workload_reuses_cache_for_same_team(team_name):
+    first_app = mock.Mock()
+    second_app = mock.Mock()
+
+    with mock.patch(
+        
"airflow.providers.celery.executors.celery_executor_utils.create_celery_app",
+        side_effect=[first_app, second_app],
+    ) as mock_create_celery_app:
+        assert celery_executor_utils._get_celery_app_for_workload(team_name) 
is first_app
+        assert celery_executor_utils._get_celery_app_for_workload(team_name) 
is first_app
+
+    mock_create_celery_app.assert_called_once()
+
+
+def test_get_celery_app_for_workload_keeps_cache_team_scoped():
+    team_a_app = mock.Mock()
+    team_b_app = mock.Mock()
+
+    with mock.patch(
+        
"airflow.providers.celery.executors.celery_executor_utils.create_celery_app",
+        side_effect=[team_a_app, team_b_app],
+    ) as mock_create_celery_app:
+        assert celery_executor_utils._get_celery_app_for_workload("team-a") is 
team_a_app
+        assert celery_executor_utils._get_celery_app_for_workload("team-b") is 
team_b_app
+
+    assert mock_create_celery_app.call_count == 2
+
+
+def test_send_workload_reuses_celery_app_for_same_team():
+    """Publishing multiple workloads for the same team reuses the cached 
Celery app."""
+    celery_executor_utils._get_celery_app_for_workload.cache_clear()

Review Comment:
   The autouse `clear_cached_workload_celery_apps` fixture already clears the 
cache before and after every test in this module, so this explicit 
`cache_clear()` (and the matching one in 
`test_send_workload_keeps_celery_app_cache_team_scoped`) is redundant. The 
other two new cache tests rely on the fixture. (The integration-test 
`cache_clear()` calls are necessary there, since that file has no such fixture.)



-- 
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]

Reply via email to