3 ms·
> > - Write() to be blocking / atomic by default. Don't return until data is safely committed. > This is the wrong thing for 99% of use cases. You get terrible
by josephg 9y ago
> > - Write() to be blocking / atomic by default. Don't return until data is safely committed.
> This is the wrong thing for 99% of use cases. You get terrible performance unless you batch things, which means that casual use ends up with severe performance issues.
The right answer would be to batch writes in the OS, but of course this would deadlock without non-blocking APIs. Maybe you're right - maybe write() should have the effectively non-blocking API it has now with fsync as a commit. But I still want my guarantee that if the system / hardware dies halfway through a write (or batch of writes) then data won't be left in a corrupt half-written state. Package installations and OS upgrades should be able to do the entirety of their work in a filesystem-level transaction. I shouldn't need to worry about my OS needing a reinstall because my laptop ran out of power halfway through an update. I just can't think of any sensible way to do this using the APIs we have today.
> Beside – someone has to write the filesystem, and that filesystem uses largely the same mechanisms in the kernel that are exposed to userspace.
But importantly not the same mechanisms. The OS has control over write ordering, and most block devices support read & write barriers. Filesystems lean heavily on these primitives for correctness - and for good reason; they're really useful primitives for building correct, performant systems. But for some reason they aren't exposed to userspace. Userspace applications are instead left scratching in the dirt with fsync. It would be much easier to build correct, performant databases in userspace if we had access to this stuff.
- colanderman 9y agoA filesystem-level transaction would be a good idea. Maybe some FSes (e.g. ZFS) already support such a thing. You can get this (somewhat awkwardly) for a single file with any FS that supports copy-on-write by making a (lazy) copy of the file, doing your updates there, and then renaming it to the original file name (which is guaranteed to be atomic on most FSes). With smaller files this is standard practice even on FSes without copy-on-write. > most block devices support read & write barriers No, they really don't. Some hardware devices might. I'm not aware of any SAN that does. (I've specifically asked this question of the developers of three.) > Filesystems lean heavily on these primitives for correctness In Linux, not since 2010 they don't [1], for exactly the reason that they're poorly implemented by devices. [1] https://lwn.net/Articles/400541/ https://lwn.net/Articles/400541/
- branko_d 9y agoNTFS has transactions... https://msdn.microsoft.com/en-us/library/windows/desktop/aa363859(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa3... ...but sadly, they seem to be on the path of being deprecated by Microsoft... https://msdn.microsoft.com/en-us/library/windows/desktop/hh802690(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/hh8...