-
Notifications
You must be signed in to change notification settings - Fork 13.6k
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
[AIRFLOW-6759] Added MLEngine operator/hook to cancel MLEngine jobs #7400
Changes from 4 commits
f5c5f5b
582e45e
b3f1cf7
7e13a62
6de75c4
e815aac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -1015,3 +1015,58 @@ def check_existing_job(existing_job): | |||
if finished_training_job['state'] != 'SUCCEEDED': | ||||
self.log.error('MLEngine training job failed: %s', str(finished_training_job)) | ||||
raise RuntimeError(finished_training_job['errorMessage']) | ||||
|
||||
|
||||
class MLEngineTrainingJobFailureOperator(BaseOperator): | ||||
|
||||
""" | ||||
Operator for cleaning up failed MLEngine training job. | ||||
|
||||
:param job_id: A unique templated id for the submitted Google MLEngine | ||||
training job. (templated) | ||||
:type job_id: str | ||||
:param project_id: The Google Cloud project name within which MLEngine training job should run. | ||||
If set to None or missing, the default project_id from the GCP connection is used. (templated) | ||||
:type project_id: str | ||||
:param gcp_conn_id: The connection ID to use when fetching connection info. | ||||
:type gcp_conn_id: str | ||||
:param delegate_to: The account to impersonate, if any. | ||||
For this to work, the service account making the request must have | ||||
domain-wide delegation enabled. | ||||
:type delegate_to: str | ||||
""" | ||||
|
||||
template_fields = [ | ||||
'_project_id', | ||||
'_job_id', | ||||
] | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let use tuple here :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an option, but I thought it'd be better to maintain consistency between the operators (all other ones uses an array). Unless, there is some other non-style related reasons? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It varies. But in my opinion, this is immutable field :) |
||||
|
||||
@apply_defaults | ||||
def __init__(self, | ||||
job_id: str, | ||||
project_id: Optional[str] = None, | ||||
gcp_conn_id: str = 'google_cloud_default', | ||||
delegate_to: Optional[str] = None, | ||||
*args, | ||||
**kwargs) -> None: | ||||
super().__init__(*args, **kwargs) | ||||
self._project_id = project_id | ||||
self._job_id = job_id | ||||
self._gcp_conn_id = gcp_conn_id | ||||
self._delegate_to = delegate_to | ||||
|
||||
if not self._project_id: | ||||
raise AirflowException('Google Cloud project id is required.') | ||||
if not self._job_id: | ||||
raise AirflowException( | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No need for that as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uhmm, I'm not super sure why it was raised either, but I see the same checking in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MLEngineOperators are not best ones :D |
||||
'An unique job id is required for Google MLEngine training ' | ||||
'job.') | ||||
|
||||
def execute(self, context): | ||||
|
||||
hook = MLEngineHook( | ||||
gcp_conn_id=self._gcp_conn_id, | ||||
delegate_to=self._delegate_to | ||||
) | ||||
|
||||
hook.cancel_job(project_id=self._project_id, job_id=_normalize_mlengine_job_id(self._job_id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as the comment above, it seems like the code raises an error if
project_id
is none/missing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All operators except BigQuery allow passing project_id as a parameter. However, if this parameter is omitted, the default value will be read from the credentials.
https://github.com/apache/airflow/blob/97a429f/airflow/providers/google/cloud/hooks/base.py#L334-L360
It is very difficult to authorize and not get project_id. This is not even possible with production deployment. You may not have project_id when using gcloud for authorization only.