Page MenuHomePhabricator

Getting logs for a long-lived job timeouts
Closed, ResolvedPublic

Description

[urbanecm@deploy1003 ~]$ kubectl get jobs -l username=urbanecm -L script
NAME                       STATUS   COMPLETIONS   DURATION   AGE   SCRIPT
mw-script.eqiad.ae74h9jw   Failed   0/1           12d        12d   revalidateLinkRecommendations.php
[urbanecm@deploy1003 ~]$ kubectl logs job/mw-script.eqiad.ae74h9jw
error: timed out waiting for the condition
[urbanecm@deploy1003 ~]$

The job is 12 days ago, but it is a long running one, and I check its status about once a week. We discussed a bit with @RLazarus:

 I know I recently complained about that... but `kubectl logs job/mw-script.eqiad.ae74h9jw` timeouts... 
19:47 <rzl> urbanecm: that job is from 12 days ago, and the pods are gone now
19:48 <rzl> I wish there were a clearer error message than just a timeout, but that's the cause
19:50 <urbanecm> rzl: that job was running for the past 12 days, it is a very long running one
19:50 <rzl> aha
19:51 <urbanecm> rzl: i see `lastProbeTime: "2026-08-18T10:49:38Z"` in the yaml output, not sure what probing means exactly though
19:52 <rzl> last time kubernetes health-checked it
19:52 <rzl> the pod is still gone, so there's no way for kubectl logs to succeed -- you could see if there's something more persistent in logstash
19:54 <urbanecm> ...but why is it gone so quickly? i check the job fairly regularly (the last time was at most last week)
19:54 <rzl> that is a good question and I don't know :)
19:54 <urbanecm> fair enough :)
19:54 <urbanecm> thanks for looking
19:54 <rzl> sorry, I can dig into this this week but I can't right now -- if you open a task we'll have a look in serviceops

I check the status of the job about once a week (I definitely checked sometime during last week), and it feels like the logs disappeared fairly quickly. I'm curious what reasonable practices would be when monitoring long running scripts – because now I have a script I had to blindly rerun, because I have no clue about why it failed.

Filling this ticket per @RLazarus's request.

Event Timeline

MLechvien-WMF moved this task from Inbox to Needs Info / Blocked on the ServiceOps board.
MLechvien-WMF subscribed.

Assigning to Reuven to take a look or tag others as needed

The script didn't crash, and rerunning it was correct.


What happened, and how do we know?

We can get the time the job failed:

rzl@deploy1003:~$ kube-env mw-script eqiad
rzl@deploy1003:~$ kubectl get job mw-script.eqiad.ae74h9jw -o json | jq .status
{
  "conditions": [
    {
      "lastProbeTime": "2026-08-18T10:49:38Z",
      "lastTransitionTime": "2026-08-18T10:49:38Z",
      "message": "Job has reached the specified backoff limit",
      "reason": "BackoffLimitExceeded",
      "status": "True",
      "type": "FailureTarget"
    },
    {
      "lastProbeTime": "2026-08-18T10:49:49Z",
      "lastTransitionTime": "2026-08-18T10:49:49Z",
      "message": "Job has reached the specified backoff limit",
      "reason": "BackoffLimitExceeded",
      "status": "True",
      "type": "Failed"
    }
  ],
  "failed": 1,
  "ready": 0,
  "startTime": "2026-08-07T16:27:40Z",
  "terminating": 0,
  "uncountedTerminatedPods": {}
}

It went to FailureTarget ("our pod is being deleted, we're configured not to retry, time to clean up") at 2026-08-18 at 10:49:38 and to the terminal status Failed eleven seconds later. That's consistent with our ten-second termination grace period, meaning something killed the script rather than it exiting of its own accord. ("Mr. Ae74h9jw didn't just die... he was murdered!")

So, why was the pod deleted? To logstash's Kubernetes Events dashboard: https://logstash.wikimedia.org/goto/26a258325b646f5a036102964ad6fa7a

