Fault injection
Machine makes things fail inside the machine, at the point you choose or by chance, and records each fault with the run, so a replay fails exactly the same way. There are four kinds of fault, each with its own command:
| Command | What it makes fail | Section |
|---|---|---|
machine inject |
a program’s system calls and its functions, the kernel’s disk requests and memory allocations, and the disk’s data | System call faults, Function faults, Kernel faults, Disk faults |
machine containers |
one service’s container: stopped, killed, crashed, paused, throttled, or limited in memory, processes, open files or local ports | Container faults, Resource exhaustion |
machine clock |
the date the machine or one container reads: stepped, drifting, jittering, or a leap second | Clock faults |
machine net |
the links between containers: slow, lossy, out of order, rate limited, or cut | Network faults |
Each machine inject fault goes into a subsystem: a program’s system calls or
functions, a place in the kernel where work can fail, such as a disk request or
a memory allocation, or the data on the disk itself. You
find out what your program does when openat says EACCES, write says
ENOSPC, the disk returns an I/O error, the kernel runs out of memory, or the
disk quietly returns the wrong bytes, at the call you choose or by chance.
| Subsystem | What fails |
|---|---|
syscall |
a system call returns an error you choose, or waits first |
function |
a function of a program, such as malloc, returns a value you choose without running |
disk |
a disk read or write returns an I/O error |
disk-timeout |
a disk request never completes, and its task waits forever |
slab, pages |
a kernel memory allocation fails, as ENOMEM in most calls |
usercopy |
the kernel fails to copy to or from the program’s memory: EFAULT |
futex-access |
a futex call fails to reach the program’s memory: EFAULT |
disk-corrupt |
a disk read or write succeeds with bytes changed |
disk-torn |
a write succeeds, but only its first sectors reach the disk |
disk-lost |
a write succeeds, and none of it reaches the disk |
Every subsystem takes the same options: --in for whose calls, --prob for the
chance, --after to let calls through first, --times to stop after a number,
--seed, and --on and --off to make the fault cycle.
The disk subsystems also take --file, --op and --sectors to say which
requests. The machine’s kernel decides each call itself, so the run does not
stop for it, and a replay fails exactly the same calls.
System call faults
A syscall fault names a system call and the errors it may return. The program’s calls to that system call then return one of those errors without running.
1 | machine inject app add syscall connect ECONNREFUSED --in /usr/bin/app |
The last command lists the faults, with how many calls each one matched and fired on. For a program that connects once, it shows that one call failed. The seed is drawn, so yours differs:
1 | # INJECT ERRORS DELAY IN FIRES PROB SEED MATCHED FIRED |
The machine’s kernel decides at each call, and the run records every fault you add, so a replay fails the same calls with the same errors. A checkpoint keeps the faults and their counts, and a branch has its parent’s.
Which calls fail
--in says whose calls fail, and you always give it:
A program, by its absolute path:
--in /usr/bin/app. The fault applies to every task running that program, and to the processes it forks without starting another program, such as a server’s workers. It starts with the program, so you can add it before the program runs. Use the pathmachine tasks appshows as EXE: a symlink counts as the file it points to, so BusyBox’s/bin/catis/bin/busybox. A program that loads shared libraries opens and reads them after it starts, so add a fault onopenat,readorstatonce it is up, or it fails the loading instead.A task’s name:
--in comm=cat. The kernel names a task after the file the command named, up to 15 bytes. That picks out one tool of a multi-call binary such as BusyBox, and a script, whose program is its interpreter but whose name is its own. The interpreter’s first open is of the script itself, so--after 1lets it read the script and fails what the script opens:1
2machine inject app add syscall open EACCES --in comm=s.sh --after 1
machine exec app run -- /tmp/s.shThe script runs in the machine’s own BusyBox shell, which calls
open. A glibc shell callsopenatand opens its shared libraries first: count them in--after, or add the fault once the script runs.One process:
--in tgid=412, its pid. Add it once the program is running. Before then, that process is still being set up to run it.One thread:
--in tid=41.Every task in the machine, the system’s own included:
--in all.
To make sure something runs before you act on it, ask first. machine tasks app exist takes the same words, lists the tasks that match, and fails when none
does. It only reads, so it changes nothing:
1 | machine tasks app exist /usr/bin/app && machine inject app add syscall openat ENOENT --in /usr/bin/app |
A rule’s --after and --times count calls across every task it matches: for
a program, the 100th write is the 100th of all its processes together.
1 | machine inject app add syscall write ENOSPC --in /usr/bin/app --after 99 --times 1 # the 100th write |
--after lets that many matching calls through first. --prob is the chance a
call fails, from 0 to 1. --times is the most calls that fail; without it there
is no end.
The chance belongs to each call, not to a stretch of time. A fault stands from
when you add it until you delete it, and every matching call draws on its own:
1,000 calls at --prob 0.1 fail about 100 of them, however long they take. To
fault a stretch, such as the next 10 seconds of machine time, add the fault, run
for that long, and delete it:
1 | machine inject app add syscall read EIO --in /usr/bin/app --prob 0.1 |
To fail one particular call, run until the program makes it, then add the fault for one call. The stop is at the start of the call, so that same call fails:
1 | machine focus app add tgid=412 # a syscall predicate needs its task focused |
Name several system calls at once and each gets a fault of its own:
1 | machine inject app add syscall open,openat,creat EACCES --in /usr/bin/app |
Which of these a program calls depends on its C library. A glibc program’s
open() calls openat, while a musl program, such as BusyBox on Alpine,
calls open.
A fault whose MATCHED count stays at 0 while its program runs names a call the
program never makes. Name both calls, open,openat, so the fault applies with
either library.
Which errors
Any Linux error goes, by name or by number, whether or not the system call’s
manual lists it. Give a set and each failing call returns one of them. The
examples here fail openat of /usr/bin/app, so add them once it runs, after
machine tasks app exist /usr/bin/app:
1 | machine inject app add syscall openat ENOENT,EACCES,EMFILE --in /usr/bin/app # one of three, each call |
valid is the errors the system call’s manual page lists. all is every error
Linux names, which is more than the 30 a fault holds, so use --pick with it.
An error the manual does not list is one Linux would not return from that
call. getpid failing with ENOSPC never happens on a real system. Machine
allows it on purpose: it tests whether the program handles an error it did not
expect. Use valid for failures a real system produces, and an error by name
or all to test the program’s handling of any error.
Picking errors at random
--pick takes a number of errors from the set when you add the fault, and the
fault keeps those. This is swarm testing: each run fails with its own few errors.
1 | machine inject app add syscall openat,read,write valid --pick 1 --in /usr/bin/app # one error each |
Every fault has a seed, and the list shows it. The seed decides the picks,
which calls fail by chance and which error each one returns. Give --seed to
draw as another run did:
1 | machine inject app add syscall openat valid --pick 2 --seed 7 --in /usr/bin/app |
Slow calls
A fault can hold a call instead of failing it. --delay takes a time, or a
span to draw one from for each call, up to a second:
1 | machine inject app add syscall read --delay 0:20ms --in /usr/bin/app # each read waits 0 to 20ms, then runs |
The wait is on the machine’s own clock, and each call draws its time from the
fault’s seed, so a replay holds the same calls for the same time. --prob,
--after, --times and --in work as they do for errors.
Function faults
function makes a function of a program return a value without running. A
malloc that returns 0 is an allocation that failed, which no system call
fault reaches: the C library serves most allocations from memory it already
has, without asking the kernel.
1 | machine inject app add function malloc --return 0 --prob 0.01 --in /usr/bin/app |
The function is found by its name in the files a running task maps, its C
library or the program itself, and the fault acts on every task that maps
the same file and that --in names. With --in naming a program that runs
now, the function is found in that program. With one that does not run yet,
name with --at a task that maps the same library, such as a shell of the
same container.
For example, --in comm=sort with --return 0 makes BusyBox’s sort exit 2
on its first allocation, while seq and tail in the same container run as
before. With --prob 0.05, four runs of sort match 60 calls and fail 4. The
seed decides which calls fail, so a replay fails the same calls.
Three details affect what a function fault does:
- errno is not set. The C library keeps errno in each thread’s own memory, so a caller that checks the return sees the failure, and one that reads errno reads what was there before.
- The function’s first instruction. The kernel puts a breakpoint there, in
the tasks
--innames alone, and when the fault fires, the call returns to the caller before the function sets up its frame. Each call traps into the kernel, which costs time in a function called often. - The task
--atnames. It must be a task inside the container, not thedocker execthat started it:machine tasks appshows each task’s program as the container sees it, such as/bin/sh.
Memory faults
Memory goes wrong in four places: a program’s allocator returns nothing, the kernel refuses memory, a service runs out of it, or the data in it is wrong. Each has its own fault:
| You want to know | Fault |
|---|---|
Does the program handle malloc returning NULL? (errno is not set) |
inject add function malloc --return 0 --after 5 --times 1 --in /usr/bin/app |
| Does it survive one allocation in a hundred failing? | inject add function malloc --return 0 --prob 0.01 --in /usr/bin/app |
| Does it handle the kernel refusing to grow its heap? | inject add syscall brk ENOMEM --in /usr/bin/app |
| Does a system call fail cleanly when the kernel cannot allocate? | inject add slab --after 50 --times 1 --in /usr/bin/app |
| What happens when a service runs out of memory? | containers memory db 64m |
| How does a service behave under memory pressure, short of a kill? | containers memory db 48m --soft |
| Does the program notice a flipped bit in its data? | mem 412 'checksum^=0x1' |
| What does a pointer set to 0 do? | mem 412 'head=0' |
| What does a buffer of garbage do? | mem 412 buf --fill 0xde --length 4096 |
A program’s allocations
A program asks its C library for memory, and the library serves most
requests from memory it already has, without a system call. A
function fault on malloc reaches every allocation:
1 | machine inject app add function malloc --return 0 --after 5 --times 1 --in comm=sort --at 628 |
1 | sort: out of memory |
sort failed on its sixth allocation, and the next run, with the fault used
up, sorted as before. calloc, realloc and strdup are functions of their
own: name each that the program calls.
The kernel’s answer
brk grows the heap and mmap maps new memory. A fault on them is the
kernel refusing, which the C library sees first: it turns a refused brk
into an mmap, and a refused mmap into malloc returning NULL.
1 | machine inject app add syscall brk ENOMEM --in /usr/bin/app |
A refused brk returns the break unchanged, as the kernel’s own refusal does,
and the C library moves to mmap: with every brk of sort failing, it
sorted 100,000 lines and exited 0. That is the test of a program that
assumes its heap is contiguous.
A program’s first mmap calls are its dynamic loader’s, mapping its
libraries, and a fault on those stops the program before it runs. In a
service deb built from debian:bookworm-slim, sort makes 8 mmap calls in all, and with
--after 5 it failed with error while loading shared libraries: libc.so.6: failed to map segment from shared object:
1 | machine inject app add syscall mmap ENOMEM --in comm=sort --after 5 |
The count of the loader’s calls depends on the program and its libraries, so
add the fault with --prob 0 first and read MATCHED to count them. A program
with a small heap makes no mmap calls of its own, so for that program
function malloc is the fault that reaches its allocations.
The kernel’s own allocations
slab and pages fail allocations inside the kernel, for work a system call
or a page fault does for the program, as Kernel faults
describes. The program sees what the kernel does with the failure: a system
call that returns ENOMEM, or a page fault that cannot be served. In one
test, failing the 51st slab allocation of sort killed it with SIGSEGV.
A service out of memory
machine containers memory limits a container’s
memory. Past the limit the machine’s kernel reclaims memory, and its
out-of-memory killer kills a process of the container when it cannot:
1 | machine containers app memory api 32m |
1 | ⚠ Exit status 137: killed by SIGKILL, as Docker reports a signal |
--soft sets a limit past which the kernel reclaims memory and slows the
container’s processes without killing any of them, as memory pressure does.
The machine has no swap, so what the program sees depends on what the kernel
can reclaim:
- Files the program reads are cached in memory the kernel can drop and read again. Past a soft limit the program keeps running, slower. For example, reading a 100MB file takes 0.20 seconds of machine time against a soft limit of 16MB and 0.02 seconds without. Its cache falls from 103MB to 8MB, the cgroup counts 202 times over the limit, and nothing is killed.
- Memory the program allocates has nowhere to go without swap, so past a soft limit its processes slow to a crawl. For example, a shell holding 40MB against a soft limit of 16MB makes no progress in 2 seconds of machine time. The cgroup counts 27,935 times over the limit and kills nothing, and the shell goes on once the limit is lifted:
1 | machine containers app memory db 16m --soft |
machine containers app shows each container’s limits in its LIMITS column,
such as memory 64MB soft 48MB.
Wrong data
machine mem writes a program’s memory while the machine is
stopped. ^= flips bits of a value, as it does in C, and --fill writes a
pattern over a span of memory. Stop the program where the data is live first, at a
breakpoint or a run --until. The names below stand for the program’s own
variables and function:
1 | machine break app add 412 process_record |
The answer names the bits, the address and the value before and after. In the
next example the target is optind, a variable of the C library that a
container’s shell maps. The shell has its own optind too, so the write names
the library’s variable by its file, as machine symbols lists it:
1 | machine mem app 628 '/lib/libc.so.6:optind^=0x1' |
1 | ✔ Flipped 0x1 at 0x7fe1a2385248 <optind> of tid 628: 0x00000001 became 0x00000000 |
Each write is recorded with the run, so a replay corrupts the same value at the same point, and a branch forked at a point before the write runs the program unharmed.
Resource exhaustion
A program runs out of a resource in two ways, and Machine tests both:
- The resource runs out. A Linux limit on one container makes its processes meet the limit, as they would on a full production host. Use it to see how a service behaves as a whole when it hits the limit. Memory, processes and CPU are shared by the container’s processes. The open-file limit is each process’s own count.
- One call fails. A system call fault makes one program get the error the resource gives, at the call chosen, while everything else runs on. Use it to test one error path, exactly and repeatably.
| Resource | Run it out | Fail one call | The error |
|---|---|---|---|
| CPU | containers throttle db --cpus 0.1 |
none: everything slows | |
| Memory | containers memory db 64m |
inject add function malloc --return 0 |
SIGKILL, or malloc returns NULL |
| Processes | containers pids db 50 |
inject add syscall fork,vfork,clone EAGAIN |
EAGAIN |
| Open files | containers files api 64 |
inject add syscall openat EMFILE |
EMFILE |
| Ephemeral ports | containers ports api 40000-40009 |
inject add syscall connect EADDRNOTAVAIL |
EADDRNOTAVAIL |
| Disk space, inodes | fill a volume of its own | inject add syscall write ENOSPC |
ENOSPC |
A fault that fails one call also takes --in, which names the program whose
call fails, as One failed call shows. Memory is covered in
Memory faults, and the rest of this section covers the other
resources.
| You want to know | Command | Example |
|---|---|---|
| Does a service survive when it cannot start a process? | containers pids db 10 |
Processes |
| Does a program cope when it runs out of file descriptors? | containers files api 32 |
Open files |
| Does a client cope when it runs out of local ports, or churns through them? | containers ports api 40000-40001 |
Ephemeral ports |
| Does the system cope with one slow service? | containers throttle db --cpus 0.1 |
CPU |
| Does a service that crashes on a limit recover once the limit is lifted? | containers memory hog 64m, then memory hog 0 |
A service that crashes on its limit |
| Does a service survive its disk filling up? | a volume of 64MB, filled with dd |
A full disk |
| Does a program report a failed fork, a full disk or no inodes? | inject add syscall fork,vfork,clone EAGAIN, write ENOSPC, mkdir ENOSPC |
One failed call |
Limits on a container
machine containers sets each limit on one container. The limit holds across
restarts until it is lifted, so a service that crashes on the limit and
restarts meets it again. Each start applies the limit before the container’s
program runs its first instruction, and a limit set on a stopped container
applies when the container starts. A container that Compose recreates is a
new container and starts without the limit. Use --for 30s to set the previous limit back after 30 seconds
of machine time, on whichever start of the container runs then. A disk built
before the machine’s kernel kept container limits is refused, with a hint to build the
disk again.
A limit of 0 sets back the container’s own: what its Compose file gives it,
such as mem_limit or pids_limit, or no limit. A container with
mem_limit: 512m and memory api 128m goes back to 512MB on memory api 0,
running or stopped.
After pids db 10, machine containers app shows the limit in the LIMITS
column:
1 | machine containers app pids db 10 |
1 | SERVICE CONTAINER ID STATE IMAGE ADDRESS LINK LIMITS |
The other limits are set the same way, and a limit of 0 sets each one back:
1 | machine containers app pids db 20 # 20 processes and threads |
Each limit is set by a command the run records, so a replay sets it at the same point, the restarts included.
A service that crashes on its limit
A service that dies of a limit and restarts by its Compose file’s
restart: policy meets the limit again, and the run goes on. Here hog
grows to 100MB five seconds after it starts, under restart: always:
1 | services: |
1 | machine containers app memory hog 64m |
The out-of-memory killer takes it each time it grows, 3 times in 40 seconds of machine time, and the console reports each exit:
1 | === hog exited by itself: code 137. Its restart policy starts it again === |
Lifting the limit with memory hog 0 while hog restarts lets it grow, and
it prints grew. A
service with no restart: policy that exits by itself ends the run, with
its code.
Processes
pids limits the processes and threads of a container. Past the limit, a
fork or a new thread fails with EAGAIN, and the processes already running
go on. Here a shell in db starts background jobs under a limit of 10:
1 | machine containers app pids db 10 |
1 | sh: can't fork: Resource temporarily unavailable |
After pids db 0, running that command again starts all 20.
Open files
files limits the files each process of a container may hold open.
Sockets and pipes count as files. Past the limit, an open, a socket or
an accept fails with EMFILE, and files already open stay open.
The limit is set on the processes running now, and the processes they start
take it. A process that docker exec starts, machine exec --container
included, takes the container’s own limit. So set the limit while the
program runs. Here a shell in api was already running when the limit was
set:
1 | machine exec app start --container api -- sh -c \ |
The shell prints 32. Then tail opens the files f1 to f100, in the
order f1, f10 to f19, f100, f2. It holds 29 of them and its
standard input, output and error, 32 files, and every file after that fails:
1 | tail: can't open 'f35': Too many open files |
A later machine exec --container api -- sh -c 'ulimit -n' prints 1024,
because a process docker exec starts does not take the limit set on the
processes already running. It takes the container’s own limit. files api 0
sets the running processes back to the container’s own limit, both its soft
and its hard number.
Ephemeral ports
ports narrows the range of local ports a container’s connections take
from. A connection is told apart by its local and remote address and port,
so connections to different servers can share a local port. The range is
not a count of connections: it runs out for one destination at a time. Here
db runs a server on port 9000, and api connects to
db’s address from the listing above, 172.18.0.4, with 2 ports:
1 | machine exec app start --container db -- tcpsvd 0 9000 cat |
1 | telnet: can't connect to remote host (172.18.0.4): Cannot assign requested address |
Two connections take both ports, and the third fails with EADDRNOTAVAIL. Four things follow from how the kernel gives out ports:
- A closed connection keeps its port for 60 seconds. Its side stays in
TIME_WAIT. Short connections in a loop therefore run out of a small range
even though no more than one is open at a time. Right after two
connections close,
netstat -tanshows both ports in TIME_WAIT, and 65 seconds later neither. - Name lookups need a port too. A DNS query takes a UDP port from the
same range. With both ports held, a client connecting to
dbby name failed withtelnet: bad address 'db', before it tried to connect. - A program that binds port 0 before it connects gets EADDRINUSE.
BusyBox
ncdoes this:nc: bind: Address already in use. - A program that binds port 0 uses an even count of the range. Of a
range of 3 ports,
ncused 2.
The range belongs to the container’s network. The command refuses a container on the machine’s network, whose range is every process’s. It also refuses a container on another container’s network, with a hint to set the range on the container whose network it is:
1 | ✗ Not set: compose-sidecar-1 is on the network of compose-db-1. |
A full disk
A container’s own files are on the machine’s disk, shared by every container, so filling them fills the machine. A volume that is a filesystem of its own fills alone: only the services that mount it meet the full disk. A Compose file makes one in two ways:
| Volume | Full as a disk is | After a restart | Costs |
|---|---|---|---|
A volume named in x-machine, with its size |
yes: an ext4 filesystem on a block device, with a journal, metadata and fsync |
its files are kept | room on the disk, up to its size |
A tmpfs volume, driver_opts with size= |
no: in memory, where fsync does nothing and no write is delayed |
its files are gone | the machine’s memory, up to its size |
Use the x-machine volume to see what a service does on a full disk. A
tmpfs volume answers whether it handles ENOSPC on a write, with no change to
how the disk is built. Here db has a volume of 64MB at /data:
1 | services: |
Filling it with one file runs it out of space, and many empty files run it out of inodes:
1 | machine exec app run --container db -- dd if=/dev/zero of=/data/fill bs=1M |
The fill stops at 54.7MB of the filesystem’s 57MB, and the write after it stores one block of 10 and fails. The files stop at the 16,373rd, with blocks still free:
1 | sh: can't create f16372: No space left on device |
Meanwhile db writes its own files outside /data, api writes its files,
and the machine’s disk is untouched. Deleting the files ends the full disk.
A tmpfs volume of 40MB stops a dd at 40MB with No space left on device,
and is empty after its container restarts.
Filling the machine’s own disk instead makes every container meet it, and
the container runtime with them: machine exec --container then fails with
OCI runtime exec failed. machine exec without --container still runs.
CPU
throttle gives a container a share of a CPU. Nothing fails: every process
of the container runs slower. For example, a loop in db that counts to
200,000 takes 4.58 seconds of machine time, and 46.80 seconds with
--cpus 0.1:
1 | machine containers app throttle db --cpus 0.1 |
One failed call
A system call fault gives one program the error a
resource gives, at the call chosen. Nothing runs out, and every other
program runs on. Each fault below was added, then the program after it was
run, then the fault was deleted with machine inject app clear. The
programs are BusyBox’s, in the containers of the
ports example, and tail reads the files of the
open-files example. worker.sh starts 3 background jobs and
waits for them, and machine put copies it into db:
1 |
|
1 | machine put app ./worker.sh /tmp/worker.sh --container db --mode 755 |
Each program reported its error:
1 | /tmp/worker.sh: line 0: can't fork: Resource temporarily unavailable |
The C library’s fork() makes a different system call in each library: glibc
calls clone, and musl, the C library of Alpine images, calls fork or
vfork. Name all three and the fault takes either. A script run by its own
path, /tmp/worker.sh, has its file name as its comm, so the fault takes
that script and no other shell. Running out of inodes gives ENOSPC from the
call that creates a file or a directory, as running out of space does.
A count of calls includes the program’s start. The dynamic loader opens the
program’s libraries with openat before main runs: 12 calls for tail
here, of 20 in all for 8 files. --after 9 made the loader fail with
error while loading shared libraries: libm.so.6, and --after 14 failed
the third file. Read MATCHED in machine inject app to
count a program’s calls, or use --prob 0.01 to fail calls at random.
Kernel faults
The other subsystems fail work inside the kernel, after a system call has started, where a syscall fault never reaches. They take no errors: each fails the way the kernel would.
1 | machine inject app add disk --times 1 --in /usr/bin/app # one disk request fails with EIO |
Five things decide whether you see the failure:
- Who sends a disk request.
--innames the task that sends the request, not the program the data belongs to. Your program sends its reads, itsO_DIRECTwrites and the writesfsyncforces, so--in /usr/bin/appfails those. Ordinary writes wait in memory and the kernel’s flusher writes them later, so only--in allreaches them. To fail a program’s writes, name its files with--fileand use--in all, as Disk faults does. - A program’s first moments. When a program starts, the kernel copies its
arguments in and allocates for it. A
usercopyor allocation fault that fires then kills the program before your code runs. Use--after, or add the fault once the program is up. - The kernel copes with some failures. A failed page allocation is often
retried, and the program never sees it.
MATCHEDandFIREDin the list still show that the fault fired. - The kernel reads a page more than once. A buffered read goes out as
readahead, and when that fails the kernel reads the pages again before it
gives up. How many requests it makes depends on how it splits the read: for
a file of 8KB,
catread the file fine with--times 1and--times 2, and gotI/O errorwith--times 3, all 3 of its requests failed. Leave out--timesfor the program to getEIO. AnO_DIRECTread fails on its first request. disk-timeouthangs. The request is lost, and the task waiting for it stays stuck until the machine stops. This is a hung device: no timeout fires and no reset follows. Use it to see which other parts of the program notice the hang. Name your program in--in: with--in allthe lost request may be the kernel’s own, and whatever waits on that disk hangs with it.
Disk faults
The faults above are errors: your program is told something failed. Real disks
also fail silently. A write reports success and the data is wrong, half there,
or not there at all. disk-corrupt, disk-torn and disk-lost are those
failures. The request succeeds, and the damage is found later, or never.
| You want to know | Fault |
|---|---|
| Does the database notice a bad block when it reads its data files? | disk-corrupt --file '/var/lib/db/*/data.bin' --in all |
| Does the program trust bytes it read without a checksum? | disk-corrupt --op read --file /srv/app/state.json --in all |
| Does recovery handle a log record only half written? | disk-torn --file /var/lib/db/wal --times 1 --in all |
| Does it notice a committed write that never arrived? | disk-lost --file /var/lib/db/wal --after 10 --times 1 --in all |
| Does the filesystem survive damage to its own metadata? | disk-corrupt --sectors LO:HI --in all, the sectors of its inode table or journal |
1 | machine inject app add disk-corrupt --file '/var/lib/db/*/data.bin' --op write --bytes 8 --times 1 --in all |
--fileis a pattern.*matches within a directory and**across directories. Programs often name their files as they run, and a pattern names them before they exist. A file the program already holds open or maps, such as a running database’s WAL, matches too. A file inside a container is named by its path in the container.--in allwith--file. Ordinary writes reach the disk from the kernel’s flusher, not from your program, so--in allis what reaches them.--filekeeps the fault on your file.- The page cache hides a damaged write. The kernel keeps the bytes your
program wrote in memory, so the program reads them back clean until they are
read from the disk again: with
O_DIRECT, afterecho 3 > /proc/sys/vm/drop_caches, or in a branch forked later. A damaged read is the other way round: the bad bytes go into the page cache, and every reader sees them until the kernel drops the page. - A tear is whole sectors. Machine tears a write at sector granularity: each 512-byte sector is written whole or not at all, so a write of one sector does not tear. A torn write keeps at least one sector of the new data and loses at least one. Real disks differ in what they write atomically; this is the model Machine applies.
--sectorsaims below the files. It names 512-byte sectors of the whole disk, so it reaches what--filecannot: the filesystem’s inodes, bitmaps and journal. The filesystem’s own tools say where those are, such asdumpe2fsanddebugfsfor ext4, in blocks of its partition. With 4 KiB blocks, block N starts at sector 8 × N plus the partition’s first sector, which/sys/class/block/vda1/startholds, or 0 for a filesystem on the whole disk. Alternatively, stop at a write withcatch add io, described below, and read its sectors from the stop message.disk-lostis one write, not a crash. The lost write reports success, and the writes after it still land. A power cut loses every write since the last flush, and the disk faults do not model one. For that, usemachine containers crash: it loses the writes a service’s volumes had not synced.
A replay damages the same bytes, so a bug you find this way is found again on every replay of the run.
A worked example: ClickHouse. ClickHouse writes each part of a table into a
directory it names as it goes, such as all_12_12_0, and checks the part’s
checksums when it reads it back. Here ClickHouse runs in the service app, as
a disk built with --from clickhouse/clickhouse-server runs it, and db.t is a
MergeTree table. clickhouse-client is in the container, so it runs with
--container app. sync and drop_caches act on the whole machine and run
without it.
The file a fault names is the path the program opens. A table of an Atomic
database, the default, is a link: data/db/t points into
/var/lib/clickhouse/store, where ClickHouse writes each part. To damage one
column file of the next part written and see what ClickHouse does:
1 | machine inject app add disk-corrupt --file '/var/lib/clickhouse/store/**/data.bin' \ |
CHECK TABLE prints 0: a part failed its checksum. With
SETTINGS check_query_single_value_result = 0 it names the part:
1 | all_2_2_0 0 Code: 40. DB::Exception: Checksum mismatch for file data.bin in data part all_2_2_0 (ca04d56da06c3776cca5507fb30255db vs 0bfa9b09000d6890888463cf7622b8a7). (CHECKSUM_DOESNT_MATCH) (version 24.8.14.39 (official build)) |
To damage one part you have already seen, find its name first on a branch, and keep your run where it is. In this run the table already holds three parts, so the branch’s insert writes the fourth:
1 | machine fork app --name peek |
1 | /var/lib/clickhouse/store/1a4/1a4d2927-2bb1-4690-a797-0350ac2bc365 |
ClickHouse writes a new part into a directory named tmp_insert_ followed by
the part’s name, then renames the directory. The fault names the directory
ClickHouse writes into, in the table’s directory under store:
1 | machine inject app add disk-corrupt --op write --bytes 8 --times 1 --in all \ |
The run is deterministic, so your run writes the same part as the branch did. The name of an insert’s part stays the same, but a merge’s part name can change, because any fault slightly changes how the machine runs. A pattern covers both cases.
Faults that cycle
Real outages come in bursts: a disk that is flaky for two seconds, a network
that drops for five. --prob fails calls one at a time and independently,
which rarely exercises retries, backoff or a circuit breaker. --on and
--off make a fault active for a stretch of machine time, rest for another,
and repeat:
1 | machine inject app add disk --file /var/lib/db/data --on 2s --off 5s --in all |
The first fails the data file’s disk requests for 2 seconds in every 7. The
cycle starts when the fault is added, active first, on the machine’s own
clock, so a slow host changes nothing and a replay crosses every edge at the
same instruction. The machine’s clock is not the date programs in the machine read, so a
machine clock change does not move a window. While a fault is off, it
matches no call and counts none, so --after and --times count only the
calls in its active windows. A cycling fault needs both --on and --off,
each a whole number of milliseconds from 1ms to a day.
The listing says which window each fault is in and how long it has left. After the second command it reads:
1 | # INJECT ERRORS DELAY IN FIRES PROB SEED MATCHED FIRED |
machine inject app later shows the window
moving: now on, 996ms left one second in, and now off, 2496ms left at two
and a half seconds.
A shell loop that opens a file twice a second under the second command sees
4 failures, then 6 successes, over and over: 2 seconds failing, 3 seconds
working. Each window has a fixed length. To try outages of other
lengths, fork branches from one checkpoint and give each its own --on and
--off.
Stopping at a fault
To look at the program at the moment a fault fires, add a catchpoint for it:
1 | machine catch app add fault openat |
The run stops where the fault fires, before a system call’s wait and error, and
says which: Stopped at catchpoint 1: fault openat EACCES, in tid 229, or
fault disk, in tid 77. See Debugging for catchpoints.
Stopping at a file’s I/O
An io catchpoint stops the run at a disk request, with no fault. --file
takes the same patterns as a disk fault, and --op takes read or write:
1 | machine catch app add io --file '/var/lib/clickhouse/store/**/data.bin' --op write |
The run stops as the request is sent, before any disk fault decides on it, and
names the request: Stopped at catchpoint 1: io write 1058144:1058151, in tid 266. A fault added there decides on that same request. Name it by its file:
1 | machine inject app add disk-lost --file '/var/lib/clickhouse/store/**/data.bin' --times 1 --in all |
Or by its sectors and its task, which name this request and no other:
1 | machine inject app add disk-corrupt --sectors 1058144:1058151 --times 1 --in tid=266 |
An ordinary write is sent by the kernel’s flusher, so the stop names a kernel
task such as kworker/u4:1. --in tid=N names that task as well as any
other.
Changing faults
1 | machine inject app delete 2 # by its number in the list |
At the control prompt the command is
/inject, with the same words, and /tasks exist checks first:
1 | /tasks exist /usr/bin/app |
Faults and plans work together. Add the faults, then run the plan: the faults apply to every stretch of execution, whatever the plan chooses. To change them partway, stop, change them, and run on.
From Python, run.inject.add(...) takes the same subsystem, errors and
options, with in_= for --in, and run.inject() lists the faults with
their counts. The SDK’s Faults has an example.
What a fault misses
- A C function is not always the system call of its name, and C libraries
differ. glibc’s
open()callsopenat, musl’s callsopen, and glibc’sstat()callsnewfstatat. Name both,open,openat, to cover either.machine syscalls applists the names the kernel uses. - Give
--seedin a script that records again. Without it each run draws its own seed. A replay needs none: the run keeps the fault as it was added. - 64-bit system calls only. A 32-bit call, made with
int 0x80, never fails. - Calls the vDSO answers never reach the kernel.
clock_gettime,gettimeofday,timeandgetcpureturn from the vDSO without a system call, so a fault on them matches nothing and MATCHED stays 0. - 16 faults, and 30 errors in one. Delete a fault to add another, and use
--pickto take 30 or fewer ofall. - A fault that fired is not a failure the program saw. The kernel retries
an allocation, rereads a page and recovers a filesystem, and a program
retries a call.
FIREDsays the fault fired, andMATCHED0 says it never met a call, so nothing was tested. Whether the program failed is a question for the program: check its output, its exit status or its data after the fault, as well as the counts. - A fault matches the system call, the task that makes it and the count of calls. It does not look at the arguments, so “openat of /etc/passwd” is not a fault. Stop at the call and fail that one instead.
ClickHouse is a registered trademark of ClickHouse, Inc. See clickhouse.com.