[Bug] 409 conflicts when updating status
Search before asking
- [X] I searched the issues and found no similar issues.
KubeRay Component
ray-operator
What happened + What you expected to happen
KubeRay operator logs sometimes show that 409 conflicts are sometimes raised when reconciling status, for example
2022-11-17T22:14:44.198Z ERROR controllers.RayCluster Update status error {"cluster name": "prod", "error": "Operation cannot be fulfilled on rayclusters.ray.io \"prod\": the object has been modified; please apply your changes to the latest version and try again"}
github.com/ray-project/kuberay/ray-operator/controllers/ray.(*RayClusterReconciler).Reconcile
/workspace/controllers/ray/raycluster_controller.go:97
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Reconcile
/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:114
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler
/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:311
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem
/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:266
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2
/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/internal/controller/controller.go:227
This happens a lot when using autoscaling, because the autoscaler writes to the RayCluster CR spec. While this is basically harmless (the operator will simply retry reconciliation), we should consider adding retry logic to the status update, at least to reduce log spam.
See https://ray-distributed.slack.com/archives/C02GFQ82JPM/p1668724199950299?thread_ts=1668711191.153149&cid=C02GFQ82JPM
Blog post for context https://alenkacz.medium.com/kubernetes-operators-best-practices-understanding-conflict-errors-d05353dff421
Reproduction script
Run an autoscaling Ray cluster, do some things to trigger autoscaling.
Anything else
No response
Are you willing to submit a PR?
- [ ] Yes I am willing to submit a PR!
I hit the same issue and wonder if there's anything I need to do, e.g. things to help/accelerate updating the status of the cluster. Is this Environment variable RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV is not set, using default value of 300 seconds {"cluster name": "ray-autoscaler"} related?
Hi @win5923, is this issue still wanted? I checked it on master (5fd88bb).
The RayCluster, RayJob, RayService and RayCronJob controllers all write status with r.Status().Update, and nothing handles a conflict. When the write is based on a stale object, Reconcile returns the 409 together with RequeueAfter. controller-runtime v0.25.0 then ignores RequeueAfter, logs Reconciler error at ERROR level, counts it in controller_runtime_reconcile_errors_total, and requeues with backoff. I reproduced this with a unit test on a fake client, and the 2025 log in #4078 shows the same error.
The next reconcile reads the latest object and succeeds, so no state is lost. But a normal conflict shows up as an error in logs and metrics.
I'd like to fix it. My plan: when the status update returns a conflict (apierrors.IsConflict), log it at info level and requeue without returning the error. I'd apply this to all four controllers, with unit tests that use a fake client returning a 409.
Does that sound right, or would you prefer a different approach?
@rueian, could you take a look when you have time? I have the fix ready for all four controllers, with unit tests. If it's easier to review as code, I can open the PR now.
I opened #5521 with the fix.
I reproduced this on a kind cluster with operator v1.7.2 (the status-update code is identical to master) to see where the the object has been modified errors come from. One scenario per run:
| Scenario | External writer | RayCluster status 409s |
|---|---|---|
| Create / delete / recreate RayCluster, delete worker pods | none | 1 per creation, 0 otherwise |
kubectl label / annotate / patch replicas, 24 writes in 60s |
kubectl | 0 caused by the writes |
| RayJob suspend / resume x2 | RayJob controller | 0 (suspend deletes the cluster, resume creates a new one) |
RayService workerGroupSpecs[0].replicas 1↔2 x2 |
RayService controller | 0 (replicas are excluded from the spec hash, so the RayCluster is never written) |
| Autoscaler scale 0→3→0 x2 | autoscaler | 0 caused by the 4 autoscaler patches; 2 during the scaling bursts |
Finding: every 409 I saw was the operator conflicting with its own previous status write. A reconcile writes status successfully, a
pod event triggers the next reconcile a few ms later, that reconcile still reads the old object from the informer cache, and its
Status().Update gets a 409. The reconcile after it (1–6 ms later) writes the same status successfully. External writes never
conflicted, because each one triggers a reconcile that already reads the new version. The autoscaler only adds pod events, which makes
the self-race more frequent. RayJob and RayService status show the same pattern.
So the conflict itself is harmless; the actual problem is that it surfaces as an error-level Reconciler error with a stack trace.
One run also shows why the optimistic lock should stay: a RayJob reconcile working from a stale cache generated a different
rayClusterName (rj-c-bvdvv vs the persisted rj-c-4sp48), and the 409 is what stopped it from being written. That rules out
switching to a plain merge patch without resourceVersion.
Options I considered:
- On conflict, re-read from the API server and compare. If the latest status already equals what this reconcile computed, treat it
as done; otherwise requeue. This handles the self-race inside the current reconcile, but it needs an
APIReaderand an extra GET. When the spec or metadata has changed, it still has to fall back to requeue, because the computed status is based on the old spec. - Expectations: remember the resourceVersion we last wrote from, and if the cache still returns it, requeue before doing any work. This avoids the 409 completely, but it adds in-memory state that has to be handled on deletion and on operator restart.
- Treat a conflict as a normal requeue rather than an error (what #5521 does). The lock stays, stale writes are still rejected, and the next reconcile, which is always triggered by our own write or by the spec change, recomputes the status from scratch.
Option 1 is the most I would add, but it only saves one reconcile in the self-race case and falls back to the next reconcile everywhere else. Given that, option 3 seems sufficient: let the next reconcile handle it, and log the conflict at info / V(1) level (e.g. "status update conflict, will be retried in the next reconcile") so it stays visible without being reported as an error.