From e8d61553600574039f527c52b43ad8b058f848d5 Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Wed, 30 Oct 2024 16:20:16 -0500 Subject: [PATCH 1/3] Add tests which ensures `KeyboardInterrupt` halts batch execution --- tests/unit/task/test_run.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/unit/task/test_run.py b/tests/unit/task/test_run.py index 7063ca4200d..8f239ccfc3a 100644 --- a/tests/unit/task/test_run.py +++ b/tests/unit/task/test_run.py @@ -14,7 +14,7 @@ from dbt.adapters.contracts.connection import AdapterResponse from dbt.adapters.postgres import PostgresAdapter from dbt.artifacts.resources.base import FileHash -from dbt.artifacts.resources.types import NodeType, RunHookType +from dbt.artifacts.resources.types import BatchSize, NodeType, RunHookType from dbt.artifacts.resources.v1.components import DependsOn from dbt.artifacts.resources.v1.config import NodeConfig from dbt.artifacts.resources.v1.model import ModelConfig @@ -27,6 +27,7 @@ from dbt.events.types import LogModelResult from dbt.exceptions import DbtRuntimeError from dbt.flags import get_flags, set_from_args +from dbt.materializations.incremental.microbatch import MicrobatchBuilder from dbt.task.run import ModelRunner, RunTask, _get_adapter_info from dbt.tests.util import safe_set_invocation_context from dbt_common.events.base_types import EventLevel @@ -265,6 +266,33 @@ class Relation: # Assert result of _is_incremental assert model_runner._is_incremental(model) == expectation + def test_keyboard_breaks__execute_microbatch_materialization( + self, + table_model: ModelNode, + manifest: Manifest, + model_runner: ModelRunner, + ) -> None: + def mock_build_batch_context(*args, **kwargs): + raise KeyboardInterrupt("Test exception") + + def mock_is_incremental(*args, **kwargs): + return True + + table_model.config.materialized = "incremental" + table_model.config.incremental_strategy = "microbatch" + table_model.config.batch_size = BatchSize.day + + with patch.object( + MicrobatchBuilder, "build_batch_context", mock_build_batch_context + ), patch.object(ModelRunner, "_is_incremental", mock_is_incremental): + try: + model_runner._execute_microbatch_materialization( + table_model, manifest, {}, MagicMock() + ) + assert False, "KeybaordInterrupt failed to escape" + except KeyboardInterrupt: + assert True + class TestRunTask: @pytest.fixture From fa57278a797071ac9540943a99cbc06576d0e1f7 Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Thu, 17 Oct 2024 15:27:04 -0700 Subject: [PATCH 2/3] Reraise `KeyboardInterrupt`s and `SystemExit`s during microbatch model execution Previously we were catching all execptions during microbatch model execution, and gracefully handling them. This ensured that one failing batch didn't cause the rest of the batches to fail / be skipped unnecessarily. However if there is a KeyboardInterrupt or SystemExit, execution should halt. To get this to happen, we're now catching and reraising `KeybaordInterrupts`s and `SystemExit`s. --- core/dbt/task/run.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/dbt/task/run.py b/core/dbt/task/run.py index ce613c0e44d..0b6fae4fc4c 100644 --- a/core/dbt/task/run.py +++ b/core/dbt/task/run.py @@ -568,6 +568,9 @@ def _execute_microbatch_materialization( # At least one batch has been inserted successfully! incremental_batch = True + except (KeyboardInterrupt, SystemExit): + # reraise it for GraphRunnableTask.execute_nodes to handle + raise except Exception as e: exception = e batch_run_result = self._build_failed_run_batch_result( From e2869d5574bf323453468b4dde47728b94115dad Mon Sep 17 00:00:00 2001 From: Quigley Malcolm Date: Thu, 17 Oct 2024 15:30:34 -0700 Subject: [PATCH 3/3] Add changie doc --- .changes/unreleased/Fixes-20241017-153022.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixes-20241017-153022.yaml diff --git a/.changes/unreleased/Fixes-20241017-153022.yaml b/.changes/unreleased/Fixes-20241017-153022.yaml new file mode 100644 index 00000000000..905a40db501 --- /dev/null +++ b/.changes/unreleased/Fixes-20241017-153022.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Ensure KeyboardInterrupt/SystemExit halts microbatch model execution +time: 2024-10-17T15:30:22.781854-07:00 +custom: + Author: QMalcolm + Issue: "10862"