-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcleanup.cpp
More file actions
447 lines (415 loc) · 14.3 KB
/
Copy pathcleanup.cpp
File metadata and controls
447 lines (415 loc) · 14.3 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
hourly session-log cleanup worker (covers .tlog and .bin)
*/
#include "cleanup.h"
#include "keydb.h"
#include <algorithm>
#include <errno.h>
#include <dirent.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <vector>
#include <string>
#include <tdb.h>
off_t port2_quota_bytes(void)
{
static off_t cached = -1;
if (cached >= 0) {
return cached;
}
cached = off_t(1024) * 1024 * 1024; // 1 GiB default
const char *env = getenv("SUPPORTPROXY_PORT2_QUOTA_BYTES");
if (env != nullptr && *env != '\0') {
// strict: plain positive bytes only. A prefix parse would turn
// a well-meant "1GB" into a 1-byte quota and let the cleanup
// pass delete nearly the whole log tree.
char *endp = nullptr;
errno = 0;
long long v = strtoll(env, &endp, 10);
if (errno == 0 && endp != env && *endp == '\0' && v > 0) {
cached = off_t(v);
} else {
::printf("ignoring invalid SUPPORTPROXY_PORT2_QUOTA_BYTES "
"'%s' (want plain bytes); using %lld\n",
env, (long long)cached);
}
}
return cached;
}
static off_t parse_quota_env(const char *name, off_t dflt)
{
const char *env = getenv(name);
if (env == nullptr || *env == '\0') {
return dflt;
}
// strict: plain positive bytes only. A prefix parse would turn a
// well-meant "1GB" into a 1-byte quota and let the cleanup pass
// delete nearly the whole log tree.
char *endp = nullptr;
errno = 0;
long long v = strtoll(env, &endp, 10);
if (errno == 0 && endp != env && *endp == '\0' && v > 0) {
return off_t(v);
}
::printf("ignoring invalid %s '%s' (want plain bytes); using %lld\n",
name, env, (long long)dflt);
return dflt;
}
off_t port2_video_quota_bytes(void)
{
static off_t cached = -1;
if (cached < 0) {
cached = parse_quota_env("SUPPORTPROXY_PORT2_VIDEO_QUOTA_BYTES",
off_t(4) * 1024 * 1024 * 1024);
}
return cached;
}
bool video_have_free_space(const char *base_dir)
{
struct statvfs vfs;
if (statvfs(base_dir, &vfs) != 0) {
return true; // can't tell; don't block recording on it
}
const uint64_t free_bytes = uint64_t(vfs.f_bavail) * vfs.f_frsize;
const uint64_t total = uint64_t(vfs.f_blocks) * vfs.f_frsize;
const uint64_t floor_abs = uint64_t(2) * 1024 * 1024 * 1024;
const uint64_t floor_pct = total / 20; // 5%
const uint64_t want = floor_abs > floor_pct ? floor_abs : floor_pct;
return free_bytes > want;
}
namespace {
struct PassCtx {
const char *base_dir;
time_t now;
};
/*
What kind of session file this is.
Retention treats both kinds identically -- one per-entry setting
covers everything -- but the quota does not: video and telemetry get
independent budgets, because a shared pool sorted by mtime would let
a few minutes of video evict a whole flight's telemetry.
*/
enum session_kind {
SESSION_NONE = 0,
SESSION_TELEM, // .tlog, .bin
SESSION_VIDEO, // .vN.ts
};
static session_kind session_file_kind(const char *name)
{
const size_t n = strlen(name);
if (n > 5 && strcmp(name + n - 5, ".tlog") == 0) {
return SESSION_TELEM;
}
if (n > 4 && strcmp(name + n - 4, ".bin") == 0) {
return SESSION_TELEM;
}
// "<session>.v<slot>.ts" -- the slot is part of the name so the
// three slots of one entry never collide.
if (n > 6 && strcmp(name + n - 3, ".ts") == 0
&& name[n - 6] == '.' && name[n - 5] == 'v'
&& name[n - 4] >= '1' && name[n - 4] <= '9') {
return SESSION_VIDEO;
}
return SESSION_NONE;
}
static bool is_session_file(const char *name)
{
return session_file_kind(name) != SESSION_NONE;
}
/*
Enforce the per-port2 disk quota by deleting oldest session files
until total bytes <= MAX_PER_PORT2_BYTES. Runs after the retention
pass so the operator's per-entry retention dictates *when* a file
can go; this dictates *whether one must go anyway* because we're
about to overflow. Walks every date dir under logs/<port2>/, sorts
files by mtime ascending, deletes from the head.
*/
// Files whose mtime is within this window are treated as belonging to
// a live session and are never deleted by the quota pass: on Linux the
// unlink would succeed while the writer keeps appending to an
// invisible unlinked inode — the log is lost on close and the disk
// usage stops being counted.
//
// mtime is a heuristic, not ownership: the hourly pass runs in the
// cleanup child and cannot know which files other children hold open.
// An open file idle for longer than the grace (a stalled stream) can
// still be unlinked, and a just-closed session is protected slightly
// longer than needed. Both are acceptable: a healthy binlog/tlog
// writes many times per second.
// Overridable for tests: with the default 60s, every segment a short
// test writes is still "live" and none is evictable, so the quota pass
// correctly frees nothing and the behaviour cannot be observed at all.
static time_t active_file_grace_s(void)
{
static time_t cached = -1;
if (cached >= 0) {
return cached;
}
cached = 60;
const char *env = getenv("SUPPORTPROXY_ACTIVE_FILE_GRACE");
if (env != nullptr && *env != '\0') {
char *endp = nullptr;
errno = 0;
long v = strtol(env, &endp, 10);
if (errno == 0 && endp != env && *endp == '\0' && v >= 0) {
cached = time_t(v);
}
}
return cached;
}
static void enforce_quota(uint32_t port2, const char *base_dir,
session_kind kind, off_t quota,
off_t needed)
{
char port_dir[768];
snprintf(port_dir, sizeof(port_dir), "%s/%u", base_dir, port2);
DIR *d = opendir(port_dir);
if (d == nullptr) {
return;
}
struct Item {
std::string path;
off_t size;
time_t mtime;
std::string date_dir;
};
std::vector<Item> items;
off_t total = 0;
struct dirent *ent;
while ((ent = readdir(d)) != nullptr) {
if (ent->d_name[0] == '.') {
continue;
}
char date_dir[1024];
snprintf(date_dir, sizeof(date_dir), "%s/%s", port_dir, ent->d_name);
struct stat st;
if (stat(date_dir, &st) != 0 || !S_ISDIR(st.st_mode)) {
continue;
}
DIR *dd = opendir(date_dir);
if (dd == nullptr) {
continue;
}
struct dirent *fent;
while ((fent = readdir(dd)) != nullptr) {
if (fent->d_name[0] == '.'
|| session_file_kind(fent->d_name) != kind) {
continue;
}
char fpath[1280];
snprintf(fpath, sizeof(fpath), "%s/%s", date_dir, fent->d_name);
struct stat fst;
if (stat(fpath, &fst) != 0) {
continue;
}
// allocated size, not apparent: .bin files are sparse and
// st_size wildly overstates what they cost on disk
const off_t alloc = off_t(fst.st_blocks) * 512;
total += alloc;
if (time(nullptr) - fst.st_mtime < active_file_grace_s()) {
// live session file: count it, never delete it
continue;
}
items.push_back({fpath, alloc, fst.st_mtime, date_dir});
}
closedir(dd);
}
closedir(d);
// `needed` is the caller's prospective growth: a write-time breach
// can happen with total still at or just under the quota, and
// without accounting for it here the pass would free nothing and
// the caller's write would be dropped forever.
if (total + needed <= quota) {
return;
}
// Sort oldest-first and delete down to 80% of quota, not just
// under it: freeing to the brim meant an active session's growth
// re-breached the cap within minutes and blocked binlog writes
// until the next hourly pass.
const off_t target = quota - quota / 5;
std::sort(items.begin(), items.end(),
[](const Item &a, const Item &b) { return a.mtime < b.mtime; });
for (const auto &it : items) {
if (total + needed <= target) {
break;
}
if (unlink(it.path.c_str()) == 0) {
::printf("log cleanup: removed %s for %s quota "
"(port2=%u total %lld > %lld)\n",
it.path.c_str(),
kind == SESSION_VIDEO ? "video" : "telemetry",
unsigned(port2),
(long long)total, (long long)quota);
total -= it.size;
// Try rmdir on the date dir in case this was its last file;
// harmless if it isn't.
(void)rmdir(it.date_dir.c_str());
}
}
}
static void retention_pass(uint32_t port2, double retention_days,
const char *base_dir, time_t now)
{
if (retention_days <= 0.0) {
return; // 0 = keep forever (per-entry); quota pass still runs below
}
double cutoff_age_s = retention_days * 86400.0;
char port_dir[768];
snprintf(port_dir, sizeof(port_dir), "%s/%u", base_dir, port2);
DIR *d = opendir(port_dir);
if (d == nullptr) {
return;
}
struct dirent *ent;
while ((ent = readdir(d)) != nullptr) {
if (ent->d_name[0] == '.') {
continue;
}
char date_dir[1024];
snprintf(date_dir, sizeof(date_dir), "%s/%s", port_dir, ent->d_name);
struct stat st;
if (stat(date_dir, &st) != 0 || !S_ISDIR(st.st_mode)) {
continue;
}
DIR *dd = opendir(date_dir);
if (dd == nullptr) {
continue;
}
unsigned remaining = 0;
struct dirent *fent;
while ((fent = readdir(dd)) != nullptr) {
if (fent->d_name[0] == '.') {
continue;
}
char fpath[1280];
snprintf(fpath, sizeof(fpath), "%s/%s", date_dir, fent->d_name);
if (is_session_file(fent->d_name)) {
struct stat fst;
if (stat(fpath, &fst) == 0) {
double age = double(now - fst.st_mtime);
if (age > cutoff_age_s) {
if (unlink(fpath) == 0) {
::printf("log cleanup: removed %s (age %.0fs > %.0fs)\n",
fpath, age, cutoff_age_s);
continue;
}
}
}
}
remaining++;
}
closedir(dd);
if (remaining == 0) {
if (rmdir(date_dir) == 0) {
::printf("log cleanup: removed empty %s\n", date_dir);
}
}
}
closedir(d);
}
static void cleanup_for_port2(uint32_t port2, double retention_days,
uint32_t video_quota_mb,
const char *base_dir, time_t now)
{
// Passes per port2:
// 1. retention_pass: per-entry "delete files older than the
// configured retention", covering both kinds. Skipped when
// retention=0 (keep forever).
// 2. one quota pass per kind, with independent budgets. Both run
// even if retention=0, so even a "keep forever" entry cannot
// fill the disk -- and video can never evict telemetry,
// because it is never a candidate in the telemetry pass.
retention_pass(port2, retention_days, base_dir, now);
enforce_quota(port2, base_dir, SESSION_TELEM, port2_quota_bytes(), 0);
const off_t vquota = video_quota_mb != 0
? off_t(video_quota_mb) * 1024 * 1024
: port2_video_quota_bytes();
enforce_quota(port2, base_dir, SESSION_VIDEO, vquota, 0);
}
static int traverse_cb(struct tdb_context *db, TDB_DATA key, TDB_DATA data, void *ptr)
{
(void)db;
auto *ctx = static_cast<PassCtx *>(ptr);
if (key.dsize != sizeof(int) || data.dsize < KEYENTRY_MIN_SIZE) {
return 0;
}
int port2 = 0;
memcpy(&port2, key.dptr, sizeof(int));
if (port2 <= 0) {
return 0;
}
struct KeyEntry k {};
size_t copy = data.dsize < sizeof(KeyEntry) ? data.dsize : sizeof(KeyEntry);
memcpy(&k, data.dptr, copy);
if (k.magic != KEY_MAGIC) {
return 0;
}
cleanup_for_port2(uint32_t(port2), double(k.log_retention_days),
k.video_quota_mb, ctx->base_dir, ctx->now);
return 0;
}
static double cleanup_interval_seconds()
{
const char *env = getenv("SUPPORTPROXY_CLEANUP_INTERVAL");
if (env != nullptr && *env != '\0') {
char *endp = nullptr;
double v = strtod(env, &endp);
if (endp != env && v > 0.0) {
return v;
}
}
return 3600.0;
}
static void sleep_seconds(double s)
{
if (s <= 0.0) {
return;
}
struct timespec ts;
ts.tv_sec = time_t(s);
ts.tv_nsec = long((s - double(ts.tv_sec)) * 1e9);
nanosleep(&ts, nullptr);
}
} // namespace
void log_cleanup_port2_quota(unsigned port2, const char *base_dir,
off_t needed)
{
// binlog's write-time gate: telemetry budget only. Freeing video
// here would let a .bin write delete a recording, which is exactly
// the cross-eviction the split budgets exist to prevent.
enforce_quota(port2, base_dir, SESSION_TELEM, port2_quota_bytes(), needed);
}
void log_cleanup_port2_video_quota(unsigned port2, const char *base_dir,
off_t quota, off_t needed)
{
enforce_quota(port2, base_dir, SESSION_VIDEO,
quota > 0 ? quota : port2_video_quota_bytes(), needed);
}
void log_cleanup_once(const char *base_dir)
{
auto *db = db_open();
if (db == nullptr) {
return;
}
PassCtx ctx { base_dir, time(nullptr) };
tdb_traverse(db, traverse_cb, &ctx);
db_close(db);
}
void log_cleanup_loop(const char *base_dir)
{
// Run an immediate pass on startup so a fresh restart still cleans up.
log_cleanup_once(base_dir);
double interval = cleanup_interval_seconds();
while (true) {
sleep_seconds(interval);
log_cleanup_once(base_dir);
}
}