narwhal-witness¶
narwhal-witness holds one lease for a router pair: the holder, the epoch that fences it, and the expiry. Run it on a third host, such as the load balancer host. Start both routers with --lease-url pointing at it, as Electing the primary through a witness describes.
export NARWHAL_ROUTER_TOKEN="$(cat <token-file>)"
narwhal-witness --state /var/lib/narwhal/witness.json --host <private-address> --port 8470
The witness decides every claim itself and compares no clock with another host's. It counts each lease's TTL on its own CLOCK_BOOTTIME from when it grants the request. The router stops serving --lease-safety-margin seconds before that TTL ends, counted from when it sent the request.
Routes¶
Every lease request and its answer are signed with the control token the routers hold, as Signed requests describes. A request without a valid signature gets HTTP 401. Bodies are JSON objects of at most 4 KiB.
| Route | Body | Answer |
|---|---|---|
POST /lease/acquire |
holder, ttl_s, after_epoch |
HTTP 200 when the lease is free or expired, or already holder's. A new holder gets the epoch max(epoch, after_epoch) + 1. HTTP 409 while another holder's lease lasts |
POST /lease/renew |
holder, epoch, ttl_s |
HTTP 200 when holder holds epoch unexpired with that ttl_s, and the lease lasts ttl_s from now. Otherwise HTTP 409, and the router fences |
POST /lease/release |
holder, epoch |
HTTP 200, and the lease is free. HTTP 409 when the lease is not that holder's epoch |
GET /lease |
The epoch, holder, ttl_s, remaining_s, negative once the lease has expired, and hold_s |
|
GET /health |
HTTP 200, unsigned | |
GET /metrics |
The witness metrics, unsigned |
holder holds at most 256 characters, and ttl_s is at most --max-ttl. A request outside these limits gets HTTP 400.
Signed requests¶
The token never crosses the network. Each request carries three headers:
X-Narwhal-Witness-Time: the sender's Unix time, with six decimal places.X-Narwhal-Witness-Nonce: 32 random hex digits.X-Narwhal-Witness-Signature:sha256=and the HMAC-SHA256, under the token, of six lines joined by newlines. The lines arenarwhal.witness-request, the method, the path, the SHA-256 of the body in hex, the time and the nonce.
The witness refuses a request with HTTP 401 in three cases:
- The signature does not match.
- The request's time is more than 30 seconds from the witness's own clock.
- The request repeats a nonce the witness has seen.
Every answer to a lease route, a refusal too, carries X-Narwhal-Witness-Signature over narwhal.witness-answer, the status, the SHA-256 of the body and the request's nonce. The router ignores an answer without that signature, so a forged or replayed grant cannot make it serve.
The witness reads a wall clock only for the 30-second window and its start time, which decide whether a request is heard. A router whose clock drifts past the window cannot renew, so it fences, and it cannot claim. Keep the hosts' clocks synchronised, such as with chrony or systemd-timesyncd.
The witness forgets the nonces it saw when it restarts. It therefore refuses with HTTP 401 an acquire, renew or release signed before it started. That signed refusal gives the witness's start as started_at, in Unix seconds. A router whose clock runs behind the witness's signs its requests with that time, rounded up to the microsecond, until its own clock passes it. It then renews at once instead of fencing.
A request captured before a restart can be replayed after it only when the signing router's clock runs ahead of the witness's by more than the time the witness was down. A replay can only repeat what a router asked for: it can delay a takeover by one TTL, but it cannot grant the lease to a router that did not ask for it.
State and restarts¶
The witness writes the lease to --state before it answers a grant to a new holder or a release, through a temporary file, fsync and a rename. It does not write renewals, which change neither the holder nor the epoch. When the write fails, the request gets HTTP 503 and nothing changes.
After a restart, a state file that names an unreleased holder keeps that lease for its TTL from the restart. Its holder may renew it at once with a request signed after the start. A primary therefore keeps serving through a witness restart that ends inside its deadline, --lease-ttl less --lease-safety-margin after its last renewal. A released lease is free at once.
When the state file is missing, the witness cannot tell whether it granted a lease before the file was lost. It grants none for --max-ttl seconds. GET /lease reports that hold as hold_s. The first grant after the hold writes a new state file. Its epoch follows the highest epoch the claiming router has seen, so epochs never go back while a router remembers one. A first start on a new host waits out the same hold.
The witness refuses to start in two cases. It exits with status 2 without a valid NARWHAL_ROUTER_TOKEN or with a state file it cannot read. It exits with status 1 when it cannot bind its address.
Security¶
Bind the witness to a private address that both router hosts reach, and allow only those hosts to reach its port. The witness speaks plain HTTP. Signatures keep a host on the network from forging, changing or replaying a lease request or answer. A host on the network can still read the holder and epoch, and can stop the pair from serving by dropping the witness's traffic. Whoever holds the token can take or release the lease. Keep the token in a mode 0600 file, as Starting a router pair describes.
Options¶
| Option | Default | Description |
|---|---|---|
--version |
Print the installed distribution version. | |
--state PATH |
required | The lease's state file on local disk. The witness creates its directory, mode 0700, and the file, mode 0600. |
--host HOST |
127.0.0.1 |
Address to bind. |
--port PORT |
8470 |
Port to listen on. |
--max-ttl SECONDS |
30 |
The longest --lease-ttl a router may ask for, and how long a witness that starts without its state file grants no lease. |
--log-level LEVEL |
info |
debug, info, warning or error. |