Isolate chunk update failures in the modded update pass

One chunk throwing in a level's update pass skipped every chunk after it, and a
retry re-ran the loot of updates that had already been applied. A failed chunk
now stays unetched and is retried on a later pass while the rest of the level
carries on; only a closed engine or mantle ends the pass. Completed updates are
forgotten before a failure propagates so a retry never refills a chest.

Upstream: 5cadd9b8c dd4633ad3
This commit is contained in:
piexel committed 2026-11-10 01:13:17 +01:00
1 parent bed98c417d
commit 8d5b605afd
2 files changed
+170 -10

No files matched your search

@@ -32,6 +32,7 @@ import art.arcane.iris.modded.ModdedTileData;
import art.arcane.iris.modded.api.ModdedCustomContentRegistry;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.iris.util.project.matter.TileWrapper;
import art.arcane.volmlib.util.function.Consumer3;
import art.arcane.volmlib.util.mantle.flag.MantleFlag;
import art.arcane.volmlib.util.mantle.runtime.Mantle;
import art.arcane.volmlib.util.mantle.runtime.MantleChunk;
@@ -39,6 +40,7 @@ import art.arcane.volmlib.util.matter.Matter;
import art.arcane.volmlib.util.matter.MatterCavern;
import art.arcane.volmlib.util.matter.MatterUpdate;
import art.arcane.volmlib.util.scheduling.PrecisionStopwatch;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import net.minecraft.core.BlockPos;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
@@ -56,6 +58,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
public final class ModdedChunkUpdateService implements ModdedTickableService {
private static final long PASS_PERIOD_MILLIS = 3_000L;
@@ -150,7 +153,7 @@ public final class ModdedChunkUpdateService implements ModdedTickableService {
int centerZ = player.blockPosition().getZ() >> 4;
for (int dx = -PLAYER_CHUNK_RADIUS; dx <= PLAYER_CHUNK_RADIUS; dx++) {
for (int dz = -PLAYER_CHUNK_RADIUS; dz <= PLAYER_CHUNK_RADIUS; dz++) {
updateChunk(engine, level, centerX + dx, centerZ + dz);
updatePassChunk(engine, level, centerX + dx, centerZ + dz);
}
}
}
@@ -158,7 +161,7 @@ public final class ModdedChunkUpdateService implements ModdedTickableService {
private void updateForcedChunks(Engine engine, ServerLevel level) {
for (long chunkKey : level.getForceLoadedChunks()) {
updateChunk(engine, level, ChunkPos.getX(chunkKey), ChunkPos.getZ(chunkKey));
updatePassChunk(engine, level, ChunkPos.getX(chunkKey), ChunkPos.getZ(chunkKey));
}
}
@@ -166,8 +169,30 @@ public final class ModdedChunkUpdateService implements ModdedTickableService {
updateChunk(engine, level, chunkX, chunkZ, false);
}
private void updateChunk(Engine engine, ServerLevel level, int chunkX, int chunkZ) {
updateChunk(engine, level, chunkX, chunkZ, true);
private void updatePassChunk(Engine engine, ServerLevel level, int chunkX, int chunkZ) {
isolateChunkFailure(
() -> updateChunk(engine, level, chunkX, chunkZ, true),
() -> engine.isClosed() || engine.getMantle().getMantle().isClosed(),
String.valueOf(level.dimension().identifier()),
chunkX,
chunkZ);
}
static void isolateChunkFailure(Runnable chunkUpdate, BooleanSupplier passEnded, String levelName, int chunkX, int chunkZ) {
try {
chunkUpdate.run();
} catch (RuntimeException failure) {
if (passEnded.getAsBoolean()) {
throw failure;
}
boolean firstFailure = IrisLogging.errorOnce(
"chunk-update:" + levelName + ":" + chunkX + "," + chunkZ + ":" + failure,
"Iris chunk update failed at %s,%s in %s, retrying each pass: %s",
chunkX, chunkZ, levelName, failure);
if (firstFailure) {
IrisLogging.reportError(failure);
}
}
}
private void updateChunk(Engine engine, ServerLevel level, int chunkX, int chunkZ, boolean requireNeighbors) {
@@ -344,15 +369,49 @@ public final class ModdedChunkUpdateService implements ModdedTickableService {
}
}
chunk.iterate(MatterUpdate.class, (Integer x, Integer yf, Integer z, MatterUpdate v) -> {
if (v != null && v.isUpdate()) {
update(engine, level, x, yf + minHeight, z, baseX, baseZ, chunk);
}
});
chunk.deleteSlices(MatterUpdate.class);
applyQueuedUpdates(chunk, (Integer x, Integer yf, Integer z) ->
update(engine, level, x, yf + minHeight, z, baseX, baseZ, chunk));
engine.getMetrics().getUpdates().put(stopwatch.getMilliseconds());
}
static void applyQueuedUpdates(MantleChunk<Matter> chunk, Consumer3<Integer, Integer, Integer> update) {
IntArrayList completed = new IntArrayList();
try {
chunk.iterate(MatterUpdate.class, (Integer x, Integer yf, Integer z, MatterUpdate v) -> {
if (v != null && v.isUpdate()) {
update.accept(x, yf, z);
completed.add(packUpdatePosition(x, yf, z));
}
});
} catch (RuntimeException | Error failure) {
forgetCompletedUpdates(chunk, completed, failure);
throw failure;
}
chunk.deleteSlices(MatterUpdate.class);
}
private static int packUpdatePosition(int x, int yf, int z) {
return (yf << 8) | ((x & 15) << 4) | (z & 15);
}
private static void forgetCompletedUpdates(MantleChunk<Matter> chunk, IntArrayList completed, Throwable failure) {
try {
for (int index = 0; index < completed.size(); index++) {
int position = completed.getInt(index);
int yf = position >>> 8;
Matter section = chunk.get(yf >> 4);
if (section != null && section.hasSlice(MatterUpdate.class)) {
section.<MatterUpdate>getSlice(MatterUpdate.class)
.set((position >> 4) & 15, yf & 15, position & 15, null);
}
}
} catch (RuntimeException | Error cleanupFailure) {
if (cleanupFailure != failure) {
failure.addSuppressed(cleanupFailure);
}
}
}
private void update(Engine engine, ServerLevel level, int x, int y, int z, int baseX, int baseZ, MantleChunk<Matter> chunk) {
if (y < level.getMinY() || y >= level.getMaxY()) {
return;
@@ -1,20 +1,34 @@
package art.arcane.iris.modded.service;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.iris.util.project.matter.TileWrapper;
import art.arcane.volmlib.util.function.Consumer4;
import art.arcane.volmlib.util.mantle.runtime.MantleChunk;
import art.arcane.volmlib.util.mantle.runtime.MantleDataAdapter;
import art.arcane.volmlib.util.mantle.runtime.MantleHooks;
import art.arcane.volmlib.util.matter.IrisMatter;
import art.arcane.volmlib.util.matter.Matter;
import art.arcane.volmlib.util.matter.MatterSlice;
import art.arcane.volmlib.util.matter.MatterUpdate;
import art.arcane.volmlib.util.matter.slices.UpdateMatter;
import org.junit.Test;
import org.mockito.MockedStatic;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
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.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;
public class ModdedChunkUpdateServiceTest {
@Test
@@ -63,6 +77,93 @@ public class ModdedChunkUpdateServiceTest {
assertEquals(List.of("materialize"), chunk.operations());
}
@Test
public void failedUpdateForgetsOnlyTheUpdatesAlreadyApplied() {
ChestUpdateMantleChunk chunk = new ChestUpdateMantleChunk();
assertThrows(IllegalStateException.class, () ->
ModdedChunkUpdateService.applyQueuedUpdates(chunk, (Integer x, Integer yf, Integer z) -> {
if (x == 2) {
throw new IllegalStateException("update failure");
}
}));
assertFalse(chunk.deleted());
assertNull(chunk.slice.get(1, 5, 1));
assertNotNull(chunk.slice.get(2, 5, 2));
}
@Test
public void failedChunkIsReportedAndTheNextChunkStillRuns() {
AtomicInteger updatedChunks = new AtomicInteger();
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
logging.when(() -> IrisLogging.errorOnce(anyString(), anyString(), any(Object[].class))).thenReturn(true);
ModdedChunkUpdateService.isolateChunkFailure(
() -> {
throw new IllegalStateException("chunk failure");
},
() -> false, "example:updates", 0, 0);
ModdedChunkUpdateService.isolateChunkFailure(
updatedChunks::incrementAndGet, () -> false, "example:updates", 4, 0);
assertEquals(1, updatedChunks.get());
logging.verify(() -> IrisLogging.errorOnce(anyString(), anyString(), any(Object[].class)));
logging.verify(() -> IrisLogging.reportError(any(Throwable.class)));
}
}
@Test
public void endedPassRethrowsTheChunkFailure() {
IllegalStateException failure = new IllegalStateException("chunk failure");
IllegalStateException thrown = assertThrows(IllegalStateException.class, () ->
ModdedChunkUpdateService.isolateChunkFailure(
() -> {
throw failure;
},
() -> true, "example:updates", 0, 0));
assertSame(failure, thrown);
}
private static final class ChestUpdateMantleChunk extends MantleChunk<Matter> {
private final Matter section = new IrisMatter(16, 16, 16);
private final MatterSlice<MatterUpdate> slice = section.slice(MatterUpdate.class);
private boolean deleted;
private ChestUpdateMantleChunk() {
super(1, 0, 0, emptyAdapter(), MantleHooks.NONE);
slice.set(1, 5, 1, UpdateMatter.ON);
slice.set(2, 5, 2, UpdateMatter.ON);
}
@Override
public Matter get(int index) {
return section;
}
@Override
public <T> void iterate(Class<T> type, Consumer4<Integer, Integer, Integer, T> iterator) {
if (type == MatterUpdate.class) {
iterator.accept(1, 5, 1, type.cast(UpdateMatter.ON));
iterator.accept(2, 5, 2, type.cast(UpdateMatter.ON));
}
}
@Override
public void deleteSlices(Class<?> type) {
if (type == MatterUpdate.class) {
deleted = true;
}
}
private boolean deleted() {
return deleted;
}
}
private static final class RecordingMantleChunk extends MantleChunk<Matter> {
private final ArrayList<String> operations = new ArrayList<>();