|
20 | 20 | from sagemaker.workflow.emr_step import EMRStep, EMRStepConfig
|
21 | 21 | from sagemaker.workflow.parameters import ParameterInteger
|
22 | 22 | from sagemaker.workflow.pipeline import Pipeline
|
| 23 | +from sagemaker.workflow.retry import StepRetryPolicy, StepExceptionTypeEnum |
23 | 24 |
|
24 | 25 |
|
25 | 26 | @pytest.fixture
|
@@ -134,3 +135,215 @@ def test_emr_with_cluster_config(sagemaker_session, role, pipeline_name, region_
|
134 | 135 | pipeline.delete()
|
135 | 136 | except Exception:
|
136 | 137 | pass
|
| 138 | + |
| 139 | + |
| 140 | +def test_emr_with_retry_policies(sagemaker_session, role, pipeline_name, region_name): |
| 141 | + """Test EMR steps with retry policies in both cluster_id and cluster_config scenarios.""" |
| 142 | + emr_step_config = EMRStepConfig( |
| 143 | + jar="s3://us-west-2.elasticmapreduce/libs/script-runner/script-runner.jar", |
| 144 | + args=["dummy_emr_script_path"], |
| 145 | + ) |
| 146 | + |
| 147 | + retry_policies = [ |
| 148 | + StepRetryPolicy( |
| 149 | + exception_types=[StepExceptionTypeEnum.SERVICE_FAULT], |
| 150 | + interval_seconds=1, |
| 151 | + max_attempts=3, |
| 152 | + backoff_rate=2.0, |
| 153 | + ) |
| 154 | + ] |
| 155 | + |
| 156 | + # Step with existing cluster and retry policies |
| 157 | + step_emr_1 = EMRStep( |
| 158 | + name="emr-step-1", |
| 159 | + cluster_id="j-1YONHTCP3YZKC", |
| 160 | + display_name="emr_step_1", |
| 161 | + description="EMR Step with retry policies", |
| 162 | + step_config=emr_step_config, |
| 163 | + retry_policies=retry_policies, |
| 164 | + ) |
| 165 | + |
| 166 | + # Step with cluster config and retry policies |
| 167 | + cluster_config = { |
| 168 | + "Instances": { |
| 169 | + "InstanceGroups": [ |
| 170 | + { |
| 171 | + "Name": "Master Instance Group", |
| 172 | + "InstanceRole": "MASTER", |
| 173 | + "InstanceCount": 1, |
| 174 | + "InstanceType": "m1.small", |
| 175 | + "Market": "ON_DEMAND", |
| 176 | + } |
| 177 | + ], |
| 178 | + "InstanceCount": 1, |
| 179 | + "HadoopVersion": "MyHadoopVersion", |
| 180 | + }, |
| 181 | + "AmiVersion": "3.8.0", |
| 182 | + "AdditionalInfo": "MyAdditionalInfo", |
| 183 | + } |
| 184 | + |
| 185 | + step_emr_2 = EMRStep( |
| 186 | + name="emr-step-2", |
| 187 | + display_name="emr_step_2", |
| 188 | + description="EMR Step with cluster config and retry policies", |
| 189 | + cluster_id=None, |
| 190 | + step_config=emr_step_config, |
| 191 | + cluster_config=cluster_config, |
| 192 | + retry_policies=retry_policies, |
| 193 | + ) |
| 194 | + |
| 195 | + pipeline = Pipeline( |
| 196 | + name=pipeline_name, |
| 197 | + steps=[step_emr_1, step_emr_2], |
| 198 | + sagemaker_session=sagemaker_session, |
| 199 | + ) |
| 200 | + |
| 201 | + try: |
| 202 | + response = pipeline.create(role) |
| 203 | + create_arn = response["PipelineArn"] |
| 204 | + assert re.match( |
| 205 | + rf"arn:aws:sagemaker:{region_name}:\d{{12}}:pipeline/{pipeline_name}", |
| 206 | + create_arn, |
| 207 | + ) |
| 208 | + finally: |
| 209 | + try: |
| 210 | + pipeline.delete() |
| 211 | + except Exception: |
| 212 | + pass |
| 213 | + |
| 214 | + |
| 215 | +def test_emr_with_expire_after_retry_policy(sagemaker_session, role, pipeline_name, region_name): |
| 216 | + """Test EMR step with retry policy using expire_after_mins.""" |
| 217 | + emr_step_config = EMRStepConfig( |
| 218 | + jar="s3://us-west-2.elasticmapreduce/libs/script-runner/script-runner.jar", |
| 219 | + args=["dummy_emr_script_path"], |
| 220 | + ) |
| 221 | + |
| 222 | + retry_policies = [ |
| 223 | + StepRetryPolicy( |
| 224 | + exception_types=[StepExceptionTypeEnum.SERVICE_FAULT], |
| 225 | + interval_seconds=1, |
| 226 | + expire_after_mins=30, |
| 227 | + backoff_rate=2.0, |
| 228 | + ) |
| 229 | + ] |
| 230 | + |
| 231 | + step_emr = EMRStep( |
| 232 | + name="emr-step-expire", |
| 233 | + cluster_id="j-1YONHTCP3YZKC", |
| 234 | + display_name="emr_step_expire", |
| 235 | + description="EMR Step with expire after retry policy", |
| 236 | + step_config=emr_step_config, |
| 237 | + retry_policies=retry_policies, |
| 238 | + ) |
| 239 | + |
| 240 | + pipeline = Pipeline( |
| 241 | + name=pipeline_name, |
| 242 | + steps=[step_emr], |
| 243 | + sagemaker_session=sagemaker_session, |
| 244 | + ) |
| 245 | + |
| 246 | + try: |
| 247 | + response = pipeline.create(role) |
| 248 | + create_arn = response["PipelineArn"] |
| 249 | + assert re.match( |
| 250 | + rf"arn:aws:sagemaker:{region_name}:\d{{12}}:pipeline/{pipeline_name}", |
| 251 | + create_arn, |
| 252 | + ) |
| 253 | + finally: |
| 254 | + try: |
| 255 | + pipeline.delete() |
| 256 | + except Exception: |
| 257 | + pass |
| 258 | + |
| 259 | + |
| 260 | +def test_emr_with_multiple_exception_types(sagemaker_session, role, pipeline_name, region_name): |
| 261 | + """Test EMR step with multiple exception types in retry policy.""" |
| 262 | + retry_policies = [ |
| 263 | + StepRetryPolicy( |
| 264 | + exception_types=[StepExceptionTypeEnum.SERVICE_FAULT, StepExceptionTypeEnum.THROTTLING], |
| 265 | + interval_seconds=1, |
| 266 | + max_attempts=3, |
| 267 | + backoff_rate=2.0, |
| 268 | + ) |
| 269 | + ] |
| 270 | + |
| 271 | + step_emr = EMRStep( |
| 272 | + name="emr-step-multi-except", |
| 273 | + cluster_id="j-1YONHTCP3YZKC", |
| 274 | + display_name="emr_step_multi_except", |
| 275 | + description="EMR Step with multiple exception types", |
| 276 | + step_config=EMRStepConfig( |
| 277 | + jar="s3://us-west-2.elasticmapreduce/libs/script-runner/script-runner.jar", |
| 278 | + args=["dummy_emr_script_path"], |
| 279 | + ), |
| 280 | + retry_policies=retry_policies, |
| 281 | + ) |
| 282 | + |
| 283 | + pipeline = Pipeline( |
| 284 | + name=pipeline_name, |
| 285 | + steps=[step_emr], |
| 286 | + sagemaker_session=sagemaker_session, |
| 287 | + ) |
| 288 | + |
| 289 | + try: |
| 290 | + response = pipeline.create(role) |
| 291 | + create_arn = response["PipelineArn"] |
| 292 | + assert re.match( |
| 293 | + rf"arn:aws:sagemaker:{region_name}:\d{{12}}:pipeline/{pipeline_name}", |
| 294 | + create_arn, |
| 295 | + ) |
| 296 | + finally: |
| 297 | + try: |
| 298 | + pipeline.delete() |
| 299 | + except Exception: |
| 300 | + pass |
| 301 | + |
| 302 | + |
| 303 | +def test_emr_with_multiple_retry_policies(sagemaker_session, role, pipeline_name, region_name): |
| 304 | + """Test EMR step with multiple retry policies.""" |
| 305 | + retry_policies = [ |
| 306 | + StepRetryPolicy( |
| 307 | + exception_types=[StepExceptionTypeEnum.SERVICE_FAULT], |
| 308 | + interval_seconds=1, |
| 309 | + max_attempts=3, |
| 310 | + backoff_rate=2.0, |
| 311 | + ), |
| 312 | + StepRetryPolicy( |
| 313 | + exception_types=[StepExceptionTypeEnum.THROTTLING], |
| 314 | + interval_seconds=5, |
| 315 | + expire_after_mins=60, |
| 316 | + backoff_rate=1.5, |
| 317 | + ), |
| 318 | + ] |
| 319 | + |
| 320 | + step_emr = EMRStep( |
| 321 | + name="emr-step-multi-policy", |
| 322 | + cluster_id="j-1YONHTCP3YZKC", |
| 323 | + display_name="emr_step_multi_policy", |
| 324 | + description="EMR Step with multiple retry policies", |
| 325 | + step_config=EMRStepConfig( |
| 326 | + jar="s3://us-west-2.elasticmapreduce/libs/script-runner/script-runner.jar", |
| 327 | + args=["dummy_emr_script_path"], |
| 328 | + ), |
| 329 | + retry_policies=retry_policies, |
| 330 | + ) |
| 331 | + |
| 332 | + pipeline = Pipeline( |
| 333 | + name=pipeline_name, |
| 334 | + steps=[step_emr], |
| 335 | + sagemaker_session=sagemaker_session, |
| 336 | + ) |
| 337 | + |
| 338 | + try: |
| 339 | + response = pipeline.create(role) |
| 340 | + create_arn = response["PipelineArn"] |
| 341 | + assert re.match( |
| 342 | + rf"arn:aws:sagemaker:{region_name}:\d{{12}}:pipeline/{pipeline_name}", |
| 343 | + create_arn, |
| 344 | + ) |
| 345 | + finally: |
| 346 | + try: |
| 347 | + pipeline.delete() |
| 348 | + except Exception: |
| 349 | + pass |
0 commit comments