← Back to Pull requests

fix(raycluster): stop recreate loop on YuniKorn ResourceReservationTimeout #5305

Open
dundysm opened this pull request with 229 lines diff across 8 files
View on GitHub
dundysm dundysm commented on Sep 17, 2026 01:43 UTC
Author

Fixes

Fixes #5301

Summary

When YuniKorn hard-gang placeholderTimeoutInSeconds expires, Pods are failed with Reason: ResourceReservationTimeout and the YuniKorn application is removed. KubeRay previously treated those as ordinary terminal Pods, deleted them, and recreated replacements against the same dead applicationId, leaving RayJob running until activeDeadlineSeconds.

This change:

1. Treats Failed + ResourceReservationTimeout as a non-recreate terminal failure in shouldDeletePod.
2. Short-circuits reconcilePods when such Pods are present (or when Status.Reason is already set) so WorkersToDelete cannot remove them and trigger recreation.
3. Surfaces the failure on RayCluster (Status.State=failed, Status.Reason=ResourceReservationTimeout, and BatchSchedulingFailed condition when the status-conditions feature gate is enabled).
4. Transitions RayJob to Failed with Reason=ResourceReservationTimeout while waiting for the cluster to become ready.

A broader BatchScheduler plugin hook (issue option 3) is left for a follow-up / maintainer design choice; this implements the smallest correct stop-recreate + surface-failure path.

