mathiaHT commented on code in PR #38830: URL: https://github.com/apache/airflow/pull/38830#discussion_r1600116985
########## airflow/providers/amazon/aws/operators/glue_session.py: ########## @@ -0,0 +1,260 @@ +# 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. +from __future__ import annotations + +from functools import cached_property +from typing import TYPE_CHECKING, Sequence + +from airflow.configuration import conf +from airflow.exceptions import AirflowException +from airflow.providers.amazon.aws.hooks.glue_session import ( + GlueSessionHook, + GlueSessionProtocol, + GlueSessionStates, +) +from airflow.providers.amazon.aws.operators.base_aws import AwsBaseOperator +from airflow.providers.amazon.aws.triggers.glue_session import GlueSessionReadyTrigger +from airflow.utils.helpers import prune_dict + +if TYPE_CHECKING: + import boto3 + + from airflow.utils.context import Context + + +class GlueSessionBaseOperator(AwsBaseOperator[GlueSessionHook]): + """This is the base operator for all Glue service operators.""" + + aws_hook_class = GlueSessionHook + + @cached_property + def client(self) -> GlueSessionProtocol | boto3.client: + """Create and return the GlueSessionHook's client.""" + return self.hook.conn + + def _complete_exec_with_session(self, context, event=None): + """To be used as trigger callback for operators that return the session description.""" + if event["status"] != "success": + raise AirflowException(f"Error while waiting for operation on session to complete: {event}") + session_id = event.get("id") + # We cannot get the cluster definition from the waiter on success, so we have to query it here. + details = self.hook.conn.get_session(Id=session_id)["Session"] + return details + + +class GlueCreateSessionOperator(GlueSessionBaseOperator): + """Create an AWS Glue Session. + + AWS Glue is a serverless Spark ETL service for running Spark Jobs on the AWS + cloud. Language support: Python and Scala. + + .. seealso:: + For more information on how to use this operator, take a look at the guide: + :ref:`howto/operator:GlueCreateSessionOperator` + + :param session_id: session id + :param session_desc: session description + :param region_name: aws region name (example: us-east-1) + :param iam_role_name: AWS IAM Role for Glue Session Execution. If set `iam_role_arn` must equal None. + :param iam_role_arn: AWS IAM Role ARN for Glue Session Execution, If set `iam_role_name` must equal None. + :param num_of_dpus: Number of AWS Glue DPUs to allocate to this Session + :param create_session_kwargs: Extra arguments for Glue Session Creation + """ + + template_fields: Sequence[str] = ( + "session_id", + "create_session_kwargs", + "iam_role_name", + "iam_role_arn", + ) + template_fields_renderers = { + "create_session_kwargs": "json", + } + + def __init__( + self, + *, + session_id: str = "aws_glue_default_session", + session_desc: str = "AWS Glue Session with Airflow", + aws_conn_id: str = "aws_default", + iam_role_name: str | None = None, + iam_role_arn: str | None = None, + num_of_dpus: int | None = None, + create_session_kwargs: dict | None = None, + wait_for_readiness: bool = True, + waiter_delay: int = 15, + waiter_max_attempts: int = 60, + deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False), + session_poll_interval: int | float = 6, + delete_session_on_kill: bool = False, + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.session_id = session_id + self.session_desc = session_desc + self.aws_conn_id = aws_conn_id + self.iam_role_name = iam_role_name + self.iam_role_arn = iam_role_arn + self.create_session_kwargs = create_session_kwargs or {} + self.wait_for_readiness = wait_for_readiness + self.waiter_delay = waiter_delay + self.waiter_max_attempts = waiter_max_attempts + self.session_poll_interval = session_poll_interval + self.delete_session_on_kill = delete_session_on_kill + self.deferrable = deferrable + + worker_type_exists = "WorkerType" in self.create_session_kwargs + num_workers_exists = "NumberOfWorkers" in self.create_session_kwargs + + if self.iam_role_arn and self.iam_role_name: + raise ValueError("Cannot set iam_role_arn and iam_role_name simultaneously") + if worker_type_exists and num_workers_exists: + if num_of_dpus is not None: + raise ValueError("Cannot specify num_of_dpus with custom WorkerType") + elif not worker_type_exists and num_workers_exists: + raise ValueError("Need to specify custom WorkerType when specifying NumberOfWorkers") + elif worker_type_exists and not num_workers_exists: + raise ValueError("Need to specify NumberOfWorkers when specifying custom WorkerType") + elif num_of_dpus is None: + self.num_of_dpus: int | float = 10 Review Comment: This is the default value of the resource in aws -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
