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

Optimize deferrable mode execution for BigQueryInsertJobOperator #31249

Merged
merged 4 commits into from May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions airflow/providers/google/cloud/operators/bigquery.py
Expand Up @@ -2709,16 +2709,19 @@ def execute(self, context: Any):
self._handle_job_error(job)

return self.job_id
self.defer(
timeout=self.execution_timeout,
trigger=BigQueryInsertJobTrigger(
conn_id=self.gcp_conn_id,
job_id=self.job_id,
project_id=self.project_id,
poll_interval=self.poll_interval,
),
method_name="execute_complete",
)
else:
if job.running():
phanikumv marked this conversation as resolved.
Show resolved Hide resolved
self.defer(
timeout=self.execution_timeout,
trigger=BigQueryInsertJobTrigger(
conn_id=self.gcp_conn_id,
job_id=self.job_id,
project_id=self.project_id,
poll_interval=self.poll_interval,
),
method_name="execute_complete",
)
self.log.info("Current state of job %s is %s", job.job_id, job.state)
phanikumv marked this conversation as resolved.
Show resolved Hide resolved

def execute_complete(self, context: Context, event: dict[str, Any]):
"""
Expand Down
29 changes: 29 additions & 0 deletions tests/providers/google/cloud/operators/test_bigquery.py
Expand Up @@ -1310,6 +1310,35 @@ def test_execute_no_force_rerun(self, mock_hook):
with pytest.raises(AirflowException):
op.execute(context=MagicMock())

@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryInsertJobOperator.defer")
@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
def test_bigquery_insert_job_operator_async_finish_before_deferred(self, mock_hook, mock_defer, caplog):
job_id = "123456"
hash_ = "hash"
real_job_id = f"{job_id}_{hash_}"

configuration = {
"query": {
"query": "SELECT * FROM any",
"useLegacySql": False,
}
}
mock_hook.return_value.insert_job.return_value = MagicMock(job_id=real_job_id, error_result=False)
mock_hook.return_value.insert_job.return_value.running.return_value = False

op = BigQueryInsertJobOperator(
task_id="insert_query_job",
configuration=configuration,
location=TEST_DATASET_LOCATION,
job_id=job_id,
project_id=TEST_GCP_PROJECT_ID,
deferrable=True,
)

op.execute(MagicMock())
assert not mock_defer.called
assert "Current state of job" in caplog.text

@mock.patch("airflow.providers.google.cloud.operators.bigquery.BigQueryHook")
def test_bigquery_insert_job_operator_async(self, mock_hook, create_task_instance_of_operator):
"""
Expand Down