bitly/nsq — pull request #214 by mreiferson (2013-05-31 15:02:37+00:00)
Title: nsqd: diskqueue cleanup

Did a pass over `diskqueue` to improve readability of its main `ioLoop()` and while in there identified a few fun things:

1. When we detect tail corruption (and fix) we were not forcing a sync. If the process crashed again you would see the same corruption detected (misleading).

2. (relating to 1 above) Add `--sync-timeout` (default `2s`) which is arguably a better way to manage how often you `fsync` vs `--sync-every` (which worked by counting number of messages). I propose we drop `--sync-every` in a future revision.

3. When we detect tail corruption we should cleanup the file we skip (`skipToNextRWFile()` did not do this)

4. We opened the `writeFile` with `O_APPEND` which causes each `write()` to be appended to the end of the file. This isn't actually what we want because we already `seek()` to our *committed* write position. It was technically possible for a process to have had uncommitted data past the recorded write position, crash, and then write to the wrong position upon restart.

5. Cleanup the main `ioLoop()` for readability (move most of the logic into functions).

cc @jehiah