WritableFileWriter: Improve accuracy of bytes_per_sync_
The bytes_per_sync_ option controls how often data is synced to disk. When the number of bytes written but not yet synced exceeds this threshold, a sync() call is issued. We rely on this to helps shape the I/O traffic to the disk.
However, the bytes_per_sync_ option is only checked in Flush(). That means if we only do Append() or Pad() without Flush(), the data will accumulate until the buf_ is full and a Flush() is eventually called. This Flush() pushs a full buf_ to the file, and the following sync() will sync the same amount of bytes to disk. The amount of data to sync may be as large as buf_size_(default to 1M) and are highly likely to exceed the bytes_per_sync_.
This patch adds checks in Append() and Pad(). If the number of new bytes exceeds the bytes_per_sync_, a sync() call will be issued to sync earlier data to disk.
Update:
Maybe I find another issue. The recent 1MB data won't be sync() for performance reasons, but the implementation counts all bytes (filesize_, includes the data in buffer) rather than only flushed bytes (writable_file_->GetFileSize()).