perf: bound request work and resource usage (#866)

* perf: bound request work and resource usage

* fix: address performance review regressions

* fix: guard content expiration and dashboard state

* fix: tolerate cache outages and retire derived paths

* fix: revalidate expiration claims and status pagination

* fix: retry interrupted repository metadata cleanup

* fix: retire obsolete content cache storage

* fix: activate tree metadata and recheck expiration claims

* fix: fence cache publication across expiration

* fix: recover staged trees and enforce cache lifecycle cleanup

* fix: prune expired transforms during database outages

* fix: finish conference removals and reload expiration candidates
This commit is contained in:
Thomas Durieux authored and GitHub committed 2026-10-08 03:38:19 +01:00
1 parent 32722169a2
commit 6434337923
74 files changed
+4752 -553

No files matched your search

+69 -5
View File
@@ -1,8 +1,14 @@
import { Model, Document } from "mongoose";
import * as schedule from "node-schedule";
import AnonymizedGistModel from "../core/model/anonymizedGists/anonymizedGists.model";
import AnonymizedPullRequestModel from "../core/model/anonymizedPullRequests/anonymizedPullRequests.model";
import Gist from "../core/Gist";
import PullRequest from "../core/PullRequest";
import Conference from "../core/Conference";
import AnonymizedRepositoryModel from "../core/model/anonymizedRepositories/anonymizedRepositories.model";
import ConferenceModel from "../core/model/conference/conferences.model";
import Repository from "../core/Repository";
import FileModel from "../core/model/files/files.model";
import { createLogger, serializeError } from "../core/logger";
import { RepositoryStatus } from "../core/types";
import { computeAndStoreDailyStats } from "./dailyStatsSnapshot";
@@ -28,9 +34,12 @@ export function conferenceStatusCheck() {
}
export function repositoryStatusCheck() {
// check every 6 hours the status of the repositories
schedule.scheduleJob("0 */6 * * *", async () => {
await runRepositoryStatusCheck();
// Claim persisted expiration work every five minutes, without overlapping runs.
let running = false;
schedule.scheduleJob("*/5 * * * *", async () => {
if (running) return;
running = true;
try { await runRepositoryStatusCheck(); } finally { running = false; }
});
}
@@ -126,8 +135,7 @@ export async function runRepositoryStatusCheck(now = new Date()) {
(async () => {
const repo = new Repository(data);
try {
await repo.resetSate();
await repo.updateStatus(RepositoryStatus.EXPIRED);
await repo.expire();
logger.info("recovered expired repository cleanup", {
repoId: repo.repoId,
});
@@ -144,6 +152,62 @@ export async function runRepositoryStatusCheck(now = new Date()) {
}
}
await flushBatch();
await expireContent(AnonymizedGistModel, data => new Gist(data), now);
await expireContent(AnonymizedPullRequestModel, data => new PullRequest(data), now);
const retiredCursor = AnonymizedRepositoryModel.find({ $or: [
{ retiredTreeGenerations: { $exists: true } }, { retiredContentPrefixes: { $exists: true } },
{ legacyContentCleanupPending: true, status: RepositoryStatus.READY },
{ "stagedFileTrees.until": { $lte: now } },
] }).cursor();
for await (const data of retiredCursor) {
batch.push(new Repository(data).cleanupRetiredFileTrees(now).catch(error => {
logger.error("retired tree cleanup failed", { ...serializeError(error), repoId: data.repoId });
}));
if (batch.length >= 10) await flushBatch();
}
await flushBatch();
const stagedCursor = FileModel.aggregate<{ _id: { repoId: string; generation: string } }>([
{ $match: { treeStaged: true } }, { $group: { _id: { repoId: "$repoId", generation: "$treeGeneration" } } },
]).cursor();
for await (const stage of stagedCursor) {
batch.push(Repository.cleanupStagedFileTree(stage._id.repoId, stage._id.generation, now).catch(error => {
logger.error("staged tree cleanup failed", { ...serializeError(error), repoId: stage._id.repoId });
}));
if (batch.length >= 10) await flushBatch();
}
await flushBatch();
const pendingCursor = FileModel.find({ metadataPending: true }).cursor();
for await (const file of pendingCursor) {
batch.push((async () => {
try {
const data = await AnonymizedRepositoryModel.findOne({ repoId: file.repoId }).exec();
if (!data || data.treeGeneration !== file.treeGeneration ||
(data.status && [RepositoryStatus.EXPIRING, RepositoryStatus.EXPIRED, RepositoryStatus.REMOVING, RepositoryStatus.REMOVED].includes(data.status))) {
await FileModel.deleteOne({ _id: file._id, metadataPending: true }).exec();
return;
}
await new Repository(data).completeRecoveredFileMetadata(file);
} catch (error) {
logger.error("recovered file invalidation failed", { ...serializeError(error), repoId: file.repoId });
}
})());
if (batch.length >= 10) await flushBatch();
}
await flushBatch();
}
async function expireContent<T extends Document>(model: Model<T>, create: (data: T) => Gist | PullRequest, now: Date) {
const cursor = model.find(contentMaintenanceQuery(now)).cursor();
for await (const data of cursor) {
try { await create(data).expire(); } catch (error) { logger.error("content expiration failed", serializeError(error)); }
}
}
export function contentMaintenanceQuery(now: Date) {
return { $or: [{ status: RepositoryStatus.EXPIRING },
{ status: RepositoryStatus.READY, "options.expirationMode": { $ne: "never" }, "options.expirationDate": { $lte: now } }] };
}
export function dailyStatsSnapshot() {