mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Pump native chunk tasks while server stop waits for pregen
On Paper only the server thread advances chunk tasks, so a stop that blocks on a running pregen can wait forever. Stop now polls the native chunk queue while joining the pregen worker, and pregen skips eviction and flush scheduling once the server is stopping.
Upstream: 083e68986
This commit is contained in:
1 parent
e87e6d9fae
commit
ae60b14402
8 files changed
+226
-3
No files matched your search
+12
@@ -1131,6 +1131,18 @@ public class NMSBinding implements INMSBinding {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pollChunkTask(World world) {
|
||||
if (J.isFolia()) {
|
||||
return false;
|
||||
}
|
||||
ServerLevel level = ((CraftWorld) world).getHandle();
|
||||
if (Thread.currentThread() != level.getServer().getRunningThread()) {
|
||||
throw new IllegalStateException("Native chunk tasks can only be polled from the server thread.");
|
||||
}
|
||||
return level.getChunkSource().pollTask();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flushChunkIO(World world) {
|
||||
try {
|
||||
|
||||
+11
@@ -18,6 +18,17 @@ public class NMSBindingShutdownBoundaryContractTest {
|
||||
assertTrue(boundary.contains("server.getRunningThread()"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void chunkTaskPollingIsServerThreadOnlyAndSkipsFolia() throws Exception {
|
||||
String source = Files.readString(Path.of(System.getProperty("iris.nmsBindingSource"))).replace("\r\n", "\n");
|
||||
String polling = section(source, "public boolean pollChunkTask", "public void flushChunkIO");
|
||||
|
||||
int foliaGuard = polling.indexOf("J.isFolia()");
|
||||
int ownerGuard = polling.indexOf("level.getServer().getRunningThread()");
|
||||
int poll = polling.indexOf("level.getChunkSource().pollTask()");
|
||||
assertTrue(foliaGuard >= 0 && ownerGuard > foliaGuard && poll > ownerGuard);
|
||||
}
|
||||
|
||||
private static String section(String source, String startMarker, String endMarker) {
|
||||
int start = source.indexOf(startMarker);
|
||||
int end = source.indexOf(endMarker, start);
|
||||
|
||||
@@ -993,7 +993,16 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware {
|
||||
StudioBridges.closeAll();
|
||||
|
||||
try {
|
||||
PregeneratorJob.shutdownAndWait(SERVER_STOP_PREGEN_TIMEOUT_MILLIS);
|
||||
if (J.isFolia()) {
|
||||
PregeneratorJob.shutdownAndWait(SERVER_STOP_PREGEN_TIMEOUT_MILLIS);
|
||||
} else {
|
||||
List<World> worlds = Bukkit.getWorlds();
|
||||
PregeneratorJob.shutdownAndWait(SERVER_STOP_PREGEN_TIMEOUT_MILLIS, () -> {
|
||||
for (World world : worlds) {
|
||||
INMS.get().pollChunkTask(world);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
Iris.reportError("Failed to quiesce the Iris pregenerator before server shutdown.", e);
|
||||
}
|
||||
|
||||
+4
-1
@@ -52,7 +52,10 @@ public class BukkitEngineLifecycleContractTest {
|
||||
String waitedShutdown = method(jobSource,
|
||||
"private static boolean shutdownAndWait(PregeneratorJob inst, long timeoutMs)");
|
||||
assertBefore(waitedShutdown, "inst.worker.interrupt()", "inst.worker.join(");
|
||||
assertTrue(waitedShutdown.contains("inst.worker.isAlive()"));
|
||||
assertBefore(waitedShutdown, "inst.worker.join(", "finishShutdown(inst, timeoutMs)");
|
||||
String finishedShutdown = method(jobSource,
|
||||
"private static boolean finishShutdown(PregeneratorJob inst, long timeoutMs)");
|
||||
assertBefore(finishedShutdown, "inst.worker.isAlive()", "instance.compareAndSet(inst, null)");
|
||||
|
||||
String hooksSource = Files.readString(Path.of(System.getProperty("iris.bukkitEnginePlatformHooksSource")));
|
||||
String hookShutdown = method(hooksSource, "public void shutdownPregenerator(Engine engine)");
|
||||
|
||||
@@ -43,6 +43,7 @@ import art.arcane.iris.util.common.scheduling.J;
|
||||
import java.awt.Color;
|
||||
import java.awt.EventQueue;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -57,6 +58,7 @@ public class PregeneratorJob implements PregenListener, PregenRenderSource {
|
||||
// 60s permit drain + 120s flush + plate reclaim), or a routine drain trips the deadline
|
||||
// and aborts the engine shutdown sequence mid-teardown.
|
||||
private static final long WORLD_SHUTDOWN_TIMEOUT_MILLIS = 200_000L;
|
||||
private static final long PENDING_TASK_POLL_MILLIS = 1L;
|
||||
private static final Color COLOR_EXISTS = parseColor("#4d7d5b");
|
||||
private static final Color COLOR_BLACK = parseColor("#4d7d5b");
|
||||
private static final Color COLOR_MANTLE = parseColor("#3c2773");
|
||||
@@ -203,6 +205,29 @@ public class PregeneratorJob implements PregenListener, PregenRenderSource {
|
||||
return shutdownAndWait(inst, timeoutMs);
|
||||
}
|
||||
|
||||
public static boolean shutdownAndWait(long timeoutMs, Runnable processPendingTasks) {
|
||||
Objects.requireNonNull(processPendingTasks, "processPendingTasks");
|
||||
PregeneratorJob inst = instance.get();
|
||||
if (inst == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
inst.pregenerator.close();
|
||||
inst.worker.interrupt();
|
||||
long started = System.nanoTime();
|
||||
long timeoutNanos = TimeUnit.MILLISECONDS.toNanos(Math.max(1L, timeoutMs));
|
||||
try {
|
||||
while (inst.worker.isAlive() && System.nanoTime() - started < timeoutNanos) {
|
||||
processPendingTasks.run();
|
||||
inst.worker.join(PENDING_TASK_POLL_MILLIS);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while stopping the Iris pregenerator.", e);
|
||||
}
|
||||
return finishShutdown(inst, timeoutMs);
|
||||
}
|
||||
|
||||
private static boolean shutdownAndWait(PregeneratorJob inst, long timeoutMs) {
|
||||
inst.pregenerator.close();
|
||||
inst.worker.interrupt();
|
||||
@@ -212,6 +237,10 @@ public class PregeneratorJob implements PregenListener, PregenRenderSource {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while stopping the Iris pregenerator.", e);
|
||||
}
|
||||
return finishShutdown(inst, timeoutMs);
|
||||
}
|
||||
|
||||
private static boolean finishShutdown(PregeneratorJob inst, long timeoutMs) {
|
||||
if (inst.worker.isAlive()) {
|
||||
throw new IllegalStateException("Timed out while stopping the Iris pregenerator after "
|
||||
+ Math.max(1L, timeoutMs) + "ms.");
|
||||
|
||||
@@ -223,6 +223,10 @@ public interface INMSBinding {
|
||||
return false;
|
||||
}
|
||||
|
||||
default boolean pollChunkTask(World world) {
|
||||
return false;
|
||||
}
|
||||
|
||||
default void flushChunkIO(World world) {
|
||||
}
|
||||
|
||||
|
||||
+27
-1
@@ -70,6 +70,7 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
private static final long CLOSE_DRAIN_WARNING_SECONDS = 20L;
|
||||
private static final long CLOSE_DRAIN_TIMEOUT_SECONDS = 60L;
|
||||
private static final long FLUSH_TIMEOUT_SECONDS = 120L;
|
||||
private static final long FLUSH_STOP_POLL_MILLIS = 100L;
|
||||
private final World world;
|
||||
private final IrisRuntimeSchedulerMode runtimeSchedulerMode;
|
||||
private final IrisPaperLikeBackendMode paperLikeBackendMode;
|
||||
@@ -349,6 +350,9 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
}
|
||||
|
||||
private void evictRegion(long c) {
|
||||
if (IrisToolbelt.isServerStopping()) {
|
||||
return;
|
||||
}
|
||||
if (!evictedRegions.add(c)) {
|
||||
return;
|
||||
}
|
||||
@@ -418,6 +422,9 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
}
|
||||
|
||||
private void flushAllRemainingChunks() {
|
||||
if (IrisToolbelt.isServerStopping()) {
|
||||
return;
|
||||
}
|
||||
List<Long> keys = new ArrayList<>(regionChunks.keySet());
|
||||
|
||||
if (foliaRuntime) {
|
||||
@@ -428,6 +435,9 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
}
|
||||
|
||||
CompletableFuture<Void> flush = J.sfut(() -> {
|
||||
if (IrisToolbelt.isServerStopping()) {
|
||||
return;
|
||||
}
|
||||
for (Long rk : keys) {
|
||||
if (!evictedRegions.add(rk)) {
|
||||
continue;
|
||||
@@ -451,7 +461,7 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
});
|
||||
|
||||
try {
|
||||
flush.get(FLUSH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
awaitFlushUnlessServerStops(flush);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
IrisLogging.warn("Interrupted while flushing pregen chunks for " + world.getName() + ".");
|
||||
@@ -463,6 +473,22 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
}
|
||||
}
|
||||
|
||||
private static void awaitFlushUnlessServerStops(CompletableFuture<Void> flush) throws Exception {
|
||||
long started = System.nanoTime();
|
||||
long timeoutNanos = TimeUnit.SECONDS.toNanos(FLUSH_TIMEOUT_SECONDS);
|
||||
while (!IrisToolbelt.isServerStopping()) {
|
||||
long remainingNanos = timeoutNanos - (System.nanoTime() - started);
|
||||
if (remainingNanos <= 0L) {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
try {
|
||||
flush.get(Math.min(remainingNanos, TimeUnit.MILLISECONDS.toNanos(FLUSH_STOP_POLL_MILLIS)), TimeUnit.NANOSECONDS);
|
||||
return;
|
||||
} catch (TimeoutException pollElapsed) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Chunk onChunkFutureFailure(int x, int z, Throwable throwable) {
|
||||
try {
|
||||
IrisLogging.warn("Failed async pregen chunk load at " + x + "," + z + ". " + metricsSnapshot());
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package art.arcane.iris.core.gui;
|
||||
|
||||
import art.arcane.iris.core.pregenerator.IrisPregenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public class PregeneratorJobOwnerDrainTest {
|
||||
@Test
|
||||
public void ownerPollingCompletesTheRequestThatKeepsTheWorkerAlive() throws Exception {
|
||||
try (Fixture fixture = new Fixture()) {
|
||||
Thread owner = Thread.currentThread();
|
||||
AtomicInteger polls = new AtomicInteger();
|
||||
assertTrue(PregeneratorJob.shutdownAndWait(1_000L, () -> {
|
||||
assertSame(owner, Thread.currentThread());
|
||||
polls.incrementAndGet();
|
||||
fixture.completion.complete(null);
|
||||
}));
|
||||
assertTrue(polls.get() > 0);
|
||||
assertFalse(fixture.worker.isAlive());
|
||||
assertNull(PregeneratorJob.getInstance());
|
||||
verify(fixture.pregenerator).close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeoutKeepsThePendingJobAndDoesNotCompleteItsRequest() throws Exception {
|
||||
try (Fixture fixture = new Fixture()) {
|
||||
AtomicInteger polls = new AtomicInteger();
|
||||
IllegalStateException failure = assertThrows(IllegalStateException.class,
|
||||
() -> PregeneratorJob.shutdownAndWait(20L, polls::incrementAndGet));
|
||||
assertEquals("Timed out while stopping the Iris pregenerator after 20ms.", failure.getMessage());
|
||||
assertTrue(polls.get() > 0);
|
||||
assertTrue(fixture.worker.isAlive());
|
||||
assertFalse(fixture.completion.isDone());
|
||||
assertSame(fixture.job, PregeneratorJob.getInstance());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interruptedOwnerKeepsTheJobAndOriginalInterruption() throws Exception {
|
||||
try (Fixture fixture = new Fixture()) {
|
||||
try {
|
||||
IllegalStateException failure = assertThrows(IllegalStateException.class,
|
||||
() -> PregeneratorJob.shutdownAndWait(1_000L, () -> Thread.currentThread().interrupt()));
|
||||
assertTrue(failure.getCause() instanceof InterruptedException);
|
||||
assertTrue(Thread.currentThread().isInterrupted());
|
||||
assertSame(fixture.job, PregeneratorJob.getInstance());
|
||||
assertFalse(fixture.completion.isDone());
|
||||
} finally {
|
||||
Thread.interrupted();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ownerTaskFailureEscapesWithItsCauseAndRetainsTheJob() throws Exception {
|
||||
try (Fixture fixture = new Fixture()) {
|
||||
IllegalStateException expected = new IllegalStateException("Native task failed", new Exception("Chunk cause"));
|
||||
IllegalStateException actual = assertThrows(IllegalStateException.class,
|
||||
() -> PregeneratorJob.shutdownAndWait(1_000L, () -> {
|
||||
throw expected;
|
||||
}));
|
||||
assertSame(expected, actual);
|
||||
assertSame(expected.getCause(), actual.getCause());
|
||||
assertSame(fixture.job, PregeneratorJob.getInstance());
|
||||
assertFalse(fixture.completion.isDone());
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Fixture implements AutoCloseable {
|
||||
private final PregeneratorJob job = mock(PregeneratorJob.class, CALLS_REAL_METHODS);
|
||||
private final IrisPregenerator pregenerator = mock(IrisPregenerator.class);
|
||||
private final CompletableFuture<Void> completion = new CompletableFuture<>();
|
||||
private final AtomicReference<PregeneratorJob> instance;
|
||||
private final PregeneratorJob previous;
|
||||
private final Thread worker;
|
||||
|
||||
private Fixture() throws Exception {
|
||||
CountDownLatch started = new CountDownLatch(1);
|
||||
worker = new Thread(() -> {
|
||||
started.countDown();
|
||||
completion.join();
|
||||
}, "Pregen owner drain test");
|
||||
set("worker", worker);
|
||||
set("pregenerator", pregenerator);
|
||||
instance = instance();
|
||||
previous = instance.getAndSet(job);
|
||||
worker.start();
|
||||
assertTrue(started.await(5L, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
completion.complete(null);
|
||||
worker.join(5_000L);
|
||||
instance.set(previous);
|
||||
assertFalse(worker.isAlive());
|
||||
}
|
||||
|
||||
private void set(String name, Object value) throws Exception {
|
||||
Field field = PregeneratorJob.class.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(job, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static AtomicReference<PregeneratorJob> instance() throws Exception {
|
||||
Field field = PregeneratorJob.class.getDeclaredField("instance");
|
||||
field.setAccessible(true);
|
||||
return (AtomicReference<PregeneratorJob>) field.get(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user