| 1 |
|
| /* po_manifest.h - which segments are current, crash-safe by construction. |
| 2 |
|
| * |
| 3 |
|
| * A block directory holds segments from N workers plus whatever compaction |
| 4 |
|
| * has produced. Something has to say which set is authoritative, and it has |
| 5 |
|
| * to survive being interrupted. |
| 6 |
|
| * |
| 7 |
|
| * The answer is an APPEND-ONLY file of generations. Each generation is one |
| 8 |
|
| * line: a number, a count, the segment names, and a terminator. The newest |
| 9 |
|
| * COMPLETE generation wins; a torn last line is ignored because it has no |
| 10 |
|
| * terminator. There is no lock, no rewrite, and no window in which the file |
| 11 |
|
| * is neither the old state nor the new one. |
| 12 |
|
| * |
| 13 |
|
| * The bus notification in phase 4 is an accelerant on top of this, not a |
| 14 |
|
| * substitute: losing a bus message costs a directory rescan, because THIS is |
| 15 |
|
| * the truth. |
| 16 |
|
| */ |
| 17 |
|
| #ifndef PO_MANIFEST_H |
| 18 |
|
| #define PO_MANIFEST_H |
| 19 |
|
| |
| 20 |
|
| #include "punk_observe/po_compat.h" |
| 21 |
|
| |
| 22 |
|
| #include <stdio.h> |
| 23 |
|
| #include <errno.h> |
| 24 |
|
| #ifndef _WIN32 |
| 25 |
|
| # include <unistd.h> |
| 26 |
|
| # include <fcntl.h> |
| 27 |
|
| #endif |
| 28 |
|
| |
| 29 |
|
| #define PO_MAN_EOL '\n' |
| 30 |
|
| #define PO_MAN_END "#\n" /* the terminator that makes a line whole */ |
| 31 |
|
| |
| 32 |
|
| /* Append a generation. Every name is written, then the terminator, then the |
| 33 |
|
| * whole thing is flushed - so a crash mid-append leaves a line without a |
| 34 |
|
| * terminator, which the reader skips. */ |
| 35 |
4 |
| static int po_manifest_append(const char *path, po_u64 gen, |
| 36 |
|
| const char *const *names, size_t n) { |
| 37 |
|
| int fd; |
| 38 |
|
| char buf[256]; |
| 39 |
|
| size_t i; |
| 40 |
|
| |
| 41 |
|
| #ifdef _WIN32 |
| 42 |
|
| fd = open(path, O_WRONLY | O_CREAT | O_APPEND | O_BINARY, 0644); |
| 43 |
|
| #else |
| 44 |
4 |
| fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); |
| 45 |
|
| #endif |
| 46 |
4 |
▶ | if (fd < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) return 0; |
0 |
4 |
4 |
|
| 47 |
|
| |
| 48 |
|
| { |
| 49 |
4 |
| int k = sprintf(buf, "g %llu %llu\n", |
| 50 |
|
| (unsigned long long)gen, (unsigned long long)n); |
| 51 |
4 |
▶ | if (write(fd, buf, (size_t)k) != k) { close(fd); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (write(fd, buf, (size_t)k) != k) { close(fd); return 0; } |
0 |
4 |
4 |
|
| 52 |
|
| } |
| 53 |
11 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
7 |
4 |
11 |
|
| 54 |
7 |
| size_t l = strlen(names[i]); |
| 55 |
7 |
▶ | if (write(fd, names[i], l) != (ssize_t)l) { close(fd); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (write(fd, names[i], l) != (ssize_t)l) { close(fd); return 0; } |
0 |
7 |
7 |
|
| 56 |
7 |
▶ | if (write(fd, "\n", 1) != 1) { close(fd); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (write(fd, "\n", 1) != 1) { close(fd); return 0; } |
0 |
7 |
7 |
|
| 57 |
|
| } |
| 58 |
4 |
▶ | if (write(fd, PO_MAN_END, 2) != 2) { close(fd); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (write(fd, PO_MAN_END, 2) != 2) { close(fd); return 0; } |
0 |
4 |
4 |
|
| 59 |
|
| #ifndef _WIN32 |
| 60 |
4 |
▶ | if (fsync(fd) != 0) { close(fd); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (fsync(fd) != 0) { close(fd); return 0; } |
0 |
4 |
4 |
|
| 61 |
|
| #endif |
| 62 |
4 |
| close(fd); |
| 63 |
4 |
| return 1; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| /* The newest complete generation. Returns its number, or 0 if there is none. |
| 67 |
|
| * `out` receives the offset and length of the names region within `buf`. */ |
| 68 |
15 |
| static po_u64 po_manifest_latest(const char *buf, size_t len, |
| 69 |
|
| size_t *names_off, size_t *names_len, |
| 70 |
|
| size_t *count) { |
| 71 |
15 |
| size_t i = 0; |
| 72 |
15 |
| po_u64 best = 0; |
| 73 |
15 |
| size_t best_off = 0, best_len = 0, best_n = 0; |
| 74 |
|
| |
| 75 |
32 |
▶ | while (i < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < len) { |
29 |
3 |
32 |
|
| 76 |
29 |
| size_t line_end = i; |
| 77 |
29 |
| po_u64 gen = 0, n = 0; |
| 78 |
29 |
| size_t body, j, seen = 0; |
| 79 |
29 |
| int complete = 0; |
| 80 |
|
| |
| 81 |
183 |
▶ | while (line_end < len && buf[line_end] != PO_MAN_EOL) line_end++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (line_end < len && buf[line_end] != PO_MAN_EOL) line_end++; |
183 |
0 |
183 |
| while (line_end < len && buf[line_end] != PO_MAN_EOL) line_end++; |
154 |
29 |
183 |
|
| 82 |
29 |
▶ | if (line_end >= len) break; /* torn header line */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (line_end >= len) break; /* torn header line */ |
0 |
29 |
29 |
|
| 83 |
|
| |
| 84 |
29 |
▶ | if (line_end - i < 3 || buf[i] != 'g') { i = line_end + 1; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (line_end - i < 3 || buf[i] != 'g') { i = line_end + 1; continue; } |
29 |
0 |
29 |
| if (line_end - i < 3 || buf[i] != 'g') { i = line_end + 1; continue; } |
3 |
26 |
29 |
|
| 85 |
|
| { |
| 86 |
26 |
| const char *p = buf + i + 2; |
| 87 |
52 |
▶ | while (p < buf + line_end && *p >= '0' && *p <= '9') |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < buf + line_end && *p >= '0' && *p <= '9') |
52 |
0 |
52 |
| while (p < buf + line_end && *p >= '0' && *p <= '9') |
26 |
26 |
52 |
| while (p < buf + line_end && *p >= '0' && *p <= '9') |
26 |
0 |
26 |
|
| 88 |
26 |
| { gen = gen * 10 + (po_u64)(*p - '0'); p++; } |
| 89 |
52 |
▶ | while (p < buf + line_end && *p == ' ') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < buf + line_end && *p == ' ') p++; |
52 |
0 |
52 |
| while (p < buf + line_end && *p == ' ') p++; |
26 |
26 |
52 |
|
| 90 |
52 |
▶ | while (p < buf + line_end && *p >= '0' && *p <= '9') |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < buf + line_end && *p >= '0' && *p <= '9') |
26 |
26 |
52 |
| while (p < buf + line_end && *p >= '0' && *p <= '9') |
26 |
0 |
26 |
| while (p < buf + line_end && *p >= '0' && *p <= '9') |
26 |
0 |
26 |
|
| 91 |
26 |
| { n = n * 10 + (po_u64)(*p - '0'); p++; } |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
26 |
| body = line_end + 1; |
| 95 |
26 |
| j = body; |
| 96 |
63 |
▶ | while (j < len && seen < n) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (j < len && seen < n) { |
61 |
2 |
63 |
| while (j < len && seen < n) { |
46 |
15 |
61 |
|
| 97 |
46 |
| size_t e = j; |
| 98 |
273 |
▶ | while (e < len && buf[e] != PO_MAN_EOL) e++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (e < len && buf[e] != PO_MAN_EOL) e++; |
264 |
9 |
273 |
| while (e < len && buf[e] != PO_MAN_EOL) e++; |
227 |
37 |
264 |
|
| 99 |
46 |
▶ | if (e >= len) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e >= len) break; |
9 |
37 |
46 |
|
| 100 |
37 |
| j = e + 1; |
| 101 |
37 |
| seen++; |
| 102 |
|
| } |
| 103 |
|
| /* Complete only if every promised name arrived AND the terminator |
| 104 |
|
| * follows. Either missing means the writer was interrupted. */ |
| 105 |
26 |
▶ | if (seen == n && j + 1 < len && buf[j] == '#' && buf[j + 1] == PO_MAN_EOL) |
Branch: 4 branches
| Branch | True | False | Total |
| if (seen == n && j + 1 < len && buf[j] == '#' && buf[j + 1] == PO_MAN_EOL) |
16 |
10 |
26 |
| if (seen == n && j + 1 < len && buf[j] == '#' && buf[j + 1] == PO_MAN_EOL) |
14 |
2 |
16 |
| if (seen == n && j + 1 < len && buf[j] == '#' && buf[j + 1] == PO_MAN_EOL) |
14 |
0 |
14 |
| if (seen == n && j + 1 < len && buf[j] == '#' && buf[j + 1] == PO_MAN_EOL) |
14 |
0 |
14 |
|
| 106 |
14 |
| complete = 1; |
| 107 |
|
| |
| 108 |
26 |
▶ | if (complete && gen >= best) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (complete && gen >= best) { |
14 |
12 |
26 |
| if (complete && gen >= best) { |
14 |
0 |
14 |
|
| 109 |
14 |
| best = gen; best_off = body; best_len = j - body; best_n = (size_t)n; |
| 110 |
|
| } |
| 111 |
26 |
▶ | if (!complete) break; /* the torn tail */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!complete) break; /* the torn tail */ |
12 |
14 |
26 |
|
| 112 |
14 |
| i = j + 2; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
15 |
▶ | if (names_off) *names_off = best_off; |
Branch: 1 branch
| Branch | True | False | Total |
| if (names_off) *names_off = best_off; |
15 |
0 |
15 |
|
| 116 |
15 |
▶ | if (names_len) *names_len = best_len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (names_len) *names_len = best_len; |
15 |
0 |
15 |
|
| 117 |
15 |
▶ | if (count) *count = best_n; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count) *count = best_n; |
15 |
0 |
15 |
|
| 118 |
15 |
| return best; |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
|
| #endif /* PO_MANIFEST_H */ |