Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Mokhov <[email protected]>
  • Loading branch information
snowleopard committed Apr 9, 2021
1 parent fa498e6 commit 5f2ed5d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
49 changes: 21 additions & 28 deletions src/dune_cache/trimmer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,27 @@ let trim_broken_metadata_entries ~trimmed_so_far =
Layout.Versioned.file_path (Version.Metadata.file_version version)
in
List.fold_left metadata_entries ~init:trimmed_so_far
~f:(fun trimmed_so_far path ->
~f:(fun trimmed_so_far (path, rule_or_action_digest) ->
let should_be_removed =
match Digest.from_hex (Path.basename path) with
| None ->
(* Keep unrecognized entries in the cache. *)
match Metadata.Versioned.restore version ~rule_or_action_digest with
| Not_found_in_cache ->
(* A concurrent process must have removed this metadata file. No
need to try removing such "phantom" metadata files again. *)
false
| Some rule_or_action_digest -> (
match
Metadata.Versioned.restore version ~rule_or_action_digest
with
| Not_found_in_cache ->
(* A concurrent process must have removed this metadata file. No
need to try removing such "phantom" metadata files again. *)
| Error _exn ->
(* If a metadata file can't be restored, let's trim it. *)
true
| Restored metadata -> (
match metadata with
| Metadata.Value _ ->
(* We do not expect to see any value entries in the cache. Let's
keep them untrimmed for now. *)
false
| Error _exn ->
(* If a metadata file can't be restored, let's trim it. *)
true
| Restored metadata -> (
match metadata with
| Metadata.Value _ ->
(* We do not expect to see any value entries in the cache.
Let's keep them untrimmed for now. *)
false
| Metadata.Artifacts { entries; _ } ->
List.exists entries
~f:(fun { Artifacts.Metadata_entry.file_digest; _ } ->
let reference = file_path ~file_digest in
not (Path.exists reference))))
| Metadata.Artifacts { entries; _ } ->
List.exists entries
~f:(fun { Artifacts.Metadata_entry.file_digest; _ } ->
let reference = file_path ~file_digest in
not (Path.exists reference)))
in
match should_be_removed with
| true ->
Expand All @@ -73,14 +66,14 @@ let files_in_cache_for_all_supported_versions () =
let file_exists_and_is_unused ~stats = stats.Unix.st_nlink = 1

let trim ~goal =
let files = files_in_cache_for_all_supported_versions () in
let files = files_in_cache_for_all_supported_versions () |> List.map ~f:fst in
let f path =
let stats = Path.stat path in
if file_exists_and_is_unused ~stats then
Some (path, stats.st_size, stats.st_ctime)
else
None
and compare (_, _, t1) (_, _, t2) = Ordering.of_int (Stdlib.compare t1 t2) in
and compare (_, _, t1) (_, _, t2) = Poly.compare t1 t2 in
let files = List.sort ~compare (List.filter_map ~f files)
and delete (trimmed_so_far : Trimming_result.t) (path, bytes, _) =
if trimmed_so_far.trimmed_bytes >= goal then
Expand All @@ -98,7 +91,7 @@ let trim ~goal =
trim_broken_metadata_entries ~trimmed_so_far

let overhead_size () =
let files = files_in_cache_for_all_supported_versions () in
let files = files_in_cache_for_all_supported_versions () |> List.map ~f:fst in
let stats =
let f p =
try
Expand Down
8 changes: 7 additions & 1 deletion src/dune_cache_storage/layout.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ let list_entries ~storage =
Ok []
| true ->
let dir = storage / dir in
Path.readdir_unsorted dir >>| List.map ~f:(Path.relative dir)
Path.readdir_unsorted dir
>>| List.filter_map ~f:(fun entry_name ->
match Digest.from_hex entry_name with
| None ->
(* Ignore entries whose names are not hex values. *)
None
| Some digest -> Some (dir / entry_name, digest))
in
match Path.readdir_unsorted storage >>= Result.List.concat_map ~f:entries with
| Ok res -> res
Expand Down
6 changes: 3 additions & 3 deletions src/dune_cache_storage/layout.mli
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ module Versioned : sig
(** List all metadata entries currently stored in the cache. Note that there
is no guarantee that the result is up-to-date, since files can be added or
removed concurrently by other processes. *)
val list_metadata_entries : Version.Metadata.t -> Path.t list
val list_metadata_entries : Version.Metadata.t -> (Path.t * Digest.t) list

(** List [list_metadata_entries] but for file entries. *)
val list_file_entries : Version.File.t -> Path.t list
val list_file_entries : Version.File.t -> (Path.t * Digest.t) list

(** List [list_metadata_entries] but for value entries. *)
val list_value_entries : Version.Value.t -> Path.t list
val list_value_entries : Version.Value.t -> (Path.t * Digest.t) list
end

0 comments on commit 5f2ed5d

Please sign in to comment.