Nightmare mode: simulating disk failures
Building a correct distributed system is really, really hard. Building a correct distributed system that has to store data and never lose it is even harder.
Many of our customers build systems where storing data reliably is paramount. Storage is hard even when nothing goes wrong. For example, some customers watch how full their disks are and compact data when necessary to reclaim space, and shard it across machines before any one disk hits its limits. Some maintain a write-ahead log so they can recover from a process restart. Others rely on a careful sequence of operations to guarantee durability and atomicity, for example writing a new copy of a file and renaming it over the old one, so that a change happens completely or not at all.
Unfortunately, storage devices are unreliable, which only adds further complexity to every interaction with the disk. Consider a single write. If it fails with an error, the system has to decide whether it is safe to retry or whether it has to give up. If it fails because the disk is out of space, the system may have to trigger compaction before it can continue. Even creating a file can fail because the filesystem has run out of inodes, and the system then has to delete other files to make room.
Moreover, unreliability can come in the form of latency. A disk can be slow, for example because of I/O contention or a failing device, which can then stretch operations out so they overlap in ways they normally wouldnāt. If a read and a write end up happening at the same time, the reader may see the old data, the new data, or a random interleaving of the two.
Complexity also arises once you assume the disk will not always be there. If a write succeeds and the machine loses power immediately afterwards, a read after the restart could see all of the data, a random part of it, or none of it.
On the Antithesis Forward Deployed Engineering team, weāve had recurring discussions with customers who build this critical software, and who would like to inject disk faults or storage errors during their Antithesis simulations to mimic these kinds of situations and to ensure their software can handle it.
One of the first things we realized is that different customers had vastly different expectations of their storage, and of the guarantees it should provide. This was further complicated by some running on the ext4 filesystem, and some on XFS or ZFS, each with its own behaviors under failure. This diversity of requirements has made it hard to build a one-size-fits-all solution up front.
Instead, we worked with individual customers to build several solutions, geared to their specific use cases. For instance, to test space reclamation logic weād simulate filling up the whole filesystem. Other approaches include shouting at the disks. Once weād got a sense of the requirements of different customers, we worked to incorporate these pieces of the puzzle into a comprehensive filesystem fault injection mechanism.
In this post, weāll give a tour of the disk faults that a ānightmare modeā fault injector needs to be able to emulate. Weāll start with the fault everyone asks for first, power failure, and explain what it took to emulate it convincingly. From there weāll get into the subtler things a disk can do: torn writes, I/O errors (including the one behind the fsyncgate bug), and the latency faults that shake out concurrency bugs. Before we get into faults, weāll give some background on how file writes work in the āhappy pathā case.
The happy path
When a program writes to a file, the data does not go to disk right away. Instead, it goes into the kernelās page cache. The kernel then writes it to disk on its own schedule, through a process called writeback.
If a program needs the data on disk immediately, it can call fsync, which tells the kernel to write that fileās data to disk and doesnāt return until the write has finished. Data that is written but not fsynced could be destroyed by a power loss.
The diagram below shows these steps for a successful write to disk:
Power loss
Now letās start looking at what happens when things go wrong. We started with power loss because it seemed like the fault most of our customers would care about.
Antithesis already has a related fault type, node kills, which randomly SIGKILL a container. The SIGKILL destroys the process, but has no effect on the machine, which keeps running, so the kernel and its page cache carry on as if nothing happened. Every write the process completed still makes its way to disk, fsync or no fsync, courtesy of the kernelās writeback mechanism. As far as the process is concerned, a SIGKILL is a fairly polite way to die, since you only lose data that the process never handed to the kernel.
The diagram below shows how data that was written but never fsynced survives the SIGKILL and lands on disk anyway:
A power failure is less polite. It loses everything the kernel accepted but hadnāt synced yet. Data that has been written but not yet fsynced is simply gone:
Unfortunately, when weāre simulating faults in Antithesis, we canāt just pull the plug on the machine. So the next best option was to fake it. That meant dealing with the two things that stop a container kill from causing data or cache loss: the kernelās page cache, which holds on to unsynced data, and the kernelās writeback, which saves unsynced data to disk. The problem was that both of these live inside the kernel, out of our reach. Currently, there is no way to ask the kernel to forget whatās in its cache, or to stop the kernelās automatic writeback.
However, we found a way around this: LazyFS, an open-source filesystem from INESC TEC and Jepsen. LazyFS uses FUSE, a Linux feature that lets a userspace program act as a filesystem. Because every read and write goes through LazyFS, it can hold written data in its own internal cache, emulating the kernelās page cache in user space. Writes to LazyFS stay in the internal cache until the program calls fsync, and only then does LazyFS write it to persistent storage. Also, there is no background writeback to save it in the meantime.
This gives us the ability to āforgetā unsynced data at any convenient moment, simply by clearing the cache. If we do that at the same moment we kill the application, the two together are indistinguishable from a real power loss. Hereās a diagram of we can use LazyFS to emulate power loss:
From LazyFS to Amnesia
Before settling on FUSE, we looked at a couple of other mechanisms for injecting faults. eBPF was tempting, since it makes some faults, like returning a particular error code from a syscall, easy to implement. However, it would be difficult to emulate power failure, since it only allows us to watch and tweak calls as they go by. It canāt hold on to data and release it (or lose it) later, which is the trick we needed.
We also thought about using a custom block device, but that had the opposite problem: it can hold data, but it sits at the wrong level of abstraction. Since itās below the filesystem, it sees blocks rather than syscalls, which makes syscall-level faults hard to implement. Power loss is especially tricky because you canāt tell data that arrived through an fsync from data the kernelās writeback happened to push out. That would cost us the strictness that catches missing fsyncs.
So LazyFS was very useful to us, and we want to extend a thank you to the authors for writing such a high quality tool. We ran a patched version (extended with additional fault types) for a while on a number of customer tenants, and it found some subtle data loss bugs. After that, we decided to implement our own FUSE filesystem: Amnesia.
We wanted to move to Rust for its memory safety, and to rework the design so the model was simpler and faster, since we planned to build a lot more on top of it. For instance, Amnesia works directly on inodes, whereas LazyFS uses the path for every operation and has to map it back to the inode. Since a path is only a name for a file, this makes operations like renames, hard links, and deleting a file that is still open awkward to track. Working with inodes instead makes that complexity disappear.
Amnesia also never touches a real disk. LazyFS writes synced data through to the backing filesystem. Amnesia keeps both the volatile state and the ādurableā state in memory, which allows us to model an fsync as a bookkeeping update instead of another real I/O. This makes it much faster, and just as safe, because the machine never actually loses power.
We made a number of other changes too, which we will get into below. Beyond the design, LazyFS introduced us to the subtler effects of a real power loss: torn writes and torn sequences of writes, both caused by the fact that the kernel and the disk do not persist data in the order or the units a program might expect. The LazyFS paper by Ramos et al. is a good read if you want the details. It also made us aware of the wrinkles of working at the FUSE layer. For instance, the kernel page cache still sits in front of the FUSE filesystem, and it can be holding copies of the data we just ālostā by emulating a power failure. Thankfully, LazyFS had already worked out the right FUSE settings and patterns to make sure the program never sees them.
Disk I/O errors
Building on FUSE made it easy to add disk I/O errors, another fault type that our customers wanted. The obvious approach is to simply return the error, and that model alone was enough to find some bugs. But because we emulate the kernel page cache, we can sometimes apply the possible side effects of these errors to it, which gives us a higher degree of fidelity. Out of the box, LazyFS does not simulate disk I/O errors as a form of fault injection, so this was new ground for us.
We added the ability to return a few disk I/O error types on syscalls. One of the most interesting ones was EIO (āError: Input/Outputā, the generic ādisk failedā error) on fsync. This error can interact with the kernel page cache in surprising ways. This is exactly whatās behind the infamous fsyncgate bug. The discovery led to PostgreSQL fixing how it handles a failed fsync (it now crashes and recovers from its log rather than trying again), and it changed how the rest of us read fsyncās promise for good.
As Craig Ringer, the PostgreSQL developer who reported the problem, put it:
When fsync() returns success it means āall writes since the last fsync have hit diskā but we assume it means āall writes since the last SUCCESSFUL fsync have hit diskā.
To understand why Craig is correct, we need some background on what happens during an fsync on a typical Linux filesystem such as ext4. The kernelās page cache holds file data in fixed-size chunks called pages, and marks a page as ādirtyā when it has been changed but not yet written to disk. When a program calls fsync, the kernel sends each of the fileās dirty pages to the storage device through a driver. Crucially, the kernel clears a pageās dirty flag before handing it off. The fsync then waits for the driver to report whether each write succeeded.
Now suppose one of those writes to the device fails. The fsync will then return EIO, but nothing goes back and marks the page dirty again. As far as the kernel is concerned, that page is now clean, so neither the next fsync nor the background writeback will ever try to submit it to the device again. Since there are no dirty pages associated with the file, the next fsync succeeds. Unless the program happens to write the same data again and re-dirties the pages, that data never reaches the disk.
We can emulate this scenario in the FUSE layer. When the fault fires, fsync returns EIO and the affected pages are marked clean in our internal cache, without being written to persistent storage. A retried fsync then succeeds, but the data will be lost at the next emulated power failure unless the program writes it again and calls fsync.
Concurrency testing
Building on FUSE also let us add ādisk latencyā, another commonly requested fault type. Disk latency is really about finding concurrency bugs. Talking to the disk can be slow and unpredictable, and programs that assume otherwise can have races. These races can occur for several reasons. For example, data may land on the disk in an unpredictable order, or land in the kernel page cache in pieces rather than all at once. So we wanted to make those races easier to find.
We currently support two kinds of latency fault:
- Completing operations out of order. Instead of answering syscalls in the order they arrive, we let the fuzzer decide the order they complete in. Since Amnesia sits underneath the program and sees every operation as a request, it can decide whether to answer immediately or hold on to the request. For instance, when data from a write operation arrives, we sometimes hold onto it with a deadline while other operations continue to be processed (the deadline provides a backstop: once it passes, the operation finishes no matter what). The fuzzer then decides when the parked operation should be executed next.
- Partially completed operations. Syscalls arenāt atomic, and a read racing a write can see the write partially applied. Suppose a program overwrites a few kilobytes of a file while another thread reads the same range. On
ext4the reader can see the old contents, the new contents, or a patchwork of old and new chunks. We let the fuzzer pick which of those the reader gets. We implemented this in Amnesia by holding a write and splitting it into small chunks. Instead of applying them all, we let the fuzzer decide which chunks are applied immediately and which are held back. Combined with the out-of-order completion above, this means a read that arrives while the write is parked will likely be answered from the partially applied state, so it sees the same mix of old and new data that a real race could produce. Similarly, reads can be held and split into pieces that are captured at different moments. For instance, a sin
Comments
No comments yet. Start the discussion.