| 1 |
|
| /* po_wal.h - the per-worker write-ahead log. |
| 2 |
|
| * |
| 3 |
|
| * This is the ingest path's only write, and the whole reason the compactor |
| 4 |
|
| * exists is so that it stays short: |
| 5 |
|
| * |
| 6 |
|
| * decode the body -> records + arena, one pass, no allocation |
| 7 |
|
| * apply the limits -> a rejected count for partial_success |
| 8 |
|
| * po_wal_append -> ONE writev of [header][records][arena] |
| 9 |
|
| * reply 200 |
| 10 |
|
| * |
| 11 |
|
| * One syscall for the data. Hyperman is syscall-bound - 73% of the request |
| 12 |
|
| * path is read plus writev - and this runs per ingested batch, so turning one |
| 13 |
|
| * writev into three writes shows up directly as CPU per megabyte. |
| 14 |
|
| * |
| 15 |
|
| * A WAL file is owned by exactly one worker and is append-only. Nobody locks |
| 16 |
|
| * it, because nobody else writes it. That is what phase 0 bought by choosing |
| 17 |
|
| * per-worker files over a dedicated writer. |
| 18 |
|
| */ |
| 19 |
|
| #ifndef PO_WAL_H |
| 20 |
|
| #define PO_WAL_H |
| 21 |
|
| |
| 22 |
|
| #include "punk_observe/po_compat.h" |
| 23 |
|
| #include "punk_observe/po_rec.h" |
| 24 |
|
| #include "punk_observe/po_time.h" |
| 25 |
|
| #include "punk_observe/po_crc32c.h" |
| 26 |
|
| |
| 27 |
|
| #include <stdio.h> |
| 28 |
|
| #include <errno.h> |
| 29 |
|
| #ifndef _WIN32 |
| 30 |
|
| # include <unistd.h> |
| 31 |
|
| # include <fcntl.h> |
| 32 |
|
| # include <sys/uio.h> |
| 33 |
|
| # include <sys/stat.h> |
| 34 |
|
| #else |
| 35 |
|
| # include <io.h> |
| 36 |
|
| # include <fcntl.h> |
| 37 |
|
| #endif |
| 38 |
|
| |
| 39 |
|
| #define PO_WAL_MAGIC 0x4C574F4Fu /* "OOWL" little-endian */ |
| 40 |
|
| |
| 41 |
|
| /* Version 2 carries the WHOLE record. Version 1 wrote a po_rec whose only |
| 42 |
|
| * populated fields were t_unix_nano, kind and the body offsets: severity, the |
| 43 |
|
| * trace and span ids, the duration, the value and the attribute block were |
| 44 |
|
| * all zeroed on the way in. A log line written by a v1 writer cannot be |
| 45 |
|
| * filtered on severity or joined to its trace, because those bytes were never |
| 46 |
|
| * stored - so a v1 frame is REFUSED at replay rather than read into records |
| 47 |
|
| * that are quietly empty. */ |
| 48 |
|
| #define PO_WAL_VERSION 2 |
| 49 |
|
| |
| 50 |
|
| /* frame flags */ |
| 51 |
|
| #define PO_WAL_F_SEALED 0x0001 /* the trailer: no more frames follow */ |
| 52 |
|
| |
| 53 |
|
| /* fsync policy */ |
| 54 |
|
| #define PO_FSYNC_NEVER 0 |
| 55 |
|
| #define PO_FSYNC_INTERVAL 1 /* the default */ |
| 56 |
|
| #define PO_FSYNC_ALWAYS 2 |
| 57 |
|
| |
| 58 |
|
| /* Every field little-endian on disk. The READER byte-swaps, not the writer, |
| 59 |
|
| * because the writer is the hot path and every box that matters is already |
| 60 |
|
| * little-endian. */ |
| 61 |
|
| typedef struct { |
| 62 |
|
| uint32_t magic; |
| 63 |
|
| uint32_t frame_len; /* bytes following this header */ |
| 64 |
|
| uint32_t n_recs; |
| 65 |
|
| uint32_t arena_len; |
| 66 |
|
| po_u64 t_min; /* the frame's timestamp span, so that a reader */ |
| 67 |
|
| po_u64 t_max; /* can skip a frame without scanning it */ |
| 68 |
|
| uint32_t crc; /* CRC-32C over records + arena, padding incl. */ |
| 69 |
|
| uint16_t flags; |
| 70 |
|
| uint16_t version; |
| 71 |
|
| } po_wal_frame; |
| 72 |
|
| |
| 73 |
|
| #define PO_WAL_HDR 40 /* asserted in t/0004-wal.t, not assumed */ |
| 74 |
|
| |
| 75 |
|
| typedef struct { |
| 76 |
|
| int fd; |
| 77 |
|
| po_u64 last_fsync_ns; |
| 78 |
|
| int policy; |
| 79 |
|
| po_u64 interval_ns; |
| 80 |
|
| /* Counters, so the tests assert the policy by COUNTING rather than by |
| 81 |
|
| * timing. A timing assertion on a loaded smoker is a flake. */ |
| 82 |
|
| po_u64 frames_written; |
| 83 |
|
| po_u64 bytes_written; |
| 84 |
|
| po_u64 fsyncs; |
| 85 |
|
| int sealed; |
| 86 |
|
| } po_wal; |
| 87 |
|
| |
| 88 |
234 |
| static int po_wal_open(po_wal *w, const char *path, int policy, |
| 89 |
|
| po_u64 interval_ns) { |
| 90 |
|
| int fd; |
| 91 |
234 |
| memset(w, 0, sizeof(*w)); |
| 92 |
|
| #ifdef _WIN32 |
| 93 |
|
| fd = open(path, O_WRONLY | O_CREAT | O_APPEND | O_BINARY, 0644); |
| 94 |
|
| #else |
| 95 |
234 |
| fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); |
| 96 |
|
| #endif |
| 97 |
234 |
▶ | if (fd < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) return 0; |
0 |
234 |
234 |
|
| 98 |
234 |
| w->fd = fd; |
| 99 |
234 |
| w->policy = policy; |
| 100 |
234 |
▶ | w->interval_ns = interval_ns ? interval_ns : 200 * PO_NS_PER_MSEC; |
Branch: 1 branch
| Branch | True | False | Total |
| w->interval_ns = interval_ns ? interval_ns : 200 * PO_NS_PER_MSEC; |
23 |
211 |
234 |
|
| 101 |
234 |
| w->last_fsync_ns = po_tick_ns(); |
| 102 |
234 |
| return 1; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
234 |
| static void po_wal_close(po_wal *w) { |
| 106 |
234 |
▶ | if (w->fd >= 0) { close(w->fd); w->fd = -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->fd >= 0) { close(w->fd); w->fd = -1; } |
234 |
0 |
234 |
|
| 107 |
234 |
| } |
| 108 |
|
| |
| 109 |
|
| /* Write all of a buffer, looping on a short write. |
| 110 |
|
| * |
| 111 |
|
| * A partial write is legal on a signal, and writev additionally caps at |
| 112 |
|
| * IOV_MAX iovecs (1024 on Linux, 16 on some BSDs). Leaving a frame |
| 113 |
|
| * half-written is fine ONLY because the length prefix and the CRC let a |
| 114 |
|
| * reader detect it; it is never fine to stop and pretend it succeeded. */ |
| 115 |
92 |
| static int po_write_all(int fd, const char *p, size_t n) { |
| 116 |
184 |
▶ | while (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n) { |
92 |
92 |
184 |
|
| 117 |
92 |
| ssize_t k = write(fd, p, n); |
| 118 |
92 |
▶ | if (k < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (k < 0) { |
0 |
92 |
92 |
|
| 119 |
0 |
▶ | if (errno == EINTR) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == EINTR) continue; |
0 |
0 |
0 |
|
| 120 |
0 |
| return 0; |
| 121 |
|
| } |
| 122 |
92 |
▶ | if (k == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == 0) return 0; |
0 |
92 |
92 |
|
| 123 |
92 |
| p += (size_t)k; n -= (size_t)k; |
| 124 |
|
| } |
| 125 |
92 |
| return 1; |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
132 |
| static int po_wal_do_fsync(po_wal *w) { |
| 129 |
|
| #ifdef _WIN32 |
| 130 |
|
| if (_commit(w->fd) != 0) return 0; |
| 131 |
|
| #elif defined(F_FULLFSYNC) |
| 132 |
|
| /* macOS: fsync(2) does NOT flush the drive's own write cache, so a power |
| 133 |
|
| * cut can still lose it. F_FULLFSYNC is the one that means it. */ |
| 134 |
|
| if (fcntl(w->fd, F_FULLFSYNC, 0) < 0 && fsync(w->fd) != 0) return 0; |
| 135 |
|
| #else |
| 136 |
132 |
▶ | if (fdatasync(w->fd) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fdatasync(w->fd) != 0) return 0; |
0 |
132 |
132 |
|
| 137 |
|
| #endif |
| 138 |
132 |
| w->fsyncs++; |
| 139 |
132 |
| w->last_fsync_ns = po_tick_ns(); |
| 140 |
132 |
| return 1; |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
|
| /* Append one frame. Returns 1, or 0 with errno set. |
| 144 |
|
| * |
| 145 |
|
| * An empty batch writes NOTHING. A frame with zero records would carry |
| 146 |
|
| * t_min = 0 and t_max = UINT64_MAX or similar nonsense, and a reader that |
| 147 |
|
| * prunes on those skips or opens exactly the wrong frames. A body that |
| 148 |
|
| * decodes to no records is a 200 and no append. */ |
| 149 |
789 |
| static int po_wal_append(po_wal *w, const po_rec *recs, size_t n, |
| 150 |
|
| const char *arena, size_t arena_len) { |
| 151 |
|
| po_wal_frame h; |
| 152 |
|
| char hdr[PO_WAL_HDR]; |
| 153 |
789 |
| po_u64 t_min = PO_U64_MAX, t_max = 0; |
| 154 |
|
| size_t i, reclen; |
| 155 |
789 |
| uint32_t crc = 0; |
| 156 |
|
| static const char pad[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 157 |
|
| size_t padlen; |
| 158 |
|
| |
| 159 |
789 |
▶ | if (n == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return 1; |
0 |
789 |
789 |
|
| 160 |
789 |
▶ | if (w->sealed) { errno = EPERM; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->sealed) { errno = EPERM; return 0; } |
0 |
789 |
789 |
|
| 161 |
|
| |
| 162 |
13961 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
13172 |
789 |
13961 |
|
| 163 |
13172 |
| po_u64 t = recs[i].t_unix_nano; |
| 164 |
13172 |
▶ | if (t < t_min) t_min = t; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t < t_min) t_min = t; |
790 |
12382 |
13172 |
|
| 165 |
13172 |
▶ | if (t > t_max) t_max = t; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t > t_max) t_max = t; |
12625 |
547 |
13172 |
|
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
789 |
| reclen = n * sizeof(po_rec); |
| 169 |
|
| /* Pad the arena to an 8-byte boundary so the next frame header is |
| 170 |
|
| * aligned. The padding is ZEROED and is INSIDE the CRC, or a reader |
| 171 |
|
| * recomputing the checksum over what it read disagrees with the writer. */ |
| 172 |
789 |
| padlen = (8 - (arena_len & 7)) & 7; |
| 173 |
|
| |
| 174 |
789 |
| crc = po_crc32c(0, recs, reclen); |
| 175 |
789 |
▶ | if (arena_len) crc = po_crc32c(crc, arena, arena_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (arena_len) crc = po_crc32c(crc, arena, arena_len); |
129 |
660 |
789 |
|
| 176 |
789 |
▶ | if (padlen) crc = po_crc32c(crc, pad, padlen); |
Branch: 1 branch
| Branch | True | False | Total |
| if (padlen) crc = po_crc32c(crc, pad, padlen); |
102 |
687 |
789 |
|
| 177 |
|
| |
| 178 |
789 |
| h.magic = PO_WAL_MAGIC; |
| 179 |
789 |
| h.frame_len = (uint32_t)(reclen + arena_len + padlen); |
| 180 |
789 |
| h.n_recs = (uint32_t)n; |
| 181 |
789 |
| h.arena_len = (uint32_t)arena_len; |
| 182 |
789 |
| h.t_min = t_min; |
| 183 |
789 |
| h.t_max = t_max; |
| 184 |
789 |
| h.crc = crc; |
| 185 |
789 |
| h.flags = 0; |
| 186 |
789 |
| h.version = PO_WAL_VERSION; |
| 187 |
|
| |
| 188 |
|
| { /* Serialise the header field by field rather than writing the struct, |
| 189 |
|
| * so the on-disk layout is this code's decision and not the |
| 190 |
|
| * compiler's padding. */ |
| 191 |
789 |
| char *q = hdr; |
| 192 |
|
| uint32_t u32; po_u64 u64; uint16_t u16; |
| 193 |
|
| #define PUT32(v) do { u32 = po_le32(v); memcpy(q, &u32, 4); q += 4; } while (0) |
| 194 |
|
| #define PUT64(v) do { u64 = po_le64(v); memcpy(q, &u64, 8); q += 8; } while (0) |
| 195 |
|
| #define PUT16(v) do { u16 = (uint16_t)(v); \ |
| 196 |
|
| q[0] = (char)(u16 & 0xFF); q[1] = (char)((u16 >> 8) & 0xFF); \ |
| 197 |
|
| q += 2; } while (0) |
| 198 |
789 |
| PUT32(h.magic); PUT32(h.frame_len); PUT32(h.n_recs); PUT32(h.arena_len); |
| 199 |
789 |
| PUT64(h.t_min); PUT64(h.t_max); |
| 200 |
789 |
| PUT32(h.crc); PUT16(h.flags); PUT16(h.version); |
| 201 |
|
| #undef PUT32 |
| 202 |
|
| #undef PUT64 |
| 203 |
|
| #undef PUT16 |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
|
| #ifndef _WIN32 |
| 207 |
|
| { /* One writev. Four iovecs at most, so IOV_MAX is never in question - |
| 208 |
|
| * the records and the arena are each ONE contiguous buffer by |
| 209 |
|
| * construction, which is what po_rec's offsets-not-pointers rule and |
| 210 |
|
| * the arena bought. */ |
| 211 |
|
| struct iovec iov[4]; |
| 212 |
789 |
| int niov = 0; |
| 213 |
789 |
| size_t total = PO_WAL_HDR + h.frame_len; |
| 214 |
|
| ssize_t k; |
| 215 |
|
| |
| 216 |
789 |
| iov[niov].iov_base = hdr; iov[niov].iov_len = PO_WAL_HDR; niov++; |
| 217 |
789 |
| iov[niov].iov_base = (void *)recs; iov[niov].iov_len = reclen; niov++; |
| 218 |
789 |
▶ | if (arena_len) { iov[niov].iov_base = (void *)arena; |
Branch: 1 branch
| Branch | True | False | Total |
| if (arena_len) { iov[niov].iov_base = (void *)arena; |
129 |
660 |
789 |
|
| 219 |
129 |
| iov[niov].iov_len = arena_len; niov++; } |
| 220 |
789 |
▶ | if (padlen) { iov[niov].iov_base = (void *)pad; |
Branch: 1 branch
| Branch | True | False | Total |
| if (padlen) { iov[niov].iov_base = (void *)pad; |
102 |
687 |
789 |
|
| 221 |
102 |
| iov[niov].iov_len = padlen; niov++; } |
| 222 |
|
| |
| 223 |
|
| for (;;) { |
| 224 |
789 |
| k = writev(w->fd, iov, niov); |
| 225 |
789 |
▶ | if (k < 0 && errno == EINTR) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (k < 0 && errno == EINTR) continue; |
0 |
789 |
789 |
| if (k < 0 && errno == EINTR) continue; |
0 |
0 |
0 |
|
| 226 |
789 |
▶ | if (k < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k < 0) return 0; |
0 |
789 |
789 |
|
| 227 |
789 |
▶ | if ((size_t)k == total) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)k == total) break; |
789 |
0 |
789 |
|
| 228 |
|
| /* Short write: finish the remainder with ordinary writes rather |
| 229 |
|
| * than rebuilding the iovec array. Rare, and correctness beats |
| 230 |
|
| * cleverness on a path that only runs when something odd |
| 231 |
|
| * happened. */ |
| 232 |
|
| { |
| 233 |
0 |
| size_t done = (size_t)k, off = 0; |
| 234 |
|
| int j; |
| 235 |
0 |
▶ | for (j = 0; j < niov; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < niov; j++) { |
0 |
0 |
0 |
|
| 236 |
0 |
| size_t len = iov[j].iov_len; |
| 237 |
0 |
▶ | if (done >= off + len) { off += len; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (done >= off + len) { off += len; continue; } |
0 |
0 |
0 |
|
| 238 |
|
| { |
| 239 |
0 |
▶ | size_t skip = done > off ? done - off : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t skip = done > off ? done - off : 0; |
0 |
0 |
0 |
|
| 240 |
0 |
▶ | if (!po_write_all(w->fd, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_write_all(w->fd, |
0 |
0 |
0 |
|
| 241 |
0 |
| (const char *)iov[j].iov_base + skip, |
| 242 |
0 |
| len - skip)) return 0; |
| 243 |
|
| } |
| 244 |
0 |
| off += len; |
| 245 |
|
| } |
| 246 |
0 |
| break; |
| 247 |
|
| } |
| 248 |
|
| } |
| 249 |
789 |
| w->bytes_written += total; |
| 250 |
|
| } |
| 251 |
|
| #else |
| 252 |
|
| if (!po_write_all(w->fd, hdr, PO_WAL_HDR)) return 0; |
| 253 |
|
| if (!po_write_all(w->fd, (const char *)recs, reclen)) return 0; |
| 254 |
|
| if (arena_len && !po_write_all(w->fd, arena, arena_len)) return 0; |
| 255 |
|
| if (padlen && !po_write_all(w->fd, pad, padlen)) return 0; |
| 256 |
|
| w->bytes_written += PO_WAL_HDR + h.frame_len; |
| 257 |
|
| #endif |
| 258 |
|
| |
| 259 |
789 |
| w->frames_written++; |
| 260 |
|
| |
| 261 |
|
| /* Durability policy. What a 200 MEANS is decided here, and the POD says |
| 262 |
|
| * so rather than implying the default is safe in general: |
| 263 |
|
| * |
| 264 |
|
| * always - flushed before the caller replies. Survives a power cut. |
| 265 |
|
| * interval - flushed on a timer (default 200ms). A process crash loses |
| 266 |
|
| * nothing, because the write already reached the page cache; |
| 267 |
|
| * a POWER CUT can lose up to the interval. |
| 268 |
|
| * never - the OS decides. |
| 269 |
|
| * |
| 270 |
|
| * The default is `interval` because of what the data IS. Losing 200ms of |
| 271 |
|
| * telemetry on a power cut is an acceptable trade in a way that losing |
| 272 |
|
| * 200ms of a job queue is not, and Punk::Queue correctly chooses the |
| 273 |
|
| * other way. */ |
| 274 |
789 |
▶ | if (w->policy == PO_FSYNC_ALWAYS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->policy == PO_FSYNC_ALWAYS) { |
20 |
769 |
789 |
|
| 275 |
20 |
▶ | if (!po_wal_do_fsync(w)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_wal_do_fsync(w)) return 0; |
0 |
20 |
20 |
|
| 276 |
|
| } |
| 277 |
769 |
▶ | else if (w->policy == PO_FSYNC_INTERVAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (w->policy == PO_FSYNC_INTERVAL) { |
91 |
678 |
769 |
|
| 278 |
91 |
| po_u64 now = po_tick_ns(); |
| 279 |
91 |
▶ | if (now - w->last_fsync_ns >= w->interval_ns) |
Branch: 1 branch
| Branch | True | False | Total |
| if (now - w->last_fsync_ns >= w->interval_ns) |
20 |
71 |
91 |
|
| 280 |
20 |
▶ | if (!po_wal_do_fsync(w)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_wal_do_fsync(w)) return 0; |
0 |
20 |
20 |
|
| 281 |
|
| } |
| 282 |
789 |
| return 1; |
| 283 |
|
| } |
| 284 |
|
| |
| 285 |
|
| /* Seal. The trailer is a zero-record frame with PO_WAL_F_SEALED and the |
| 286 |
|
| * file's total record count in n_recs - the one frame allowed to have no |
| 287 |
|
| * records, because it carries no timestamps to prune on. |
| 288 |
|
| * |
| 289 |
|
| * The compactor only consumes SEALED files, so it never races a writer. */ |
| 290 |
92 |
| static int po_wal_seal(po_wal *w, po_u64 total_records) { |
| 291 |
|
| char hdr[PO_WAL_HDR]; |
| 292 |
92 |
| char *q = hdr; |
| 293 |
|
| uint32_t u32; po_u64 u64; |
| 294 |
|
| |
| 295 |
92 |
▶ | if (w->sealed) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->sealed) return 1; |
0 |
92 |
92 |
|
| 296 |
|
| #define PUT32(v) do { u32 = po_le32(v); memcpy(q, &u32, 4); q += 4; } while (0) |
| 297 |
|
| #define PUT64(v) do { u64 = po_le64(v); memcpy(q, &u64, 8); q += 8; } while (0) |
| 298 |
92 |
| PUT32(PO_WAL_MAGIC); PUT32(0); |
| 299 |
92 |
| PUT32((uint32_t)total_records); PUT32(0); |
| 300 |
92 |
| PUT64(0); PUT64(0); |
| 301 |
92 |
| PUT32(0); |
| 302 |
|
| #undef PUT32 |
| 303 |
|
| #undef PUT64 |
| 304 |
92 |
| q[0] = (char)(PO_WAL_F_SEALED & 0xFF); |
| 305 |
92 |
| q[1] = (char)((PO_WAL_F_SEALED >> 8) & 0xFF); |
| 306 |
92 |
| q[2] = (char)(PO_WAL_VERSION & 0xFF); |
| 307 |
92 |
| q[3] = (char)((PO_WAL_VERSION >> 8) & 0xFF); |
| 308 |
|
| |
| 309 |
92 |
▶ | if (!po_write_all(w->fd, hdr, PO_WAL_HDR)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_write_all(w->fd, hdr, PO_WAL_HDR)) return 0; |
0 |
92 |
92 |
|
| 310 |
92 |
▶ | if (!po_wal_do_fsync(w)) return 0; /* a seal is always durable */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_wal_do_fsync(w)) return 0; /* a seal is always durable */ |
0 |
92 |
92 |
|
| 311 |
92 |
| w->sealed = 1; |
| 312 |
92 |
| return 1; |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
|
| /* ---- replay -------------------------------------------------------------- */ |
| 316 |
|
| |
| 317 |
|
| typedef struct { |
| 318 |
|
| po_u64 frames; |
| 319 |
|
| po_u64 records; |
| 320 |
|
| po_u64 bytes_ok; /* bytes of complete, verified frames */ |
| 321 |
|
| po_u64 bytes_truncated; /* what was left over and could not be read */ |
| 322 |
|
| int sealed; |
| 323 |
|
| int stopped_reason; /* PO_REPLAY_* */ |
| 324 |
|
| } po_wal_replay; |
| 325 |
|
| |
| 326 |
|
| #define PO_REPLAY_EOF 0 /* clean end of file */ |
| 327 |
|
| #define PO_REPLAY_SHORT 1 /* a partial frame at the tail */ |
| 328 |
|
| #define PO_REPLAY_CRC 2 /* a frame that did not verify */ |
| 329 |
|
| #define PO_REPLAY_MAGIC 3 /* not a frame header at all */ |
| 330 |
|
| #define PO_REPLAY_SEALED 4 /* hit the seal trailer */ |
| 331 |
|
| #define PO_REPLAY_VERSION 5 /* a frame this build cannot read */ |
| 332 |
|
| |
| 333 |
|
| typedef void (*po_replay_cb)(void *ud, const po_rec *recs, size_t n, |
| 334 |
|
| const char *arena, size_t arena_len); |
| 335 |
|
| |
| 336 |
|
| /* Replay a WAL from a buffer (the caller mmaps or slurps it). |
| 337 |
|
| * |
| 338 |
|
| * THE RULE: this reads frames until one fails its magic, its bounds or its |
| 339 |
|
| * CRC, then STOPS and reports how far it got. That is a success, not an |
| 340 |
|
| * error. |
| 341 |
|
| * |
| 342 |
|
| * A WAL being appended to is SUPPOSED to have a ragged end, and a crash |
| 343 |
|
| * guarantees one. A recovery path that errors on a truncated tail turns "the |
| 344 |
|
| * last 40ms did not make it" into "the server will not start" - the data was |
| 345 |
|
| * already lost, and now the service is too. That is the wrong-way failure, |
| 346 |
|
| * and it is why bytes_truncated is reported and logged rather than raised. */ |
| 347 |
3351 |
| static void po_wal_replay_buf(const char *buf, size_t len, |
| 348 |
|
| po_wal_replay *out, |
| 349 |
|
| po_replay_cb cb, void *ud) { |
| 350 |
3351 |
| size_t off = 0; |
| 351 |
3351 |
| memset(out, 0, sizeof(*out)); |
| 352 |
|
| |
| 353 |
25722 |
| for (;;) { |
| 354 |
|
| uint32_t magic, frame_len, n_recs, arena_len, crc, want; |
| 355 |
|
| uint16_t flags, version; |
| 356 |
|
| po_u64 t_min, t_max; |
| 357 |
|
| const char *h; |
| 358 |
|
| size_t reclen, padlen; |
| 359 |
|
| |
| 360 |
29073 |
▶ | if (len - off < PO_WAL_HDR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len - off < PO_WAL_HDR) { |
1130 |
27943 |
29073 |
|
| 361 |
1130 |
| out->stopped_reason = (len - off == 0) ? PO_REPLAY_EOF : PO_REPLAY_SHORT; |
| 362 |
1130 |
| out->bytes_truncated = (po_u64)(len - off); |
| 363 |
3351 |
| return; |
| 364 |
|
| } |
| 365 |
27943 |
| h = buf + off; |
| 366 |
27943 |
| memcpy(&magic, h, 4); magic = po_le32(magic); |
| 367 |
27943 |
▶ | if (magic != PO_WAL_MAGIC) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (magic != PO_WAL_MAGIC) { |
0 |
27943 |
27943 |
|
| 368 |
0 |
| out->stopped_reason = PO_REPLAY_MAGIC; |
| 369 |
0 |
| out->bytes_truncated = (po_u64)(len - off); |
| 370 |
0 |
| return; |
| 371 |
|
| } |
| 372 |
27943 |
| memcpy(&frame_len, h + 4, 4); frame_len = po_le32(frame_len); |
| 373 |
27943 |
| memcpy(&n_recs, h + 8, 4); n_recs = po_le32(n_recs); |
| 374 |
27943 |
| memcpy(&arena_len, h + 12, 4); arena_len = po_le32(arena_len); |
| 375 |
27943 |
| memcpy(&t_min, h + 16, 8); t_min = po_le64(t_min); |
| 376 |
27943 |
| memcpy(&t_max, h + 24, 8); t_max = po_le64(t_max); |
| 377 |
27943 |
| memcpy(&crc, h + 32, 4); crc = po_le32(crc); |
| 378 |
27943 |
| flags = (uint16_t)((unsigned char)h[36] | ((unsigned char)h[37] << 8)); |
| 379 |
27943 |
| version = (uint16_t)((unsigned char)h[38] | ((unsigned char)h[39] << 8)); |
| 380 |
|
| (void)t_min; (void)t_max; |
| 381 |
|
| |
| 382 |
|
| /* Checked BEFORE the frame is read, and before the seal trailer is |
| 383 |
|
| * honoured. A version this build does not know is not a ragged tail: |
| 384 |
|
| * the bytes are intact and mean something else, so reading them as |
| 385 |
|
| * po_rec would hand back records whose fields are whatever the older |
| 386 |
|
| * writer happened to leave there. Stopping is the honest answer. */ |
| 387 |
27943 |
▶ | if (version != PO_WAL_VERSION) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (version != PO_WAL_VERSION) { |
3 |
27940 |
27943 |
|
| 388 |
3 |
| out->stopped_reason = PO_REPLAY_VERSION; |
| 389 |
3 |
| out->bytes_truncated = (po_u64)(len - off); |
| 390 |
3 |
| return; |
| 391 |
|
| } |
| 392 |
|
| |
| 393 |
27940 |
▶ | if (flags & PO_WAL_F_SEALED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & PO_WAL_F_SEALED) { |
452 |
27488 |
27940 |
|
| 394 |
452 |
| out->sealed = 1; |
| 395 |
452 |
| out->stopped_reason = PO_REPLAY_SEALED; |
| 396 |
452 |
| out->bytes_ok += PO_WAL_HDR; |
| 397 |
452 |
| return; |
| 398 |
|
| } |
| 399 |
|
| |
| 400 |
27488 |
▶ | if (len - off - PO_WAL_HDR < (size_t)frame_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len - off - PO_WAL_HDR < (size_t)frame_len) { |
1764 |
25724 |
27488 |
|
| 401 |
1764 |
| out->stopped_reason = PO_REPLAY_SHORT; |
| 402 |
1764 |
| out->bytes_truncated = (po_u64)(len - off); |
| 403 |
1764 |
| return; |
| 404 |
|
| } |
| 405 |
|
| |
| 406 |
25724 |
| reclen = (size_t)n_recs * sizeof(po_rec); |
| 407 |
25724 |
| padlen = (8 - (arena_len & 7)) & 7; |
| 408 |
|
| /* The header's own arithmetic must agree with itself before a single |
| 409 |
|
| * byte of it is trusted. A frame_len that does not equal |
| 410 |
|
| * records + arena + padding is a corrupt header, not a long frame. */ |
| 411 |
25724 |
▶ | if (reclen + arena_len + padlen != (size_t)frame_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (reclen + arena_len + padlen != (size_t)frame_len) { |
1 |
25723 |
25724 |
|
| 412 |
1 |
| out->stopped_reason = PO_REPLAY_CRC; |
| 413 |
1 |
| out->bytes_truncated = (po_u64)(len - off); |
| 414 |
1 |
| return; |
| 415 |
|
| } |
| 416 |
|
| |
| 417 |
25723 |
| want = po_crc32c(0, h + PO_WAL_HDR, (size_t)frame_len); |
| 418 |
25723 |
▶ | if (want != crc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (want != crc) { |
1 |
25722 |
25723 |
|
| 419 |
|
| /* Stop HERE. Not "skip this frame and try the next": a bad CRC |
| 420 |
|
| * means the bytes are not what was written, so the length that |
| 421 |
|
| * would find the next frame is not trustworthy either. */ |
| 422 |
1 |
| out->stopped_reason = PO_REPLAY_CRC; |
| 423 |
1 |
| out->bytes_truncated = (po_u64)(len - off); |
| 424 |
1 |
| return; |
| 425 |
|
| } |
| 426 |
|
| |
| 427 |
25722 |
▶ | if (cb) cb(ud, (const po_rec *)(h + PO_WAL_HDR), (size_t)n_recs, |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb) cb(ud, (const po_rec *)(h + PO_WAL_HDR), (size_t)n_recs, |
0 |
25722 |
25722 |
|
| 428 |
0 |
| h + PO_WAL_HDR + reclen, (size_t)arena_len); |
| 429 |
|
| |
| 430 |
25722 |
| out->frames++; |
| 431 |
25722 |
| out->records += n_recs; |
| 432 |
25722 |
| out->bytes_ok += PO_WAL_HDR + frame_len; |
| 433 |
25722 |
| off += PO_WAL_HDR + frame_len; |
| 434 |
|
| } |
| 435 |
|
| } |
| 436 |
|
| |
| 437 |
2573 |
| static const char *po_replay_reason(int r) { |
| 438 |
2573 |
| switch (r) { |
| 439 |
25 |
| case PO_REPLAY_EOF: return "eof"; |
| 440 |
2544 |
| case PO_REPLAY_SHORT: return "short"; |
| 441 |
2 |
| case PO_REPLAY_CRC: return "crc"; |
| 442 |
0 |
| case PO_REPLAY_MAGIC: return "magic"; |
| 443 |
1 |
| case PO_REPLAY_SEALED: return "sealed"; |
| 444 |
1 |
| case PO_REPLAY_VERSION: return "version"; |
| 445 |
0 |
| default: return "unknown"; |
| 446 |
|
| } |
| 447 |
|
| } |
| 448 |
|
| |
| 449 |
|
| #endif /* PO_WAL_H */ |