Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check in BigQueryInsertJobOperator to verify the Job state before marking it as success #40863

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,14 @@ def execute(self, context: Any):
job.result(timeout=self.result_timeout, retry=self.result_retry)
self._handle_job_error(job)

while job.state in ("PENDING", "RUNNING"):
job = hook.get_job(
project_id=self.project_id,
location=self.location,
job_id=self.job_id,
)
job.result(timeout=self.result_timeout, retry=self.result_retry)
self._handle_job_error(job)
return self.job_id
else:
if job.running():
Expand Down
86 changes: 82 additions & 4 deletions providers/tests/google/cloud/operators/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,73 @@ def test_execute_query_success(self, mock_hook):

assert result == real_job_id

@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
@mock.patch("airflow.providers.google.cloud.hooks.bigquery.BigQueryJob")
def test_execute_task_transitions_from_running_to_completed(self, mock_job, mock_hook):
job_id = "123456"
hash_ = "hash"
real_job_id = f"{job_id}_{hash_}"

configuration = {
"query": {
"query": "SELECT * FROM any",
"useLegacySql": False,
}
}

mock_job.job_id = real_job_id
mock_job.error_result = False
mock_job.state = "RUNNING"

mock_hook.return_value.insert_job.return_value = mock_job
mock_hook.return_value.generate_job_id.return_value = real_job_id

job_running = MagicMock(
job_id=real_job_id,
error_result=None,
state="RUNNING",
done=lambda: False,
)
job_completed = MagicMock(
job_id=real_job_id,
error_result=None,
state="COMPLETED",
done=lambda: True,
)

# Simulate the sequence of job states when `get_job` is called
mock_hook.return_value.get_job.side_effect = [job_running, job_completed]
mock_hook.return_value.generate_job_id.return_value = real_job_id

# Instantiate the operator and execute
op = BigQueryInsertJobOperator(
task_id="insert_query_job",
configuration=configuration,
location=TEST_DATASET_LOCATION,
job_id=job_id,
project_id=TEST_GCP_PROJECT_ID,
)
result = op.execute(context=MagicMock())

# Validate the number of calls
assert mock_hook.return_value.get_job.call_count == 2
assert job_running.result.call_count == 1
assert job_completed.result.call_count == 1

mock_hook.return_value.get_job.assert_any_call(
location=TEST_DATASET_LOCATION,
job_id=real_job_id,
project_id=TEST_GCP_PROJECT_ID,
)

job_running.result.assert_called_once_with(
retry=DEFAULT_RETRY,
timeout=None,
)

# Validate the final result
assert result == real_job_id

@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
def test_execute_copy_success(self, mock_hook):
job_id = "123456"
Expand Down Expand Up @@ -1004,13 +1071,20 @@ def test_execute_reattach(self, mock_hook):
}

mock_hook.return_value.insert_job.side_effect = Conflict("any")
job = MagicMock(
job_running = MagicMock(
job_id=real_job_id,
error_result=False,
state="RUNNING",
done=lambda: False,
)
mock_hook.return_value.get_job.return_value = job
job_completed = MagicMock(
job_id=real_job_id,
error_result=False,
state="COMPLETED",
done=lambda: True,
)

mock_hook.return_value.get_job.side_effect = [job_running, job_completed]
mock_hook.return_value.generate_job_id.return_value = real_job_id

op = BigQueryInsertJobOperator(
Expand All @@ -1023,13 +1097,17 @@ def test_execute_reattach(self, mock_hook):
)
result = op.execute(context=MagicMock())

mock_hook.return_value.get_job.assert_called_once_with(
assert mock_hook.return_value.get_job.call_count == 2
assert job_running.result.call_count == 1
assert job_completed.result.call_count == 1

mock_hook.return_value.get_job.assert_any_call(
location=TEST_DATASET_LOCATION,
job_id=real_job_id,
project_id=TEST_GCP_PROJECT_ID,
)

job.result.assert_called_once_with(
job_running.result.assert_any_call(
retry=DEFAULT_RETRY,
timeout=None,
)
Expand Down