~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

pds-store: a journal segment leaves pds_log_bytes as it is deleted

Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:harness/log-bytes-spans
.changeset/log-bytes-journal-segments.mdnew+7−0
@@ -0,0 +1,7 @@
---
default: patch
---
# `pds_log_bytes` no longer counts journal segments already deleted
With `PDS_SYNC` `together`, the `pds_log_bytes` gauge, and `_stats`' `logBytes`, could count the journal's segments for a while after they were deleted: until the journal had deleted all of them, syncing the directory after each. A segment's bytes now leave the gauge as its file is deleted.
crates/pds-store/src/journal.rs+21−6
@@ -758,8 +758,12 @@ struct State {
/// Earlier segments whose records may not be durable: a sync takes
/// them in first, and a checkpoint before it counts them durable.
older: Vec<Older>,
/// The bytes of records in the earlier segments still there.
/// The bytes of records in the earlier segments still there, and of
/// each, by its number: a segment's bytes leave `kept` as its file is
/// deleted, under this lock, so that `bytes` never counts a segment
/// gone, nor misses one there (`spec/OPERATIONS.md`, `pds_log_bytes`).
kept: u64,
kept_by: std::collections::BTreeMap<u64, u64>,
/// The logs appended to since the checkpoint before began, by path
/// relative to the data directory.
touched: BTreeSet<PathBuf>,
@@ -973,6 +977,11 @@ impl Journal {
written: adopted.last,
older,
kept: adopted.bytes,
kept_by: adopted
.segments
.iter()
.map(|k| (k.number, k.end - MAGIC_LEN))
.collect(),
touched: adopted.paths,
appended: 0,
closed: false,
@@ -1369,7 +1378,9 @@ impl Journal {
};
st.older.push(o);
}
st.kept += st.len - MAGIC_LEN;
let held = st.len - MAGIC_LEN;
st.kept += held;
st.kept_by.insert(number, held);
st.number = number + 1;
st.len = MAGIC_LEN;
st.size = MAGIC_LEN;
@@ -1467,9 +1478,14 @@ impl Journal {
// gap, which hold every `forget` their records need.
for n in first..=number {
let path = segment_path(&self.dir, n);
match std::fs::remove_file(&path) {
Err(e) if e.kind() != io::ErrorKind::NotFound => return Err(named(&path, e)),
_ => {}
{
let mut st = lock(&self.state);
match std::fs::remove_file(&path) {
Err(e) if e.kind() != io::ErrorKind::NotFound => return Err(named(&path, e)),
_ => {}
}
let held = st.kept_by.remove(&n).unwrap_or(0);
st.kept -= held;
}
self.note_dir(Some(&path));
if let Err(e) = sync_dir(&self.dir) {
@@ -1480,7 +1496,6 @@ impl Journal {
self.note_dir(None);
lock(&self.state).first = n + 1;
}
lock(&self.state).kept = 0;
Ok(())
}
harness/checks/operations.mjs+27−3
@@ -383,7 +383,7 @@ async function canary() {
rev = wrote.body?.commit?.rev
const after = gauge(await scrape('after a canary record'), 'pds_log_bytes')
if (changedFile(files, filesUnder(dataDir))) continue
if (!(after > before)) await broke('metrics', { metric: 'pds_log_bytes', after: 'a createRecord', meanwhile: 'no file in the data directory made, deleted, replaced or cut short' }, `more than ${before}`, after)
if (!(after > before)) await broke('metrics', { metric: 'pds_log_bytes', after: 'a createRecord', record: n + 1, meanwhile: 'no file in the data directory made, deleted, replaced or cut short' }, `more than ${before}`, after)
held = true
}
count('metrics: pds_log_bytes across a write that let nothing go', held)
@@ -638,6 +638,26 @@ function mayBeLost(t) {
return server.lives.some((l) => l.how === 'SIGKILL' && l.ended !== null && t >= (l.signalled ?? l.ended) - 1500 && t <= l.ended)
}
/// Whether the spans of a request that `life`'s server took from `from`
/// to `to` may be in an export it dropped, as its log says (spec/
/// OPERATIONS.md, "Export", "Events"): each span is exported after it
/// ends, in an export that fails `OTEL_BSP_EXPORT_TIMEOUT` after it began
/// at the latest, and begins once the one before it ended, within the
/// schedule delay; and a failure is logged but once a minute while exports
/// keep failing, none taken. So: a `trace export failed` line of that life
/// written from `from` until the schedule delay, two timeouts and a
/// second and a half after `to`; or the last before `from`, within the
/// minute and the timeout before it, with no export taken by the receiver
/// between the two, so that the failures may have run on unlogged.
function exportDropped(from, to, life) {
const delay = Number(OTEL_ENV.OTEL_BSP_SCHEDULE_DELAY)
const timeout = Number(OTEL_ENV.OTEL_BSP_EXPORT_TIMEOUT)
const failed = (life?.lines ?? []).filter(({ obj }) => obj?.msg === 'trace export failed').map(({ obj }) => Date.parse(obj.time))
if (failed.some((f) => f >= from && f <= to + delay + 2 * timeout + 1500)) return true
const last = Math.max(...failed.filter((f) => f < from))
return last >= from - 60000 - timeout - delay && !otlp.exports.some((e) => e.at > last && e.at < from)
}
/// Every span held to its request: one SERVER span for each request
/// sampled, the child of the span its traceparent named, with the
/// attributes the spec gives; none for one not sampled; a sync under every
@@ -680,6 +700,9 @@ async function checkSpans(upTo) {
if (any.length) await broke('spans', request, 'none: the traceparent is not sampled', any.slice(0, 2))
continue
}
// A span missing from an export the server says it dropped.
const dropped = () => exportDropped(rec.started, rec.ended, lives[rec.life])
if (!spans.length && dropped()) continue
if (spans.length !== 1) {
// When it ended, among the server's lives, so that a span lost shows where.
const at = (t) => (t === null || t === undefined ? null : new Date(t).toISOString())
@@ -696,7 +719,7 @@ async function checkSpans(upTo) {
const [sp] = spans
await checkServerSpan(sp, rec, request, lives[rec.life])
const under = children.get(sp.spanId) ?? []
if (rec.commit && rec.status < 300 && !under.some((c) => c.name === 'fsync' && c.kind === 'INTERNAL' && c.traceId === rec.traceId)) await broke('spans', request, 'an INTERNAL `fsync` span under a request that made a commit', under)
if (rec.commit && rec.status < 300 && !under.some((c) => c.name === 'fsync' && c.kind === 'INTERNAL' && c.traceId === rec.traceId) && !dropped()) await broke('spans', request, 'an INTERNAL `fsync` span under a request that made a commit', under)
if (rec.route === 'com.atproto.server.createAccount' && rec.status === 200) {
// An account made with a `did` posts no operation: its call reads the
// DID's document (spec/MIGRATION.md), from the directory for a
@@ -709,7 +732,7 @@ async function checkSpans(upTo) {
const at = new URL(base)
const port = BigInt(at.port || (at.protocol === 'https:' ? 443 : 80))
const call = under.find((c) => c.kind === 'CLIENT' && c.name === verb && String(c.attributes['url.full'] ?? '').startsWith(base))
if (!call || call.attributes['http.request.method'] !== verb || call.attributes['server.address'] !== at.hostname || call.attributes['server.port'] !== port || call.attributes['http.response.status_code'] !== 200n) await broke('spans', request, `a CLIENT span \`${verb}\` under it, of the call to ${host ? 'the DID\'s host' : 'the PLC directory'} at ${base}`, call ?? under)
if ((!call && !dropped()) || (call && (call.attributes['http.request.method'] !== verb || call.attributes['server.address'] !== at.hostname || call.attributes['server.port'] !== port || call.attributes['http.response.status_code'] !== 200n))) await broke('spans', request, `a CLIENT span \`${verb}\` under it, of the call to ${host ? 'the DID\'s host' : 'the PLC directory'} at ${base}`, call ?? under)
}
checked += 1
}
@@ -731,6 +754,7 @@ async function checkSpans(upTo) {
const t = Date.parse(obj.time)
if (t > upTo - 1000 || mayBeLost(t)) continue
const spans = byTrace.get(obj.trace_id) ?? []
if (!spans.length && exportDropped(t - (obj.ms ?? 0), t, life)) continue
if (spans.length !== 1 || spans[0].parentSpanId) await broke('spans', { line: obj }, 'one SERVER span, the root of its trace', spans.length ? spans : 'none')
}
}
spec/OPERATIONS.md+17−7
@@ -284,10 +284,15 @@ may start and which the server may run in the background at any time;
and the journal's records, once what they hold is durable where it is
kept (`DURABILITY.md`). It lets go of nothing but as it makes, deletes,
replaces or cuts short a file in `PDS_DATA_DIR`, and what it lets go of
leaves the gauge no sooner than that. So across a write during which no
file there was made, deleted, replaced or cut short, each only grown or
left as it was, the gauge grows, by however little; across one during
which one was, it may be less after than before.
leaves the gauge as that file changes, neither before nor after: a read
of the gauge begun once the change can be seen in the directory counts
none of it, and one ended before the change counts all of it, however
long the server takes over what it does next (a journal's segment
deleted leaves the gauge as it goes, not once the directory is synced or
the other segments are deleted). So across a write during which no file
there was made, deleted, replaced or cut short, each only grown or left
as it was, the gauge grows, by however little; across one during which
one was, it may be less after than before.
The buckets:
@@ -701,9 +706,14 @@ is back at two thirds; the steps go on. Then, at the end:
a root span for every request line of a client that sent no
`traceparent`; no `SERVER` span without a line; every export a protobuf
`POST /v1/traces` with the header. Spans of requests near the outage or
just before a kill may be lost. While the receiver hung, the requests'
median time is under a second, and a `trace export failed` line is
written.
just before a kill may be lost, and those of an export the server says
it dropped (Export, above): a span missing, with a `trace export
failed` line from the request's start until `OTEL_BSP_SCHEDULE_DELAY`,
twice `OTEL_BSP_EXPORT_TIMEOUT` and 1.5 s after its answer; or with the
last such line before its start within a minute and the timeout, and no
export taken since, while exports kept failing unlogged. While the
receiver hung, the requests' median time is under a second, and a
`trace export failed` line is written.
- **Starts**: at `warn`, with a PLC directory that answers 500 and
`OTEL_SERVICE_NAME` unset: `createAccount` is refused, the directory gets
a `traceparent` in the request's trace, a `plc failure` line names the