|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Pipeline parameters and conditions for workflow.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +from enum import Enum |
| 17 | +from typing import List |
| 18 | +import attr |
| 19 | + |
| 20 | +from sagemaker.workflow.entities import Entity, DefaultEnumMeta, RequestType |
| 21 | + |
| 22 | + |
| 23 | +DEFAULT_BACKOFF_RATE = 2.0 |
| 24 | +DEFAULT_INTERVAL_SECONDS = 1 |
| 25 | +MAX_ATTEMPTS_CAP = 20 |
| 26 | +MAX_EXPIRE_AFTER_MIN = 14400 |
| 27 | + |
| 28 | + |
| 29 | +class StepExceptionTypeEnum(Enum, metaclass=DefaultEnumMeta): |
| 30 | + """Step ExceptionType enum.""" |
| 31 | + |
| 32 | + SERVICE_FAULT = "Step.SERVICE_FAULT" |
| 33 | + THROTTLING = "Step.THROTTLING" |
| 34 | + |
| 35 | + |
| 36 | +class SageMakerJobExceptionTypeEnum(Enum, metaclass=DefaultEnumMeta): |
| 37 | + """SageMaker Job ExceptionType enum.""" |
| 38 | + |
| 39 | + INTERNAL_ERROR = "SageMaker.JOB_INTERNAL_ERROR" |
| 40 | + CAPACITY_ERROR = "SageMaker.CAPACITY_ERROR" |
| 41 | + RESOURCE_LIMIT = "SageMaker.RESOURCE_LIMIT" |
| 42 | + |
| 43 | + |
| 44 | +@attr.s |
| 45 | +class RetryPolicy(Entity): |
| 46 | + """RetryPolicy base class |
| 47 | +
|
| 48 | + Attributes: |
| 49 | + backoff_rate (float): The multiplier by which the retry interval increases |
| 50 | + during each attempt (default: 2.0) |
| 51 | + interval_seconds (int): An integer that represents the number of seconds before the |
| 52 | + first retry attempt (default: 1) |
| 53 | + max_attempts (int): A positive integer that represents the maximum |
| 54 | + number of retry attempts. (default: None) |
| 55 | + expire_after_mins (int): A positive integer that represents the maximum minute |
| 56 | + to expire any further retry attempt (default: None) |
| 57 | + """ |
| 58 | + |
| 59 | + backoff_rate: float = attr.ib(default=DEFAULT_BACKOFF_RATE) |
| 60 | + interval_seconds: int = attr.ib(default=DEFAULT_INTERVAL_SECONDS) |
| 61 | + max_attempts: int = attr.ib(default=None) |
| 62 | + expire_after_mins: int = attr.ib(default=None) |
| 63 | + |
| 64 | + @backoff_rate.validator |
| 65 | + def validate_backoff_rate(self, _, value): |
| 66 | + """Validate the input back off rate type""" |
| 67 | + if value: |
| 68 | + assert value >= 0.0, "backoff_rate should be non-negative" |
| 69 | + |
| 70 | + @interval_seconds.validator |
| 71 | + def validate_interval_seconds(self, _, value): |
| 72 | + """Validate the input interval seconds""" |
| 73 | + if value: |
| 74 | + assert value >= 0.0, "interval_seconds rate should be non-negative" |
| 75 | + |
| 76 | + @max_attempts.validator |
| 77 | + def validate_max_attempts(self, _, value): |
| 78 | + """Validate the input max attempts""" |
| 79 | + if value: |
| 80 | + assert ( |
| 81 | + MAX_ATTEMPTS_CAP >= value >= 1 |
| 82 | + ), f"max_attempts must in range of (0, {MAX_ATTEMPTS_CAP}] attempts" |
| 83 | + |
| 84 | + @expire_after_mins.validator |
| 85 | + def validate_expire_after_mins(self, _, value): |
| 86 | + """Validate expire after mins""" |
| 87 | + if value: |
| 88 | + assert ( |
| 89 | + MAX_EXPIRE_AFTER_MIN >= value >= 0 |
| 90 | + ), f"expire_after_mins must in range of (0, {MAX_EXPIRE_AFTER_MIN}] minutes" |
| 91 | + |
| 92 | + def to_request(self) -> RequestType: |
| 93 | + """Get the request structure for workflow service calls.""" |
| 94 | + if (self.max_attempts is None) == self.expire_after_mins is None: |
| 95 | + raise ValueError("Only one of [max_attempts] and [expire_after_mins] can be given.") |
| 96 | + |
| 97 | + request = { |
| 98 | + "BackoffRate": self.backoff_rate, |
| 99 | + "IntervalSeconds": self.interval_seconds, |
| 100 | + } |
| 101 | + |
| 102 | + if self.max_attempts: |
| 103 | + request["MaxAttempts"] = self.max_attempts |
| 104 | + |
| 105 | + if self.expire_after_mins: |
| 106 | + request["ExpireAfterMin"] = self.expire_after_mins |
| 107 | + |
| 108 | + return request |
| 109 | + |
| 110 | + |
| 111 | +class StepRetryPolicy(RetryPolicy): |
| 112 | + """RetryPolicy for a retryable step. The pipeline service will retry |
| 113 | +
|
| 114 | + `sagemaker.workflow.retry.StepRetryExceptionTypeEnum.SERVICE_FAULT` and |
| 115 | + `sagemaker.workflow.retry.StepRetryExceptionTypeEnum.THROTTLING` regardless of |
| 116 | + pipeline step type by default. However, for step defined as retryable, you can override them |
| 117 | + by specifying a StepRetryPolicy. |
| 118 | +
|
| 119 | + Attributes: |
| 120 | + exception_types (List[StepExceptionTypeEnum]): the exception types to match for this policy |
| 121 | + backoff_rate (float): The multiplier by which the retry interval increases |
| 122 | + during each attempt (default: 2.0) |
| 123 | + interval_seconds (int): An integer that represents the number of seconds before the |
| 124 | + first retry attempt (default: 1) |
| 125 | + max_attempts (int): A positive integer that represents the maximum |
| 126 | + number of retry attempts. (default: None) |
| 127 | + expire_after_mins (int): A positive integer that represents the maximum minute |
| 128 | + to expire any further retry attempt (default: None) |
| 129 | + """ |
| 130 | + |
| 131 | + def __init__( |
| 132 | + self, |
| 133 | + exception_types: List[StepExceptionTypeEnum], |
| 134 | + backoff_rate: float = 2.0, |
| 135 | + interval_seconds: int = 1, |
| 136 | + max_attempts: int = None, |
| 137 | + expire_after_mins: int = None, |
| 138 | + ): |
| 139 | + super().__init__(backoff_rate, interval_seconds, max_attempts, expire_after_mins) |
| 140 | + for exception_type in exception_types: |
| 141 | + if not isinstance(exception_type, StepExceptionTypeEnum): |
| 142 | + raise ValueError(f"{exception_type} is not of StepExceptionTypeEnum.") |
| 143 | + self.exception_types = exception_types |
| 144 | + |
| 145 | + def to_request(self) -> RequestType: |
| 146 | + """Gets the request structure for retry policy.""" |
| 147 | + request = super().to_request() |
| 148 | + request["ExceptionType"] = [e.value for e in self.exception_types] |
| 149 | + return request |
| 150 | + |
| 151 | + |
| 152 | +class SageMakerJobStepRetryPolicy(RetryPolicy): |
| 153 | + """RetryPolicy for exception thrown by SageMaker Job. |
| 154 | +
|
| 155 | + Attributes: |
| 156 | + exception_types (List[SageMakerJobExceptionTypeEnum]): |
| 157 | + The SageMaker exception to match for this policy. The SageMaker exceptions |
| 158 | + captured here are the exceptions thrown by synchronously |
| 159 | + creating the job. For instance the resource limit exception. |
| 160 | + failure_reason_types (List[SageMakerJobExceptionTypeEnum]): the SageMaker |
| 161 | + failure reason types to match for this policy. The failure reason type |
| 162 | + is presented in FailureReason field of the Describe response, it indicates |
| 163 | + the runtime failure reason for a job. |
| 164 | + backoff_rate (float): The multiplier by which the retry interval increases |
| 165 | + during each attempt (default: 2.0) |
| 166 | + interval_seconds (int): An integer that represents the number of seconds before the |
| 167 | + first retry attempt (default: 1) |
| 168 | + max_attempts (int): A positive integer that represents the maximum |
| 169 | + number of retry attempts. (default: None) |
| 170 | + expire_after_mins (int): A positive integer that represents the maximum minute |
| 171 | + to expire any further retry attempt (default: None) |
| 172 | + """ |
| 173 | + |
| 174 | + def __init__( |
| 175 | + self, |
| 176 | + exception_types: List[SageMakerJobExceptionTypeEnum] = None, |
| 177 | + failure_reason_types: List[SageMakerJobExceptionTypeEnum] = None, |
| 178 | + backoff_rate: float = 2.0, |
| 179 | + interval_seconds: int = 1, |
| 180 | + max_attempts: int = None, |
| 181 | + expire_after_mins: int = None, |
| 182 | + ): |
| 183 | + super().__init__(backoff_rate, interval_seconds, max_attempts, expire_after_mins) |
| 184 | + |
| 185 | + if not exception_types and not failure_reason_types: |
| 186 | + raise ValueError( |
| 187 | + "At least one of the [exception_types, failure_reason_types] needs to be given." |
| 188 | + ) |
| 189 | + |
| 190 | + self.exception_type_list: List[SageMakerJobExceptionTypeEnum] = [] |
| 191 | + if exception_types: |
| 192 | + self.exception_type_list += exception_types |
| 193 | + if failure_reason_types: |
| 194 | + self.exception_type_list += failure_reason_types |
| 195 | + |
| 196 | + for exception_type in self.exception_type_list: |
| 197 | + if not isinstance(exception_type, SageMakerJobExceptionTypeEnum): |
| 198 | + raise ValueError(f"{exception_type} is not of SageMakerJobExceptionTypeEnum.") |
| 199 | + |
| 200 | + def to_request(self) -> RequestType: |
| 201 | + """Gets the request structure for retry policy.""" |
| 202 | + request = super().to_request() |
| 203 | + request["ExceptionType"] = [e.value for e in self.exception_type_list] |
| 204 | + return request |
0 commit comments