Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Zhang <[email protected]>
  • Loading branch information
v01dstar committed Nov 15, 2023
1 parent 0d2924b commit a59609a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/file_pipe_log/log_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,21 @@ impl<F: FileSystem> LogFileWriter<F> {
self.write(&buf, 0)
}

pub fn close(&mut self) {
pub fn close(&mut self) -> Result<()> {
// Necessary to truncate extra zeros from fallocate().
self.truncate();
self.truncate()?;
self.sync();
Ok(())
}

pub fn truncate(&mut self) {
pub fn truncate(&mut self) -> Result<()> {
if self.written < self.capacity {
fail_point!("file_pipe_log::log_file_writer::skip_truncate", |_| {});
// Panic if truncate fails, in case of data loss.
self.writer.truncate(self.written).unwrap();
self.writer.truncate(self.written)?;
self.capacity = self.written;
}
Ok(())
}

pub fn write(&mut self, buf: &[u8], target_size_hint: usize) -> IoResult<()> {
Expand Down
8 changes: 4 additions & 4 deletions src/file_pipe_log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub mod debug {
len,
});
}
writer.close();
writer.close().unwrap();
// Read and verify.
let mut reader =
LogItemReader::new_file_reader(file_system.clone(), &file_path).unwrap();
Expand Down Expand Up @@ -280,7 +280,7 @@ pub mod debug {
true, /* create */
)
.unwrap();
writer.close();
writer.close().unwrap();

assert!(LogItemReader::new_file_reader(file_system.clone(), dir.path()).is_err());
assert!(
Expand Down Expand Up @@ -330,7 +330,7 @@ pub mod debug {
.unwrap();
let f = std::fs::OpenOptions::new().write(true).open(&path).unwrap();
let len = writer.offset();
writer.close();
writer.close().unwrap();
if shorter {
f.set_len(len as u64 - 1).unwrap();
}
Expand All @@ -341,7 +341,7 @@ pub mod debug {
false, /* create */
)
.unwrap();
writer.close();
writer.close().unwrap();
let mut reader = build_file_reader(file_system.as_ref(), &path).unwrap();
assert_eq!(reader.parse_format().unwrap(), to);
std::fs::remove_file(&path).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/file_pipe_log/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(super) struct SinglePipe<F: FileSystem> {
impl<F: FileSystem> Drop for SinglePipe<F> {
fn drop(&mut self) {
let mut writable_file = self.writable_file.lock();
writable_file.writer.close();
writable_file.writer.close().unwrap();
let mut recycled_files = self.recycled_files.write();
let mut next_reserved_seq = recycled_files
.iter()
Expand Down Expand Up @@ -246,7 +246,7 @@ impl<F: FileSystem> SinglePipe<F> {
let new_seq = writable_file.seq + 1;
debug_assert!(new_seq > DEFAULT_FIRST_FILE_SEQ);

writable_file.writer.close();
writable_file.writer.close().unwrap();

let (path_id, handle) = self
.recycle_file(new_seq)
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<F: FileSystem> SinglePipe<F> {
}
let start_offset = writer.offset();
if let Err(e) = writer.write(bytes.as_bytes(&ctx), self.target_file_size) {
writer.truncate();
writer.truncate()?;
if is_no_space_err(&e) {
// TODO: There exists several corner cases should be tackled if
// `bytes.len()` > `target_file_size`. For example,
Expand Down
2 changes: 1 addition & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl RhaiFilterMachine {
)?;
log_batch.drain();
}
writer.close();
writer.close().unwrap();
}
}
// Delete backup file and defuse the guard.
Expand Down

0 comments on commit a59609a

Please sign in to comment.