-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Asynchronous execution of Dataproc jobs with a Sensor (#10673)
- Loading branch information
1 parent
527ea81
commit ece685b
Showing
6 changed files
with
289 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
""" | ||
This module contains a Dataproc Job sensor. | ||
""" | ||
# pylint: disable=C0302 | ||
|
||
from google.cloud.dataproc_v1beta2.types import JobStatus | ||
|
||
from airflow.providers.google.cloud.hooks.dataproc import DataprocHook | ||
from airflow.sensors.base_sensor_operator import BaseSensorOperator | ||
from airflow.utils.decorators import apply_defaults | ||
from airflow.exceptions import AirflowException | ||
|
||
|
||
class DataprocJobSensor(BaseSensorOperator): | ||
""" | ||
Check for the state of a previously submitted Dataproc job. | ||
:param project_id: The ID of the google cloud project in which | ||
to create the cluster. (templated) | ||
:type project_id: str | ||
:param dataproc_job_id: The Dataproc job ID to poll. (templated) | ||
:type dataproc_job_id: str | ||
:param location: Required. The Cloud Dataproc region in which to handle the request. (templated) | ||
:type location: str | ||
:param gcp_conn_id: The connection ID to use connecting to Google Cloud Platform. | ||
:type gcp_conn_id: str | ||
""" | ||
|
||
template_fields = ('project_id', 'location', 'dataproc_job_id') | ||
ui_color = '#f0eee4' | ||
|
||
@apply_defaults | ||
def __init__( | ||
self, | ||
*, | ||
project_id: str, | ||
dataproc_job_id: str, | ||
location: str, | ||
gcp_conn_id: str = 'google_cloud_default', | ||
**kwargs, | ||
) -> None: | ||
super().__init__(**kwargs) | ||
self.project_id = project_id | ||
self.gcp_conn_id = gcp_conn_id | ||
self.dataproc_job_id = dataproc_job_id | ||
self.location = location | ||
|
||
def poke(self, context): | ||
hook = DataprocHook(gcp_conn_id=self.gcp_conn_id) | ||
job = hook.get_job(job_id=self.dataproc_job_id, location=self.location, project_id=self.project_id) | ||
state = job.status.state | ||
|
||
if state == JobStatus.ERROR: | ||
raise AirflowException('Job failed:\n{}'.format(job)) | ||
elif state in {JobStatus.CANCELLED, JobStatus.CANCEL_PENDING, JobStatus.CANCEL_STARTED}: | ||
raise AirflowException('Job was cancelled:\n{}'.format(job)) | ||
elif JobStatus.DONE == state: | ||
self.log.debug("Job %s completed successfully.", self.dataproc_job_id) | ||
return True | ||
elif JobStatus.ATTEMPT_FAILURE == state: | ||
self.log.debug("Job %s attempt has failed.", self.dataproc_job_id) | ||
|
||
self.log.info("Waiting for job %s to complete.", self.dataproc_job_id) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import unittest | ||
from unittest import mock | ||
|
||
from google.cloud.dataproc_v1beta2.types import JobStatus | ||
from airflow import AirflowException | ||
from airflow.providers.google.cloud.sensors.dataproc import DataprocJobSensor | ||
|
||
from airflow.version import version as airflow_version | ||
|
||
AIRFLOW_VERSION = "v" + airflow_version.replace(".", "-").replace("+", "-") | ||
|
||
DATAPROC_PATH = "airflow.providers.google.cloud.sensors.dataproc.{}" | ||
|
||
TASK_ID = "task-id" | ||
GCP_PROJECT = "test-project" | ||
GCP_LOCATION = "test-location" | ||
GCP_CONN_ID = "test-conn" | ||
TIMEOUT = 120 | ||
|
||
|
||
class TestDataprocJobSensor(unittest.TestCase): | ||
def create_job(self, state: int): | ||
job = mock.Mock() | ||
job.status = mock.Mock() | ||
job.status.state = state | ||
return job | ||
|
||
@mock.patch(DATAPROC_PATH.format("DataprocHook")) | ||
def test_done(self, mock_hook): | ||
job = self.create_job(JobStatus.DONE) | ||
job_id = "job_id" | ||
mock_hook.return_value.get_job.return_value = job | ||
|
||
sensor = DataprocJobSensor( | ||
task_id=TASK_ID, | ||
location=GCP_LOCATION, | ||
project_id=GCP_PROJECT, | ||
dataproc_job_id=job_id, | ||
gcp_conn_id=GCP_CONN_ID, | ||
timeout=TIMEOUT, | ||
) | ||
ret = sensor.poke(context={}) | ||
|
||
mock_hook.return_value.get_job.assert_called_once_with( | ||
job_id=job_id, location=GCP_LOCATION, project_id=GCP_PROJECT | ||
) | ||
self.assertTrue(ret) | ||
|
||
@mock.patch(DATAPROC_PATH.format("DataprocHook")) | ||
def test_error(self, mock_hook): | ||
job = self.create_job(JobStatus.ERROR) | ||
job_id = "job_id" | ||
mock_hook.return_value.get_job.return_value = job | ||
|
||
sensor = DataprocJobSensor( | ||
task_id=TASK_ID, | ||
location=GCP_LOCATION, | ||
project_id=GCP_PROJECT, | ||
dataproc_job_id=job_id, | ||
gcp_conn_id=GCP_CONN_ID, | ||
timeout=TIMEOUT, | ||
) | ||
|
||
with self.assertRaisesRegex(AirflowException, "Job failed"): | ||
sensor.poke(context={}) | ||
|
||
mock_hook.return_value.get_job.assert_called_once_with( | ||
job_id=job_id, location=GCP_LOCATION, project_id=GCP_PROJECT | ||
) | ||
|
||
@mock.patch(DATAPROC_PATH.format("DataprocHook")) | ||
def test_wait(self, mock_hook): | ||
job = self.create_job(JobStatus.RUNNING) | ||
job_id = "job_id" | ||
mock_hook.return_value.get_job.return_value = job | ||
|
||
sensor = DataprocJobSensor( | ||
task_id=TASK_ID, | ||
location=GCP_LOCATION, | ||
project_id=GCP_PROJECT, | ||
dataproc_job_id=job_id, | ||
gcp_conn_id=GCP_CONN_ID, | ||
timeout=TIMEOUT, | ||
) | ||
ret = sensor.poke(context={}) | ||
|
||
mock_hook.return_value.get_job.assert_called_once_with( | ||
job_id=job_id, location=GCP_LOCATION, project_id=GCP_PROJECT | ||
) | ||
self.assertFalse(ret) | ||
|
||
@mock.patch(DATAPROC_PATH.format("DataprocHook")) | ||
def test_cancelled(self, mock_hook): | ||
job = self.create_job(JobStatus.CANCELLED) | ||
job_id = "job_id" | ||
mock_hook.return_value.get_job.return_value = job | ||
|
||
sensor = DataprocJobSensor( | ||
task_id=TASK_ID, | ||
location=GCP_LOCATION, | ||
project_id=GCP_PROJECT, | ||
dataproc_job_id=job_id, | ||
gcp_conn_id=GCP_CONN_ID, | ||
timeout=TIMEOUT, | ||
) | ||
with self.assertRaisesRegex(AirflowException, "Job was cancelled"): | ||
sensor.poke(context={}) | ||
|
||
mock_hook.return_value.get_job.assert_called_once_with( | ||
job_id=job_id, location=GCP_LOCATION, project_id=GCP_PROJECT | ||
) |