amoghrajesh commented on code in PR #76514:
URL: https://github.com/apache/airflow/pull/76514#discussion_r4751089651
##########
airflow-core/tests/unit/jobs/test_triggerer_job.py:
##########
@@ -1576,6 +1579,105 @@ def
test_trigger_logger_fd_closed_when_upload_to_remote_raises(jobless_superviso
assert 42 not in jobless_supervisor.running_triggers
[email protected](sys.platform == "darwin", reason="shutdown(SHUT_RD)
discards queued data on macOS")
+def
test_trigger_log_uploaded_when_triggerer_stops_before_trigger_finishes(session):
Review Comment:
It writes real log files into the real `base_log_folder` and never cleans
them up. Use `tmp_path` with `conf_vars`.
##########
airflow-core/tests/unit/jobs/test_triggerer_job.py:
##########
@@ -1576,6 +1579,105 @@ def
test_trigger_logger_fd_closed_when_upload_to_remote_raises(jobless_superviso
assert 42 not in jobless_supervisor.running_triggers
[email protected](sys.platform == "darwin", reason="shutdown(SHUT_RD)
discards queued data on macOS")
+def
test_trigger_log_uploaded_when_triggerer_stops_before_trigger_finishes(session):
+ """The log a still-running trigger wrote before shutdown reaches remote
storage."""
+ trigger = TimeDeltaTrigger(datetime.timedelta(hours=1))
+ _, _, trigger_orm, _ = create_trigger_in_db(session, trigger)
+
+ base_log_folder = Path(conf.get("logging", "base_log_folder"))
+ uploaded: dict[str, bytes] = {}
+
+ def fake_upload(path, ti):
+ uploaded[str(path)] = (base_log_folder / path).read_bytes()
+
+ remote_handler = MagicMock(processors=())
+ remote_handler.upload.side_effect = fake_upload
+ with patch("airflow.sdk.log.load_remote_log_handler",
return_value=remote_handler):
+ trigger_runner_supervisor =
TriggerRunnerSupervisor.start(job=Job(id=123456), capacity=10)
+ try:
+ trigger_runner_supervisor.load_triggers()
+ # Wait until the runner has started the trigger and its first log
line has been written.
+ for _ in range(300):
+ trigger_runner_supervisor._service_subprocess(0.1)
+ factory =
trigger_runner_supervisor.logger_cache.get(trigger_orm.id)
+ if factory is not None and hasattr(factory, "bound_logger"):
+ break
+ finally:
+ trigger_runner_supervisor.kill(force=False)
+ trigger_runner_supervisor.stop_taking_output()
+ trigger_runner_supervisor.cleanup_sockets_after_kill()
+ trigger_runner_supervisor.close_trigger_log_files()
+
+ assert len(uploaded) == 1
+ (path, content) = next(iter(uploaded.items()))
+ assert path.endswith(".trigger.123456.log")
+ assert b"starting" in content
+ # Written by the runner as it stops, after kill() was called, so the
upload includes the shutdown.
+ assert b"trigger completed" in content
+ assert trigger_runner_supervisor.logger_cache == {}
+
+
+def
test_drain_after_kill_ends_while_a_trigger_child_keeps_writing(jobless_supervisor):
Review Comment:
This test depends on timing: a real subprocess, a thread, a
time.sleep(0.001) reader, and polling deadlines.
##########
airflow-core/src/airflow/jobs/triggerer_job_runner.py:
##########
@@ -941,13 +943,26 @@ def _warn_trigger_log_error(self, trigger_id: int | None,
error: OSError) -> Non
error=str(error),
)
+ def stop_taking_output(self) -> None:
Review Comment:
nit: can you rename to `shutdown_socket_reads` pls? This name is vague.
##########
airflow-core/tests/unit/jobs/test_triggerer_job.py:
##########
@@ -1576,6 +1579,105 @@ def
test_trigger_logger_fd_closed_when_upload_to_remote_raises(jobless_superviso
assert 42 not in jobless_supervisor.running_triggers
[email protected](sys.platform == "darwin", reason="shutdown(SHUT_RD)
discards queued data on macOS")
+def
test_trigger_log_uploaded_when_triggerer_stops_before_trigger_finishes(session):
+ """The log a still-running trigger wrote before shutdown reaches remote
storage."""
+ trigger = TimeDeltaTrigger(datetime.timedelta(hours=1))
+ _, _, trigger_orm, _ = create_trigger_in_db(session, trigger)
+
+ base_log_folder = Path(conf.get("logging", "base_log_folder"))
+ uploaded: dict[str, bytes] = {}
+
+ def fake_upload(path, ti):
+ uploaded[str(path)] = (base_log_folder / path).read_bytes()
+
+ remote_handler = MagicMock(processors=())
+ remote_handler.upload.side_effect = fake_upload
Review Comment:
No spec here.
##########
airflow-core/tests/unit/jobs/test_triggerer_job.py:
##########
@@ -1576,6 +1579,105 @@ def
test_trigger_logger_fd_closed_when_upload_to_remote_raises(jobless_superviso
assert 42 not in jobless_supervisor.running_triggers
[email protected](sys.platform == "darwin", reason="shutdown(SHUT_RD)
discards queued data on macOS")
+def
test_trigger_log_uploaded_when_triggerer_stops_before_trigger_finishes(session):
+ """The log a still-running trigger wrote before shutdown reaches remote
storage."""
+ trigger = TimeDeltaTrigger(datetime.timedelta(hours=1))
+ _, _, trigger_orm, _ = create_trigger_in_db(session, trigger)
+
+ base_log_folder = Path(conf.get("logging", "base_log_folder"))
+ uploaded: dict[str, bytes] = {}
+
+ def fake_upload(path, ti):
+ uploaded[str(path)] = (base_log_folder / path).read_bytes()
+
+ remote_handler = MagicMock(processors=())
+ remote_handler.upload.side_effect = fake_upload
+ with patch("airflow.sdk.log.load_remote_log_handler",
return_value=remote_handler):
+ trigger_runner_supervisor =
TriggerRunnerSupervisor.start(job=Job(id=123456), capacity=10)
+ try:
+ trigger_runner_supervisor.load_triggers()
+ # Wait until the runner has started the trigger and its first log
line has been written.
+ for _ in range(300):
+ trigger_runner_supervisor._service_subprocess(0.1)
Review Comment:
If the trigger never starts, the wait loop just ends.
--
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]