mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Contain failed placements and let engine failures end the chunk
Placement code caught Throwable and logged it, so a closed session, a stopping executor or an OOM inside a placement left a half-built chunk that was then persisted. A classifier in the api separates those failures from content failures: the first are rethrown, the second are skipped with one rate-limited warning per kind, subject and failure type. Upstream:54f3595baebe4aa1e47b38f7584f4f7e92a48cf9e01d9
This commit is contained in:
1 parent
fc5c6ce0ab
commit
de1c6e887f
13 files changed
+318
-22
No files matched your search
@@ -0,0 +1,7 @@
|
||||
package com.volmit.generation;
|
||||
|
||||
/** Marks an exception that says the engine, not one piece of content, failed: a closed session, a stopping runtime. The
|
||||
* placement code that skips a failed piece lets it through instead; see {@link GenerationFailures}.
|
||||
*/
|
||||
public interface EngineFailure {
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.volmit.generation;
|
||||
|
||||
import com.volmit.ModuleLog;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/** Tells failures of the engine, its executors or the JVM from failures of one piece of content. Code that places a
|
||||
* list of objects or decorations may skip a piece that fails, because every rebuild skips it the same way. An engine
|
||||
* failure is transient, so skipping it would leave a half-built chunk that a healthy engine never produces: it must
|
||||
* end the chunk instead.
|
||||
*/
|
||||
public final class GenerationFailures {
|
||||
private static final int MAXIMUM_CAUSE_DEPTH = 32;
|
||||
private static final int MAXIMUM_SKIP_KEYS = 512;
|
||||
private static final String OVERFLOW_SKIP_KEY = "overflow";
|
||||
private static final String PACKAGE_PREFIX = "art.arcane.";
|
||||
static final long SKIP_WARNING_INTERVAL_NANOS = 60_000_000_000L;
|
||||
private static final ConcurrentHashMap<String, SkipWarning> SKIP_WARNINGS = new ConcurrentHashMap<>();
|
||||
|
||||
private GenerationFailures() {
|
||||
}
|
||||
|
||||
/** Whether {@code failure} or one of its causes is an {@link Error}, an {@link EngineFailure}, an I/O or timeout
|
||||
* failure, or a stopping executor or thread; any failure on an interrupted thread counts too.
|
||||
*/
|
||||
public static boolean isEngineFailure(Throwable failure) {
|
||||
return Thread.currentThread().isInterrupted() || hasCause(failure, cause -> cause instanceof Error
|
||||
|| cause instanceof EngineFailure
|
||||
|| cause instanceof IOException
|
||||
|| cause instanceof UncheckedIOException
|
||||
|| cause instanceof TimeoutException
|
||||
|| cause instanceof InterruptedException
|
||||
|| cause instanceof RejectedExecutionException
|
||||
|| cause instanceof CancellationException);
|
||||
}
|
||||
|
||||
/** Throws {@code failure} when it is an engine failure, so a catch block can call it first and then handle the
|
||||
* content failures that remain. A checked failure is wrapped in an {@link IllegalStateException}.
|
||||
*/
|
||||
public static void rethrowEngineFailure(Throwable failure) {
|
||||
if (!isEngineFailure(failure)) {
|
||||
return;
|
||||
}
|
||||
if (failure instanceof RuntimeException runtimeFailure) {
|
||||
throw runtimeFailure;
|
||||
}
|
||||
if (failure instanceof Error error) {
|
||||
throw error;
|
||||
}
|
||||
if (failure instanceof InterruptedException) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
throw new IllegalStateException("Iris generation stopped: " + failure, failure);
|
||||
}
|
||||
|
||||
/** Ends one failed placement: an engine failure leaves through {@link #rethrowEngineFailure}, anything else is one
|
||||
* warning on {@code log} per minute for each {@code kind}, {@code subject} and failure type, counting the ones
|
||||
* skipped meanwhile, and the caller moves on. The limit only decides what is logged, never what is placed.
|
||||
*
|
||||
* @param kind what was being placed, such as {@code decoration}
|
||||
* @param subject the pack content that failed, such as a block or an object name; it keys the warning, so it must
|
||||
* not hold a coordinate
|
||||
* @param where where it failed, such as {@code at 1,2,3}; it is only printed
|
||||
*/
|
||||
public static void skipPlacement(ModuleLog log, Throwable failure, String kind, String subject, String where) {
|
||||
rethrowEngineFailure(failure);
|
||||
long suppressed = claimSkipWarning(kind + '|' + subject + '|' + failure.getClass().getName(), System.nanoTime());
|
||||
if (suppressed >= 0L) {
|
||||
log.warn(skipWarning(kind, subject, where, failure, suppressed));
|
||||
}
|
||||
}
|
||||
|
||||
static long claimSkipWarning(String key, long now) {
|
||||
SkipWarning warning = SKIP_WARNINGS.get(key);
|
||||
if (warning == null) {
|
||||
String resolved = SKIP_WARNINGS.size() >= MAXIMUM_SKIP_KEYS ? OVERFLOW_SKIP_KEY : key;
|
||||
warning = SKIP_WARNINGS.computeIfAbsent(resolved, ignored -> new SkipWarning(now - SKIP_WARNING_INTERVAL_NANOS));
|
||||
}
|
||||
return warning.claim(now);
|
||||
}
|
||||
|
||||
static String skipWarning(String kind, String subject, String where, Throwable failure, long suppressed) {
|
||||
String message = failure.getMessage() == null ? "" : ": " + failure.getMessage().replaceAll("\\s+", " ");
|
||||
StringBuilder line = new StringBuilder("Skipped ").append(kind).append(' ').append(subject)
|
||||
.append(' ').append(where).append(": ").append(failure.getClass().getSimpleName()).append(message);
|
||||
StackTraceElement origin = origin(failure);
|
||||
if (origin != null) {
|
||||
String className = origin.getClassName();
|
||||
line.append(" (").append(className.substring(className.lastIndexOf('.') + 1)).append('.')
|
||||
.append(origin.getMethodName()).append(')');
|
||||
}
|
||||
if (suppressed > 0L) {
|
||||
line.append(", ").append(suppressed).append(" more");
|
||||
}
|
||||
return line.toString();
|
||||
}
|
||||
|
||||
private static StackTraceElement origin(Throwable failure) {
|
||||
StackTraceElement[] frames = failure.getStackTrace();
|
||||
for (StackTraceElement frame : frames) {
|
||||
if (frame.getClassName().startsWith(PACKAGE_PREFIX)) {
|
||||
return frame;
|
||||
}
|
||||
}
|
||||
return frames.length == 0 ? null : frames[0];
|
||||
}
|
||||
|
||||
private static boolean hasCause(Throwable failure, Predicate<Throwable> match) {
|
||||
Throwable current = failure;
|
||||
for (int depth = 0; current != null && depth < MAXIMUM_CAUSE_DEPTH; depth++, current = current.getCause()) {
|
||||
if (match.test(current)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final class SkipWarning {
|
||||
private final AtomicLong last;
|
||||
private final AtomicLong suppressed = new AtomicLong();
|
||||
|
||||
private SkipWarning(long start) {
|
||||
last = new AtomicLong(start);
|
||||
}
|
||||
|
||||
private long claim(long now) {
|
||||
long previous = last.get();
|
||||
if (now - previous >= SKIP_WARNING_INTERVAL_NANOS && last.compareAndSet(previous, now)) {
|
||||
return suppressed.getAndSet(0L);
|
||||
}
|
||||
suppressed.incrementAndGet();
|
||||
return -1L;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package com.volmit.generation;
|
||||
|
||||
import com.volmit.ModuleLog;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class GenerationFailuresTest {
|
||||
private static final long START_NANOS = 1_000_000_000L;
|
||||
|
||||
private static final class ClosedSession extends Exception implements EngineFailure {
|
||||
ClosedSession(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static ModuleLog logInto(List<String> warnings) {
|
||||
return (ModuleLog) Proxy.newProxyInstance(ModuleLog.class.getClassLoader(), new Class<?>[] {ModuleLog.class},
|
||||
(proxy, method, arguments) -> {
|
||||
if (method.getName().equals("warn")) {
|
||||
warnings.add((String) arguments[0]);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void engineFailuresAreRecognizedThroughWrappers() {
|
||||
assertTrue(GenerationFailures.isEngineFailure(new ClosedSession("sealed")));
|
||||
assertTrue(GenerationFailures.isEngineFailure(new RejectedExecutionException("pool shut down")));
|
||||
assertTrue(GenerationFailures.isEngineFailure(new OutOfMemoryError()));
|
||||
assertTrue(GenerationFailures.isEngineFailure(new UncheckedIOException(new IOException("plate read"))));
|
||||
assertTrue(GenerationFailures.isEngineFailure(new CompletionException(
|
||||
new IllegalStateException("placement failed", new ClosedSession("sealed")))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void contentFailuresAreNotEngineFailures() {
|
||||
assertFalse(GenerationFailures.isEngineFailure(new IllegalArgumentException("malformed object")));
|
||||
assertFalse(GenerationFailures.isEngineFailure(new NullPointerException("missing block")));
|
||||
assertFalse(GenerationFailures.isEngineFailure(new IllegalStateException("unrelated")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anyFailureOnAnInterruptedThreadIsAnEngineFailure() {
|
||||
Thread.currentThread().interrupt();
|
||||
try {
|
||||
assertTrue(GenerationFailures.isEngineFailure(new IllegalArgumentException("malformed object")));
|
||||
} finally {
|
||||
Thread.interrupted();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rethrowPropagatesEngineFailuresAndReturnsForContentFailures() {
|
||||
var closed = new IllegalStateException("closed", new RejectedExecutionException());
|
||||
assertSame(closed, assertThrows(IllegalStateException.class, () -> GenerationFailures.rethrowEngineFailure(closed)));
|
||||
|
||||
var checked = new ClosedSession("sealed");
|
||||
var wrapped = assertThrows(IllegalStateException.class, () -> GenerationFailures.rethrowEngineFailure(checked));
|
||||
assertSame(checked, wrapped.getCause());
|
||||
|
||||
var error = new OutOfMemoryError();
|
||||
assertSame(error, assertThrows(OutOfMemoryError.class, () -> GenerationFailures.rethrowEngineFailure(error)));
|
||||
|
||||
GenerationFailures.rethrowEngineFailure(new IllegalArgumentException("malformed object"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void skippedPlacementRethrowsEngineFailuresAndWarnsOnContentFailures() {
|
||||
var warnings = new ArrayList<String>();
|
||||
var log = logInto(warnings);
|
||||
GenerationFailures.skipPlacement(log, new IllegalArgumentException("Invalid\nblockFace"),
|
||||
"decoration", "minecraft:vine", "at 1,2,3");
|
||||
assertEquals(1, warnings.size());
|
||||
assertTrue(warnings.get(0).startsWith(
|
||||
"Skipped decoration minecraft:vine at 1,2,3: IllegalArgumentException: Invalid blockFace ("), warnings.get(0));
|
||||
assertTrue(warnings.get(0).contains("GenerationFailuresTest."), warnings.get(0));
|
||||
|
||||
var closed = new ClosedSession("sealed");
|
||||
var thrown = assertThrows(IllegalStateException.class,
|
||||
() -> GenerationFailures.skipPlacement(log, closed, "decoration", "minecraft:vine", "at 1,2,3"));
|
||||
assertSame(closed, thrown.getCause());
|
||||
assertEquals(1, warnings.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatedSkipsAreLimitedPerKeyAndCountedOnTheNextLine() {
|
||||
assertEquals(0L, GenerationFailures.claimSkipWarning("limit-test", START_NANOS));
|
||||
assertEquals(-1L, GenerationFailures.claimSkipWarning("limit-test", START_NANOS + 1L));
|
||||
assertEquals(-1L, GenerationFailures.claimSkipWarning("limit-test", START_NANOS + 2L));
|
||||
assertEquals(0L, GenerationFailures.claimSkipWarning("limit-test-other", START_NANOS + 2L));
|
||||
assertEquals(2L, GenerationFailures.claimSkipWarning("limit-test",
|
||||
START_NANOS + GenerationFailures.SKIP_WARNING_INTERVAL_NANOS + 1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theSkipWarningNamesTheFirstIrisFrameAndTheSuppressedCount() {
|
||||
var failure = new IllegalArgumentException("bad");
|
||||
failure.setStackTrace(new StackTraceElement[] {
|
||||
new StackTraceElement("java.util.HashMap", "get", "HashMap.java", 1),
|
||||
new StackTraceElement("art.arcane.iris.engine.decorator.DecoratorCore", "place", "DecoratorCore.java", 2)});
|
||||
assertEquals("Skipped decoration minecraft:vine at 1,2,3: IllegalArgumentException: bad (DecoratorCore.place), 4 more",
|
||||
GenerationFailures.skipWarning("decoration", "minecraft:vine", "at 1,2,3", failure, 4L));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user