CarlosGitto opened a new issue, #77594:
URL: https://github.com/apache/airflow/issues/77594

   ### Under which category would you file this issue?
   
   Providers
   
   ### Apache Airflow version
   
   3.1.8
   
   ### What happened and how to reproduce it?
   
   ### Deployment details
   
   CeleryExecutor on Azure.
   
   ### What happened
   
   We use event-driven scheduling: Azure Event Grid -> Service Bus topic -> one 
subscription per asset ->
   `AzureServiceBusSubscriptionTrigger` -> `AssetWatcher` -> DAG (6-7 assets, 
one watcher each, all on
   subscriptions of the same topic).
   
   **1. `max_message_count` is documented but not accepted.**
   The trigger docs page (`connections/message_bus.rst`,
   
https://airflow.apache.org/docs/apache-airflow-providers-microsoft-azure/stable/connections/message_bus.html)
   shows `max_message_count=1` in the examples for both 
`AzureServiceBusQueueTrigger` and
   `AzureServiceBusSubscriptionTrigger` (present since #53356). Neither trigger 
accepts it:
   
   ```
   TypeError: AzureServiceBusSubscriptionTrigger.__init__() got an unexpected 
keyword argument 'max_message_count'
   ```
   
   **2. Throughput is capped at one message per poll.**
   The triggers call `MessageHook.read_message` / 
`MessageHook.read_subscription_message`, which hard-code
   `receive_messages(max_message_count=1, ...)`, and then sleep `poll_interval` 
(default 60s). With the defaults a
   watcher consumes about one message per minute even when the subscription has 
a backlog. Lowering
   `poll_interval` / `max_wait_time` (we tried 10 / 5) increases the frequency 
but still processes one message per cycle.
   
   **3. Service Bus triggers in the same triggerer serialize on one thread.**
   `run()` wraps the sync hook with `asgiref.sync_to_async` using the default 
`thread_sensitive=True`, so all those calls in a triggerer process run on a 
single shared thread. A watcher whose subscription is empty holds that thread 
for up to `max_wait_time` and delays every other Service Bus watcher in the 
process.
   
   In a local simulation using the real trigger code with a mocked hook 
(`poll_interval=1`, `max_wait_time=2`), a watcher with a backlog dropped from 
~48 messages/min (alone) to ~9/min with 3 idle watchers in the same process, 
and to ~6/min with 6. The event loop itself is not blocked, so no "Triggerer's 
async thread was blocked" warning is logged; the slowdown is silent.
   
   With the default `max_wait_time=None`: from reading the SDK source, 
azure-servicebus <= 7.14 applies no receive deadline in that case (an empty 
subscription would hold the shared thread until a message arrives), while 
azure-servicebus 7.15.0 defaults to 60s.
   
   ### How to reproduce
   
   1. Create a topic `repro-topic` with a subscription `sub-backlog` and three 
subscriptions `sub-idle-{0,1,2}`
      whose rule matches nothing (`SqlRuleFilter("1=0")`).
   2. Send 50 messages to the topic.
   3. Deploy this DAG with default trigger settings:
   
   ```python
   from airflow.providers.microsoft.azure.triggers.message_bus import 
AzureServiceBusSubscriptionTrigger
   from airflow.sdk import Asset, AssetWatcher, dag, task
   
   
   def watched_asset(sub: str) -> Asset:
       trigger = AzureServiceBusSubscriptionTrigger(
           topics=["repro-topic"],
           subscription_name=sub,
           azure_service_bus_conn_id="azure_service_bus_default",
       )
       return Asset(f"asb_repro_{sub}", 
watchers=[AssetWatcher(name=f"watcher_{sub}", trigger=trigger)])
   
   
   backlog = watched_asset("sub-backlog")
   idle = [watched_asset(f"sub-idle-{i}") for i in range(3)]
   
   
   @dag(schedule=[backlog])
   def asb_repro():
       @task
       def show(**context):
           print(len(context["triggering_asset_events"][backlog]))
   
       show()
   
   
   @dag(schedule=idle)
   def asb_repro_idle():
       @task
       def noop():
           pass
   
       noop()
   
   
   asb_repro()
   asb_repro_idle()
   ```
   
   4. Observe that `sub-backlog` drains at about one message per 
`poll_interval`, and slower once the idle watchers are active (triggerer log 
"Trigger fired event" timestamps, or the subscription's active message count).
   5. Add `max_message_count=10` to the trigger: `TypeError`.
   
   ### What you think should happen instead?
   
   - Both triggers accept `max_message_count`, receive up to that many messages 
per poll and yield one
     `TriggerEvent` per message, as the documentation suggests.
   - Watchers on idle subscriptions should not starve other watchers running in 
the same triggerer.
   
   ### Operating System
   
   Ubuntu 24.04.2 LTS
   
   ### Deployment
   
   Astronomer
   
   ### Apache Airflow Provider(s)
   
   microsoft-azure
   
   ### Versions of Apache Airflow Providers
   
   apache-airflow-providers-microsoft-azure==14.0.0
   azure-servicebus==7.15.0
   Reproduced on every version tested up to 15.3.1rc1; the trigger and hook 
code is unchanged in 15.4.0 and in main.
   
   ### Official Helm Chart version
   
   Not Applicable
   
   ### Kubernetes Version
   
   Not Applicable
   
   ### Helm Chart configuration
   
   Not Applicable
   
   ### Docker Image customizations
   
   Base image: Astro Runtime 3.1-15 (Airflow 3.1.8).
   Extra Python packages installed via requirements.txt, including
   apache-airflow-providers-microsoft-azure==14.0.0.
   No other customizations relevant to this issue.
   
   ### Anything else?
   
   
   Proposed changes, happy to discuss the approach before opening any PR:
   
   1. Add `max_message_count` to both triggers (backward compatible, default 
`1`). I have a working patch with tests and can open a PR once the approach is 
agreed.
   2. Use a finite default for `max_wait_time`, or document that it should 
always be set explicitly.
   3. Stop sharing one thread across Service Bus triggers: either 
`sync_to_async(..., thread_sensitive=False)` or
      the async SDK (`azure.servicebus.aio`). `thread_sensitive=False` would 
let hook calls such as `get_connection` run concurrently from worker threads, 
which may interact with the triggerer comms issues fixed in #66412, so the 
async SDK may be the safer option.
   4. Messages are completed before the trigger event is persisted 
(at-most-once delivery). The shared-stream ack mode in main could address this 
later.
   
   cc @RushabhNovelis  , who found this with me.
   
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


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