Affects Version/s: None
Fix Version/s: 2.7.6
Most likely the issue manifests itself as the following critical error:
It appears that there following sequence is possible:
- Thread A attempts to log a large record which does not fit segment, addRecord fails and the thread A starts segment rollover. I successfully runs flushOrWait(null) and gets de-scheduled before adding switch segment record
- Thread B attempts to log another record, which fits exactly till the end of the current segment. The record is added to the buffer
- Thread A resumes and fails to add the switch segment record. No flush is performed and the thread immediately proceeds for wal-writer close
- WAL writer thread wakes up, sees that there is a CLOSE request, closes the file IO and immediately proceeds to write unflushed changes causing the exception.
Unconditional flush after switch segment record write should fix the issue.
Besides the bug itself, I suggest the following changes to the FileWriteHandleImpl (FileWriteAheadLogManager in earlier versions):
- There is an fsync(filePtr) call inside close(); however, fsync() checks the stop flag (which is set inside close) and returns immediately after flushOrWait() if the flag is set - this is very confusing. After all, the close() itself explicitly calls force after flush
- There is an ignored IO exception in mmap mode - this should be propagated to the failure handler
- In WAL writer, we check for file CLOSE and then attemp to write to (possibly) the same write handle - write should be always before close
- In WAL writer, there are racy reads of current handle - it would be better if we read the current handle once and then operate on it during the whole loop iteration