← Back to Pull requests

fix(trainer): deduplicate restarted TrainJob pods #632

Merged
reckless-sherixx merged 2 files (104 lines) on Jul 23, 2026 00:21 UTC
View on GitHub
reckless-sherixx reckless-sherixx commented on Jul 22, 2026 16:23 UTC
Author

Summary

Fixes #25 by returning only the newest Pod for each TrainJob component in get_job().

  • Sorts Pods by creation_timestamp descending and keeps the first occurrence.

  • Uses the component role for initializer keys and role-index for launcher/node keys.

  • Adds a parametrized restart scenario covering both node-0 and node-1, with newer Pending Pods taking precedence over older Running Pods.
  • Why

    Kubernetes can recreate Pods under restart policies. Without de-duplication, get_job() returns stale and current Pods for the same component, producing conflicting statuses.

    Validation

  • python -m uv lock --check

  • python -m uv run ruff check --show-fixes --output-format=github .

  • python -m uv run ruff format --check kubeflow

  • python -m uv run ty check kubeflow/hub

  • python -m uv run coverage run --source=kubeflow -m pytest ./kubeflow/ (629 passed)
  • Supersedes the focused deduplication work in #160 without its unrelated changes.

    Diff: 104 lines (+98 / -6) across 2 files Reviewer: andreyvelich Review duration: 478m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.32) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.32 · 104 lines diff across 2 files
    0.32
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.90 · 5 substantive comment(s)
    0.90
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    87%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.278
    Real PR Conversation Timeline
    12 items
    github-actions[bot] github-actions[bot] commented on Jul 22, 2026 16:23 UTC
    Bot

    🎉 Welcome to the Kubeflow SDK! 🎉

    Thanks for opening your first PR! We're happy to have you as part of our community 🚀

    Here's what happens next:

  • If you haven't already, please check out our Contributing Guide for repo-specific guidelines and the Kubeflow Contributor Guide for general community standards

  • Our team will review your PR soon! cc @kubeflow/kubeflow-sdk-team
  • Join the community:

  • Slack: Join our #kubeflow-ml-experience and #kubeflow-trainer Slack channels

  • Meetings: Attend the Kubeflow SDK and ML Experience bi-weekly meetings
  • Feel free to ask questions in the comments if you need any help or clarification!
    Thanks again for contributing to Kubeflow! 🙏

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Jul 22, 2026 17:03 UTC
    Reviewer

    Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

    reckless-sherixx reckless-sherixx commented on Jul 22, 2026 17:03 UTC
    Author

    @andreyvelich Review this

    andreyvelich andreyvelich commented on code on Jul 22, 2026 17:18 UTC
    Code Review
    kubeflow/trainer/backends/kubernetes/backend_test.py:1
    @@ -210,6 +217,30 @@ def get_mock_pod_list():
         )
     
     
    +def get_mock_pod_list_with_restarts() -> models.IoK8sApiCoreV1PodList:
    +    """Create Pods where newer replacements share the same TrainJob component roles."""
    +    old_timestamp = datetime.datetime(2025, 6, 1, 10, 0, 0)
    +    new_timestamp = datetime.datetime(2025, 6, 1, 11, 0, 0)
    +    old_pods = get_mock_pod_list().items
    +    node_1_pod = copy.deepcopy(old_pods[-1])

    Select the node pod by role instead of position — old_pods[-1] silently breaks if get_mock_pod_list() is ever reordered.

        node_0_pod = next(
            p for p in old_pods if p.metadata.labels[constants.JOBSET_RJOB_NAME_LABEL] == constants.NODE
        )
        node_1_pod = copy.deepcopy(node_0_pod)
    

    andreyvelich andreyvelich commented on Jul 22, 2026 17:18 UTC
    Reviewer

    Thanks for tackling this, @reckless-sherixx! This review was performed with the help of AI tools.

    andreyvelich andreyvelich commented on code on Jul 22, 2026 17:18 UTC
    Code Review
    kubeflow/trainer/backends/kubernetes/backend_test.py:1
    @@ -763,6 +795,25 @@ def get_train_job_data_type(
         )
     
     
    +def get_train_job_with_restarted_pods_data_type(
    +    runtime_name: str,
    +    train_job_name: str,
    +) -> types.TrainJob:
    +    """Create the expected TrainJob after newer replacement Pods are selected."""
    +    train_job = get_train_job_data_type(runtime_name, train_job_name)
    +
    +    for step in train_job.steps:
    +        step.pod_name = f"{step.pod_name}-restarted"
    +        step.status = constants.POD_PENDING
    +
    +    node_1_step = copy.deepcopy(train_job.steps[-1])

    Same positional fragility — clone the node-0 step by name rather than by index.

        node_0_step = next(s for s in train_job.steps if s.name == "node-0")
        node_1_step = copy.deepcopy(node_0_step)
    

    andreyvelich andreyvelich commented on code on Jul 22, 2026 17:18 UTC
    Code Review
    kubeflow/trainer/backends/kubernetes/backend.py:1
    @@ -698,14 +698,32 @@ def __get_trainjob_from_cr(
                 if not pod_list:
                     return trainjob
     
    -            for pod in pod_list.items:
    +            sorted_pods = sorted(
    +                pod_list.items,
    +                key=lambda pod: (
    +                    pod.metadata is not None and pod.metadata.creation_timestamp is not None,

    This is not None guard exists to survive Pods with no creation_timestamp, but no test exercises it. A future simplification to key=lambda pod: pod.metadata.creation_timestamp would pass every test yet raise TypeError: '<' not supported between 'datetime' and 'NoneType' in production. Worth adding a case where one restarted Pod has creation_timestamp=None and the timestamped Pod still wins.

    andreyvelich andreyvelich commented on Jul 22, 2026 17:22 UTC
    Contributor

    /ok-to-test

    reckless-sherixx reckless-sherixx commented on code on Jul 22, 2026 17:29 UTC
    Code Review
    kubeflow/trainer/backends/kubernetes/backend.py:1
    @@ -698,14 +698,32 @@ def __get_trainjob_from_cr(
                 if not pod_list:
                     return trainjob
     
    -            for pod in pod_list.items:
    +            sorted_pods = sorted(
    +                pod_list.items,
    +                key=lambda pod: (
    +                    pod.metadata is not None and pod.metadata.creation_timestamp is not None,

    added

    andreyvelich andreyvelich commented on Jul 23, 2026 00:18 UTC
    Reviewer

    Thanks for this fix @reckless-sherixx!
    /lgtm
    /approve

    google-oss-prow[bot] google-oss-prow[bot] commented on Jul 23, 2026 00:18 UTC
    Bot

    [APPROVALNOTIFIER] This PR is APPROVED

    This pull-request has been approved by: <a href="https://github.com/kubeflow/sdk/pull/632#pullrequestreview-4759837644" title="Approved">andreyvelich</a>

    The full list of commands accepted by this bot can be found here.

    The pull request process is described here

    <details >
    Needs approval from an approver in each of these files:

  • ~~OWNERS~~ [andreyvelich]
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":[]} -->

    Reviewers
    Assignees
    reckless-sherixx reckless-sherixx
    Labels
    risk: low confidence: 87.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 32.3% (LOW) · Higher = larger blast radius
    32.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 87.4% · Lower = rushed or shallow review
    87.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.1% · Safe to merge
    4.1%
    Repository