Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pipe: reseek after write failure #245

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
### Bug Fixes

* Unconditionally tolerate `fallocate` failures as a fix to its portability issue. Errors other than `EOPNOTSUPP` will still emit a warning.
* Avoid leaving fractured write after failure by reseeking the file writer. Panic if the reseek fails as well.

### New Features

3 changes: 3 additions & 0 deletions src/env/default.rs
Original file line number Diff line number Diff line change
@@ -232,6 +232,9 @@ impl Read for LogFile {

impl Seek for LogFile {
fn seek(&mut self, pos: SeekFrom) -> IoResult<u64> {
fail_point!("log_file::seek::err", |_| {
Err(std::io::Error::new(std::io::ErrorKind::Other, "fp"))
});
match pos {
SeekFrom::Start(offset) => self.offset = offset as usize,
SeekFrom::Current(i) => self.offset = (self.offset as i64 + i) as usize,
9 changes: 8 additions & 1 deletion src/file_pipe_log/log_file.rs
Original file line number Diff line number Diff line change
@@ -104,7 +104,14 @@ impl<F: FileSystem> LogFileWriter<F> {
}
self.capacity += alloc;
}
self.writer.write_all(buf)?;
self.writer.write_all(buf).map_err(|e| {
self.writer
.seek(SeekFrom::Start(self.written as u64))
.unwrap_or_else(|e| {
panic!("failed to reseek after write failure: {}", e);
});
e
})?;
self.written = new_written;
Ok(())
}
62 changes: 62 additions & 0 deletions tests/failpoints/test_io_error.rs
Original file line number Diff line number Diff line change
@@ -287,6 +287,68 @@ fn test_concurrent_write_error() {
);
}

#[test]
fn test_non_atomic_write_error() {
let dir = tempfile::Builder::new()
.prefix("test_non_atomic_write_error")
.tempdir()
.unwrap();
let cfg = Config {
dir: dir.path().to_str().unwrap().to_owned(),
bytes_per_sync: ReadableSize::kb(1024),
target_file_size: ReadableSize::kb(1024),
..Default::default()
};
let fs = Arc::new(ObfuscatedFileSystem::default());
let entry = vec![b'x'; 1024];
let rid = 1;

{
// Write partially succeeds. We can reopen.
let engine = Engine::open_with_file_system(cfg.clone(), fs.clone()).unwrap();
let _f1 = FailGuard::new("log_fd::write::err", "return");
engine
.write(&mut generate_batch(rid, 0, 1, Some(&entry)), true)
.unwrap_err();
}
{
let engine = Engine::open_with_file_system(cfg.clone(), fs.clone()).unwrap();
assert_eq!(engine.first_index(rid), None);
}
{
// Write partially succeeds. We can overwrite.
let engine = Engine::open_with_file_system(cfg.clone(), fs.clone()).unwrap();
let _f1 = FailGuard::new("log_fd::write::err", "1*off->1*return->off");
engine
.write(&mut generate_batch(rid, 0, 1, Some(&entry)), true)
.unwrap_err();
engine
.write(&mut generate_batch(rid, 5, 6, Some(&entry)), true)
.unwrap();
assert_eq!(engine.first_index(rid).unwrap(), 5);
}
{
let engine = Engine::open_with_file_system(cfg.clone(), fs.clone()).unwrap();
assert_eq!(engine.first_index(rid).unwrap(), 5);
}
{
// Write partially succeeds and can't be reverted. We panic.
let engine = Engine::open_with_file_system(cfg.clone(), fs.clone()).unwrap();
let _f1 = FailGuard::new("log_fd::write::err", "return");
let _f2 = FailGuard::new("log_file::seek::err", "return");
assert!(catch_unwind_silent(|| {
engine
.write(&mut generate_batch(rid, 6, 7, Some(&entry)), true)
.unwrap_err();
})
.is_err());
}
{
let engine = Engine::open_with_file_system(cfg, fs).unwrap();
assert_eq!(engine.last_index(rid), Some(5));
}
}

#[cfg(feature = "scripting")]
#[test]
fn test_error_during_repair() {