Rebuild: a lease never waits for prefetching; one-shot hashes

A lease was only granted for a range with spooled chunks, and the
prefetcher kept ranges ahead per active lease: once the spool ran dry a
worker got none and slept 30 s, fewer ranges were leased, fewer
prefetched. Three workers held one lease between them (~50k headers/s).
A lease now takes a spooled range first, else the next planned one
(leased ranges are fetched first; the stream waits).

crypto.hash instead of createHash().update().digest() for file and
release keys: same digests, ~2.5x faster (~4.6% of a worker's time).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daan MeijerandClaude Opus 5.5 committed 2026-10-09 03:08:19 +02:00
1 parent 168605148f
commit 9dc0a27bb4
5 files changed
+30 -25

No files matched your search

+20 -13
View File
@@ -7,23 +7,30 @@ import type { Lease } from '#lib/edge/index'
export class LeaseLost extends Error {} export class LeaseLost extends Error {}
/** /**
* Ranges for workers (REBUILD.md, coordinator): the next range whose first * Ranges for workers (REBUILD.md, coordinator): the next range with chunks
* chunk is spooled, by priority (oldest era first); a lease whose worker * spooled, by priority (oldest era first), else the next planned one (the
* went quiet for LEASE_TTL_SECONDS is taken over and resumes at its resume * prefetcher fetches leased ranges first; the worker's stream waits). A
* point. * worker never waits for prefetching to choose its range: refused leases
* left workers idle for 30 s, so fewer ranges were leased and fewer
* prefetched (LOG.md 2026-10-09). A lease whose worker went quiet for
* LEASE_TTL_SECONDS is taken over and resumes at its resume point.
*/ */
class Leases { class Leases {
async lease(nodeId: number): Promise<Lease | null> { async lease(nodeId: number): Promise<Lease | null> {
const rules = await RuleSets.current() const rules = await RuleSets.current()
const { rows } = await db.rawQuery( const take = (spooled: boolean) =>
`UPDATE ranges r SET status = 'leased', node_id = ?, leased_at = now(), heartbeat_at = now(), attempts = attempts + 1, rule_version = ? db.rawQuery(
WHERE r.id = ( `UPDATE ranges r SET status = 'leased', node_id = ?, leased_at = now(), heartbeat_at = now(), attempts = attempts + 1, rule_version = ?
SELECT id FROM ranges WHERE r.id = (
WHERE spooled_chunks > 0 AND next_id <= end_id AND (status = 'planned' OR (status = 'leased' AND heartbeat_at < now() - make_interval(secs => ?))) SELECT id FROM ranges
ORDER BY priority, id LIMIT 1 FOR UPDATE SKIP LOCKED) WHERE ${spooled ? 'spooled_chunks > 0' : 'true'} AND next_id <= end_id
RETURNING r.id, r.group_id, r.start_id, r.end_id, r.next_id`, AND (status = 'planned' OR (status = 'leased' AND heartbeat_at < now() - make_interval(secs => ?)))
[nodeId, rules.version, coordinatorConfig.leaseTtlSeconds] ORDER BY priority, id LIMIT 1 FOR UPDATE SKIP LOCKED)
) RETURNING r.id, r.group_id, r.start_id, r.end_id, r.next_id`,
[nodeId, rules.version, coordinatorConfig.leaseTtlSeconds]
)
let { rows } = await take(true)
if (rows.length === 0) ({ rows } = await take(false))
const row = rows[0] const row = rows[0]
if (!row) return null if (!row) return null
await db.from('nodes').where('id', nodeId).update({ last_seen_at: new Date() }) await db.from('nodes').where('id', nodeId).update({ last_seen_at: new Date() })
+2 -2
View File
@@ -1,4 +1,4 @@
import { createHash } from 'node:crypto' import { hash } from 'node:crypto'
import type { Overview } from '../nntp/index.js' import type { Overview } from '../nntp/index.js'
import { FileAggregator, type AggregatedFile, isArchiveStart, isLaterVolume, sevenZipVolume } from '../files/index.js' import { FileAggregator, type AggregatedFile, isArchiveStart, isLaterVolume, sevenZipVolume } from '../files/index.js'
import { RuleSet, isRandomFile, uploadTool, type Rules } from '../rules/index.js' import { RuleSet, isRandomFile, uploadTool, type Rules } from '../rules/index.js'
@@ -281,7 +281,7 @@ export class RangeIndexer {
/** The release key; a file without a recognisable release becomes a release of its own. */ /** The release key; a file without a recognisable release becomes a release of its own. */
export function releaseKeyOf(s: EdgeSegment): string { export function releaseKeyOf(s: EdgeSegment): string {
return s.file.release?.key ?? createHash('sha1').update(`file\n${s.fileKey}`).digest('hex') return s.file.release?.key ?? hash('sha1', `file\n${s.fileKey}`)
} }
function postedOf(segments: EdgeSegment[]): number | null { function postedOf(segments: EdgeSegment[]): number | null {
+4 -6
View File
@@ -1,4 +1,4 @@
import { createHash } from 'node:crypto' import { hash } from 'node:crypto'
/** /**
* Identity of one uploaded file: every segment of it has the same poster, the * Identity of one uploaded file: every segment of it has the same poster, the
@@ -7,7 +7,7 @@ import { createHash } from 'node:crypto'
* See CONCEPT.md (D12). * See CONCEPT.md (D12).
*/ */
export function fileKey(poster: string, base: string, totalSegments: number): string { export function fileKey(poster: string, base: string, totalSegments: number): string {
return createHash('sha1').update(`${poster}\n${base}\n${totalSegments}`).digest('hex') return hash('sha1', `${poster}\n${base}\n${totalSegments}`)
} }
/** True when a yEnc name looks like a real filename (it has an extension), not a random token. */ /** True when a yEnc name looks like a real filename (it has an extension), not a random token. */
@@ -26,7 +26,7 @@ export function looksLikeFilename(name: string): boolean {
*/ */
export function weakFileKey(name: string, size: number, totalParts: number): string { export function weakFileKey(name: string, size: number, totalParts: number): string {
const identity = looksLikeFilename(name) ? `name\n${name}` : 'shape' const identity = looksLikeFilename(name) ? `name\n${name}` : 'shape'
return createHash('sha1').update(`weak\n${identity}\n${size}\n${totalParts}`).digest('hex') return hash('sha1', `weak\n${identity}\n${size}\n${totalParts}`)
} }
/** /**
@@ -41,7 +41,5 @@ export function counterFileKey(segment: { poster: string; subject: string; total
const counter = /\[(\d+)\/(\d+)\]/.exec(segment.subject) const counter = /\[(\d+)\/(\d+)\]/.exec(segment.subject)
if (!counter || segment.totalParts < 2) return null if (!counter || segment.totalParts < 2) return null
const day = segment.postedAt && !Number.isNaN(segment.postedAt.getTime()) ? segment.postedAt.toISOString().slice(0, 10) : '' const day = segment.postedAt && !Number.isNaN(segment.postedAt.getTime()) ? segment.postedAt.toISOString().slice(0, 10) : ''
return createHash('sha1') return hash('sha1', `counter\n${segment.poster}\n${Number(counter[1])}/${Number(counter[2])}\n${segment.totalParts}\n${day}`)
.update(`counter\n${segment.poster}\n${Number(counter[1])}/${Number(counter[2])}\n${segment.totalParts}\n${day}`)
.digest('hex')
} }
+2 -2
View File
@@ -1,4 +1,4 @@
import { createHash } from 'node:crypto' import { hash as digest } from 'node:crypto'
import { looksLikeFilename } from '../headers/file_key.js' import { looksLikeFilename } from '../headers/file_key.js'
export interface ReleaseIdentity { export interface ReleaseIdentity {
@@ -103,7 +103,7 @@ export function namesRelease(remainder: string): boolean {
} }
function hash(...parts: (string | number | undefined)[]) { function hash(...parts: (string | number | undefined)[]) {
return createHash('sha1').update(parts.map((p) => p ?? '').join('\n')).digest('hex') return digest('sha1', parts.map((p) => p ?? '').join('\n'))
} }
/** /**
+2 -2
View File
@@ -1,4 +1,4 @@
import { createHash } from 'node:crypto' import { hash } from 'node:crypto'
/** /**
* Upload signatures (CONCEPT.md D42): which tool posted an article, read from * Upload signatures (CONCEPT.md D42): which tool posted an article, read from
@@ -48,5 +48,5 @@ export function signatureKey(tool: string, group: string) {
*/ */
export function explored(key: string, percent: number): boolean { export function explored(key: string, percent: number): boolean {
if (percent <= 0) return false if (percent <= 0) return false
return createHash('md5').update(key).digest().readUInt32BE(0) % 10_000 < percent * 100 return hash('md5', key, 'buffer').readUInt32BE(0) % 10_000 < percent * 100
} }