Files
usenet-indexer/lib/seed/runner.ts
T
Daan MeijerandClaude Opus 5.5 8c36744088
Test and deploy / test (push) Successful in 1m4s
Test and deploy / deploy (push) Successful in 1m37s
Seed orchestrator: remote nodes lease ranges and post results over HTTPS
- seed_nodes (hashed bearer tokens) and seed_ranges (planned ranges with
  lease, resume point and counts); migration …140.
- SeedOrchestrator: lease (oldest plan first, FOR UPDATE SKIP LOCKED; a
  lease without a batch for 15 min passes to the next node and resumes at
  next_id), batch (stores files and fragments, moves next_id), finish,
  release, plan (from below live indexing / the backfill / earlier plans
  down to N days back; takes over the group's backfill), status.
- Seed API under /api/v1/seed (lease, ranges/:id/batches|done|release,
  status) behind a node-token middleware; JSON bodies up to 64 MB after
  gzip.
- seed:node runs a node: no database or Redis, outgoing HTTPS only, N
  connections, gzip batches, retries for network errors and 5xx, drops a
  range on 409, gives its ranges back on SIGTERM. seed-node/ has a compose
  file that builds the image from this repository.
- seed:token, seed:plan, seed:status for the operator. Header parsing moved
  to lib/ingest and the range loop to lib/seed/runner, shared by seed:range
  and seed:node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 12:02:01 +02:00

79 lines
3.0 KiB
TypeScript

import type { XoverClient, Overview } from '../nntp/xover_client.js'
import { segmentFromHeader, type IngestSegment, type OverviewHeader } from '../ingest/segment.js'
import { FileAggregator } from '../aggregate/index.js'
import { toFragment, toSeedFile, type SeedBatch } from './wire.js'
export interface RunRangeOptions {
client: XoverClient
group: string
/** Inclusive article numbers. */
start: number
end: number
/** Articles per XOVER. */
chunk?: number
/** Batches without a new part before an open file leaves as fragments. */
idle?: number
/** XOVER batches per SeedBatch handed to `sink`. */
submitEvery?: number
/** Headers left to the deferred-body path (counted, not indexed). */
isDeferred?: (header: OverviewHeader) => boolean
/** Receives batches in order; the next XOVER is already in flight meanwhile. */
sink: (batch: SeedBatch) => Promise<void>
}
/**
* Indexes one article range on a seeding node (CONCEPT.md D37): XOVER in
* chunks over one connection (the next chunk in flight while the previous
* one is handled), headers parsed with the live rules, grouped into files
* in memory; complete files and given-up fragments go to `sink` in order.
* Shared by seed:range (stores directly) and seed:node (submits over HTTPS).
*/
export async function runRange(options: RunRangeOptions): Promise<void> {
const chunk = options.chunk ?? 20_000
const idle = options.idle ?? 5
const submitEvery = options.submitEvery ?? 5
const aggregator = new FileAggregator<IngestSegment>()
await options.client.group(options.group)
const fetch = (from: number) => {
const promise = options.client.xover(from, Math.min(from + chunk - 1, options.end))
// Awaited after the current batch is handled; until then a failure must not count as unhandled.
promise.catch(() => {})
return promise
}
let batch = emptyBatch(options.start)
let pending = 0
let from = options.start
let next: Promise<Overview[]> | null = fetch(from)
while (next) {
const overviews = await next
from += chunk
next = from <= options.end ? fetch(from) : null
const segments: IngestSegment[] = []
for (const [articleNumber, header] of overviews) {
if (!Number.isFinite(articleNumber) || !header['message-id']) continue
const segment = segmentFromHeader(header, options.group, articleNumber)
if (segment) segments.push(segment)
else if (options.isDeferred?.(header)) batch.deferred++
else batch.obfuscated++
}
batch.headers += overviews.length
batch.files.push(...aggregator.add(segments).map(toSeedFile))
batch.fragments.push(...aggregator.evict(idle).map(toFragment))
batch.next = Math.min(from, options.end + 1)
if (++pending >= submitEvery || !next) {
if (!next) batch.fragments.push(...aggregator.evict(0).map(toFragment))
await options.sink(batch)
batch = emptyBatch(batch.next)
pending = 0
}
}
}
function emptyBatch(next: number): SeedBatch {
return { next, headers: 0, obfuscated: 0, deferred: 0, files: [], fragments: [] }
}