amoghrajesh opened a new pull request, #76413:
URL: https://github.com/apache/airflow/pull/76413

    <!-- SPDX-License-Identifier: Apache-2.0
         https://www.apache.org/licenses/LICENSE-2.0 -->
   
   <!--
   Thank you for contributing!
   
   Please provide above a brief description of the changes made in this pull 
request.
   Write a good git commit message following this guide: 
https://chris.beams.io/posts/git-commit/
   
   Please make sure that your code changes are covered with tests.
   And in case of new features or big changes remember to adjust the 
documentation.
   
   For user-facing UI changes, please attach before/after screenshots (or a 
short
   screen recording) so reviewers can assess the visual impact.
   
   Feel free to ping (in general) for the review if you do not see reaction for 
a few days
   (72 Hours is the minimum reaction time you can expect from volunteers) - we 
sometimes miss notifications.
   
   In case of an existing issue, reference it using one of the following:
   
   * closes: #ISSUE
   * related: #ISSUE
   -->
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, 
please
   change below checkbox to `[X]` followed by the name of the tool, uncomment 
the "Generated-by".
   -->
   
   - [ ] Yes (please specify the tool below)
   
   <!--
   Generated-by: [Tool Name] following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   -->
   
   Follow-up to #76033, which added this for `SageMakerTrainingOperator`.
   
   `SageMakerProcessingOperator` has no `on_kill`. Kill the task, clear it, or 
let it hit its timeout, and the SageMaker processing job keeps running and 
keeps billing, with nothing left in Airflow pointing at it. The only way to 
stop it is to find it
   in the console.
   
   This adds an opt-in `stop_processing_job_on_kill` flag that stops the job 
instead, matching `stop_training_job_on_kill` on the training operator. It 
defaults to `False`, so nothing changes for existing Dags.
   
   The job name is recorded before the submit call rather than after, so a kill 
landing in that window still has a name to act on, and after the timestamp 
rename that `_get_unique_job_name` may apply, so `on_kill` stops the job that 
was actually
   submitted rather than the name in the config.
   
   ### Shared trigger change
   
   A deferred task is cancelled from the Triggerer, and `SageMakerTrigger` is 
shared across every SageMaker job type. Its `on_kill` currently returns early 
unless `job_type` is Training. That becomes a map instead:
   
   ```python
   STOP_METHODS_BY_JOB_TYPE: ClassVar[dict[str, str]] = {
       "training": "stop_training_job",
       "processing": "stop_processing_job",
   }
   ```
   
   A job type absent from the map is a no-op, so Transform, Tuning and Endpoint 
behave exactly as before and cannot receive another type's stop call. Adding 
one later is a map entry plus a hook method.
   
   `SageMakerHook.stop_processing_job` mirrors `stop_training_job`: it swallows 
`ValidationException`, which is what SageMaker returns when the job already 
reached a terminal state or never existed, since that race is expected when a 
task is killed as its job finishes. Any other `ClientError` is re-raised.
   
   ### Testing
   
   Using this processing image: `export 
SAGEMAKER_PROCESSING_IMAGE='141502667606.dkr.ecr.eu-west-1.amazonaws.com/sagemaker-scikit-learn:1.2-1-cpu-py3'`
   
   Using the Dag:
   
   ```python
   from __future__ import annotations
   
   import os
   from datetime import datetime, timedelta
   
   from airflow.providers.amazon.aws.operators.sagemaker import 
SageMakerProcessingOperator
   from airflow.sdk import DAG
   
   ROLE_ARN = os.environ["SAGEMAKER_ROLE_ARN"]
   IMAGE_URI = os.environ["SAGEMAKER_PROCESSING_IMAGE"]
   REGION = os.environ.get("SAGEMAKER_REGION", "eu-west-1")
   STOP_ON_KILL = os.environ.get("SAGEMAKER_STOP_ON_KILL", "1") != "0"
   DEFERRABLE = os.environ.get("SAGEMAKER_DEFERRABLE") == "1"
   
   PROCESSING_CONFIG = {
       "ProcessingJobName": "airflow-on-kill-processing",
       "AppSpecification": {
           "ImageUri": IMAGE_URI,
           "ContainerEntrypoint": ["sleep", "900"],
       },
       "ProcessingResources": {
           "ClusterConfig": {
               "InstanceCount": 1,
               "InstanceType": "ml.t3.medium",
               "VolumeSizeInGB": 10,
           }
       },
       "StoppingCondition": {"MaxRuntimeInSeconds": 1200},
       "RoleArn": ROLE_ARN,
   }
   
   with DAG(
       dag_id="test_sagemaker_processing_on_kill",
       schedule=None,
       start_date=datetime(2026, 1, 1),
       catchup=False,
       tags=["manual-test"],
   ) as dag:
       # SageMaker rejects an image from a different region than the client, so 
the region is
       # pinned here rather than left to the connection's default.
       SageMakerProcessingOperator(
           task_id="process",
           config=PROCESSING_CONFIG,
           region_name=REGION,
           check_interval=10,
           deferrable=DEFERRABLE,
           stop_processing_job_on_kill=STOP_ON_KILL,
           execution_timeout=timedelta(minutes=20),
       )
   ```
   
   Ran it and marked task as fail once the processing job was in `InProgress` 
state -
   
   Worker mode:
   
   <img width="2555" height="1309" alt="image" 
src="https://github.com/user-attachments/assets/1d9769f2-0aec-410b-af43-65f09d1c5397";
 />
   
   
   <img width="474" height="133" alt="image" 
src="https://github.com/user-attachments/assets/ecd726d1-d294-416a-9fd3-1092788d2890";
 />
   
   
   Deferrable mode:
   
   Ran same steps there too -
   
   <img width="2500" height="1185" alt="image" 
src="https://github.com/user-attachments/assets/1aa2db31-56fa-4d2e-b0e8-878724453925";
 />
   
   
   <img width="542" height="149" alt="image" 
src="https://github.com/user-attachments/assets/71cc4c35-f6c5-40f5-928c-d711230b18ba";
 />
   
   
   
   
   ### Next steps
   
   `SageMakerTransformOperator`, `SageMakerTuningOperator` and 
`SageMakerAutoMLOperator` still have no `on_kill`. Transform and Tuning are now 
small additions on top of this; Tuning is worth doing first, since one tuning 
job fans out into many training jobs and is the most expensive to leak. AutoML 
has no deferrable path, so it would be worker-side only. 
`SageMakerStartPipelineOperator` is a separate case: the hook already has 
`stop_pipeline` and `SageMakerStopPipelineOperator` already exists.
   
   
   ---
   
   * Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information. Note: commit author/co-author name and email in commits 
become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License 
Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: 
`{pr_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
 You can add this file in a follow-up commit after the PR is created so you 
know the PR number.
   


-- 
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]

Reply via email to