You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »

Status

StateDraft
Discussion Thread

https://lists.apache.org/thread/zxpbhwb0nb28st4q9ddnf2900d828xsm

Vote Thread
Vote Result Thread
Progress Tracking (PR/GitHub Project/Issue Label)
Date Created

03.12.2024 13:40

Version Released
AuthorsDavid Blain

Motivation

This AIP was a proposition to support an alternative way of expanding multiple XCom’s on operators within the same task instance (e.g. worker instance), especially in Airflow 2.x, hence we will wait until Airflow 3.0 is released and re-evaluate how well it will then handle +10k XCom’s.

Beside that, I would like to introduce streamable XCom's.  In the current Airflow implementation, only list, dicts and simple build-in Python types are supported as XCom types.  I would like to add support for iterable's.  The reason why I would like to introduce the support of iterables is to allow the implementation of streamable XCom's.  In case of the HttpOperator or the MSGraphAsyncOperator for example, when those return pages results, the operator depending on the results of that operator needs to wait until all pages have been loaded before being able to process them.

This has 2 disadvantages:

  • performance, as depending operators wait until all pages have been loaded before being able to start
  • memory usage, as all pages have to be loaded into memory as an XCom before the next operator can start

Next to that, in Airflow 2.x we encountered performance issues when we had to expand like +10k XCom’s (e.g. which is also the case when processing pages results)

The performance issues where 2 fold:

  • Even though we use the CeleryExecutor, pgbouncer as a connection pool to our postgres database and defined dedicated queues for DAG’s that had to process a lot of task instances, the Airflow UI became unresponsive due to the large amount of mapped task instances being created.
  • A lot of time was spend orchestrating the execution state of all mapped task instances, even though we have a pool of workers ready to run. The problem here is the communication between the workers and the scheduler, which most of the time took more time than the task execution itself.  Also we discovered that the scheduler was actually DDOS-ing the database, due to the large amount of mapped task instances.

Another issue with the current partial/expand mechanism is that it expects to know how many tasks will be expanded in advance (e.g. sequence), which is not the case with an iterable (and streaming of course).

Proposal

Possible workaround?

You could argue that you could write a PythonOperator or a task decorated method in which you loop over the multiple inputs from the XCom to pass as an argument to the operator or even a hook.  While the later would be a valuable solution, the first one wouldn’t as it’s a bad practise to execute an operator from within a PythonOperator, see the discussion about this topic on the devlist.

There is already an safeguard implemented for this which checks if an operator is executed from a PythonOperator, and if so, logs a warning stating an operator cannot be called outside of a TaskInstance.  In the future this will probably become prohibited and will raise an AirflowException in that case.

But let’s hypothetical assume this would still be allowed, how would you loop the inputs from an XCom to an operator if that operator is deferrable?  You would need to take multiple aspects into account.

First of all, you would need to catch the raised TaskDeferred exception, as this is how a deferrable operator works.  The TaskDeferred exception contains the triggerer associated to the deferred operator to be executed, which behinds the scenes returns an async generator.  This means that you will have to cope with the event loop of asyncio to be able to run the async triggerer from your PythonOperator, as the later one isn’t executed in an async way.  Maybe this is also a good moment to start thinking of natively supporting async method’s in the PythonOperator without worrying about coping with the event loop (e.g. PythonTriggerer)?

Next to that, once you achieved to execute the triggerer, you will also have to check if a next_method was specified, which has to be executed on the deferred operator once the trigger has completed.

And last but not least, the execution of the next_method could also re-raise a TaskDeferred exception if the deferred operator implements the producer/consumer pattern, which means you’ll have to take into account recursion.  For example the MSGraphAsyncOperator implements the producer/consumer pattern in such a way that the worker triggers the request to the MS Graph API, but instead of blocking the worker waiting for the response to arrive, releases it and delegates it to the triggerer, avoiding blocking workers unnecessarily.  That way when the triggerer receives the response, it gives the received response back to the operator (e.g. worker) without blocking the worker while awaiting for the response.

That’s already a lot of technical challenges you have to solve if you want to execute a (deferrable) operator from within a loop in a PythonOperator.

Beside that, you maybe would also like to introduce some multithreading to speed up the processing instead of just looping in a sequential manner, unless you want sequential execution in the given input order, which is also a issue raised in this discussion.  But there you will also have to be careful, because you just can’t execute a deferable operator having async code through a ThreadPoolExecutor.


What problem does it solve?

Faster execution of multiple task instances related to one and the same operator within the same worker instance, having the advantage of sharing the same memory without overloading the Airflow scheduler with multiple task instances related to one and the same operator.  Avoiding unnecessary waste of time by waiting until a paged result is completely loaded by an operator before passing it to the next operator. 

Why is it needed?

Simplifies concurrent or sequential iteration of multiple XCom's over one (deferrable) operator within the same task instance.

Are there any downsides to this change?

With the current proposal the iteration logic is handled within the same IteratorOperator instead of the scheduler, but the current proposition could be seen as a facilitation to iterate multiple XCom's over one operator.  This also mean you won't see the progres of each individual processed XCom with the Airflow UI, until it completely done.  Another downside in the current proposition is that there isn't any state persisted regarding the progress of the already processed XCom's within the iteration, so there we should then have to think about a solution to persist the state of already iterated XCom's, as we can't persist it as an XCom as those are flushed each time a task fails for idempotency reasons.  The better solution would be to have this alternative way of expanding task instances within the scheduler itself.

Which users are affected by the change?

None

How are users affected by the change? (e.g. DB upgrade required?)

None

What is the level of migration effort (manual and automated) needed for the users to adapt to the breaking changes? (especially in context of Airflow 3)

None, as this is just an alternative way of expanding XCom's on a operator, instead of calling the existing partial method of the MappedOperator, you can now call the iterate method.


Other considerations?


What defines this AIP as "done"?


  • No labels