(Aside: Notice the timestamps in Logstash are like half an hour later; there was a delay and then some catchup, see the gap at https://logstash.wikimedia.org/goto/1abeb581efc15faa370b8b2cad487bcf, so that timestamp mismatch is an artifact.)

No immediate answers, although it lets us recover the pod name, mw-script.eqiad.ae74h9jw-zndcp. We just find out the pod was killed, its containers were stopped, and the job went to BackoffLimitExceeded as we already knew. We also find out the pod had been running on wikikube-worker1046.eqiad.wmnet.

But we take that hostname "wikikube-worker1046.eqiad.wmnet" over to the SAL, and find: https://sal.toolforge.org/log/rd59FKABffdvpiTrD-Ss The machine was drained for maintenance starting at 10:48. That's the root cause; our pod, like everything else on the node, would have been deleted so that the machine could shut down.

Finally, kubectl logs can't retrieve anything from a deleted pod, but it's persisted over in Logstash. We can take the pod name over to the App Logs logstash dashboard: https://logstash.wikimedia.org/goto/44837eeec727829ff863b73ce9f0ac6e (Timestamp filter set pretty arbitrarily for a two-hour window covering the end of the job.)


So, what should have happened here instead?

  • The node restart, and all work on that host being interrupted: I know it's frustrating to hear, but, this was pretty close to as intended. Node restarts are part of the normal course of things in Kubernetes (in this case it was part of a wave of worker node reboots to apply kernel security updates) and workloads don't generally care which machine they're running on, so it's normal to move them around. When this happens to serving workloads (like mw-web, or shellbox, or anything else that's expected to sit there forever and receive load-balanced requests) the pod is deleted from the outgoing host and a replacement is started up somewhere else. For maintenance jobs, creating a replacement would mean restarting the script -- and we don't know a priori whether that's safe to do for any given script, so we don't restart them automatically. (If we were writing MediaWiki from scratch for a modern Kubernetes environment, we'd have designed every maintenance script to be idempotent, but without that luxury, we set the safest common default.) The same goes for any number of other reasons a pod might need to be shuffled from one machine to another -- that's just part of how the whole cluster is managed.
    • From your comments in T434319, it sounds like revalidateLinkRecommendations.php in particular can be restarted safely (although it'll take longer to complete). That's good. I'd like for users to be able to opt into automatic retries for situations like this, so you don't have to restart the job yourself -- config-wise, that would mean setting a restartPolicy, either via a command-line flag to mwscript-k8s or, better, based on some lookup by maintenance script name. We don't have that yet.
    • The longer a job runs, the more likely something will disrupt it before it completes (and the more painful it is to restart it from scratch). Running for twelve days at a stretch, it becomes more and more likely something will happen, so more and more important to handle this kind of event gracefully. Breaking the job up into smaller chunks of work can help, as can keeping checkpoints in some persistent storage (i.e., outside the pod) to let the new instance pick up where the last one left off.
  • mwscript-k8s offers you the kubectl logs command that you can use while the pod exists, but doesn't point you to Logstash to read the script output there, which continues to work after the pod is deleted. We can look at adding a link to the "job started" output, if you think it would have helped you find the logs you were looking for.
  • On the original title of the task: Agreed, it would be nice if kubectl logs errored promptly instead of timing out. That happens when there are no pods matching job/mw-script.eqiad.ae74h9jw in your command. (Why doesn't it just say so and exit immediately? Because if there's about to be such a pod -- such as if you run kubectl logs right as you start your job -- then it can start streaming the logs when it appears, so it waits to see if that's about to happen. In a generic way, because job/ is just a pod selector and doesn't invoke any Job-specific logic, kubectl logs doesn't know whether that's the case or not. You can at least set it to --pod-running-timeout=1s to give up faster than the default twenty seconds, but of course that only makes sense if you know it's going to be a problem.)

From the above, I've spun off the TODOs for ServiceOps into T436204 and T436205. Unless I hear otherwise, I'll resolve this task as having answered the question, but let me know if we can add any more information here.