Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/budget-window-four-years.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API v2 runs at most four years of a budget window at once instead of five: the simulation service starts up to a new window's first four years, and each year's coordinator, when its year ends, starts the earliest waiting years until four are under way (running, or started and not yet running). The container limits of API v2's Modal functions are unchanged, so they now have room for three windows at four years each beside 13 other reports.
8 changes: 4 additions & 4 deletions docs/migration/stage-12-modal-worker-foundation.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ simulation submission or polling contracts.
report starts 40 region-group calculation calls. These are invocations;
Modal may reuse a warm container for more than one invocation.
- The report coordinator has a Modal `max_containers` value of 25: three
budget windows at five years each, and ten other reports. Each
budget windows at four years each, and 13 other reports. Each
country-specific single-simulation function has 50, both simulations of
every coordinator call, and the separate US region-group function has
1,000, every segment of every US simulation call, so calculation work
Expand Down Expand Up @@ -521,9 +521,9 @@ single-year report's runs, and the reverse.
input and context from the first year's: only the year, its three runs, the
artifact prefix (`report_run_artifact_prefix`) and the call's creation time
differ.
- **At most five at once.** `BudgetWindowStore.advance_window` locks the
- **At most four at once.** `BudgetWindowStore.advance_window` locks the
window run's row, then starts the earliest waiting years (pending with no
call) until five are under way (running, or started with a call recorded),
call) until four are under way (running, or started with a call recorded),
recording each call while the year is still pending with no call. The lock
makes advances that run at once start each year once; a year a duplicate
call reaches is claimed by only one call.
Expand All @@ -549,7 +549,7 @@ single-year report's runs, and the reverse.
when they have all ended, or as stalled (`budget_window_stalled`) when years
are still waiting.

