GeezerVison is showing github.com — usage: test the thinning gap check; end a failure streak once its ret… · scuttletoby/t3code@9652718
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 +124Lines changed: 124 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -618,6 +618,130 @@ 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("with an interval longer than two hours, a streak lasts that interval overdue", () => | | 662 | + Effect.gen(function* () { | | 663 | + // Interval 3 h: the failure at T retries at T+3h. Back past that retry by `overdue`. | | 664 | + const interval = 3 * HOUR; | | 665 | + const settings = settingsWith({ headlessUsageRefreshInterval: interval }); | | 666 | + const cases = [ | | 667 | + [2 * HOUR + 1, false], | | 668 | + [3 * HOUR, false], | | 669 | + [3 * HOUR + 1, true], | | 670 | + ] as const; | | 671 | + for (const [overdue, newStreak] of cases) { | | 672 | + const label = `overdue ${overdue} ms`; | | 673 | + yield* startAt(NOW_MS); | | 674 | + let signedOut = false; | | 675 | + const fake = fakeTarget(provider(NOW_MS - interval), { | | 676 | + checksOnInterval: () => !signedOut, | | 677 | + }); | | 678 | + fake.setOutcome("noReading"); | | 679 | + const log = logCollector(); | | 680 | + const { sweep } = yield* make( | | 681 | + deps([fake.target], { getSettings: Effect.succeed(settings) }), | | 682 | + ); | | 683 | + const at = (ms: number) => | | 684 | + TestClock.setTime(NOW_MS + ms).pipe(Effect.andThen(sweep), Effect.provide(log.layer)); | | 685 | + yield* at(0); | | 686 | + assert.equal(log.warnings.length, 1, label); | | 687 | + signedOut = true; | | 688 | + fake.setSnapshot(provider(NOW_MS, { auth: { status: "unauthenticated" } })); | | 689 | + const backAt = interval + overdue; | | 690 | + for (let ms = HOUR; ms < backAt; ms += HOUR) yield* at(ms); | | 691 | + assert.equal(fake.calls(), 1, label); | | 692 | + signedOut = false; | | 693 | + fake.setSnapshot(provider(NOW_MS + backAt - interval)); | | 694 | + yield* at(backAt); | | 695 | + assert.equal(fake.calls(), 2, label); | | 696 | + assert.equal(log.warnings.length, newStreak ? 2 : 1, label); | | 697 | + } | | 698 | + }), | | 699 | + ); | | 700 | + | | 701 | + it.effect("a good refresh after an expired streak does not log works again", () => | | 702 | + Effect.gen(function* () { | | 703 | + yield* startAt(NOW_MS); | | 704 | + let signedOut = false; | | 705 | + const fake = fakeTarget(provider(NOW_MS - HOUR), { checksOnInterval: () => !signedOut }); | | 706 | + fake.setOutcome("noReading"); | | 707 | + const log = logCollector(); | | 708 | + const { sweep } = yield* make(deps([fake.target])); | | 709 | + const at = (ms: number) => | | 710 | + TestClock.setTime(NOW_MS + ms).pipe(Effect.andThen(sweep), Effect.provide(log.layer)); | | 711 | + yield* at(0); | | 712 | + signedOut = true; | | 713 | + fake.setSnapshot(provider(NOW_MS, { auth: { status: "unauthenticated" } })); | | 714 | + yield* at(36 * HOUR); | | 715 | + signedOut = false; | | 716 | + fake.setSnapshot(provider(NOW_MS + 72 * HOUR - HOUR)); | | 717 | + fake.setOutcome("ok"); | | 718 | + yield* at(72 * HOUR); | | 719 | + assert.equal(fake.calls(), 2); | | 720 | + assert.deepEqual(log.infos, []); | | 721 | + // The streak is gone: the next failure is a new outage, logged again. | | 722 | + fake.setOutcome("noReading"); | | 723 | + yield* at(73 * HOUR + INTERVAL); | | 724 | + assert.equal(fake.calls(), 3); | | 725 | + assert.equal(log.warnings.length, 2); | | 726 | + }), | | 727 | + ); | | 728 | + | | 729 | + it.effect("a sweep in the same millisecond as a failed refresh waits out the backoff", () => | | 730 | + Effect.gen(function* () { | | 731 | + yield* startAt(NOW_MS); | | 732 | + // An error leaves the last check an hour old, so the instance stays due. | | 733 | + const fake = fakeTarget(provider(NOW_MS - HOUR)); | | 734 | + fake.setOutcome("error"); | | 735 | + const { sweep } = yield* make(deps([fake.target])); | | 736 | + yield* sweep; | | 737 | + yield* sweep; | | 738 | + assert.equal(fake.calls(), 1); | | 739 | + yield* TestClock.adjust(2 * INTERVAL); | | 740 | + yield* sweep; | | 741 | + assert.equal(fake.calls(), 2); | | 742 | + }), | | 743 | + ); | | 744 | + | 621 | 745 | it("backoff doubles and stops at two hours (or the interval, if longer)", () => { | 622 | 746 | assert.equal(backoffMs(INTERVAL, 1), 2 * INTERVAL); | 623 | 747 | assert.equal(backoffMs(INTERVAL, 3), 8 * INTERVAL); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/HeadlessUsageRefresher.ts +17-2Lines changed: 17 additions & 2 deletions | 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,9 +150,15 @@ 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 | | - yield* endStreak(target.instanceId); | | 159 | + // An expired streak is already over: drop it without a "works again". | | 160 | + if (previous === undefined) failing.delete(target.instanceId); | | 161 | + else yield* endStreak(target.instanceId); | 147 | 162 | return; | 148 | 163 | } | 149 | 164 | const failures = (previous?.failures ?? 0) + 1; | |
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 | () => | |
|