GeezerVison is showing github.com — usage: test the thinning gap check; end a failure streak once its ret… · scuttletoby/t3code@b9864e2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/HeadlessUsageRefresher.test.ts +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -618,6 +618,62 @@ describe("HeadlessUsageRefresher", () => { | 618 | 618 | }), | 619 | 619 | ); | 620 | 620 |
| | 621 | + it.effect("a streak whose retry is overdue by more than two hours is over: a new outage", () => | | 622 | + Effect.gen(function* () { | | 623 | + // One refresh fails at T (retry at T+20); the instance is then skipped as signed out until | | 624 | + // it comes back, signed in, past its retry by `overdue`, and the refresh fails again. | | 625 | + const cases = [ | | 626 | + [2 * HOUR, false], | | 627 | + [2 * HOUR + 1, true], | | 628 | + [72 * HOUR, true], | | 629 | + ] as const; | | 630 | + for (const [overdue, newStreak] of cases) { | | 631 | + const label = `overdue ${overdue} ms`; | | 632 | + yield* startAt(NOW_MS); | | 633 | + let signedOut = false; | | 634 | + const fake = fakeTarget(provider(NOW_MS - HOUR), { checksOnInterval: () => !signedOut }); | | 635 | + fake.setOutcome("noReading"); | | 636 | + const log = logCollector(); | | 637 | + const { sweep } = yield* make(deps([fake.target])); | | 638 | + const at = (ms: number) => | | 639 | + TestClock.setTime(NOW_MS + ms).pipe(Effect.andThen(sweep), Effect.provide(log.layer)); | | 640 | + yield* at(0); | | 641 | + assert.equal(log.warnings.length, 1, label); | | 642 | + signedOut = true; | | 643 | + fake.setSnapshot(provider(NOW_MS, { auth: { status: "unauthenticated" } })); | | 644 | + const backAt = 2 * INTERVAL + overdue; | | 645 | + for (let ms = HOUR; ms < backAt; ms += HOUR) yield* at(ms); | | 646 | + assert.equal(fake.calls(), 1, label); | | 647 | + signedOut = false; | | 648 | + fake.setSnapshot(provider(NOW_MS + backAt - HOUR)); | | 649 | + yield* at(backAt); | | 650 | + assert.equal(fake.calls(), 2, label); | | 651 | + // A new streak: logged again, and retried after 2 intervals, not 4. | | 652 | + assert.equal(log.warnings.length, newStreak ? 2 : 1, label); | | 653 | + yield* at(backAt + 2 * INTERVAL); | | 654 | + assert.equal(fake.calls(), newStreak ? 3 : 2, label); | | 655 | + yield* at(backAt + 4 * INTERVAL); | | 656 | + assert.equal(fake.calls(), 3, label); | | 657 | + } | | 658 | + }), | | 659 | + ); | | 660 | + | | 661 | + it.effect("a sweep in the same millisecond as a failed refresh waits out the backoff", () => | | 662 | + Effect.gen(function* () { | | 663 | + yield* startAt(NOW_MS); | | 664 | + // An error leaves the last check an hour old, so the instance stays due. | | 665 | + const fake = fakeTarget(provider(NOW_MS - HOUR)); | | 666 | + fake.setOutcome("error"); | | 667 | + const { sweep } = yield* make(deps([fake.target])); | | 668 | + yield* sweep; | | 669 | + yield* sweep; | | 670 | + assert.equal(fake.calls(), 1); | | 671 | + yield* TestClock.adjust(2 * INTERVAL); | | 672 | + yield* sweep; | | 673 | + assert.equal(fake.calls(), 2); | | 674 | + }), | | 675 | + ); | | 676 | + | 621 | 677 | it("backoff doubles and stops at two hours (or the interval, if longer)", () => { | 622 | 678 | assert.equal(backoffMs(INTERVAL, 1), 2 * INTERVAL); | 623 | 679 | assert.equal(backoffMs(INTERVAL, 3), 8 * INTERVAL); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/HeadlessUsageRefresher.ts +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -93,6 +93,15 @@ export function backoffMs(intervalMs: number, failures: number): number { | 93 | 93 | return Math.min(intervalMs * 2 ** failures, Math.max(MAX_BACKOFF_MS, intervalMs)); | 94 | 94 | } | 95 | 95 |
| | 96 | +/** | | 97 | + * A failure streak whose retry is overdue by more than this is over: the instance went that long | | 98 | + * without a refresh of ours (skipped while signed out, refreshed by a client, a locked host), so | | 99 | + * a failure now is a new outage. The longest backoff is far beyond a sweep that merely runs late. | | 100 | + */ | | 101 | +function streakExpiryMs(intervalMs: number): number { | | 102 | + return Math.max(MAX_BACKOFF_MS, intervalMs); | | 103 | +} | | 104 | + | 96 | 105 | /** Room for a sweep that runs late: scheduler ticks, and another instance refreshed first. */ | 97 | 106 | const SWEEP_SLACK_MS = REFRESH_TIMEOUT_MS + 60 * 1000; | 98 | 107 |
| @@ -141,7 +150,11 @@ export const make = Effect.fn("HeadlessUsageRefresher.make")(function* (deps: Re | 141 | 150 | const after = yield* target.snapshot.getSnapshot; | 142 | 151 | const timedOut = Exit.isSuccess(exit) && Option.isNone(exit.value); | 143 | 152 | const ok = Exit.isSuccess(exit) && !timedOut && gotNewReading(before, after); | 144 | | - const previous = failing.get(target.instanceId); | | 153 | + const recorded = failing.get(target.instanceId); | | 154 | + const previous = | | 155 | + recorded !== undefined && nowMs - recorded.retryAtMs > streakExpiryMs(intervalMs) | | 156 | + ? undefined | | 157 | + : recorded; | 145 | 158 | if (ok) { | 146 | 159 | yield* endStreak(target.instanceId); | 147 | 160 | return; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/UsageReadingsHistory.test.ts +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -740,6 +740,50 @@ it.layer(NodeServices.layer)("UsageReadingsHistory", (it) => { | 740 | 740 | }), | 741 | 741 | ); | 742 | 742 |
| | 743 | + it.effect("thinning keeps a break whose reading before the step fell to retention", () => | | 744 | + Effect.gen(function* () { | | 745 | + yield* TestClock.setTime(NOW_MS); | | 746 | + const directory = yield* historyDirectory; | | 747 | + const start = -31 * DAY; | | 748 | + const minutes = (from: number, percent: number) => | | 749 | + Array.from({ length: 30 }, (_, n) => reading(at(start + (from + n) * MINUTE), percent)); | | 750 | + // A reading a minute for 30 minutes at day -31; then the clock steps back to day -36 (older | | 751 | + // than retention, so that reading goes), then readings resume 20 minutes before the first | | 752 | + // pass. The new pass starts before the last reading kept, so thinning starts again there. | | 753 | + const file = yield* seedHistory( | | 754 | + directory, | | 755 | + INSTANCE, | | 756 | + jsonLines([ | | 757 | + ...minutes(0, 40), | | 758 | + reading(at(-36 * DAY), 45), | | 759 | + ...minutes(-20, 50), | | 760 | + reading(at(-MINUTE), 60), | | 761 | + ]), | | 762 | + ); | | 763 | + const history = yield* make({ | | 764 | + directory, | | 765 | + retentionMs: 35 * DAY, | | 766 | + thinAfterMs: DAY, | | 767 | + thinIntervalMs: 15 * MINUTE, | | 768 | + }); | | 769 | + yield* history.compact; | | 770 | + | | 771 | + // Minutes 0 and 15 and the last reading (29) before the step, then minutes -20 and -5 of | | 772 | + // the new pass, and the recent reading. The break between 29 and -20 stays. | | 773 | + const kept = yield* history.readings(INSTANCE); | | 774 | + assert.deepStrictEqual( | | 775 | + kept.map((entry) => (Date.parse(entry.checkedAt) - (NOW_MS + start)) / MINUTE).slice(0, 5), | | 776 | + [0, 15, 29, -20, -5], | | 777 | + ); | | 778 | + assert.deepStrictEqual(percents(kept), [40, 40, 40, 50, 50, 60]); | | 779 | + assert.deepStrictEqual(segments(kept), [0, 0, 0, 1, 1, 1]); | | 780 | + // Run again: a second compaction keeps the same lines. | | 781 | + const once = yield* fileLines(file); | | 782 | + yield* history.compact; | | 783 | + assert.deepStrictEqual(yield* fileLines(file), once); | | 784 | + }), | | 785 | + ); | | 786 | + | 743 | 787 | it.effect( | 744 | 788 | "keeps readings ahead of a clock that is behind and serves them once it is put right", | 745 | 789 | () => | |
|