diff --git a/CONTRACTS.md b/CONTRACTS.md index a226d26dc..b3da2335a 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -165,7 +165,8 @@ running Iris is the pack publisher's responsibility. - Keep one `PackWorld` per world and pack snapshot and pass it to every invocation of the world (`InvocationContext.world()`), to service calls and to hooks. Use `WorldStates` (or the same rules) for its store. - Call the hooks with `LifecycleRunner`: `packLoaded` once per pack snapshot before its first world starts, - `worldStarted` before the world generates anything, `worldStopped` when it stops. On a hotload, and when the pack's + `worldStarted` before the world generates anything, `warmUp` after it (on a thread of your choice, and wait for it before + the world's first generation; report a failure naming the module and carry on), `worldStopped` when it stops. On a hotload, and when the pack's resources are read again while worlds run, stop each world, load the new snapshot and start the world again with a new store. Close a stopped world's store only once no invocation uses it any more. - A failed `packLoaded` or `worldStarted` keeps the world from starting and names the module; report `worldStopped` diff --git a/DESIGN.md b/DESIGN.md index 4e12dd4ef..db7b4b422 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -858,7 +858,8 @@ each world a state store and tells modules when worlds start and stop. on its own thread, since creation holds one re-entrant lock, and asking for the type being created fails naming the chain instead of recursing or deadlocking. A factory that throws keeps nothing. Reads of existing state take no lock. `WorldStates` is the reference store hosts use. -- **Hooks.** `ModuleLifecycle` has `packLoaded(resources, settings)`, `worldStarted(world)` and `worldStopped(world)`, +- **Hooks.** `ModuleLifecycle` has `packLoaded(resources, settings)`, `worldStarted(world)`, `warmUp(world)` and + `worldStopped(world)`, each a no-op by default, registered with `FeatureRegistry.registerLifecycle` like validators, so the host knows the owning module for errors and orders hooks by module id. A module class may implement it and register itself. `packLoaded` runs once per snapshot before its first world starts; a pack that is only validated or edited in studio @@ -871,8 +872,16 @@ each world a state store and tells modules when worlds start and stop. - **Failures.** A failing `packLoaded` or `worldStarted` keeps the world from starting, naming the module, and the modules that had started get `worldStopped`. Every `worldStopped` runs even when one fails; the failures are reported together. `LifecycleRunner` implements these rules for hosts. -- **Threading.** Hooks of one world run one at a time on the thread that starts or stops it, in module id order and - stops in reverse. State is safe from any thread. +- **Warm-up.** `warmUp` is a second start hook for caches that take long to build. `worldStarted` runs on the thread + that starts the world and delays it, so a studio host would stall its start; `warmUp` lets the host pick: wait for it + on the starting thread (server worlds, which must not generate before the caches exist) or run it in the background + and make the first generation wait (studio, where the world is usable at once). A world therefore never generates + with half-built caches, and a module need not guard its own cache against a still-running warm-up beyond the + state store's rules. It only builds caches: a failure is reported per module and does not stop the world, since the + cache is built on first use anyway. `LifecycleRunner.warmUp` runs every module's hook even when one fails. +- **Threading.** `packLoaded`, `worldStarted` and `worldStopped` of one world run one at a time on the thread that + starts or stops it, in module id order and stops in reverse; `warmUp` runs where the host chose, after + `worldStarted`. State is safe from any thread. - **Not added (yet):** a pack-level store shared by the worlds of one snapshot. Every known consumer (decoration streams, warm-up, placement workers, structure indexes) is per world; per-world state built from pack data costs a little duplication when several worlds share a pack. diff --git a/README.md b/README.md index 9e83ee724..a74ed6952 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,10 @@ public final class RuinsModule implements IrisModule, ModuleLifecycle { holds every entry of the whole pack. - `worldStarted(world)` runs before the world generates anything, `worldStopped(world)` when it stops; its state is still readable there. +- `warmUp(world)` runs after every module's `worldStarted` and may be slow: build caches in the world's state there, + not in `worldStarted`. The host may run it off the main thread and waits for it before the world's first + generation. A failing `warmUp` does not stop the world; the host reports it naming the module and the cache is built + when first needed, so never put anything there the world cannot do without. - A hotload, or a reload of the pack's resources while worlds run (a tool rewrote files), stops each world, loads the new snapshot and starts the worlds again with empty state. State never outlives the resources it was built from. - Hooks run in module id order, stops in reverse. A failing `packLoaded` or `worldStarted` stops the world from @@ -642,6 +646,7 @@ The host keeps one `PackWorld` per world and pack snapshot. `WorldStates` is the var lifecycles = new LifecycleRunner(pack.catalog()); lifecycles.packLoaded(resources, packSettings); // once per snapshot, before its first world starts lifecycles.worldStarted(world); // then hand world to every invocation of the world +lifecycles.warmUp(world); // slow cache building, on any thread, before the first generation ... lifecycles.worldStopped(world); // then close the world's WorldStates diff --git a/src/main/java/com/volmit/ModuleLifecycle.java b/src/main/java/com/volmit/ModuleLifecycle.java index 0ce81ea38..182b2035c 100644 --- a/src/main/java/com/volmit/ModuleLifecycle.java +++ b/src/main/java/com/volmit/ModuleLifecycle.java @@ -9,7 +9,8 @@ import com.volmit.world.PackWorld; * register itself. * *

Order: {@link #packLoaded} for a pack snapshot before the first world using it starts, then - * {@link #worldStarted} before that world generates anything, and {@link #worldStopped} when it stops. A hotload, or a + * {@link #worldStarted} before that world generates anything, then {@link #warmUp} (a slow worldStarted the host may + * run off the main thread), and {@link #worldStopped} when it stops. A hotload, or a * reload of the pack's resources while worlds run, stops each world of the old snapshot, loads the new snapshot and * starts its worlds again, with empty state (see {@link PackWorld}). The host calls the hooks of a pack's modules in * module id order and stops them in reverse, one hook at a time, on the thread that starts or stops the world. @@ -30,6 +31,17 @@ public interface ModuleLifecycle { */ default void worldStarted(PackWorld world) {} + /** {@code world} started: builds the caches that make its first chunks fast, which may be slow. Runs after + * {@link #worldStarted} of every module. The host may run it off the main thread: synchronously for a server's + * worlds, in the background for studio. It waits for every module's warm-up before the world's first generation, so + * a hook that is slow delays that, not the start of the world. A world that stops before its warm-up runs may skip it. + * + *

Only build caches in the module's world state ({@link PackWorld#state}): nothing here may change what the world + * generates, because the module also works when the warm-up never ran. A hook that throws does not stop the world; + * the host reports the failure, naming the module, and the cache is built when first needed. + */ + default void warmUp(PackWorld world) {} + /** {@code world} stops: it generates nothing more with this pack snapshot. Its state is still readable here and is * closed afterwards. The host runs every module's hook even when one throws, then reports the failures. */ diff --git a/src/main/java/com/volmit/host/LifecycleRunner.java b/src/main/java/com/volmit/host/LifecycleRunner.java index 6d2769689..7ccdc8782 100644 --- a/src/main/java/com/volmit/host/LifecycleRunner.java +++ b/src/main/java/com/volmit/host/LifecycleRunner.java @@ -13,6 +13,7 @@ import java.util.Objects; * var lifecycles = new LifecycleRunner(pack.catalog()); * lifecycles.packLoaded(resources, packSettings); // once per pack snapshot, before its first world starts * lifecycles.worldStarted(world); // before the world generates anything + * lifecycles.warmUp(world); // then, possibly on another thread, before its first generation * ... * lifecycles.worldStopped(world); // then close the world's state * } @@ -60,6 +61,30 @@ public final class LifecycleRunner { } } + /** Runs every {@code warmUp} hook in order, each even when an earlier one threw; afterwards throws the first + * failure, naming its module, with the later ones suppressed. The caller picks the thread and waits for this + * before the world generates. + */ + public void warmUp(PackWorld world) { + Objects.requireNonNull(world, "world"); + IllegalStateException failure = null; + for (var registration : lifecycles) { + try { + registration.lifecycle().warmUp(world); + } catch (RuntimeException warmFailure) { + var named = failed("warming up a world", registration, warmFailure); + if (failure == null) { + failure = named; + } else { + failure.addSuppressed(named); + } + } + } + if (failure != null) { + throw failure; + } + } + /** Runs every {@code worldStopped} hook in reverse order, each even when an earlier one threw; afterwards throws the * first failure, naming its module, with the later ones suppressed. */ diff --git a/src/test/java/com/volmit/host/LifecycleRunnerTest.java b/src/test/java/com/volmit/host/LifecycleRunnerTest.java index 52032aa34..c3ac6d1cb 100644 --- a/src/test/java/com/volmit/host/LifecycleRunnerTest.java +++ b/src/test/java/com/volmit/host/LifecycleRunnerTest.java @@ -28,6 +28,7 @@ class LifecycleRunnerTest { registry.registerLifecycle(new ModuleLifecycle() { public void packLoaded(PackResources resources, PackSettings settings) { record("loaded"); } public void worldStarted(PackWorld world) { record("started"); } + public void warmUp(PackWorld world) { record("warmed"); } public void worldStopped(PackWorld world) { record("stopped"); } private void record(String hook) { @@ -86,6 +87,45 @@ class LifecycleRunnerTest { assertEquals(List.of("stopped test:gamma", "stopped test:beta", "stopped test:alpha"), calls); } + @Test + void warmsUpInModuleIdOrderAfterTheModulesStarted() { + var calls = new ArrayList(); + var runner = runner(module("test:zeta", calls, null), module("test:alpha", calls, null)); + try (var world = new TestWorld()) { + runner.worldStarted(world); + runner.warmUp(world); + } + + assertEquals(List.of("started test:alpha", "started test:zeta", "warmed test:alpha", "warmed test:zeta"), calls); + } + + @Test + void everyModuleWarmsUpEvenWhenOneFailsAndEveryFailureIsReported() { + var calls = new ArrayList(); + var runner = runner(module("test:alpha", calls, "warmed"), module("test:beta", calls, null), module("test:gamma", calls, "warmed")); + try (var world = new TestWorld()) { + var failure = assertThrows(IllegalStateException.class, () -> runner.warmUp(world)); + assertTrue(failure.getMessage().startsWith("Module test:alpha from host classpath failed while warming up a world"), + failure.getMessage()); + assertEquals(1, failure.getSuppressed().length); + assertTrue(failure.getSuppressed()[0].getMessage().startsWith("Module test:gamma")); + } + + assertEquals(List.of("warmed test:alpha", "warmed test:beta", "warmed test:gamma"), calls); + } + + @Test + void modulesWithoutAWarmUpHookWarmUpNothing() { + var module = new IrisModule() { + public ModuleInfo info() { return ModuleInfo.of("test:plain", "Plain", "1"); } + public void install(StageGraph graph) {} + public void register(FeatureRegistry registry) { registry.registerLifecycle(new ModuleLifecycle() {}); } + }; + try (var world = new TestWorld()) { + runner(module).warmUp(world); + } + } + @Test void aFailedPackLoadNamesTheModuleAndSkipsTheRest() { var calls = new ArrayList();