-
Notifications
You must be signed in to change notification settings - Fork 9
fix(songstats-backfill): backoff on 429 + defer instead of churn (chat#1797 — pacing/backoff + logging) #673
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c250b34
fix(songstats-backfill): exponential backoff on 429 + defer instead o…
sweetmantech f6eed90
refactor(songstats): address PR #673 review — SRP, retry scope, stran…
sweetmantech 805bef4
refactor(songstats): co-locate releaseSongstatsBackfillRows in update…
sweetmantech e9a1b05
refactor(songstats): KISS — one updateSongstatsBackfillQueue(ids[], f…
sweetmantech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { songstatsBackfillWorkflow } from "../songstatsBackfillWorkflow"; | ||
|
|
||
| import { getBackfillBudgetStep } from "../getBackfillBudgetStep"; | ||
| import { claimBackfillRowsStep } from "../claimBackfillRowsStep"; | ||
| import { backfillTrackStep } from "../backfillTrackStep"; | ||
| import { releaseClaimedRowsStep } from "../releaseClaimedRowsStep"; | ||
|
|
||
| vi.mock("../getBackfillBudgetStep", () => ({ getBackfillBudgetStep: vi.fn() })); | ||
| vi.mock("../claimBackfillRowsStep", () => ({ claimBackfillRowsStep: vi.fn() })); | ||
| vi.mock("../backfillTrackStep", () => ({ backfillTrackStep: vi.fn() })); | ||
| vi.mock("../releaseClaimedRowsStep", () => ({ releaseClaimedRowsStep: vi.fn() })); | ||
|
|
||
| const row = (id: string) => ({ id, song: id }) as never; | ||
|
|
||
| describe("songstatsBackfillWorkflow", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.spyOn(console, "log").mockImplementation(() => {}); | ||
| vi.mocked(releaseClaimedRowsStep).mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| it("releases the rest of the claimed batch to pending when a track defers", async () => { | ||
| vi.mocked(getBackfillBudgetStep).mockResolvedValue(100); | ||
| vi.mocked(claimBackfillRowsStep).mockResolvedValue([row("r1"), row("r2"), row("r3")]); | ||
| vi.mocked(backfillTrackStep) | ||
| .mockResolvedValueOnce({ ok: true, hitsSpent: 1 }) // r1 | ||
| .mockResolvedValueOnce({ ok: false, hitsSpent: 0, deferred: true }); // r2 defers | ||
|
|
||
| const result = await songstatsBackfillWorkflow(); | ||
|
|
||
| // r2 is set pending by the step itself; the unprocessed remainder (r3) is released here | ||
| expect(releaseClaimedRowsStep).toHaveBeenCalledWith(["r3"]); | ||
| expect(backfillTrackStep).toHaveBeenCalledTimes(2); // stopped at the defer, never reached r3 | ||
| expect(result).toEqual({ backfilled: 1, failed: 0, deferred: true }); | ||
| }); | ||
|
|
||
| it("drains until the queue is empty and never releases when nothing defers", async () => { | ||
| vi.mocked(getBackfillBudgetStep).mockResolvedValue(100); | ||
| vi.mocked(claimBackfillRowsStep) | ||
| .mockResolvedValueOnce([row("a"), row("b")]) | ||
| .mockResolvedValueOnce([]); // queue drained | ||
| vi.mocked(backfillTrackStep) | ||
| .mockResolvedValueOnce({ ok: true, hitsSpent: 1 }) | ||
| .mockResolvedValueOnce({ ok: false, hitsSpent: 1 }); // terminal (e.g. 404) | ||
|
|
||
| const result = await songstatsBackfillWorkflow(); | ||
|
|
||
| expect(releaseClaimedRowsStep).not.toHaveBeenCalled(); | ||
| expect(result).toEqual({ backfilled: 1, failed: 1, deferred: false }); | ||
| }); | ||
|
|
||
| it("does not drain when there is no budget", async () => { | ||
| vi.mocked(getBackfillBudgetStep).mockResolvedValue(0); | ||
|
|
||
| const result = await songstatsBackfillWorkflow(); | ||
|
|
||
| expect(claimBackfillRowsStep).not.toHaveBeenCalled(); | ||
| expect(result).toEqual({ backfilled: 0, failed: 0, deferred: false }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { updateSongstatsBackfillQueue } from "@/lib/supabase/songstats_backfill_queue/updateSongstatsBackfillQueue"; | ||
|
|
||
| /** | ||
| * Durable step: return unprocessed claimed rows to `pending` when the drain | ||
| * stops early on a defer, so the next run retries them immediately instead of | ||
| * waiting on the stale-reclaim sweep. | ||
| * | ||
| * @param ids - Queue row ids still `in_progress` from the aborted batch. | ||
| */ | ||
| export async function releaseClaimedRowsStep(ids: string[]): Promise<void> { | ||
| "use step"; | ||
| if (ids.length === 0) return; | ||
| await updateSongstatsBackfillQueue(ids, { status: "pending" }); | ||
| console.log(`[songstats-backfill] released ${ids.length} claimed rows back to pending`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,54 @@ | ||
| import { getBackfillBudgetStep } from "@/app/workflows/getBackfillBudgetStep"; | ||
| import { claimBackfillRowsStep } from "@/app/workflows/claimBackfillRowsStep"; | ||
| import { backfillTrackStep } from "@/app/workflows/backfillTrackStep"; | ||
| import { releaseClaimedRowsStep } from "@/app/workflows/releaseClaimedRowsStep"; | ||
|
|
||
| const BATCH_SIZE = 25; | ||
|
|
||
| /** | ||
| * Durable Songstats backfill drain (recoupable/chat#1791 write path): check | ||
| * the rolling-window budget, claim value-ranked rows via the SKIP LOCKED RPC, | ||
| * backfill each track's historic series into the measurement store, and stop | ||
| * when the queue or the budget is dry. Every quota hit converts into | ||
| * permanent owned data (fetch-once: captured history is never refetched). | ||
| * Durable Songstats backfill drain (recoupable/chat#1791 write path): claim | ||
| * value-ranked rows via the SKIP LOCKED RPC and backfill each track's historic | ||
| * series, with per-track exponential backoff handling Songstats' rate limit | ||
| * (chat#1797). **Stops as soon as a track defers** — Songstats still | ||
| * rate-limiting it past the backoff bound — releasing the rest of the claimed | ||
| * batch back to `pending` (so the next drain retries them immediately rather | ||
| * than waiting on stale-reclaim) instead of hammering a saturated API. Every | ||
| * successful hit converts into permanent owned data (fetch-once: captured | ||
| * history is never refetched). | ||
| */ | ||
| export async function songstatsBackfillWorkflow() { | ||
| "use workflow"; | ||
|
|
||
| let budget = await getBackfillBudgetStep(); | ||
| let backfilled = 0; | ||
| let failed = 0; | ||
| let deferred = false; | ||
|
|
||
| while (budget > 0) { | ||
| drain: while (budget > 0) { | ||
| const rows = await claimBackfillRowsStep(Math.min(budget, BATCH_SIZE)); | ||
| if (rows.length === 0) break; | ||
| console.log(`[songstats-backfill] claimed ${rows.length} rows`); | ||
|
|
||
| for (const row of rows) { | ||
| const result = await backfillTrackStep(row); | ||
| for (let i = 0; i < rows.length; i += 1) { | ||
| const result = await backfillTrackStep(rows[i]); | ||
| if (result.deferred) { | ||
| // Songstats is saturated — stop now. The deferred row is already back to | ||
| // `pending`; release the rest of this claimed batch too so they don't sit | ||
| // `in_progress` until stale-reclaim. | ||
| deferred = true; | ||
| await releaseClaimedRowsStep(rows.slice(i + 1).map(r => r.id)); | ||
| break drain; | ||
| } | ||
| budget -= result.hitsSpent; | ||
| if (result.ok) backfilled += 1; | ||
| else failed += 1; | ||
| if (budget <= 0) break; | ||
| } | ||
| } | ||
|
|
||
| console.log(`[songstats-backfill] done: ${backfilled} backfilled, ${failed} failed`); | ||
| return { backfilled, failed }; | ||
| console.log( | ||
| `[songstats-backfill] done: ${backfilled} backfilled, ${failed} terminal` + | ||
| (deferred ? ", deferred (rate-limited)" : ""), | ||
| ); | ||
| return { backfilled, failed, deferred }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.