Commit 492518e
authored
Switch the Supervisor/task process from line-based to length-prefixed (#51699)
* Switch the Supervisor/task process from line-based to length-prefixed
The existing JSON Lines based approach had two major drawbacks
1. In the case of really large lines (in the region of 10 or 20MB) the python
line buffering could _sometimes_ result in a partial read
2. The JSON based approach didn't have the ability to add any metadata (such
as errors).
3. Not every message type/call-site waited for a response, which meant those
client functions could never get told about an error
One of the ways this line-based approach fell down was if you suddenly tried
to run 100s of triggers at the same time you would get an error like this:
```
Traceback (most recent call last):
File "/Users/ash/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/asyncio/streams.py", line 568, in readline
line = await self.readuntil(sep)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ash/.local/share/uv/python/cpython-3.12.7-macos-aarch64-none/lib/python3.12/asyncio/streams.py", line 663, in readuntil
raise exceptions.LimitOverrunError(
asyncio.exceptions.LimitOverrunError: Separator is found, but chunk is longer than limit
```
The other way this caused problems was if you parse a large dag (as in one
with 20k tasks or more) the DagFileProcessor could end up getting a partial
read which would be invalid JSON.
This changes the communications protocol in in a couple of ways.
First off at the python level the separate send and receive methods in the
client/task side have been removed and replaced with a single `send()` that
sends the request, reads the response and raises an error if one is returned.
(But note, right now almost nothing in the supervisor side sets the error,
that will be a future PR.)
Secondly the JSON Lines approach has been changed from a line-based protocol
to a binary "frame" one. The protocol (which is the same for whichever side is
sending) is length-prefixed, i.e. we first send the length of the data as a
4byte big-endian integer, followed by the data itself. This should remove the
possibility of JSON parse errors due to reading incomplete lines
Finally the last change made in this PR is to remove the "extra" requests
socket/channel. Upon closer examination with this comms path I realised that
this socket is unnecessary: Since we are in 100% control of the client side we
can make use of the bi-directional nature of `socketpair` and save file
handles. This also happens to help the `run_as_user` feature which is
currently broken, as without extra config to `sudoers` file, `sudo` will close
all filehandles other than stdin, stdout, and stderr -- so by introducing this
change we make it easier to re-add run_as_user support.
In order to support this in the DagFileProcessor (as the fact that the proc
manager uses a single selector for multiple processes) means I have moved the
`on_close` callback to be part of the object we store in the `selector` object
in the supervisors, previoulsy it was the "on_read" callback, now we store a
tuple of `(on_read, on_close)` and on_close is called once universally.
This also changes the way comms are handled from the (async) TriggerRunner
process. Previously we had a sync+async lock, but that made it possible to end
up deadlocking things. The change now is to have `send` on
`TriggerCommsDecoder` "go back" to the async even loop via `async_to_sync`, so
that only async code deals with the socket, and we can use an async lock
(rather than the hybrid sync and async lock we tried before). This seems to
help the deadlock issue, but I'm not 100% sure it will remove it entirely, but
it makes it much much harder to hit - I've not been able to reprouce it with
this change
* Deal with compat in tests
This compat issue is only in tests, as nothing in the runtime of airflow-core
imports/calls methods directly on SUPERVISOR_COMMS, we are only importing it
in tests to mkae assertions about the behavour/to stub the return values.1 parent af94629 commit 492518e
48 files changed
Lines changed: 1249 additions & 1218 deletions
File tree
- airflow-core
- src/airflow
- dag_processing
- jobs
- tests/unit
- dag_processing
- hooks
- jobs
- models
- devel-common/src/tests_common
- providers
- amazon/tests/unit/amazon/aws/links
- common/io/tests/unit/common/io/xcom
- dbt/cloud/tests/unit/dbt/cloud/operators
- google/tests/unit/google/cloud
- links
- operators
- microsoft/azure/tests/unit/microsoft/azure/operators
- task-sdk
- src/airflow/sdk
- bases
- definitions/asset
- execution_time
- tests/task_sdk
- bases
- definitions
- execution_time
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| |||
388 | 390 | | |
389 | 391 | | |
390 | 392 | | |
391 | | - | |
| 393 | + | |
392 | 394 | | |
393 | 395 | | |
394 | 396 | | |
| |||
397 | 399 | | |
398 | 400 | | |
399 | 401 | | |
400 | | - | |
401 | | - | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
402 | 405 | | |
403 | 406 | | |
404 | 407 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
72 | 71 | | |
73 | 72 | | |
74 | 73 | | |
| |||
102 | 101 | | |
103 | 102 | | |
104 | 103 | | |
105 | | - | |
| 104 | + | |
106 | 105 | | |
107 | 106 | | |
108 | | - | |
109 | | - | |
110 | | - | |
| 107 | + | |
| 108 | + | |
111 | 109 | | |
112 | 110 | | |
113 | | - | |
| 111 | + | |
114 | 112 | | |
115 | 113 | | |
116 | | - | |
117 | 114 | | |
118 | 115 | | |
119 | 116 | | |
| |||
125 | 122 | | |
126 | 123 | | |
127 | 124 | | |
128 | | - | |
| 125 | + | |
129 | 126 | | |
130 | 127 | | |
131 | 128 | | |
| |||
266 | 263 | | |
267 | 264 | | |
268 | 265 | | |
269 | | - | |
270 | 266 | | |
271 | 267 | | |
272 | | - | |
| 268 | + | |
273 | 269 | | |
274 | | - | |
| 270 | + | |
275 | 271 | | |
276 | 272 | | |
277 | 273 | | |
278 | 274 | | |
279 | 275 | | |
280 | 276 | | |
281 | | - | |
282 | | - | |
| 277 | + | |
283 | 278 | | |
284 | 279 | | |
285 | 280 | | |
| |||
301 | 296 | | |
302 | 297 | | |
303 | 298 | | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
304 | 306 | | |
305 | 307 | | |
306 | | - | |
307 | | - | |
| 308 | + | |
308 | 309 | | |
309 | 310 | | |
310 | 311 | | |
311 | 312 | | |
312 | 313 | | |
313 | 314 | | |
314 | 315 | | |
315 | | - | |
| 316 | + | |
316 | 317 | | |
317 | 318 | | |
318 | 319 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| |||
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
| |||
70 | 73 | | |
71 | 74 | | |
72 | 75 | | |
73 | | - | |
74 | | - | |
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
| |||
181 | 182 | | |
182 | 183 | | |
183 | 184 | | |
184 | | - | |
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
| |||
295 | 295 | | |
296 | 296 | | |
297 | 297 | | |
298 | | - | |
| 298 | + | |
299 | 299 | | |
300 | 300 | | |
301 | 301 | | |
| |||
342 | 342 | | |
343 | 343 | | |
344 | 344 | | |
345 | | - | |
346 | | - | |
| 345 | + | |
| 346 | + | |
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
| |||
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
358 | | - | |
| 358 | + | |
359 | 359 | | |
360 | 360 | | |
361 | 361 | | |
| |||
454 | 454 | | |
455 | 455 | | |
456 | 456 | | |
457 | | - | |
458 | | - | |
| 457 | + | |
459 | 458 | | |
460 | 459 | | |
461 | 460 | | |
| |||
628 | 627 | | |
629 | 628 | | |
630 | 629 | | |
631 | | - | |
| 630 | + | |
632 | 631 | | |
633 | 632 | | |
634 | 633 | | |
| |||
691 | 690 | | |
692 | 691 | | |
693 | 692 | | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
694 | 741 | | |
695 | 742 | | |
696 | 743 | | |
697 | 744 | | |
698 | | - | |
699 | | - | |
700 | | - | |
701 | | - | |
| 745 | + | |
| 746 | + | |
702 | 747 | | |
703 | 748 | | |
704 | 749 | | |
| |||
726 | 771 | | |
727 | 772 | | |
728 | 773 | | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
| 774 | + | |
733 | 775 | | |
734 | 776 | | |
735 | 777 | | |
| |||
740 | 782 | | |
741 | 783 | | |
742 | 784 | | |
743 | | - | |
744 | 785 | | |
745 | 786 | | |
746 | 787 | | |
| |||
796 | 837 | | |
797 | 838 | | |
798 | 839 | | |
799 | | - | |
| 840 | + | |
| 841 | + | |
800 | 842 | | |
801 | | - | |
802 | | - | |
803 | | - | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
804 | 846 | | |
805 | 847 | | |
806 | | - | |
807 | | - | |
808 | | - | |
809 | | - | |
810 | | - | |
811 | | - | |
812 | | - | |
813 | | - | |
814 | | - | |
| 848 | + | |
815 | 849 | | |
816 | | - | |
| 850 | + | |
817 | 851 | | |
818 | | - | |
819 | 852 | | |
820 | 853 | | |
821 | 854 | | |
822 | | - | |
823 | | - | |
824 | | - | |
825 | | - | |
826 | | - | |
827 | | - | |
828 | | - | |
829 | 855 | | |
830 | 856 | | |
831 | 857 | | |
| |||
934 | 960 | | |
935 | 961 | | |
936 | 962 | | |
937 | | - | |
938 | | - | |
939 | 963 | | |
940 | 964 | | |
941 | 965 | | |
| |||
961 | 985 | | |
962 | 986 | | |
963 | 987 | | |
964 | | - | |
965 | | - | |
966 | | - | |
967 | | - | |
968 | | - | |
969 | | - | |
970 | | - | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
971 | 992 | | |
972 | 993 | | |
| 994 | + | |
| 995 | + | |
973 | 996 | | |
974 | | - | |
975 | 997 | | |
976 | | - | |
| 998 | + | |
977 | 999 | | |
978 | 1000 | | |
979 | 1001 | | |
| |||
0 commit comments