Skip to content

Commit

Permalink
Local Disk Cache: Only write to disk if target hash doesn't already e…
Browse files Browse the repository at this point in the history
…xist.

Under Windows, the default permissions used when writing to the local disk cache prevent the files from being overwritten.

This CR adds a check: if the target file already exists, return early. This is a performance improvement and will fix the error described above as existing files will no longer need to be overwritten. Similar features have been implemented on the remote gRPC cache implementations, see bazel issue bazelbuild#4789.

Closes bazelbuild#4886.

PiperOrigin-RevId: 190060233
  • Loading branch information
RNabel authored and Copybara-Service committed Mar 22, 2018
1 parent d01e8ad commit 7fc3410
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ Yue Gan <yueg@google.com>
Yun Peng <pcloudy@google.com>
Dmitry Babkin <dbabkin@google.com>
Klaus Aehlig <aehlig@google.com>
Robin Nabel <rnabel@ucdavis.edu>
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ public boolean getActionResult(String key, OutputStream out)

@Override
public void put(String key, long length, InputStream in) throws IOException {
Path target = toPath(key);
if (target.exists()) {
return;
}

// Write a temporary file first, and then rename, to avoid data corruption in case of a crash.
Path temp = toPath(UUID.randomUUID().toString());
try (OutputStream out = temp.getOutputStream()) {
ByteStreams.copy(in, out);
}
// TODO(ulfjack): Fsync temp here before we rename it to avoid data loss in the case of machine
// crashes (the OS may reorder the writes and the rename).
Path f = toPath(key);
temp.renameTo(f);
temp.renameTo(target);
}

@Override
Expand Down

0 comments on commit 7fc3410

Please sign in to comment.