Skip to content

Background deletion

Stop deletes the TrainJob with a DELETE whose preconditions.uid is the UID from the object reference and whose propagationPolicy is Background. Background propagation means the TrainJob is removed at once, and Kubernetes' garbage collector deletes the things it owns (the JobSet, its Jobs, the pods and the runtime copy) afterwards.

It was not always so. The adapter's first design deleted in the foreground, so that the TrainJob stayed visible until everything beneath it was gone, which would have made "the TrainJob exists" mean "something of it may still run". On Trainer v2.3.0 that never completes, in two cases that were observed on a kind cluster.

What goes wrong in the foreground

The TrainJob never finishes terminating. The adapter gives each job a copy of its runtime, a TrainingRuntime with the TrainJob's own name, and the collector deletes that copy along with the other dependants. Trainer's validating webhook then refuses the update that lifts the TrainJob's foregroundDeletion finalizer, because the runtime the TrainJob names no longer exists. The TrainJob stays Terminating for ever. This is kubeflow/trainer#4203, open against v2.3.0 at the time of writing.

A suspended TrainJob being deleted gets its JobSet back. Trainer recreates the JobSet each time the collector deletes it: a new JobSet UID about every three seconds, for 30 seconds to over a minute.

Background deletion avoids both: the TrainJob is gone at once, so there is no finalizer to wedge and nothing left to recreate the JobSet.

What it costs

Because the TrainJob is gone before its pods, processes are tracked by pods, not by the TrainJob. A pod on a reachable node goes when its kubelet confirms termination; one on an unreachable node stays until the Node is deleted or tainted node.kubernetes.io/out-of-service.

While they terminate, Find reports the leftover JobSet, runtime copy and pods as orphans of a TrainJob that is gone. Each orphan is its own object, with an object reference whose ID is <namespace>/jobset/<name>, <namespace>/pod/<name> or <namespace>/trainingruntime/<name>, with that object's UID. Stop on such a reference deletes that one object, under its UID precondition. Nothing is re-admitted while the pods remain (never two executions).

From go/dispatch-kubeflow v0.2.1, whether an orphan is Foreign depends on whether anything will remove it:

  • Kubernetes is removing it, so it is the job's own, of its submission. That is an orphan with a deletionTimestamp; a JobSet or runtime copy that still has an owner reference, which the garbage collector follows once its owner is gone; every pod of such a JobSet; and a pod whose JobSet is already gone and whose Job is still owned or being deleted. dispatch waits for these to go before it settles the job, and its clean-up deadline still escalates one that never does, such as a pod on a dead node.
  • Nothing will remove it, so it is Foreign: a JobSet or runtime copy that is not being deleted and has no owner reference, and the pods of that JobSet that are not being deleted. Deleting a TrainJob with propagationPolicy: Orphan leaves exactly this, because it strips the owner reference from the JobSet and the runtime copy. Deleting the JobSet that way does the same to its Jobs, so from the first release after v0.2.1 a pod whose JobSet is gone is Foreign when its Job has no owner and is not being deleted. Telling that needs get on batch Jobs; without it, and in v0.2.1, the pod counts as one Kubernetes is removing. dispatch escalates a foreign object, and it blocks settlement until a person deals with it.

v0.2.0 and earlier report every orphan as Foreign, so a job stopped by one process and watched by another, such as a cancel beside a supervising submit, escalates its own terminating pods and never settles (go/dispatch-kubeflow#16). Upgrading stops new cases, but does not clear a record that already holds such an escalation: a foreign object once recorded stays until a person abandons it, as respond to escalations shows for foreign:<object id>.

An orphan is found the same way a stray of an earlier crash is: by its name prefix and labels. A runtime copy that Start created but never got to own, because the adapter died between the two steps, is found the same way and deleted by StopJob. Because orphans are routine, writing requires delete on JobSets and pods: an adapter without it opens observe-only rather than leave a StopJob it cannot finish.

Stop and StopJob

  • Stop deletes one object instance. A missing object is success. A UID that no longer matches, meaning the name now belongs to another instance, is success too: the instance the reference names is gone, and Stop must not touch its replacement.
  • StopJob deletes every object Find returns, orphans included, each under its own UID precondition. It then repeats Find until nothing of the job remains or timeouts.stopJob passes (ten minutes by default). It reports, per object:
Report Means
Stopped the object and every pod beneath it are gone
Alive something remains and at least one remaining pod is on a Ready node, or nothing remains but the object itself
Unproven something remains, and every remaining pod is on a Node that is NotReady, Unknown or gone: its kubelet cannot confirm the processes stopped

The bound ends the waiting, not the stopping. Once timeouts.stopJob has passed, StopJob asks only what no pass has yet asked to stop. If its read finds nothing unasked, it reports that read at once. Otherwise it takes one more pass to ask it, and reports the read after that, whatever it finds. New objects appearing, as a Job controller replacing deleted pods makes them, cannot keep a stop going.

From the first release after dispatch-kubeflow v0.2.1, an object that only the reported read finds is reported Alive, because nothing asked it to stop. That includes the pods of a TrainJob a pass did ask, when the TrainJob is gone by the reported read and its pods are not yet. The TrainJob is still reported, since its pods remain, and each pod is also named in Alive on its own. A pod's own entry is Alive even where its TrainJob's is Unproven, as on a node whose state is Unknown. It is the safe direction, since the caller does not take the job for stopped, only a noisier report.

A stop can overrun its bound by about five seconds from that release: up to one poll before the read that finds the bound passed, then that read and the reported one, each waiting up to two seconds for a deletion the Watch has not yet handled. v0.2.1 also pauses a poll before the reported read, so it can overrun by about six.

Unproven needs permission to read Nodes. Without it, nothing is proven and remaining pods count as Alive.

Cancel means delete: a stopped object leaves no trace in the cluster. dispatch's record remembers it, and AccountingHistory is no.