|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +try: |
| 4 | + from inspect import getfullargspec as _getargspec |
| 5 | +except ImportError: |
| 6 | + from inspect import getargspec as _getargspec |
| 7 | + |
| 8 | +from flytekit import __version__ |
| 9 | +import sys as _sys |
| 10 | +import six as _six |
| 11 | +from flytekit.common.tasks import task as _base_tasks |
| 12 | +from flytekit.common.types import helpers as _helpers, primitives as _primitives |
| 13 | + |
| 14 | +from flytekit.models import literals as _literal_models, task as _task_models |
| 15 | +from google.protobuf.json_format import MessageToDict as _MessageToDict |
| 16 | +from flytekit.common import interface as _interface |
| 17 | +from flytekit.common.exceptions import user as _user_exceptions |
| 18 | +from flytekit.common.exceptions import scopes as _exception_scopes |
| 19 | + |
| 20 | +from flytekit.configuration import internal as _internal_config |
| 21 | + |
| 22 | +input_types_supported = { _primitives.Integer, |
| 23 | + _primitives.Boolean, |
| 24 | + _primitives.Float, |
| 25 | + _primitives.String, |
| 26 | + _primitives.Datetime, |
| 27 | + _primitives.Timedelta, |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | +class SdkGenericSparkTask( _base_tasks.SdkTask): |
| 32 | + """ |
| 33 | + This class includes the additional logic for building a task that executes as a Spark Job. |
| 34 | +
|
| 35 | + """ |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + task_type, |
| 39 | + discovery_version, |
| 40 | + retries, |
| 41 | + interruptible, |
| 42 | + task_inputs, |
| 43 | + deprecated, |
| 44 | + discoverable, |
| 45 | + timeout, |
| 46 | + spark_type, |
| 47 | + main_class, |
| 48 | + main_application_file, |
| 49 | + spark_conf, |
| 50 | + hadoop_conf, |
| 51 | + environment, |
| 52 | + ): |
| 53 | + """ |
| 54 | + :param Text task_type: string describing the task type |
| 55 | + :param Text discovery_version: string describing the version for task discovery purposes |
| 56 | + :param int retries: Number of retries to attempt |
| 57 | + :param bool interruptible: Whether or not task is interruptible |
| 58 | + :param Text deprecated: |
| 59 | + :param bool discoverable: |
| 60 | + :param datetime.timedelta timeout: |
| 61 | + :param Text spark_type: Type of Spark Job: Scala/Java |
| 62 | + :param Text main_class: Main class to execute for Scala/Java jobs |
| 63 | + :param Text main_application_file: Main application file |
| 64 | + :param dict[Text,Text] spark_conf: |
| 65 | + :param dict[Text,Text] hadoop_conf: |
| 66 | + :param dict[Text,Text] environment: [optional] environment variables to set when executing this task. |
| 67 | + """ |
| 68 | + |
| 69 | + spark_job = _task_models.SparkJob( |
| 70 | + spark_conf=spark_conf, |
| 71 | + hadoop_conf=hadoop_conf, |
| 72 | + spark_type = spark_type, |
| 73 | + application_file=main_application_file, |
| 74 | + main_class=main_class, |
| 75 | + executor_path=_sys.executable, |
| 76 | + ).to_flyte_idl() |
| 77 | + |
| 78 | + super(SdkGenericSparkTask, self).__init__( |
| 79 | + task_type, |
| 80 | + _task_models.TaskMetadata( |
| 81 | + discoverable, |
| 82 | + _task_models.RuntimeMetadata( |
| 83 | + _task_models.RuntimeMetadata.RuntimeType.FLYTE_SDK, |
| 84 | + __version__, |
| 85 | + 'spark' |
| 86 | + ), |
| 87 | + timeout, |
| 88 | + _literal_models.RetryStrategy(retries), |
| 89 | + interruptible, |
| 90 | + discovery_version, |
| 91 | + deprecated |
| 92 | + ), |
| 93 | + _interface.TypedInterface({}, {}), |
| 94 | + _MessageToDict(spark_job), |
| 95 | + ) |
| 96 | + |
| 97 | + # Add Inputs |
| 98 | + if task_inputs is not None: |
| 99 | + task_inputs(self) |
| 100 | + |
| 101 | + # Container after the Inputs have been updated. |
| 102 | + self._container = self._get_container_definition( |
| 103 | + environment=environment |
| 104 | + ) |
| 105 | + |
| 106 | + def _validate_inputs(self, inputs): |
| 107 | + """ |
| 108 | + :param dict[Text, flytekit.models.interface.Variable] inputs: Input variables to validate |
| 109 | + :raises: flytekit.common.exceptions.user.FlyteValidationException |
| 110 | + """ |
| 111 | + for k, v in _six.iteritems(inputs): |
| 112 | + sdk_type =_helpers.get_sdk_type_from_literal_type(v.type) |
| 113 | + if sdk_type not in input_types_supported: |
| 114 | + raise _user_exceptions.FlyteValidationException( |
| 115 | + "Input Type '{}' not supported. Only Primitives are supported for Scala/Java Spark.".format(sdk_type) |
| 116 | + ) |
| 117 | + super(SdkGenericSparkTask, self)._validate_inputs(inputs) |
| 118 | + |
| 119 | + @_exception_scopes.system_entry_point |
| 120 | + def add_inputs(self, inputs): |
| 121 | + """ |
| 122 | + Adds the inputs to this task. This can be called multiple times, but it will fail if an input with a given |
| 123 | + name is added more than once, a name collides with an output, or if the name doesn't exist as an arg name in |
| 124 | + the wrapped function. |
| 125 | + :param dict[Text, flytekit.models.interface.Variable] inputs: names and variables |
| 126 | + """ |
| 127 | + self._validate_inputs(inputs) |
| 128 | + self.interface.inputs.update(inputs) |
| 129 | + |
| 130 | + def _get_container_definition( |
| 131 | + self, |
| 132 | + environment=None, |
| 133 | + ): |
| 134 | + """ |
| 135 | + :rtype: Container |
| 136 | + """ |
| 137 | + |
| 138 | + args = [] |
| 139 | + for k, v in _six.iteritems(self.interface.inputs): |
| 140 | + args.append("--{}".format(k)) |
| 141 | + args.append("{{{{.Inputs.{}}}}}".format(k)) |
| 142 | + |
| 143 | + return _task_models.Container( |
| 144 | + image=_internal_config.IMAGE.get(), |
| 145 | + command=[], |
| 146 | + args=args, |
| 147 | + resources=_task_models.Resources([], []), |
| 148 | + env=environment, |
| 149 | + config={} |
| 150 | + ) |
0 commit comments