Launching TestFlows Machine: self-service deterministic simulation testing environment Read the post

Container faults

A project run as containers is a small cluster, and each service is a node. machine containers puts a fault on one container while the others keep running:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
machine containers app stop db                   # SIGTERM, then SIGKILL after 10s
machine containers app stop db --grace 2s # SIGKILL after 2s of machine time
machine containers app kill db # SIGKILL, no cleanup
machine containers app crash db # a power cut: its volumes lose what was not synced
machine containers app pause db --for 5s # frozen for 5s, then thawed
machine containers app throttle db --cpus 0.1 --for 30s
machine containers app throttle db --cpus 0 # the container's own limit again
machine containers app memory db 64m --for 30s # past 64MB, its out-of-memory killer
machine containers app memory db 0 # the container's own limit again
machine containers app memory db 48m --soft # past 48MB, reclaimed and slowed, not killed
machine containers app pids db 20 # past 20 processes, fork fails with EAGAIN
machine containers app files api 64 # a process past 64 open files: open fails with EMFILE
machine containers app ports api 40000-40009 # 10 local ports: connect fails with EADDRNOTAVAIL
machine containers app start db
You want to know Fault
Does the cluster keep serving while one node restarts cleanly? stop db --restart 10s
Does a client retry when its server dies mid-request? kill db --restart 5s
Does the database keep what it committed through a power cut? crash db --restart 0
Do the other services notice a node that hangs without dying? pause db --for 30s
Does the system cope with one slow node? throttle db --cpus 0.1 --for 1m
What happens when a node runs out of memory? memory db 64m
What happens when a node cannot start a process, open a file or connect? pids db 20, files api 64, ports api 40000-40009; see Resource exhaustion
Does the service keep state it never put in a volume? kill db --restart 0 --fresh

A container you stop, kill or crash stays down. --restart 5s brings it back after 5 seconds of machine time, and --restart 0 at once. The other containers keep running through the outage:

1
2
3
machine containers app kill db --restart 5s
machine containers app crash db --restart 0 # a power cycle
machine containers app kill db --restart 0 --fresh # back from its image
1
2
✔ Killed compose-db-1 (db)
Starts again in 5s of the machine's time, process 28493p1

A restart is a reboot: the container comes back with every file it had. --fresh replaces it instead, as a failed node is replaced by a new one from the same image. The files it wrote outside its volumes are gone, and its volumes are kept. Use it to find state a service keeps in its own files when it should keep it in a volume.

Right after a container starts, Machine does not know its name yet, so a command that names it by its service or its name is refused for that moment. Name it by the id that machine containers app shows, or let the machine run for a second first with machine run app --within 1s.

A limit holds when the container starts again, as a limit in its Compose file would: a container that --restart brings back has the same limit, set before the container’s program runs. Use throttle db --cpus 0 to set back the container’s own limit, its Compose file’s or none, or --for 30s to restore the previous limit after 30 seconds of machine time, on whichever start of the container runs then. A limit set on a stopped container applies when it starts. A container that Compose recreates, or start --fresh replaces, is a new container and starts without the limit.

memory limits the memory a container’s processes may use, as its cgroup’s memory.max. A limit below what they use makes the machine’s kernel reclaim memory, and its out-of-memory killer kill a process of the container when it cannot. A process killed that way exits 137, by SIGKILL:

1
2
3
4
machine containers app memory api 32m
machine exec app run --container api -- sh -c 'x=$(head -c 64000000 /dev/zero | tr "\0" a)'
echo $? # 137: killed by its out-of-memory killer
machine containers app memory api 0

The out-of-memory killer stops the process that uses the most memory. Here that is the shell the example started. If it were the container’s main program, such as a database server, the container would exit. When a service exits on its own and its restart policy does not start it again, the whole project ends.

memory takes --for, and its limit holds through restarts, as throttle‘s does. An out-of-memory kill is part of the run, so a replay kills the same process at the same point.

machine containers app lists each container’s limits in its LIMITS column, cpus 0.1 memory 64MB or - for none. A limit on a disk built before container limits is refused, with a hint to build the disk again.

Each fault is recorded with the run, so a replay faults the same container at the same point. Put one where the run is stopped, at a breakpoint, a catchpoint or a run --until, to fault a service at the moment that matters.

A crash

kill loses nothing the program wrote: the kernel inside the machine keeps running and writes it out later. crash is a power cut of the service’s node. Its volumes keep what was synced and lose the writes still in memory. When the service starts again, each volume replays its journal and the service runs its own recovery. A crash test finds the bugs that recovery exposes: a commit acknowledged before its data was synced, a rename whose directory was never synced, and a log record that survived while the data it describes did not.

A crash cuts the volumes the project names in x-machine; see Volumes a crash cuts. The cut is when the volumes stop writing to the disk, just after the container is frozen; a write the kernel finishes in between is kept, as one a disk finished before the power went. A volume another running container also mounts is refused, since cutting it would cut that container too.

1
machine containers app crash db /var/lib/postgresql/data --restart 0   # that volume alone
1
2
3
✔ Crashed compose-db-1 (db)
Cut compose_db-data
Started again

The volume is named as Docker names it, after the Compose project.

A crash test, start to end. Stop where the database syncs, keep the run there, and crash a branch of it. The branch loses what the database had not synced yet; the query tells you whether it had already told a client the data was safe:

1
2
3
4
5
6
7
machine catch app add syscall fsync fdatasync --in comm=postgres   # where it syncs
machine run app --iters 0 # stops at the next sync
machine fork app --name crash # the run keeps its place
machine catch app/crash delete 1 # the branch has its parent's
machine containers app/crash crash db --restart 0
machine run app/crash --within 5s # the database recovers
machine exec app/crash run --container db -- psql -U postgres -c 'select count(*) from t'

The run is deterministic, so the branch crashes at exactly that sync every time. To crash at another point, run on to the next sync and fork again.

From Python, run.containers.crash("db", restart="0") and the other verbs take the same options as keywords. The SDK’s Container faults has an example.