Test plan

  • Unit tests for shouldDeletePod with Failed + ResourceReservationTimeout

  • Fake-client reconcilePods test: RRT Pods are not deleted/recreated; Status.Reason short-circuit skips recreation after Pods are gone

  • Fake-client calculateStatus test: sets Failed state/reason and BatchSchedulingFailed condition

  • Utils helper unit tests for RRT detection
  • Diff: 229 lines (+228 / -1) across 8 files Reviewer: win5923 Review duration: 25561m
    All Vouch review confidence checks passed
    Residual risk 0.06 = Change Risk (0.46) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 229 lines diff across 8 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 5 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    86%
    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.325
    Real PR Conversation Timeline
    13 items
    win5923 win5923 commented on Sep 17, 2026 02:12 UTC
    Reviewer

    Could you manually verify this and share a screenshot or the relevant kubectl output to confirm the fix works as expected?

    dundysm dundysm commented on Sep 17, 2026 09:47 UTC
    Author

    thanks @win5923; completed a live kind + yunikorn verify of this branch (kuberay/operator:rrt-5305 / --batch-scheduler=yunikorn).

    setup: kind kuberay-rrt, helm yunikorn, operator from this PR. submitted an unsatisfiable hard-gang RayCluster (nvidia.com/gpu: 1 x2 workers, placeholderTimeoutInSeconds=30 gangSchedulingStyle=Hard on the pod templates).

    after timeout, pods stay failed with ResourceReservationTimeout (same uids 30s later; no recreate loop), and the cluster surfaces state=failed + BatchSchedulingFailed=True:

    $ kubectl get pods -n rrt-demo -o wide
    NAME                                     READY   STATUS                       RESTARTS   AGE
    rrt-hard-gang-gpu-workers-worker-7x8qq   0/1     ResourceReservationTimeout   0          71s
    rrt-hard-gang-gpu-workers-worker-drq7q   0/1     ResourceReservationTimeout   0          71s
    rrt-hard-gang-head-mwz7b                 0/1     ResourceReservationTimeout   0          71s
    
    $ kubectl get pods -n rrt-demo -o custom-columns=NAME:.metadata.name,PHASE:.status.phase,REASON:.status.reason,UID:.metadata.uid
    NAME                                     PHASE    REASON                         UID
    rrt-hard-gang-gpu-workers-worker-7x8qq   Failed   ResourceReservationTimeout     27070f50-fbe4-41c8-8c39-0ff42f7f0dfa
    rrt-hard-gang-gpu-workers-worker-drq7q   Failed   ResourceReservationTimeout     9b53a2c1-6f29-4ff9-8fc7-37981edeed59
    rrt-hard-gang-head-mwz7b                 Failed   ResourceReservationTimeout     b1845b36-82ad-40d6-afe8-72572b2202d2
    
    # +30s later: same three pods / same uids (no recreate)
    
    $ kubectl get raycluster rrt-hard-gang -n rrt-demo -o jsonpath='{.status.state} {.status.reason}{"\n"}'
    failed ResourceReservationTimeout
    
    # condition
    type=BatchSchedulingFailed status=True reason=ResourceReservationTimeout
    message=Batch scheduler failed the gang reservation with ResourceReservationTimeout; Pod recreation is stopped
    

    operator log on reconcile:

    reconcilePods: ResourceReservationTimeout Pods observed; skipping pod create/delete to avoid looping against a terminated batch-scheduler application
    reconcilePods: skipping pod create/delete because batch scheduler gang reservation failed terminally reason=ResourceReservationTimeout
    

    pinging @kevin85421 @andrewsykim @rueian @MortalHappiness for review when you have a chance.

    win5923 win5923 commented on Sep 19, 2026 02:43 UTC
    Contributor

    cc @fscnick @CheyuWu @HsiaoHungKai to take a look when you have time.

    fscnick fscnick commented on code on Sep 21, 2026 14:47 UTC
    Code Review
    ray-operator/controllers/ray/utils/util.go:1
    @@ -1244,3 +1244,21 @@ func IsGatewayEqual(existing, desired *gwv1.Gateway) bool {
     	}
     	return true
     }
    +
    +// IsResourceReservationTimeoutPod reports whether the Pod was failed by a batch
    +// scheduler with Reason ResourceReservationTimeout (YuniKorn hard-gang timeout).
    +func IsResourceReservationTimeoutPod(pod corev1.Pod) bool {
    +	return pod.Status.Phase == corev1.PodFailed && pod.Status.Reason == ResourceReservationTimeoutReason
    +}
    +
    +// HasResourceReservationTimeoutPods reports whether any Pod in the list failed
    +// with Reason ResourceReservationTimeout.
    +func HasResourceReservationTimeoutPods(pods corev1.PodList) bool {
    +	return slices.ContainsFunc(pods.Items, IsResourceReservationTimeoutPod)
    +}
    +
    +// IsRayClusterBatchSchedulingFailed reports whether the RayCluster status indicates
    +// a terminal batch-scheduler gang reservation failure.
    +func IsRayClusterBatchSchedulingFailed(status rayv1.RayClusterStatus) bool {

    The function name Failed is a bit broad and doesn't clearly convey what it's checking. Since it checks ResourceReservationTimeoutReason, would it make sense to rename it to something more descriptive?

    fscnick fscnick commented on code on Sep 21, 2026 14:53 UTC
    Code Review
    ray-operator/controllers/ray/raycluster_controller.go:1
    @@ -1509,6 +1527,19 @@ func shouldDeletePod(pod corev1.Pod, nodeType rayv1.RayNodeType) (bool, string)
     	// Based on the logic of the change of the status of the K8S pod, the following judgment is made.
     	// https://github.com/kubernetes/kubernetes/blob/3361895612dac57044d5dacc029d2ace1865479c/pkg/kubelet/kubelet_pods.go#L1556
     
    +	// ResourceReservationTimeout means the batch scheduler (e.g. YuniKorn hard-gang)
    +	// rejected the gang reservation terminally and removed the application. Deleting
    +	// and recreating these Pods would reattach them to the same dead applicationId
    +	// and loop forever. See https://github.com/ray-project/kuberay/issues/5301.
    +	if utils.IsResourceReservationTimeoutPod(pod) {
    +		reason := fmt.Sprintf(
    +			"The %s Pod %s failed with Reason %s. "+
    +				"This indicates a terminal batch-scheduler gang reservation failure; "+
    +				"KubeRay will not delete or recreate the Pod.",
    +			nodeType, pod.Name, pod.Status.Reason)
    +		return false, reason
    +	}

    Why do we need to check ResourceReservationTimeout again? This condition seems unreachable here since all three shouldDeletePod() calls are made inside reconcilePods(), which has an early return.

    dundysm dundysm commented on code on Sep 21, 2026 15:27 UTC
    Code Review
    ray-operator/controllers/ray/utils/util.go:1
    @@ -1244,3 +1244,21 @@ func IsGatewayEqual(existing, desired *gwv1.Gateway) bool {
     	}
     	return true
     }
    +
    +// IsResourceReservationTimeoutPod reports whether the Pod was failed by a batch
    +// scheduler with Reason ResourceReservationTimeout (YuniKorn hard-gang timeout).
    +func IsResourceReservationTimeoutPod(pod corev1.Pod) bool {
    +	return pod.Status.Phase == corev1.PodFailed && pod.Status.Reason == ResourceReservationTimeoutReason
    +}
    +
    +// HasResourceReservationTimeoutPods reports whether any Pod in the list failed
    +// with Reason ResourceReservationTimeout.
    +func HasResourceReservationTimeoutPods(pods corev1.PodList) bool {
    +	return slices.ContainsFunc(pods.Items, IsResourceReservationTimeoutPod)
    +}
    +
    +// IsRayClusterBatchSchedulingFailed reports whether the RayCluster status indicates
    +// a terminal batch-scheduler gang reservation failure.
    +func IsRayClusterBatchSchedulingFailed(status rayv1.RayClusterStatus) bool {

    good call. renamed to IsRayClusterResourceReservationTimeout so it matches the ResourceReservationTimeout reason it actually checks.

    dundysm dundysm commented on code on Sep 21, 2026 15:27 UTC
    Code Review
    ray-operator/controllers/ray/raycluster_controller.go:1
    @@ -1509,6 +1527,19 @@ func shouldDeletePod(pod corev1.Pod, nodeType rayv1.RayNodeType) (bool, string)
     	// Based on the logic of the change of the status of the K8S pod, the following judgment is made.
     	// https://github.com/kubernetes/kubernetes/blob/3361895612dac57044d5dacc029d2ace1865479c/pkg/kubelet/kubelet_pods.go#L1556
     
    +	// ResourceReservationTimeout means the batch scheduler (e.g. YuniKorn hard-gang)
    +	// rejected the gang reservation terminally and removed the application. Deleting
    +	// and recreating these Pods would reattach them to the same dead applicationId
    +	// and loop forever. See https://github.com/ray-project/kuberay/issues/5301.
    +	if utils.IsResourceReservationTimeoutPod(pod) {
    +		reason := fmt.Sprintf(
    +			"The %s Pod %s failed with Reason %s. "+
    +				"This indicates a terminal batch-scheduler gang reservation failure; "+
    +				"KubeRay will not delete or recreate the Pod.",
    +			nodeType, pod.Name, pod.Status.Reason)
    +		return false, reason
    +	}

    you are right; with the early returns in reconcilePods (status reason + HasResourceReservationTimeoutPods), none of the three shouldDeletePod call sites are reachable for RRT pods (including the multihost path, which is only called from reconcilePods). removed that branch and kept the protection on the early-return path; unit coverage stays on TestReconcilePods_ResourceReservationTimeout.

    dundysm dundysm commented on Sep 21, 2026 15:27 UTC
    Author

    @fscnick thanks for the review. addressed both points in the latest push:

    1. renamed IsRayClusterBatchSchedulingFailed → IsRayClusterResourceReservationTimeout
    2. removed the unreachable RRT check from shouldDeletePod (early returns in reconcilePods already cover it)

    unit tests for the RRT helpers / TestReconcilePods_ResourceReservationTimeout / TestShouldDeletePod are green locally.

    fscnick fscnick commented on Sep 23, 2026 16:37 UTC
    Contributor

    Just noticed that while this PR could fix the issue, it also introduces YuniKorn-specific logic into the general reconciliation flow.

    dundysm dundysm commented on Sep 25, 2026 17:44 UTC
    Author

    @fscnick agreed; the ResourceReservationTimeout reason string is yunikorn-specific.

    the operator has no scheduler callback. it only sees Failed pods with that reason, and recreate is decided inside reconcilePods. that is why the stop lives there. we already removed the duplicate check from shouldDeletePod after your earlier review.

    a BatchScheduler plugin hook (issue option 3) was left out of this pr on purpose so other schedulers do not grow inside the general path. happy to do that as a follow-up, or to tuck the reason check behind a small named helper in this pr if you want the boundary now. which do you prefer?

    cc @CheyuWu

    fscnick fscnick commented on Sep 27, 2026 03:37 UTC
    Contributor

    Discussed with the team offline, and they have some concerns about leaking YuniKorn-specific logic into the general reconciliation flow. Sorry for not noticing this earlier.

    In addition, there has been some discussion about whether this PR is actually necessary if the current behavior is considered acceptable. Or, whether there’s a reasonable workaround.

    dundysm dundysm commented on Sep 28, 2026 12:56 UTC
    Author

    @fscnick thanks for the offline sync and for calling this out.

    agreed: hard-coding ResourceReservationTimeout into general reconcilePods / calculateStatus leaks yunikorn-specific logic across the batchscheduler boundary. that was the wrong layer for this fix, even though it stops the recreate loop.

    on necessity / workaround:

  • for rayjob, spec.preRunningDeadlineSeconds (and activeDeadlineSeconds) already fail jobs stuck before running, which matches what you asked on #5301. that looks like a reasonable operator-native workaround for the job path without this pr.

  • gap: bare raycluster (no rayjob) still has no analogous deadline, so pods can keep recreating against the dead applicationId until someone deletes the cluster. soft gang style is also not a substitute for hard-gang semantics.
  • proposal:
    1. i can close #5305 (or convert to draft) so we do not merge yunikorn reason checks into general reconcile.
    2. please choose one:
    (a) close #5301 as workaround-accepted and we document preRunningDeadlineSeconds for yunikorn hard-gang timeouts on rayjob; or
    (b) greenlight a thin follow-up that implements issue option 3 only: a generic BatchScheduler terminal-failure hook (or factory watch) with the rrt string living only under batchscheduler/yunikorn/, and general reconcile calling the interface without knowing yunikorn.

    i will not push the current design further. happy to take (b) if you want the automatic stop-recreate + status signal without the layering leak; otherwise (a) seems aligned with the team's "is this necessary?" discussion.

    cc @win5923 @CheyuWu @kevin85421

    Reviewers
    Assignees
    dundysm dundysm
    Labels
    risk: low confidence: 86.9% 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: 45.8% (MEDIUM) · Higher = larger blast radius
    45.8%
    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: 86.9% · Lower = rushed or shallow review
    86.9%
    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: 6.0% · Safe to merge
    6.0%