File: 0004-supervisor-Fix-invalid-stored-cache-size.patch

package info (click to toggle)
firebuild 0.2.12-3
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm
  • size: 2,764 kB
  • sloc: cpp: 11,564; ansic: 7,048; python: 1,818; sh: 215; makefile: 35; awk: 33
file content (75 lines) | stat: -rw-r--r-- 2,901 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
From 2d63afd4b7f6cb9418ccf2da8fc21a0ec37f3778 Mon Sep 17 00:00:00 2001
From: Balint Reczey <balint@balintreczey.hu>
Date: Wed, 12 Apr 2023 21:25:11 +0200
Subject: [PATCH 4/5] supervisor: Fix invalid stored cache size

(Adjusted patch to apply cleanly)

---
 src/firebuild/execed_process_cacher.cc | 24 +++++++++++++++++++++---
 src/firebuild/execed_process_cacher.h  |  4 ++++
 2 files changed, 25 insertions(+), 3 deletions(-)

--- a/src/firebuild/execed_process_cacher.cc
+++ b/src/firebuild/execed_process_cacher.cc
@@ -1689,11 +1689,16 @@
   off_t cached_bytes = 0;
   if ((f = fopen(size_file.c_str(), "r"))) {
     if (fscanf(f, "%ld\n", &cached_bytes) != 1) {
-      fb_error("Invalid size file format in " + size_file);
+      fb_error("Invalid size file format in " + size_file + ", fixing it.");
+      fclose(f);
+      return fix_stored_bytes();
     }
     fclose(f);
   }
-  assert_cmp(cached_bytes, >=, 0);
+  if (cached_bytes < 0) {
+    fb_error("Invalid size in " + size_file + ", fixing it.");
+    cached_bytes = fix_stored_bytes();
+  }
   return cached_bytes;
 }
 
@@ -1703,7 +1708,7 @@
 
 void ExecedProcessCacher::update_stored_bytes() {
   // FIXME(rbalint) There is a slight chance for two parallel builds updating the size at the
-  // same time making them inaccurate
+  // same time making the file content inaccurate.
   const std::string size_file = cache_dir_ + "/" + kCacheSizeFile;
   const off_t new_size = this_runs_cached_bytes_ + stored_cached_bytes_;
   if (file_overwrite_printf(size_file, "%ld\n", new_size) < 0) {
@@ -1712,6 +1717,19 @@
   }
 }
 
+off_t ExecedProcessCacher::fix_stored_bytes() const {
+  // FIXME(rbalint) There is a slight chance for two parallel builds updating the size at the
+  // same time making the file content inaccurate.
+  const std::string size_file = cache_dir_ + "/" + kCacheSizeFile;
+  off_t starting_cached_bytes =  obj_cache->gc_collect_total_objects_size()
+      + blob_cache->gc_collect_total_blobs_size() - this_runs_cached_bytes_;
+  if (file_overwrite_printf(size_file, "%ld\n", starting_cached_bytes) < 0) {
+    fb_error("writing cache size file failed");
+    exit(EXIT_FAILURE);
+  }
+  return starting_cached_bytes;
+}
+
 bool ExecedProcessCacher::is_gc_needed() const {
   return (get_stored_bytes_from_cache() + this_runs_cached_bytes_) > max_cache_size;
 }
--- a/src/firebuild/execed_process_cacher.h
+++ b/src/firebuild/execed_process_cacher.h
@@ -91,6 +91,10 @@
   void read_stored_cached_bytes();
   /** Store number of bytes cached to cachedir/size file. */
   void update_stored_bytes();
+  /**
+   * Fix number of bytes cached in cachedir/size file and return fixed value.
+   */
+  off_t fix_stored_bytes() const;
   /** Register cache size change occurred in the current run. */
   void update_cached_bytes(off_t bytes);
   /* A garbage collection run is needed, e.g. because the cache is too big. */