-
-
Notifications
You must be signed in to change notification settings - Fork 731
perf: reduce DB bandwidth ~1.5 TB/day #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
53f901a
49e2bf4
417012d
b904504
6e5589a
e352123
aefaf1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { | |
| type TrustTier, | ||
| } from './lib/skillQuality' | ||
| import { generateSkillSummary } from './lib/skillSummary' | ||
| import { computeIsSuspicious } from './lib/skillSafety' | ||
| import { hashSkillFiles } from './lib/skills' | ||
|
|
||
| const DEFAULT_BATCH_SIZE = 50 | ||
|
|
@@ -1529,6 +1530,91 @@ export const backfillDenormalizedBadgesInternal = internalMutation({ | |
| }, | ||
| }) | ||
|
|
||
| /** | ||
| * Backfill `latestVersionSummary` on all skills. Cursor-based paginated mutation | ||
| * that self-schedules until done. Reads each skill's latestVersionId, extracts | ||
| * the summary fields, and patches the skill. | ||
| */ | ||
| export const backfillLatestVersionSummaryInternal = internalMutation({ | ||
| args: { | ||
| cursor: v.optional(v.string()), | ||
| batchSize: v.optional(v.number()), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| const batchSize = clampInt(args.batchSize ?? 50, 10, 200) | ||
| const { page, continueCursor, isDone } = await ctx.db | ||
| .query('skills') | ||
| .paginate({ cursor: args.cursor ?? null, numItems: batchSize }) | ||
|
|
||
| let patched = 0 | ||
| for (const skill of page) { | ||
| if (skill.latestVersionSummary) continue | ||
|
||
| if (!skill.latestVersionId) continue | ||
| const version = await ctx.db.get(skill.latestVersionId) | ||
| if (!version) continue | ||
|
|
||
| await ctx.db.patch(skill._id, { | ||
| latestVersionSummary: { | ||
| version: version.version, | ||
| createdAt: version.createdAt, | ||
| changelog: version.changelog, | ||
| changelogSource: version.changelogSource, | ||
| clawdis: version.parsed?.clawdis, | ||
| }, | ||
| }) | ||
| patched++ | ||
| } | ||
|
|
||
| if (!isDone) { | ||
| await ctx.scheduler.runAfter( | ||
| 0, | ||
| internal.maintenance.backfillLatestVersionSummaryInternal, | ||
| { | ||
| cursor: continueCursor, | ||
| batchSize: args.batchSize, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| return { patched, isDone, scanned: page.length } | ||
| }, | ||
| }) | ||
|
|
||
| /** | ||
| * Backfill `isSuspicious` on all skills. Cursor-based paginated mutation | ||
| * that self-schedules until done. | ||
| */ | ||
| export const backfillIsSuspiciousInternal = internalMutation({ | ||
| args: { | ||
| cursor: v.optional(v.string()), | ||
| batchSize: v.optional(v.number()), | ||
| }, | ||
| handler: async (ctx, args) => { | ||
| const batchSize = clampInt(args.batchSize ?? 100, 10, 200) | ||
| const { page, continueCursor, isDone } = await ctx.db | ||
| .query('skills') | ||
| .paginate({ cursor: args.cursor ?? null, numItems: batchSize }) | ||
|
|
||
| let patched = 0 | ||
| for (const skill of page) { | ||
| const expected = computeIsSuspicious(skill) | ||
| if (skill.isSuspicious !== expected) { | ||
| await ctx.db.patch(skill._id, { isSuspicious: expected }) | ||
| patched++ | ||
| } | ||
| } | ||
|
|
||
| if (!isDone) { | ||
| await ctx.scheduler.runAfter(0, internal.maintenance.backfillIsSuspiciousInternal, { | ||
| cursor: continueCursor, | ||
| batchSize: args.batchSize, | ||
| }) | ||
| } | ||
|
|
||
| return { patched, isDone, scanned: page.length } | ||
| }, | ||
| }) | ||
|
|
||
| function clampInt(value: number, min: number, max: number) { | ||
| const rounded = Math.trunc(value) | ||
| if (!Number.isFinite(rounded)) return min | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sequential daily queries could be parallelized
The 7 daily queries are issued serially with
awaitinside theforloop, adding 7 round-trips instead of 1. Since there are no cross-day dependencies, these can be parallelized withPromise.allfor better latency in the cron job:This is low-priority for a cron job, but it's an easy win.
Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e352123 — parallelized with
Promise.all.