NEXItaliano

Durable fsync on three copies

When a program saves something that matters (a database, a mail server, an editor saving a file) it calls fsync. The promise of fsync is simple: when it returns, the data is on the disk, and a power cut will not take it away. On a single machine the kernel and the disk keep that promise. On a distributed filesystem, where every file lives on several machines, keeping it takes some care, and it is one of the things we cared about most in NEXFS, the filesystem of NEX.

What NEXFS promises

In NEXFS, fsync returns only when the data is on the disk of every copy. Not in the memory of one machine, not "on its way": on the disks, all of them. If one machine loses power right after fsync returns, the other copies have the data; if all the machines lose power together, every copy has it.

It sounds obvious, but it is a choice. Answering fsync as soon as the data is in memory is faster, and some systems do it by default. We chose the other way, because the programs that call fsync are exactly the ones that need the answer to be true.

How it works

  1. Every write goes to all the copies. A file is cut into blocks, and each block lives on one, two or three machines. The client sends a write to the first copy, which writes it and passes it to the others; the client gets the answer when all the copies have it.
  2. fsync asks every copy to put the block on its disk, and waits for all of them. Only then it answers the program.
  3. A copy that does not answer leaves the list. If a machine is switched off or cut off, its copy is excluded and the others go on. The machine rebuilds its copy by itself when it comes back. Meanwhile the cluster knows that block has fewer copies, and does not pretend otherwise.
  4. Never too few copies to write. A block that should have three copies is written only while at least two are complete. With fewer, whoever writes waits for a machine to come back instead of writing on a single disk.
  5. A machine that restarts after a crash checks itself first. Before serving any data it tells the cluster which blocks had writes not yet on its disk, so a copy that missed something is rebuilt and never read.
  6. A client that is cut off stops writing. The rights a client has on a file expire by themselves after a few seconds without contact, so a machine on the wrong side of a broken network cannot keep writing over the others.

How we check it

Promises like this are only worth the tests behind them. Among the ones NEXFS goes through:

  • every machine switched off at once, again and again, under load: after each restart, everything a program had confirmed with fsync must be there, line by line;
  • long endurance tests with random faults: disks failing, networks cut, machines killed, while programs keep writing and checking what they wrote;
  • a copy left behind on purpose: a machine is stopped, the file is rewritten elsewhere, and the machine is read while it comes back; it must never return the old data.

Speed where you want it, safety where you need it

Three durable copies cost something on every write. Not every folder needs them: a build folder, a cache or scratch data can be redone. In NEXFS the number of copies is chosen per folder, from one to three:

nexfsctl copies 1 /projects/scratch     # fast: one copy
nexfsctl copies 3 /home                 # safe: three copies

fsync keeps its promise in both cases; what changes is how many machines must lose their disk before the data is gone. You choose, folder by folder.

More

  • NEXFS: what it is and what it does
  • Try NEX: how to try it on your machines