Nomad Reschedule Stanza and Task Writes: Where Should Idempotency Live?
0 reputation · 10 Jun 2023, 07:57 UTC
0 reputation · 10 Jun 2023, 07:57 UTC
A Nomad job specification separates two re-execution paths. The restart stanza governs task-level exits inside an existing allocation, while the reschedule stanza governs replacement of an allocation after node loss or drain. Both control when a task process runs again; neither is documented to inject a scheduler-generated idempotency key into the work the task performs.
The design goal is a job, assumed to be Nomad 1.x, whose task writes to an external datastore and must not create duplicate records when the task is re-run. The constraint is that the write path has to supply whatever uniqueness the datastore enforces, because the scheduler only decides when the process starts.
What remains unclear is which Nomad-provided values stay stable across a restart versus a reschedule, and whether that stability is documented per version. Stanza fields and defaults have differed across Nomad 1.x releases, so the reference for the version in use matters.
restart and a reschedule, which Nomad-provided values remain stable enough to key a task's write?32275 reputation · 10 Jun 2023, 10:40 UTC
Assuming Nomad 1.x, the scheduler provides no documented duplicate-write mechanism and injects no idempotency key. The restart and reschedule stanzas only decide when a process starts; the task write path must supply whatever uniqueness the datastore enforces.
Across a restart, the task runs inside the same allocation, so NOMAD_ALLOC_ID, NOMAD_ALLOC_NAME, and NOMAD_SHORT_ALLOC_ID keep their values and can key writes deduplicated within one allocation. NOMAD_JOB_NAME, NOMAD_TASK_NAME, and NOMAD_NAMESPACE are also stable, but they are shared by every attempt.
Across a reschedule, Nomad creates a replacement allocation, so NOMAD_ALLOC_ID and NOMAD_ALLOC_NAME change. No documented per-attempt identifier survives a reschedule. NOMAD_ALLOC_INDEX is the only plausible cross-reschedule key, but whether it is reused or incremented is version-sensitive and should be treated as unverified until observed on the running server.
Because the scheduler only controls process timing, the write path must enforce uniqueness. Two patterns fit the confirmed behavior:
NOMAD_ALLOC_ID plus an attempt counter maintained by the task. The counter advances on restart; a reschedule receives a new NOMAD_ALLOC_ID.Does a reschedule mean the same logical write continues, or that a new execution begins? Continuation needs a job-scoped natural key; distinct execution needs alloc-ID-plus-attempt keying. Confirm this intent before choosing the key.
Because stanza defaults and environment stability vary across Nomad 1.x, verify against the deployed version before depending on any field:
nomad job inspect <job>Run a count=1 service, record NOMAD_ALLOC_ID and NOMAD_ALLOC_INDEX, drain the node to force a reschedule, then diff the new allocation environment. Trigger an in-allocation restart and confirm NOMAD_ALLOC_ID is unchanged.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.