|
98 | 98 | _VARIABLE_WRITE_DESCRIPTION = "written by the Java SDK e2e test" |
99 | 99 | _TASK_STATE_DAG_ID = "java_task_state" |
100 | 100 | _TASK_STATE_TASK_ID = "roundtrip_task_state" |
| 101 | +_STOP_REQUEST_DAG_ID = "java_stop_request" |
| 102 | +_STOP_REQUEST_TASK_ID = "wait_for_stop_request" |
| 103 | +_ON_KILL_DAG_ID = "java_stop_request_on_kill" |
| 104 | +_ON_KILL_TASK_ID = "block_until_killed" |
| 105 | +_ON_KILL_EVENT = "onKill ran" |
| 106 | +_STOPPING_EVENT = "Stopping at Airflow's request" |
101 | 107 |
|
102 | 108 |
|
103 | 109 | @dataclass |
@@ -427,6 +433,65 @@ def test_scratch_variable_deleted_by_java_task_is_gone(self, variable_write_run: |
427 | 433 | ) |
428 | 434 |
|
429 | 435 |
|
| 436 | +class TestJavaSDKStopRequest: |
| 437 | + """Verify that a running Java task can stop when Airflow asks it to. |
| 438 | +
|
| 439 | + Marking a running task instance failed makes the supervisor send the task's JVM SIGTERM. For a |
| 440 | + ``StoppableTask``, the SDK keeps the JVM running and tells the task. ``WaitForStopRequest`` in the |
| 441 | + java-test-bundle fixture project returns from ``Context.awaitCancellation``. ``OnKillHandlers`` |
| 442 | + there blocks until its ``@Airflow.OnKill`` method releases it. Each task then logs that it is |
| 443 | + stopping. A JVM that exited on the signal could not write that log line. |
| 444 | + """ |
| 445 | + |
| 446 | + @pytest.mark.parametrize( |
| 447 | + ("dag_id", "task_id", "expected_events"), |
| 448 | + [ |
| 449 | + pytest.param(_STOP_REQUEST_DAG_ID, _STOP_REQUEST_TASK_ID, [_STOPPING_EVENT], id="context"), |
| 450 | + pytest.param(_ON_KILL_DAG_ID, _ON_KILL_TASK_ID, [_ON_KILL_EVENT, _STOPPING_EVENT], id="on-kill"), |
| 451 | + ], |
| 452 | + ) |
| 453 | + def test_running_task_stops_at_airflows_request(self, dag_id, task_id, expected_events): |
| 454 | + client = AirflowClient() |
| 455 | + run_id = client.trigger_dag(dag_id, json={"logical_date": datetime.now(UTC).isoformat()})[ |
| 456 | + "dag_run_id" |
| 457 | + ] |
| 458 | + run = _CompletedRun(client=client, dag_id=dag_id, run_id=run_id) |
| 459 | + |
| 460 | + # The SDK catches the stop signal only once it has started the task, so mark the task instance |
| 461 | + # failed only after the task logs that it is waiting. |
| 462 | + deadline = time.monotonic() + _JAVA_TASK_TIMEOUT |
| 463 | + while (state := _get_task_state(client, dag_id, run_id, task_id)) != "running": |
| 464 | + assert state in {None, "scheduled", "queued"}, f"the task ended before it ran; state: {state!r}" |
| 465 | + assert time.monotonic() < deadline, f"the task never started; state: {state!r}" |
| 466 | + time.sleep(3) |
| 467 | + waiting, records = run.wait_for_log_record( |
| 468 | + task_id, 1, lambda r: r.get("event") == "Waiting for a stop request" |
| 469 | + ) |
| 470 | + assert waiting is not None, ( |
| 471 | + f"the task never started waiting; events seen: {[r.get('event') for r in records]}" |
| 472 | + ) |
| 473 | + |
| 474 | + client.set_task_instance_state(dag_id, run_id, task_id, "failed") |
| 475 | + |
| 476 | + stopping, records = run.wait_for_log_record(task_id, 1, lambda r: r.get("event") == _STOPPING_EVENT) |
| 477 | + assert stopping is not None, ( |
| 478 | + f"the task should see the request to stop; events seen: {[r.get('event') for r in records]}" |
| 479 | + ) |
| 480 | + events = [r.get("event") for r in records if r.get("event") in {_ON_KILL_EVENT, _STOPPING_EVENT}] |
| 481 | + assert events == expected_events, f"the task should stop in this order; records: {records}" |
| 482 | + caught = [r.get("signal") for r in records if r.get("event") == "Asking the task to stop"] |
| 483 | + assert caught == ["SIGTERM"], f"the SDK should log the SIGTERM it caught; records: {records}" |
| 484 | + assert client.wait_for_dag_run(dag_id, run_id, timeout=_JAVA_TASK_TIMEOUT) == "failed" |
| 485 | + assert _get_task_state(client, dag_id, run_id, task_id) == "failed", ( |
| 486 | + "Airflow should keep the state the test set" |
| 487 | + ) |
| 488 | + |
| 489 | + |
| 490 | +def _get_task_state(client: AirflowClient, dag_id: str, run_id: str, task_id: str) -> str | None: |
| 491 | + task_instances = client.get_task_instances(dag_id, run_id).get("task_instances", []) |
| 492 | + return next((ti.get("state") for ti in task_instances if ti["task_id"] == task_id), None) |
| 493 | + |
| 494 | + |
430 | 495 | class TestJavaSDKInterfaceExample: |
431 | 496 | """Verify tasks written against ``InputTask`` receive their bound arguments.""" |
432 | 497 |
|
|
0 commit comments