mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Run module warm-ups with the generation cache warm-up, and for hotloaded and restarted worlds
IrisEngine warms modules after GenerationCacheWarmer for the first world (studio in the background, server worlds synchronously) and after every hotload or in-place restart; generation waits for all of them through GenerationCacheWarmGate.
This commit is contained in:
1 parent
d6228c3074
commit
2775a4965b
7 files changed
+367
-24
No files matched your search
@@ -86,6 +86,26 @@ public final class ModuleWorlds {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the modules' warm-up of the started world; does nothing before {@link #start()}, after {@link #close()}, or
|
||||
* when the pack's modules failed to load. May be slow: callers choose the thread. The lock is not held while it
|
||||
* runs, so a restart or close is not delayed by it.
|
||||
*
|
||||
* @throws IllegalStateException naming the modules whose warm-up failed
|
||||
*/
|
||||
public void warmUp() {
|
||||
LifecycleRunner lifecycles;
|
||||
IrisPackWorld world;
|
||||
synchronized (this) {
|
||||
lifecycles = startedWith;
|
||||
world = current;
|
||||
}
|
||||
if (lifecycles == null || world == null) {
|
||||
return;
|
||||
}
|
||||
lifecycles.warmUp(world);
|
||||
}
|
||||
|
||||
/** Stops the world, if it started, and closes the state of the current and the previously retired world. */
|
||||
public synchronized void close() {
|
||||
if (phase == Phase.CLOSED) {
|
||||
|
||||
@@ -123,6 +123,7 @@ final class EngineHotloader {
|
||||
EngineRuntime nextRuntime = engine.runtimeBuilder.buildRuntime(replacementTarget);
|
||||
engine.runtimeBuilder.publishRuntime(nextRuntime, previousRuntime);
|
||||
published = true;
|
||||
engine.warmRestartedWorld();
|
||||
previousData.unregisterEngine(engine);
|
||||
Throwable previousDataFailure = runCleanup(null, previousData::close);
|
||||
if (previousDataFailure != null) {
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.engine;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* Whether the engine's worlds have warmed their generation caches, which generation waits for. The first world is warmed
|
||||
* by engine initialisation: the host's caches ({@link GenerationCacheWarmer}), then the modules' warm-up
|
||||
* ({@link com.volmit.ModuleLifecycle#warmUp}). Worlds started after that, by a hotload or an in-place restart, only have
|
||||
* the modules' warm-up; each one adds a warm-up that generation also waits for.
|
||||
*/
|
||||
final class GenerationCacheWarmGate {
|
||||
private final CompletableFuture<Void> firstWorld = new CompletableFuture<>();
|
||||
private final AtomicReference<CompletableFuture<Void>> restartedWorlds = new AtomicReference<>(CompletableFuture.completedFuture(null));
|
||||
|
||||
/** Runs both warm-ups on this thread, the host's first; the first world is warm once both returned. A failure leaves it failed. */
|
||||
void warmFirstWorld(Runnable hostWarmUp, Runnable moduleWarmUp) {
|
||||
try {
|
||||
hostWarmUp.run();
|
||||
moduleWarmUp.run();
|
||||
firstWorld.complete(null);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
firstWorld.completeExceptionally(failure);
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
/** The first world needs no warm-up. */
|
||||
void skipFirstWorld() {
|
||||
firstWorld.complete(null);
|
||||
}
|
||||
|
||||
/** Makes generation wait for a world started after the first, until the returned future completes. */
|
||||
CompletableFuture<Void> beginRestartedWorld() {
|
||||
CompletableFuture<Void> warm = new CompletableFuture<>();
|
||||
restartedWorlds.updateAndGet(previous -> CompletableFuture.allOf(previous, warm));
|
||||
return warm;
|
||||
}
|
||||
|
||||
/** Blocks until every world started so far is warm. */
|
||||
void await() {
|
||||
await(firstWorld);
|
||||
await(restartedWorlds.get());
|
||||
}
|
||||
|
||||
boolean isPending() {
|
||||
return !firstWorld.isDone() || !restartedWorlds.get().isDone();
|
||||
}
|
||||
|
||||
private static void await(CompletableFuture<Void> warm) {
|
||||
if (warm.isDone() && !warm.isCompletedExceptionally()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
warm.join();
|
||||
} catch (CompletionException failure) {
|
||||
throw new IllegalStateException("Iris generation caches did not become ready.", failure.getCause());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,6 @@ import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -95,7 +94,9 @@ public class IrisEngine implements Engine {
|
||||
private final ChronoLatch perSecondLatch;
|
||||
private final ChronoLatch perSecondBudLatch;
|
||||
private final EngineMetrics metrics;
|
||||
private final CompletableFuture<Void> generationCacheWarm;
|
||||
@Getter(AccessLevel.NONE)
|
||||
@Setter(AccessLevel.NONE)
|
||||
private final GenerationCacheWarmGate generationCacheWarm = new GenerationCacheWarmGate();
|
||||
private final boolean studio;
|
||||
private final AtomicRollingSequence wallClock;
|
||||
@Getter(AccessLevel.NONE)
|
||||
@@ -188,7 +189,6 @@ public class IrisEngine implements Engine {
|
||||
bud = new AtomicInteger(0);
|
||||
buds = new AtomicInteger(0);
|
||||
metrics = new EngineMetrics(32);
|
||||
generationCacheWarm = new CompletableFuture<>();
|
||||
cleanLatch = new ChronoLatch(10000);
|
||||
generatedLast = new AtomicInteger(0);
|
||||
perSecond = new AtomicDouble(0);
|
||||
@@ -242,7 +242,7 @@ public class IrisEngine implements Engine {
|
||||
warmGenerationCaches(phaseStartedAt);
|
||||
}
|
||||
} else {
|
||||
generationCacheWarm.complete(null);
|
||||
generationCacheWarm.skipFirstWorld();
|
||||
logStudioInitializationPhase("generation_cache_warm", phaseStartedAt, true);
|
||||
}
|
||||
EngineTickRegistry.registerTicking(this);
|
||||
@@ -254,18 +254,49 @@ public class IrisEngine implements Engine {
|
||||
}
|
||||
|
||||
public void awaitGenerationCacheWarm() {
|
||||
if (generationCacheWarm.isDone() && !generationCacheWarm.isCompletedExceptionally()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
generationCacheWarm.join();
|
||||
} catch (CompletionException failure) {
|
||||
throw new IllegalStateException("Iris generation caches did not become ready.", failure.getCause());
|
||||
}
|
||||
generationCacheWarm.await();
|
||||
}
|
||||
|
||||
public boolean isGenerationCacheWarmPending() {
|
||||
return !generationCacheWarm.isDone();
|
||||
return generationCacheWarm.isPending();
|
||||
}
|
||||
|
||||
/**
|
||||
* Warms the modules' caches of a world that started after the engine initialised: the one a hotload built, or the
|
||||
* one an in-place restart replaced. The first world is warmed by initialisation, together with
|
||||
* {@link GenerationCacheWarmer}. Same mode as there: studio warms in the background, server worlds before this
|
||||
* returns, and generation waits for it either way.
|
||||
*/
|
||||
void warmRestartedWorld() {
|
||||
if (!initializationMode.warmGenerationCaches()) {
|
||||
return;
|
||||
}
|
||||
CompletableFuture<Void> warm = generationCacheWarm.beginRestartedWorld();
|
||||
if (!studio) {
|
||||
warmModulesThenComplete(warm);
|
||||
return;
|
||||
}
|
||||
if (!backgroundTasks.scheduleTrackedTask(() -> warmModulesThenComplete(warm))) {
|
||||
warm.complete(null);
|
||||
IrisLogging.warn("Iris background task admission closed before module warm-up; caches are built when first used.");
|
||||
}
|
||||
}
|
||||
|
||||
private void warmModulesThenComplete(CompletableFuture<Void> warm) {
|
||||
try {
|
||||
warmModules();
|
||||
} finally {
|
||||
warm.complete(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void warmModules() {
|
||||
try {
|
||||
getGeneration().warmModules();
|
||||
} catch (RuntimeException failure) {
|
||||
IrisLogging.reportError(failure);
|
||||
IrisLogging.warn("Module warm-up failed, so its caches are built when first used: " + failure.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void logStudioInitializationPhase(String phase, long startedAtNanos, boolean skipped) {
|
||||
@@ -289,17 +320,7 @@ public class IrisEngine implements Engine {
|
||||
private void warmGenerationCaches(long phaseStartedAtNanos) {
|
||||
long startedAtMillis = M.ms();
|
||||
try {
|
||||
GenerationCacheWarmer.warm(this);
|
||||
generationCacheWarm.complete(null);
|
||||
} catch (Throwable failure) {
|
||||
generationCacheWarm.completeExceptionally(failure);
|
||||
if (failure instanceof Error error) {
|
||||
throw error;
|
||||
}
|
||||
if (failure instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
throw new IllegalStateException("Generation cache warming failed.", failure);
|
||||
generationCacheWarm.warmFirstWorld(() -> GenerationCacheWarmer.warm(this), this::warmModules);
|
||||
} finally {
|
||||
IrisLogging.debug("[IrisEngine timing] cache warm total=" + (M.ms() - startedAtMillis) + "ms");
|
||||
logStudioInitializationPhase("generation_cache_warm", phaseStartedAtNanos, false);
|
||||
@@ -516,6 +537,7 @@ public class IrisEngine implements Engine {
|
||||
return;
|
||||
}
|
||||
current.generation().restartModules();
|
||||
warmRestartedWorld();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -70,6 +70,11 @@ public final class EngineGeneration {
|
||||
modules.start();
|
||||
}
|
||||
|
||||
/** Runs the modules' warm-up for the world they started; see {@link ModuleWorlds#warmUp()}. */
|
||||
public void warmModules() {
|
||||
modules.warmUp();
|
||||
}
|
||||
|
||||
/** Restarts the modules' world with the pack's reloaded resources; see {@link ModuleWorlds#restart()}. */
|
||||
public void restartModules() {
|
||||
modules.restart();
|
||||
|
||||
@@ -108,6 +108,11 @@ public class ModuleWorldsTest {
|
||||
world.state(Remembered.class, ignored -> new Remembered(calls));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warmUp(PackWorld world) {
|
||||
record("warmed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void worldStopped(PackWorld world) {
|
||||
record("stopped");
|
||||
@@ -198,6 +203,40 @@ public class ModuleWorldsTest {
|
||||
assertThrows(IllegalStateException.class, () -> world.state(Remembered.class, ignored -> null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theWarmUpRunsOnlyForAStartedWorldAndWarmsTheWorldItStarted() {
|
||||
EngineGeneration generation = generation();
|
||||
generation.warmModules();
|
||||
assertEquals("nothing started yet", List.of(), calls);
|
||||
|
||||
generation.startModules();
|
||||
generation.warmModules();
|
||||
assertEquals(List.of("loaded", "started", "warmed"), calls);
|
||||
|
||||
reloadResources();
|
||||
generation.restartModules();
|
||||
calls.clear();
|
||||
generation.warmModules();
|
||||
assertEquals("the restarted world is warmed too", List.of("warmed"), calls);
|
||||
|
||||
generation.close();
|
||||
calls.clear();
|
||||
generation.warmModules();
|
||||
assertEquals("a closed runtime warms nothing", List.of(), calls);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aFailedWarmUpNamesTheModuleAndLeavesTheWorldRunning() {
|
||||
failingHook = "warmed";
|
||||
EngineGeneration generation = generation();
|
||||
generation.startModules();
|
||||
|
||||
IllegalStateException failure = assertThrows(IllegalStateException.class, generation::warmModules);
|
||||
|
||||
assertTrue(failure.getMessage(), failure.getMessage().contains("test:recording"));
|
||||
generation.world().state(Remembered.class, ignored -> new Remembered(calls));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void everyWorldOfOneSnapshotStartsButThePackLoadsOnce() {
|
||||
EngineGeneration first = generation();
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.engine;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.modules.ModuleResourceFixture;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.framework.EngineGeneration;
|
||||
import art.arcane.iris.engine.framework.SeedManager;
|
||||
import com.volmit.FeatureRegistry;
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.ModuleLifecycle;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import com.volmit.world.PackWorld;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class GenerationCacheWarmGateTest {
|
||||
private static final long WAIT_SECONDS = 10;
|
||||
|
||||
@Test
|
||||
public void theFirstWorldIsWarmOnlyAfterTheHostAndThenTheModulesWarmedUp() {
|
||||
GenerationCacheWarmGate gate = new GenerationCacheWarmGate();
|
||||
List<String> steps = new ArrayList<>();
|
||||
assertTrue(gate.isPending());
|
||||
|
||||
gate.warmFirstWorld(() -> {
|
||||
steps.add("host");
|
||||
assertTrue("modules warm up after the host", gate.isPending());
|
||||
}, () -> {
|
||||
steps.add("modules");
|
||||
assertTrue(gate.isPending());
|
||||
});
|
||||
|
||||
assertEquals(List.of("host", "modules"), steps);
|
||||
assertFalse(gate.isPending());
|
||||
gate.await();
|
||||
}
|
||||
|
||||
/** A module whose warm-up takes as long as the test says, and records the world it warmed. */
|
||||
private static final class SlowWarmUpModule implements IrisModule, ModuleLifecycle {
|
||||
private final CountDownLatch warming = new CountDownLatch(1);
|
||||
private final CountDownLatch release = new CountDownLatch(1);
|
||||
private volatile PackWorld warmed;
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("test:slow", "Slow", "1");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(FeatureRegistry registry) {
|
||||
registry.registerLifecycle(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warmUp(PackWorld world) {
|
||||
warming.countDown();
|
||||
awaitQuietly(release);
|
||||
warmed = world;
|
||||
}
|
||||
}
|
||||
|
||||
private static EngineGeneration generationOf(SlowWarmUpModule module) {
|
||||
IrisData data = mock(IrisData.class);
|
||||
ModuleResourceFixture.serve(data);
|
||||
ModuleResourceFixture.serveModules(data, module);
|
||||
SeedManager seeds = mock(SeedManager.class);
|
||||
Engine engine = mock(Engine.class);
|
||||
when(engine.getData()).thenReturn(data);
|
||||
when(engine.getSeedManager()).thenReturn(seeds);
|
||||
return new EngineGeneration(engine);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generationWaitsForAModulesSlowWarmUp() throws Exception {
|
||||
GenerationCacheWarmGate gate = new GenerationCacheWarmGate();
|
||||
SlowWarmUpModule module = new SlowWarmUpModule();
|
||||
EngineGeneration generation = generationOf(module);
|
||||
generation.startModules();
|
||||
ExecutorService background = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
CompletableFuture<Void> warmUp = CompletableFuture.runAsync(
|
||||
() -> gate.warmFirstWorld(() -> {
|
||||
}, generation::warmModules), background);
|
||||
assertTrue(module.warming.await(WAIT_SECONDS, TimeUnit.SECONDS));
|
||||
CompletableFuture<Void> generating = CompletableFuture.runAsync(gate::await);
|
||||
|
||||
assertThrows(TimeoutException.class, () -> generating.get(200, TimeUnit.MILLISECONDS));
|
||||
assertTrue(gate.isPending());
|
||||
|
||||
module.release.countDown();
|
||||
warmUp.get(WAIT_SECONDS, TimeUnit.SECONDS);
|
||||
generating.get(WAIT_SECONDS, TimeUnit.SECONDS);
|
||||
assertFalse(gate.isPending());
|
||||
assertSame("the module warmed the world it started", generation.world(), module.warmed);
|
||||
} finally {
|
||||
module.release.countDown();
|
||||
background.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aFailedWarmUpFailsWhatWaitsForIt() {
|
||||
GenerationCacheWarmGate gate = new GenerationCacheWarmGate();
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> gate.warmFirstWorld(() -> {
|
||||
throw new IllegalStateException("host cannot warm");
|
||||
}, () -> {
|
||||
}));
|
||||
|
||||
IllegalStateException refused = assertThrows(IllegalStateException.class, gate::await);
|
||||
assertEquals("host cannot warm", refused.getCause().getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void worldsStartedLaterHoldGenerationUntilTheyAreWarmToo() {
|
||||
GenerationCacheWarmGate gate = new GenerationCacheWarmGate();
|
||||
gate.skipFirstWorld();
|
||||
assertFalse(gate.isPending());
|
||||
|
||||
CompletableFuture<Void> hotloaded = gate.beginRestartedWorld();
|
||||
CompletableFuture<Void> restarted = gate.beginRestartedWorld();
|
||||
assertTrue(gate.isPending());
|
||||
|
||||
hotloaded.complete(null);
|
||||
assertTrue("the second world is still warming", gate.isPending());
|
||||
restarted.complete(null);
|
||||
assertFalse(gate.isPending());
|
||||
gate.await();
|
||||
}
|
||||
|
||||
private static void awaitQuietly(CountDownLatch latch) {
|
||||
try {
|
||||
latch.await(WAIT_SECONDS, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user