|
25 | 25 | ExceptionProcessFunction - Raises exception during process (test) |
26 | 26 | ExceptionFinalizeFunction - Raises exception during finalize (test) |
27 | 27 | SumAllColumnsSimpleDistributed - Distributed aggregation via callback API |
| 28 | +CrashOnProcessFunction - SIGKILLs the worker mid-process (test) |
| 29 | +CrashOnCombineFunction - Raises during combine() (test) |
| 30 | +CrashOnFinalizeFunction - Raises during finalize() (test) |
| 31 | +HangOnProcessFunction - Sleeps forever in process() (manual cancel test) |
| 32 | +LargeStateFunction - Buffers ~N MB per state_id (IPC chunking test) |
28 | 33 | """ |
29 | 34 |
|
30 | 35 | from __future__ import annotations |
31 | 36 |
|
| 37 | +import os |
| 38 | +import signal |
| 39 | +import time |
32 | 40 | from dataclasses import dataclass, field |
33 | 41 | from collections.abc import Iterator |
34 | 42 | from typing import Annotated, Any |
|
59 | 67 | "SumAllColumnsSimpleDistributed", |
60 | 68 | "ExceptionProcessFunction", |
61 | 69 | "ExceptionFinalizeFunction", |
| 70 | + "CrashOnProcessFunction", |
| 71 | + "CrashOnCombineFunction", |
| 72 | + "CrashOnFinalizeFunction", |
| 73 | + "HangOnProcessFunction", |
| 74 | + "LargeStateFunction", |
62 | 75 | ] |
63 | 76 |
|
64 | 77 |
|
@@ -755,3 +768,171 @@ def finish( |
755 | 768 | sums[field.name] = pc.sum(table.column(field.name)) |
756 | 769 |
|
757 | 770 | return [pa.RecordBatch.from_pylist([{name: val for name, val in sums.items()}], schema=params.output_schema)] |
| 771 | + |
| 772 | + |
| 773 | +# ============================================================================ |
| 774 | +# Failure-injection fixtures |
| 775 | +# ============================================================================ |
| 776 | +# These are buffered_table functions designed to exercise crash/error paths in |
| 777 | +# the C++ Sink+Source operator. Each one fails in a specific phase so we can |
| 778 | +# test that the operator: throws cleanly, drains the worker pool, doesn't leak |
| 779 | +# in-flight workers, and recovers on the next query. |
| 780 | + |
| 781 | + |
| 782 | +class CrashOnProcessFunction(BufferInputFunction): |
| 783 | + """SIGKILLs its own worker process on the first process() call. |
| 784 | +
|
| 785 | + Tests the C++ side's handling of an abrupt worker death mid-RPC: |
| 786 | + subprocess writes the request, worker dies before responding, ReadUnary |
| 787 | + sees EOF / EPIPE / SIGCHLD depending on timing. The exception should |
| 788 | + propagate cleanly and gstate teardown should cancel-dispatch peer workers. |
| 789 | + """ |
| 790 | + |
| 791 | + class Meta(BufferInputFunction.Meta): |
| 792 | + name = "crash_on_process" |
| 793 | + description = "Worker SIGKILLs itself during process (test)" |
| 794 | + categories = ["test", "crash"] |
| 795 | + |
| 796 | + @classmethod |
| 797 | + def process( # type: ignore[override] |
| 798 | + cls, |
| 799 | + params: ProcessParams[SingleTableArguments], |
| 800 | + state: BufferInputState, |
| 801 | + batch: pa.RecordBatch, |
| 802 | + out: OutputCollector, |
| 803 | + ) -> None: |
| 804 | + # SIGKILL with no handler; the process dies immediately. The C++ |
| 805 | + # side has a pending unary RPC awaiting the response. |
| 806 | + os.kill(os.getpid(), signal.SIGKILL) |
| 807 | + |
| 808 | + |
| 809 | +class CrashOnCombineFunction(BufferInputFunction): |
| 810 | + """Buffers input normally; raises during combine(). |
| 811 | +
|
| 812 | + Tests that a combine RPC error propagates as IOException, that the |
| 813 | + combine_worker is dropped (not pushed back into gstate.workers), and |
| 814 | + that the rest of the worker pool drains. |
| 815 | + """ |
| 816 | + |
| 817 | + class Meta(BufferInputFunction.Meta): |
| 818 | + name = "crash_on_combine" |
| 819 | + description = "Worker raises during combine (test)" |
| 820 | + categories = ["test", "crash"] |
| 821 | + |
| 822 | + @classmethod |
| 823 | + def combine( # type: ignore[override] |
| 824 | + cls, state_ids: list[int], params: ProcessParams[SingleTableArguments] |
| 825 | + ) -> list[int]: |
| 826 | + raise RuntimeError("Intentional exception during combine()") |
| 827 | + |
| 828 | + |
| 829 | +class CrashOnFinalizeFunction(BufferInputFunction): |
| 830 | + """Buffers input, combine returns normally, finalize raises on first yield. |
| 831 | +
|
| 832 | + Tests source-phase error propagation: GetData calls |
| 833 | + RpcBufferedTableFinalize, the worker raises, the IOException unwinds the |
| 834 | + source thread, and remaining workers are still released cleanly. |
| 835 | + """ |
| 836 | + |
| 837 | + class Meta(BufferInputFunction.Meta): |
| 838 | + name = "crash_on_finalize" |
| 839 | + description = "Worker raises during finalize (test)" |
| 840 | + categories = ["test", "crash"] |
| 841 | + |
| 842 | + @classmethod |
| 843 | + def finalize( # type: ignore[override] |
| 844 | + cls, finalize_state_id: int, params: ProcessParams[SingleTableArguments] |
| 845 | + ) -> Iterator[pa.RecordBatch]: |
| 846 | + raise RuntimeError("Intentional exception during finalize()") |
| 847 | + yield # type: ignore[unreachable] # make this a generator function |
| 848 | + |
| 849 | + |
| 850 | +class HangOnProcessFunction(BufferInputFunction): |
| 851 | + """Sleeps for an hour in process(); used by the manual cancellation smoke. |
| 852 | +
|
| 853 | + sqllogictest can't simulate Ctrl-C cleanly, so this fixture is driven by |
| 854 | + scripts/smoke_buffered_cancel.sh — it starts the query, waits a moment, |
| 855 | + then SIGINTs the duckdb process and asserts the query was cancelled. |
| 856 | + """ |
| 857 | + |
| 858 | + class Meta(BufferInputFunction.Meta): |
| 859 | + name = "hang_on_process" |
| 860 | + description = "Worker sleeps in process (manual cancel test)" |
| 861 | + categories = ["test", "hang"] |
| 862 | + |
| 863 | + @classmethod |
| 864 | + def process( # type: ignore[override] |
| 865 | + cls, |
| 866 | + params: ProcessParams[SingleTableArguments], |
| 867 | + state: BufferInputState, |
| 868 | + batch: pa.RecordBatch, |
| 869 | + out: OutputCollector, |
| 870 | + ) -> None: |
| 871 | + time.sleep(3600) |
| 872 | + |
| 873 | + |
| 874 | +@dataclass(kw_only=True) |
| 875 | +class LargeStateState(ArrowSerializableDataclass): |
| 876 | + """Holds a single large bytes buffer; the C++ side ships this through |
| 877 | + combine/finalize via the RPC's outer envelope, exercising IPC chunking |
| 878 | + on the response path.""" |
| 879 | + |
| 880 | + payload: bytes = b"" |
| 881 | + |
| 882 | + |
| 883 | +class LargeStateFunction(TableInOutGenerator[SingleTableArguments, LargeStateState]): |
| 884 | + """Accumulates a large payload per state_id and emits it during finalize. |
| 885 | +
|
| 886 | + Each process() call appends N KB to the per-state_id payload. The total |
| 887 | + bytes per finalize_state_id grows linearly with input row count. The |
| 888 | + test passes ~100 MB through combine to exercise the IPC chunking that |
| 889 | + happens transparently in vgi_rpc for large messages. |
| 890 | + """ |
| 891 | + |
| 892 | + state_class = LargeStateState |
| 893 | + |
| 894 | + class Meta: |
| 895 | + name = "large_state" |
| 896 | + description = "Buffers ~1 MB per input batch into state (IPC test)" |
| 897 | + categories = ["test", "memory"] |
| 898 | + buffered_table = True |
| 899 | + |
| 900 | + @classmethod |
| 901 | + def initial_state(cls, params: ProcessParams[SingleTableArguments]) -> LargeStateState: |
| 902 | + return LargeStateState() |
| 903 | + |
| 904 | + @classmethod |
| 905 | + def process( |
| 906 | + cls, |
| 907 | + params: ProcessParams[SingleTableArguments], |
| 908 | + state: LargeStateState, |
| 909 | + batch: pa.RecordBatch, |
| 910 | + out: OutputCollector, |
| 911 | + ) -> None: |
| 912 | + # Append 1 MB of zero bytes per input batch. Combine ships the full |
| 913 | + # accumulated payload back via Arrow IPC; chunking is the worker |
| 914 | + # library's responsibility — we just want to land a multi-MB message |
| 915 | + # on the wire. |
| 916 | + state.payload += b"\x00" * (1024 * 1024) |
| 917 | + out.emit(empty_batch(params.output_schema)) |
| 918 | + |
| 919 | + @classmethod |
| 920 | + def combine( |
| 921 | + cls, state_ids: list[int], params: ProcessParams[SingleTableArguments] |
| 922 | + ) -> list[int]: |
| 923 | + return state_ids |
| 924 | + |
| 925 | + @classmethod |
| 926 | + def finalize( |
| 927 | + cls, finalize_state_id: int, params: ProcessParams[SingleTableArguments] |
| 928 | + ) -> Iterator[pa.RecordBatch]: |
| 929 | + stored = params.storage.aggregate_get([finalize_state_id]) |
| 930 | + if not stored or stored[0] is None: |
| 931 | + return |
| 932 | + state = LargeStateState.deserialize_from_bytes(stored[0][1]) |
| 933 | + # Emit one row with the payload length so the test can assert |
| 934 | + # we round-tripped the right number of bytes through combine. |
| 935 | + yield pa.RecordBatch.from_pydict( |
| 936 | + {name: [len(state.payload)] for name in params.output_schema.names}, |
| 937 | + schema=params.output_schema, |
| 938 | + ) |
0 commit comments