Skip to content

Commit

Permalink
Merge pull request #308 from samoht/fsync
Browse files Browse the repository at this point in the history
use fsync to flush metadata when reoperning a file with new headers
  • Loading branch information
samoht committed Apr 28, 2021
2 parents 44b3396 + be81d69 commit dcce632
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
progress: the `log_async` file was not cached properly and fully reloaded
from disk every time. (#310)

- Added fsync after `Index.clear` to signal more quickly to read-only instances
than something has changed in the file (#308)

## Changed

- Specialise `IO.v` to create read-only or read-write instances. (#291)
Expand Down
13 changes: 7 additions & 6 deletions src/unix/index_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ module IO : Index.IO = struct
m "[%s] raw set_header %a" file Header.pp { offset; generation });
Raw.Header.set raw header;
Raw.Fan.set raw "";
Raw.fsync raw;
raw

let clear ~generation ?(hook = fun () -> ()) ~reopen t =
Expand Down Expand Up @@ -202,23 +203,23 @@ module IO : Index.IO = struct
let v ?flush_callback ~fresh ~generation ~fan_size file =
let v = v_instance ?flush_callback ~readonly:false file in
mkdir (Filename.dirname file);
let header =
{ Raw.Header.offset = Int63.zero; version = current_version; generation }
in
match Sys.file_exists file with
| false ->
let x = Unix.openfile file Unix.[ O_CREAT; O_CLOEXEC; O_RDWR ] 0o644 in
let raw = Raw.v x in
Raw.Offset.set raw Int63.zero;
Raw.Header.set raw header;
Raw.Fan.set_size raw fan_size;
Raw.Version.set raw current_version;
Raw.Generation.set raw generation;
v ~fan_size ~offset:Int63.zero raw
| true ->
let x = Unix.openfile file Unix.[ O_EXCL; O_CLOEXEC; O_RDWR ] 0o644 in
let raw = Raw.v x in
if fresh then (
Raw.Offset.set raw Int63.zero;
Raw.Header.set raw header;
Raw.Fan.set_size raw fan_size;
Raw.Version.set raw current_version;
Raw.Generation.set raw generation;
Raw.fsync raw;
v ~fan_size ~offset:Int63.zero raw)
else
let version = Raw.Version.get raw in
Expand Down

0 comments on commit dcce632

Please sign in to comment.