The coordinator may run 25 calls at once (three windows at five years, and ten
The coordinator may run 25 calls at once (three windows at four years, and 13
other reports), so each simulation worker may run 50 and the US segment
function 1,000.

Expand Down
2 changes: 1 addition & 1 deletion libs/policyengine-stage12-persistence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ and ends the process with a failure if the check runs for 15 minutes.
advances the window. An advance locks the window run's row, then fails the
window and its waiting years when a year failed, marks it succeeded when
every year succeeded, or starts the earliest waiting years until
`BUDGET_WINDOW_MAX_RUNNING_YEARS` (five) are under way, recording each
`BUDGET_WINDOW_MAX_RUNNING_YEARS` (four) are under way, recording each
call while the year is still pending with no call. A year's coordinator
call marks its window running. The stuck-run sweep leaves a waiting year
to its window, fails the window of a year it fails, and settles a window
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

# The most years of one budget window under way at once: running, or started
# with a Modal call recorded and waiting for Modal to run it.
BUDGET_WINDOW_MAX_RUNNING_YEARS = 5
BUDGET_WINDOW_MAX_RUNNING_YEARS = 4

# Each failure records a fixed code, then a summary that holds no
# calculation detail; a failed year's own message stays on that year's run.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ def test_an_advance_starts_the_earliest_waiting_years(
assert advance == WindowAdvance(
status=RunStatus.PENDING,
changed=False,
started_years=(2026, 2027, 2028, 2029, 2030),
started_years=(2026, 2027, 2028, 2029),
)
assert starter.started == [2026, 2027, 2028, 2029, 2030]
assert starter.started == [2026, 2027, 2028, 2029]
years = windows.year_rows(window)
assert years[2026]["modal_call_id"].startswith("fc-started-2026")
assert years[2031]["modal_call_id"] is None
assert years[2030]["modal_call_id"] is None
assert statements.commits == 1
[lock] = [sent for sent in statements.sent if sent.endswith("FOR UPDATE")]
assert "public.budget_window_runs" in lock
Expand All @@ -122,13 +122,13 @@ def advance(_advancer: int) -> WindowAdvance:
with ThreadPoolExecutor(max_workers=advancers) as executor:
list(executor.map(advance, range(advancers)))

assert sorted(starter.started) == [2026, 2027, 2028, 2029, 2030]
assert sorted(starter.started) == [2026, 2027, 2028, 2029]
started = [
year
for year, row in windows.year_rows(window).items()
if row["modal_call_id"] is not None
]
assert sorted(started) == [2026, 2027, 2028, 2029, 2030]
assert sorted(started) == [2026, 2027, 2028, 2029]


def test_a_refused_start_fails_the_year_and_the_window(store, windows, window) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A budget window's years are report runs naming its budget-window run. The
simulation service creates them all pending and starts the first ones; each
year's coordinator, when its year ends, starts the earliest years still
waiting, keeping at most five under way, and the last year to end marks the
waiting, keeping at most four under way, and the last year to end marks the
window. These run against a disposable Postgres that the Public API's v2
migrations created (see ``report_run_database``).
"""
Expand Down Expand Up @@ -116,35 +116,35 @@ def _succeeded(windows, window, years) -> None:
)


def test_the_window_runs_at_most_five_years_at_once() -> None:
assert BUDGET_WINDOW_MAX_RUNNING_YEARS == 5
def test_the_window_runs_at_most_four_years_at_once() -> None:
assert BUDGET_WINDOW_MAX_RUNNING_YEARS == 4


def test_the_first_advance_starts_the_five_earliest_years(
def test_the_first_advance_starts_the_four_earliest_years(
store, windows, window
) -> None:
starter = RecordingStarter()

advance = store.advance_window(window.window_run_id, now=LATER, start_year=starter)

assert starter.started == _run_ids(window, range(2026, 2031))
assert starter.started == _run_ids(window, range(2026, 2030))
assert advance == WindowAdvance(
status=RunStatus.PENDING,
changed=False,
started_years=(2026, 2027, 2028, 2029, 2030),
started_years=(2026, 2027, 2028, 2029),
)
rows = windows.year_rows(window)
assert [rows[year]["modal_call_id"] for year in ALL_YEARS] == [
"fc-started-1",
"fc-started-2",
"fc-started-3",
"fc-started-4",
"fc-started-5",
None,
None,
None,
]
assert {rows[year]["status"] for year in ALL_YEARS} == {"pending"}
assert [rows[year]["updated_at"] for year in range(2026, 2031)] == [LATER] * 5
assert [rows[year]["updated_at"] for year in range(2026, 2030)] == [LATER] * 4
assert windows.window_row(window)["status"] == "pending"


Expand Down Expand Up @@ -177,9 +177,30 @@ def test_an_advance_fills_only_the_free_places(store, windows, window) -> None:

advance = store.advance_window(window.window_run_id, now=LATER, start_year=starter)

assert starter.started == _run_ids(window, [2030, 2031])
assert advance.started_years == (2030, 2031)
assert windows.year_rows(window)[2032]["modal_call_id"] is None
# Three years are under way, so one place is free.
assert starter.started == _run_ids(window, [2030])
assert advance.started_years == (2030,)
rows = windows.year_rows(window)
assert [rows[year]["modal_call_id"] for year in (2031, 2032)] == [None, None]


def test_an_advance_with_more_years_under_way_than_the_limit_starts_none(
store, windows, window
) -> None:
# Coordinator calls already running when the limit dropped from five
# may have started one year more than the limit.
under_way = range(2026, 2026 + BUDGET_WINDOW_MAX_RUNNING_YEARS + 1)
windows.set_years(window, under_way, status="running", modal_call_id="fc-run")
starter = RecordingStarter()

advance = store.advance_window(window.window_run_id, now=LATER, start_year=starter)

assert starter.started == []
assert advance == WindowAdvance(status=RunStatus.PENDING, changed=False)
rows = windows.year_rows(window)
waiting = [year for year in ALL_YEARS if year not in under_way]
assert waiting
assert [rows[year]["modal_call_id"] for year in waiting] == [None] * len(waiting)


def test_an_advance_without_a_starter_starts_nothing(store, windows, window) -> None:
Expand Down Expand Up @@ -391,7 +412,7 @@ def claim_then_time_out(year_to_start: YearToStart) -> str:
assert advance == WindowAdvance(
status=RunStatus.PENDING,
changed=False,
started_years=(2026, 2027, 2028, 2029, 2030),
started_years=(2026, 2027, 2028, 2029),
)
rows = windows.year_rows(window)
assert (rows[2026]["status"], rows[2026]["modal_call_id"]) == ("running", "fc-late")
Expand Down Expand Up @@ -471,13 +492,12 @@ def advance() -> WindowAdvance:
with ThreadPoolExecutor(max_workers=2) as pool:
advances = list(pool.map(lambda _: advance(), range(2)))

assert sorted(starter.started) == sorted(_run_ids(window, range(2026, 2031)))
assert sorted(starter.started) == sorted(_run_ids(window, range(2026, 2030)))
assert sorted(year for finished in advances for year in finished.started_years) == [
2026,
2027,
2028,
2029,
2030,
]


Expand Down Expand Up @@ -648,8 +668,8 @@ def test_a_reports_later_window_run_starts_only_its_own_years(

advance = store.advance_window(rerun.window_run_id, now=LATER, start_year=starter)

assert starter.started == _run_ids(rerun, range(2026, 2031))
assert advance.started_years == (2026, 2027, 2028, 2029, 2030)
assert starter.started == _run_ids(rerun, range(2026, 2030))
assert advance.started_years == (2026, 2027, 2028, 2029)
assert windows.window_row(window)["status"] == "succeeded"
assert {row["status"] for row in windows.year_rows(window).values()} == {
"succeeded"
Expand Down
4 changes: 2 additions & 2 deletions projects/policyengine-simulation-entry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ that hasn't failed and isn't a recalculation, or else creates one
(recording its .py version) and, for each year, a report run naming it and
the year, on each side's simulation run of that year, found or inserted.
Only for a window run it created does it then start up to the earliest
five years, as its own role, under the window run's row lock
four years, as its own role, under the window run's row lock
(`BudgetWindowStore.advance_window`): the first year's coordinator input is
built as an economy report's, and each later year's is derived from it. A
joined window run is answered as it is, with no new call.
Expand Down Expand Up @@ -441,7 +441,7 @@ A window created by key with no window run, as `POST /reports/without-run`
creates one, has each year's simulations found by their keys instead, when
the report's stored key is still the key its stored fields give, so its
first re-run is its first calculation.
Only a window run the re-run created has its first years started. Up to five years start at once, as a new window's do (a later year
Only a window run the re-run created has its first years started. Up to four years start at once, as a new window's do (a later year
Modal doesn't confirm in time waits for an earlier year's coordinator); it
answers 201 with the budget-window create's fields, naming the window run
and every year.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(``find_or_create_budget_window``), all pending with no Modal call. Only
for a window run it created does the service then advance the window as
its own role (``BudgetWindowStore.advance_window``): under the window
run's row lock, up to the earliest five years' coordinators are started
run's row lock, up to the earliest four years' coordinators are started
and their calls recorded. Each year's coordinator starts the next ones when
its year ends. A joined window run is answered as it is.

Expand Down Expand Up @@ -135,7 +135,7 @@ def advance_window(
now: datetime,
start_year: YearStarter | None = None,
) -> WindowAdvance:
"""Start the earliest waiting years, at most five under way."""
"""Start the earliest waiting years, at most four under way."""
...


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
calculates both simulations again, as an extra run marked forced that
nothing joins.
A budget-window report's run is a budget-window run with a report run
per year, a new one's first five started at once as a new window's are;
per year, up to a new one's first four started at once as a new window's are;
it answers as the budget-window create does, naming the window run and
every year's runs. A household report's re-run joins or creates its run as an
economy report's does, each side's run of its own household simulation,
Expand Down Expand Up @@ -306,8 +306,8 @@ async def answer( # the route, its record and how to re-run it
"create does; a budget window's or household report's re-run "
"asking for cliffs is refused (422 cliffs_not_available). A "
"budget-window report's run is a "
"budget-window run with a report run per year, a new one's first "
"five started at once, each year on its own year's simulations. "
"budget-window run with a report run per year, up to a new one's "
"first four started at once, each year on its own year's simulations. "
"A household report's run calculates its stored household in the "
"report's year, in one household function call."
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
``budget_window`` it finds or creates a budget-window report by key, each
year's simulations by their year's keys, and joins its window run on the
current versions or creates one with a report run per year, starting up
to the first five years only of a window run it created, answering 201
to the first four years only of a window run it created, answering 201
with the report, the first year's run, the window run, its status and
every year's runs. With
``household`` it finds or creates a household report and a household
Expand Down Expand Up @@ -563,7 +563,7 @@ def _submission_routes(
"include_cliffs is never stored or keyed. "
"A budget-window report (report_type budget_window) compares them "
"in each year of a window of 1 to 75 years, with a report run per "
"year, at most five running at once, found or created by key as "
"year, at most four running at once, found or created by key as "
"an economy report is: its window run on the current versions is "
"joined, and each year's sides join their simulations' runs, which "
"a single-year report of that year shares. "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Creating a budget-window report and starting its first years.

The service creates every year's runs in one transaction, then advances the
window, which starts the earliest years (at most five) by calling the
window, which starts the earliest years (at most four) by calling the
coordinator with each year's input. The first year's input is built as a
single-year report's is, and every later year's is derived from it. The
store and the window advance are faked here; both are tested against
Expand Down Expand Up @@ -214,7 +214,7 @@ def _submit(submissions, **changes):
)


def test_a_window_is_created_with_a_run_per_year_and_its_first_five_started() -> None:
def test_a_window_is_created_with_a_run_per_year_and_its_first_four_started() -> None:
store = FakeWindowStore()
caller = FakeCaller()
advancer = FakeAdvancer(store)
Expand Down Expand Up @@ -244,8 +244,14 @@ def test_a_window_is_created_with_a_run_per_year_and_its_first_five_started() ->
assert report.population == CATALOG.population(2026)
assert report.inputs == {"spm": {"geography_kind": "national"}}
assert advancer.advances == [report.window_run_id]
# The first five years were started, each with its own year and runs.
assert len(caller.report_runs) == 5
# The first four years were started, each with its own year and runs.
assert [year_to_start.year for year_to_start, _ in advancer.started] == [
2026,
2027,
2028,
2029,
]
assert len(caller.report_runs) == 4
for (year_to_start, call_id), (_, payload, context, observed) in zip(
advancer.started, caller.report_runs, strict=True
):
Expand Down Expand Up @@ -431,7 +437,7 @@ async def start_report_run(self, worker, report_payload, context_payload, **kwar


def test_the_first_years_share_one_wait_for_modal() -> None:
# A request starting five years one after another never waits more than
# A request starting several years one after another never waits more than
# one call's limit: a later year not started in time is left for the
# started year's coordinator to start.
store = FakeWindowStore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def run_single_simulation_uk(
# the production-result wait, and time to finish and record the report.
timeout=COORDINATOR_TIMEOUT_SECONDS,
retries=0,
# Three budget windows at five years each, and ten other reports.
# Three budget windows at four years each, and 13 other reports.
max_containers=25,
secrets=report_run_writer_secrets,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
simulation service creates every year and starts the first ones. A year's
coordinator call marks the window running before it claims its year and,
once the year has ended, advances the window: it starts the earliest waiting years
in its own Modal application, so that at most five are under way, or marks
the window failed or succeeded (see ``BudgetWindowStore``).
in its own Modal application, so that at most ``BUDGET_WINDOW_MAX_RUNNING_YEARS``
are under way, or marks the window failed or succeeded (see
``BudgetWindowStore``).

Neither step changes the year's own outcome. A failed write is logged,
without the error's text, and the stuck-run sweep finishes a window that
Expand Down Expand Up @@ -127,9 +128,10 @@ def coordinate_window_year[CoordinatorResult](
years in this application. A pre-emption advances nothing: Modal runs
the call again, and the restarted call advances when it ends, even when
it finds its year already finished. Advancing more than once is safe,
because an advance starts only waiting years and never more than five
under way. A report run outside any window, a call naming no report run,
or a store that cannot be built makes no window writes. When the lookup
because an advance starts only waiting years and never more than
``BUDGET_WINDOW_MAX_RUNNING_YEARS`` under way. A report run outside any
window, a call naming no report run, or a store that cannot be built
makes no window writes. When the lookup
itself fails, nothing is marked and the window is looked up again once
the year ends.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from test_stage12_modal_app import _load, declared_modal_options

# Budget windows the coordinator has room for at once, each at its cap,
# beside the coordinator calls it had for other reports.
# beside coordinator calls for other reports.
WINDOWS_AT_ONCE = 3
OTHER_REPORTS = 10
OTHER_REPORTS = 13
SIMULATIONS_PER_REPORT = 2


Expand Down
Loading