-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcleanup.cpp
More file actions
338 lines (313 loc) · 10.5 KB
/
Copy pathcleanup.cpp
File metadata and controls
338 lines (313 loc) · 10.5 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
/*
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/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;
}
namespace {
struct PassCtx {
const char *base_dir;
time_t now;
};
/*
Predicate for "this is a session file we should age out under
log_retention_days". Covers both .tlog (raw MAVLink frames) and
.bin (ArduPilot dataflash logs) so the retention rule is uniform —
per spec, both file types share the entry's retention setting.
*/
static bool is_session_file(const char *name)
{
size_t n = strlen(name);
return (n > 5 && strcmp(name + n - 5, ".tlog") == 0) ||
(n > 4 && strcmp(name + n - 4, ".bin") == 0);
}
/*
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.
static constexpr time_t ACTIVE_FILE_GRACE_S = 60;
static void enforce_port2_quota(uint32_t port2, const char *base_dir,
off_t needed = 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 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] == '.' || !is_session_file(fent->d_name)) {
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.
const off_t quota = port2_quota_bytes();
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 quota "
"(port2=%u total %lld > %lld)\n",
it.path.c_str(), 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,
const char *base_dir, time_t now)
{
// Two passes per port2:
// 1. retention_pass: per-entry "delete files older than the
// configured retention". Skipped when retention=0 (keep
// forever).
// 2. enforce_port2_quota: hard 1 GiB cap. Runs even if
// retention=0, so even a "keep forever" entry can't fill
// the disk.
retention_pass(port2, retention_days, base_dir, now);
enforce_port2_quota(port2, base_dir);
}
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),
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)
{
enforce_port2_quota(port2, base_dir, 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);
}
}