mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Fail a running pregen when chunk generation throws
A throw inside chunk generation left the server's chunk futures unsettled, so the async pregen waited on requests that never completed. Generation failures now reach the running job for the same world, which fails every outstanding request with the failing chunk as cause, and close gives up on stragglers after a 60 second drain deadline.
Upstream: 6c7796f12
This commit is contained in:
1 parent
128e90ea9b
commit
e87e6d9fae
12 files changed
+418
-11
No files matched your search
@@ -224,6 +224,14 @@ public class PregeneratorJob implements PregenListener, PregenRenderSource {
|
||||
return instance.get();
|
||||
}
|
||||
|
||||
public static void reportChunkGenerationFailure(String worldIdentity, int chunkX, int chunkZ, Throwable failure) {
|
||||
PregeneratorJob inst = instance.get();
|
||||
if (inst == null || !inst.targetsWorldIdentity(worldIdentity)) {
|
||||
return;
|
||||
}
|
||||
inst.pregenerator.onChunkGenerationFailed(chunkX, chunkZ, failure);
|
||||
}
|
||||
|
||||
public static boolean pauseResume() {
|
||||
PregeneratorJob inst = instance.get();
|
||||
if (inst == null) {
|
||||
|
||||
@@ -195,6 +195,10 @@ public class IrisPregenerator {
|
||||
shutdown.set(true);
|
||||
}
|
||||
|
||||
public void onChunkGenerationFailed(int chunkX, int chunkZ, Throwable failure) {
|
||||
generator.onChunkGenerationFailed(chunkX, chunkZ, failure);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
PrecisionStopwatch p = PrecisionStopwatch.start();
|
||||
boolean completed = false;
|
||||
|
||||
@@ -83,6 +83,9 @@ public interface PregeneratorMethod {
|
||||
default void onRegionBounds(int minRegionX, int minRegionZ, int maxRegionX, int maxRegionZ) {
|
||||
}
|
||||
|
||||
default void onChunkGenerationFailed(int chunkX, int chunkZ, Throwable failure) {
|
||||
}
|
||||
|
||||
default void onRegionSubmitted(int regionX, int regionZ) {
|
||||
}
|
||||
|
||||
|
||||
+5
@@ -87,6 +87,11 @@ public class AsyncOrMedievalPregenMethod implements PregeneratorMethod {
|
||||
method.onRegionBounds(minRegionX, minRegionZ, maxRegionX, maxRegionZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkGenerationFailed(int chunkX, int chunkZ, Throwable failure) {
|
||||
method.onChunkGenerationFailed(chunkX, chunkZ, failure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRegionSubmitted(int regionX, int regionZ) {
|
||||
method.onRegionSubmitted(regionX, regionZ);
|
||||
|
||||
+90
-9
@@ -44,6 +44,7 @@ import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -56,6 +57,7 @@ import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
// THREAD_COUNT records the pool's original size while boosted; BOOST_HOLDERS counts live
|
||||
@@ -65,7 +67,8 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
private static final AtomicInteger BOOST_HOLDERS = new AtomicInteger();
|
||||
private static final int ADAPTIVE_SLOW_REQUEST_STEP = 3;
|
||||
private static final int ADAPTIVE_RECOVERY_INTERVAL = 8;
|
||||
private static final long CLOSE_DRAIN_WARNING_SECONDS = 60L;
|
||||
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 final World world;
|
||||
private final IrisRuntimeSchedulerMode runtimeSchedulerMode;
|
||||
@@ -108,6 +111,8 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
private final AtomicLong failed = new AtomicLong();
|
||||
private final AtomicLong lastProgressAt = new AtomicLong(M.ms());
|
||||
private final AtomicBoolean closing = new AtomicBoolean();
|
||||
private final PregenInFlightRequests inFlightRequests = new PregenInFlightRequests();
|
||||
private final AtomicReference<GenerationFailure> generationFailure = new AtomicReference<>();
|
||||
private final AtomicBoolean holdsWorkerBoost = new AtomicBoolean();
|
||||
private final Object permitMonitor = new Object();
|
||||
private volatile Engine metricsEngine;
|
||||
@@ -470,10 +475,20 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
}
|
||||
|
||||
private void completeChunk(int x, int z, PregenListener listener, Chunk chunk, Throwable throwable) {
|
||||
PregenListener owner = inFlightRequests.settle(PregenInFlightRequests.key(x, z));
|
||||
if (owner == null) {
|
||||
return;
|
||||
}
|
||||
settleChunk(x, z, owner, chunk, throwable, true);
|
||||
}
|
||||
|
||||
private void settleChunk(int x, int z, PregenListener listener, Chunk chunk, Throwable throwable, boolean reportFailure) {
|
||||
boolean success = false;
|
||||
try {
|
||||
if (throwable != null) {
|
||||
onChunkFutureFailure(x, z, throwable);
|
||||
if (reportFailure) {
|
||||
onChunkFutureFailure(x, z, throwable);
|
||||
}
|
||||
onChunkFailedToLoad(x, z);
|
||||
listener.onChunkFailed(x, z);
|
||||
} else if (chunk == null) {
|
||||
@@ -651,11 +666,53 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
+ " stalledForMs=" + stalledFor;
|
||||
}
|
||||
|
||||
private void markSubmitted() {
|
||||
private void markSubmitted(int x, int z, PregenListener listener) {
|
||||
inFlightRequests.add(PregenInFlightRequests.key(x, z), listener);
|
||||
submitted.incrementAndGet();
|
||||
inFlight.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkGenerationFailed(int chunkX, int chunkZ, Throwable failure) {
|
||||
GenerationFailure recorded = new GenerationFailure(chunkX, chunkZ, failure);
|
||||
if (generationFailure.compareAndSet(null, recorded)) {
|
||||
IrisLogging.error("Async pregen aborting: Iris failed to generate chunk " + chunkX + "," + chunkZ
|
||||
+ " and the server's chunk system will not complete the " + inFlightRequests.size()
|
||||
+ " outstanding chunk request(s). " + metricsSnapshot());
|
||||
}
|
||||
failOutstandingRequests(failure);
|
||||
}
|
||||
|
||||
private void failOutstandingRequests(Throwable failure) {
|
||||
settleAll(inFlightRequests.drain(), failure);
|
||||
}
|
||||
|
||||
private void throwIfGenerationFailed() {
|
||||
GenerationFailure failure = generationFailure.get();
|
||||
if (failure != null) {
|
||||
throw new IllegalStateException("Pregen aborted: Iris failed to generate chunk " + failure.chunkX() + ","
|
||||
+ failure.chunkZ() + " in world " + world.getName()
|
||||
+ " and the server's chunk system stopped completing requests.", failure.cause());
|
||||
}
|
||||
}
|
||||
|
||||
private void abandonOutstandingRequests(Drain drain) {
|
||||
Map<Long, PregenListener> abandoned = inFlightRequests.drain();
|
||||
IrisLogging.error("Async pregen abandoned " + abandoned.size() + " chunk request(s) that did not complete within "
|
||||
+ CLOSE_DRAIN_TIMEOUT_SECONDS + "s of closing (" + drain.outstanding()
|
||||
+ " permit(s) still held). They are not cached and regenerate on the next run. " + metricsSnapshot());
|
||||
settleAll(abandoned, new IllegalStateException(
|
||||
"Async chunk request never completed before the pregen closed in world " + world.getName() + "."));
|
||||
}
|
||||
|
||||
private void settleAll(Map<Long, PregenListener> requests, Throwable failure) {
|
||||
for (Map.Entry<Long, PregenListener> entry : requests.entrySet()) {
|
||||
long key = entry.getKey();
|
||||
settleChunk(PregenInFlightRequests.chunkX(key), PregenInFlightRequests.chunkZ(key), entry.getValue(),
|
||||
null, failure, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void markFinished(boolean success) {
|
||||
if (success) {
|
||||
completed.incrementAndGet();
|
||||
@@ -773,13 +830,18 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
// A stop request interrupts the pregen worker; shield the drain and flush so chunks still hit disk.
|
||||
boolean interrupted = Thread.interrupted();
|
||||
try {
|
||||
interrupted |= awaitDrain(
|
||||
Drain drain = awaitDrain(
|
||||
semaphore,
|
||||
threads,
|
||||
CLOSE_DRAIN_WARNING_SECONDS,
|
||||
CLOSE_DRAIN_TIMEOUT_SECONDS,
|
||||
TimeUnit.SECONDS,
|
||||
() -> IrisLogging.warn("Async pregen is still draining outstanding chunks. " + metricsSnapshot())
|
||||
);
|
||||
interrupted |= drain.interrupted();
|
||||
if (!drain.drained()) {
|
||||
abandonOutstandingRequests(drain);
|
||||
}
|
||||
|
||||
flushAllRemainingChunks();
|
||||
executor.shutdown();
|
||||
@@ -793,18 +855,25 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
}
|
||||
}
|
||||
|
||||
static boolean awaitDrain(
|
||||
static Drain awaitDrain(
|
||||
Semaphore semaphore,
|
||||
int permits,
|
||||
long warningInterval,
|
||||
long deadline,
|
||||
TimeUnit timeUnit,
|
||||
Runnable onWait
|
||||
) {
|
||||
long warningNanos = timeUnit.toNanos(warningInterval);
|
||||
long deadlineAt = System.nanoTime() + timeUnit.toNanos(deadline);
|
||||
boolean interrupted = false;
|
||||
while (true) {
|
||||
long remaining = deadlineAt - System.nanoTime();
|
||||
if (remaining <= 0L) {
|
||||
return new Drain(false, interrupted, permits - semaphore.availablePermits());
|
||||
}
|
||||
try {
|
||||
if (semaphore.tryAcquire(permits, warningInterval, timeUnit)) {
|
||||
return interrupted;
|
||||
if (semaphore.tryAcquire(permits, Math.min(warningNanos, remaining), TimeUnit.NANOSECONDS)) {
|
||||
return new Drain(true, interrupted, 0);
|
||||
}
|
||||
onWait.run();
|
||||
} catch (InterruptedException e) {
|
||||
@@ -814,7 +883,7 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
}
|
||||
|
||||
private boolean isCancelled() {
|
||||
return closing.get() || Thread.currentThread().isInterrupted();
|
||||
return closing.get() || generationFailure.get() != null || Thread.currentThread().isInterrupted();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -833,6 +902,7 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
|
||||
@Override
|
||||
public void generateChunk(int x, int z, PregenListener listener) {
|
||||
throwIfGenerationFailed();
|
||||
listener.onChunkGenerating(x, z);
|
||||
backpressure.enforceMantleBudget();
|
||||
backpressure.awaitHeapHeadroom();
|
||||
@@ -871,9 +941,14 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
return;
|
||||
}
|
||||
|
||||
throwIfGenerationFailed();
|
||||
regionPending.computeIfAbsent(rkey(x >> 5, z >> 5), k -> new AtomicInteger(1)).incrementAndGet();
|
||||
markSubmitted();
|
||||
markSubmitted(x, z, listener);
|
||||
executor.generate(x, z, listener);
|
||||
GenerationFailure failure = generationFailure.get();
|
||||
if (failure != null) {
|
||||
failOutstandingRequests(failure.cause());
|
||||
}
|
||||
}
|
||||
|
||||
private CompletableFuture<Chunk> requestChunkAsync(int x, int z) {
|
||||
@@ -1111,6 +1186,12 @@ public class AsyncPregenMethod implements PregeneratorMethod {
|
||||
}
|
||||
}
|
||||
|
||||
record Drain(boolean drained, boolean interrupted, int outstanding) {
|
||||
}
|
||||
|
||||
private record GenerationFailure(int chunkX, int chunkZ, Throwable cause) {
|
||||
}
|
||||
|
||||
private record ChunkAsyncMethodSelection(Method urgentMethod, Method standardMethod, String mode) {
|
||||
}
|
||||
}
|
||||
@@ -175,6 +175,11 @@ public class CachedPregenMethod implements PregeneratorMethod {
|
||||
method.onRegionBounds(minRegionX, minRegionZ, maxRegionX, maxRegionZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkGenerationFailed(int chunkX, int chunkZ, Throwable failure) {
|
||||
method.onChunkGenerationFailed(chunkX, chunkZ, failure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRegionSubmitted(int regionX, int regionZ) {
|
||||
method.onRegionSubmitted(regionX, regionZ);
|
||||
|
||||
@@ -80,6 +80,11 @@ public class HybridPregenMethod implements PregeneratorMethod {
|
||||
inWorld.onRegionBounds(minRegionX, minRegionZ, maxRegionX, maxRegionZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkGenerationFailed(int chunkX, int chunkZ, Throwable failure) {
|
||||
inWorld.onChunkGenerationFailed(chunkX, chunkZ, failure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRegionSubmitted(int regionX, int regionZ) {
|
||||
inWorld.onRegionSubmitted(regionX, regionZ);
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package art.arcane.iris.core.pregenerator.methods;
|
||||
|
||||
import art.arcane.iris.core.pregenerator.PregenListener;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public final class PregenInFlightRequests {
|
||||
private final ConcurrentHashMap<Long, PregenListener> requests = new ConcurrentHashMap<>();
|
||||
|
||||
public static long key(int chunkX, int chunkZ) {
|
||||
return ((long) chunkX << 32) | (chunkZ & 0xFFFFFFFFL);
|
||||
}
|
||||
|
||||
public static int chunkX(long key) {
|
||||
return (int) (key >> 32);
|
||||
}
|
||||
|
||||
public static int chunkZ(long key) {
|
||||
return (int) key;
|
||||
}
|
||||
|
||||
public boolean add(long key, PregenListener listener) {
|
||||
return requests.putIfAbsent(key, Objects.requireNonNull(listener, "listener")) == null;
|
||||
}
|
||||
|
||||
public PregenListener settle(long key) {
|
||||
return requests.remove(key);
|
||||
}
|
||||
|
||||
public Map<Long, PregenListener> drain() {
|
||||
HashMap<Long, PregenListener> drained = new HashMap<>();
|
||||
for (Map.Entry<Long, PregenListener> entry : requests.entrySet()) {
|
||||
if (requests.remove(entry.getKey(), entry.getValue())) {
|
||||
drained.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
return drained;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return requests.size();
|
||||
}
|
||||
}
|
||||
@@ -836,6 +836,15 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun
|
||||
|
||||
@Override
|
||||
public void generateNoise(@NotNull WorldInfo world, @NotNull Random random, int x, int z, @NotNull ChunkGenerator.ChunkData d) {
|
||||
try {
|
||||
generateNoiseChunk(world, random, x, z, d);
|
||||
} catch (Throwable e) {
|
||||
PregeneratorJob.reportChunkGenerationFailure(this.world.identity(), x, z, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void generateNoiseChunk(WorldInfo world, Random random, int x, int z, ChunkGenerator.ChunkData d) {
|
||||
if (closing) {
|
||||
throw new IllegalStateException("Iris chunk generation was rejected while the generator is closing.");
|
||||
}
|
||||
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
package art.arcane.iris.core.pregenerator.methods;
|
||||
|
||||
import art.arcane.iris.core.pregenerator.PregenListener;
|
||||
import art.arcane.iris.core.pregenerator.PregenMantleBackpressure;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.World;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class AsyncPregenGenerationFailureTest {
|
||||
private static final int OUTSTANDING_REQUESTS = 2;
|
||||
|
||||
@Test
|
||||
public void aGenerationFailureFailsEveryOutstandingRequestAndReleasesTheirPermits() throws Exception {
|
||||
Harness harness = Harness.withOutstanding(OUTSTANDING_REQUESTS);
|
||||
IllegalStateException cause = new IllegalStateException("Accepted hydrology layer exceeds the mantle bounds");
|
||||
|
||||
harness.method.onChunkGenerationFailed(48, -4, cause);
|
||||
|
||||
assertEquals(OUTSTANDING_REQUESTS, harness.semaphore.availablePermits());
|
||||
assertEquals(OUTSTANDING_REQUESTS, harness.failed.get());
|
||||
assertEquals(0, harness.inFlight.get());
|
||||
assertEquals(0, harness.requests.size());
|
||||
verify(harness.listener).onChunkFailed(10, 0);
|
||||
verify(harness.listener).onChunkFailed(11, 0);
|
||||
|
||||
IllegalStateException abort = assertThrows(IllegalStateException.class,
|
||||
() -> harness.method.generateChunk(12, 0, harness.listener));
|
||||
assertSame(cause, abort.getCause());
|
||||
verify(harness.listener, never()).onChunkGenerating(12, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aLateCompletionForASettledRequestIsIgnored() throws Exception {
|
||||
Harness harness = Harness.withOutstanding(OUTSTANDING_REQUESTS);
|
||||
harness.method.onChunkGenerationFailed(48, -4, new IllegalStateException("boom"));
|
||||
|
||||
harness.completeChunk(10, 0, null, null);
|
||||
harness.completeChunk(11, 0, null, new IllegalStateException("late failure"));
|
||||
|
||||
assertEquals(OUTSTANDING_REQUESTS, harness.failed.get());
|
||||
assertEquals(0, harness.completed.get());
|
||||
assertEquals(0, harness.inFlight.get());
|
||||
assertEquals(OUTSTANDING_REQUESTS, harness.semaphore.availablePermits());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closingAbandonsRequestsThatNeverCompleteAndReturnsTheirPermits() throws Exception {
|
||||
Harness harness = Harness.withOutstanding(OUTSTANDING_REQUESTS);
|
||||
|
||||
harness.abandon(new AsyncPregenMethod.Drain(false, false, OUTSTANDING_REQUESTS));
|
||||
|
||||
assertEquals(OUTSTANDING_REQUESTS, harness.semaphore.availablePermits());
|
||||
assertEquals(OUTSTANDING_REQUESTS, harness.failed.get());
|
||||
assertEquals(0, harness.requests.size());
|
||||
verify(harness.listener).onChunkFailed(10, 0);
|
||||
verify(harness.listener).onChunkFailed(11, 0);
|
||||
}
|
||||
|
||||
private static final class Harness {
|
||||
private final AsyncPregenMethod method;
|
||||
private final Semaphore semaphore;
|
||||
private final PregenInFlightRequests requests;
|
||||
private final PregenListener listener;
|
||||
private final AtomicInteger inFlight;
|
||||
private final AtomicLong failed;
|
||||
private final AtomicLong completed;
|
||||
|
||||
private Harness(AsyncPregenMethod method, Semaphore semaphore, PregenInFlightRequests requests,
|
||||
PregenListener listener, AtomicInteger inFlight, AtomicLong failed, AtomicLong completed) {
|
||||
this.method = method;
|
||||
this.semaphore = semaphore;
|
||||
this.requests = requests;
|
||||
this.listener = listener;
|
||||
this.inFlight = inFlight;
|
||||
this.failed = failed;
|
||||
this.completed = completed;
|
||||
}
|
||||
|
||||
static Harness withOutstanding(int outstanding) throws Exception {
|
||||
AsyncPregenMethod method = mock(AsyncPregenMethod.class, CALLS_REAL_METHODS);
|
||||
World world = mock(World.class);
|
||||
when(world.getName()).thenReturn("qa");
|
||||
Semaphore semaphore = new Semaphore(outstanding, true);
|
||||
semaphore.acquire(outstanding);
|
||||
PregenInFlightRequests requests = new PregenInFlightRequests();
|
||||
PregenListener listener = mock(PregenListener.class);
|
||||
AtomicInteger inFlight = new AtomicInteger(outstanding);
|
||||
AtomicLong failed = new AtomicLong();
|
||||
AtomicLong completed = new AtomicLong();
|
||||
for (int index = 0; index < outstanding; index++) {
|
||||
requests.add(PregenInFlightRequests.key(10 + index, 0), listener);
|
||||
}
|
||||
set(method, "world", world);
|
||||
set(method, "semaphore", semaphore);
|
||||
set(method, "backpressure", mock(PregenMantleBackpressure.class));
|
||||
set(method, "inFlightRequests", requests);
|
||||
set(method, "generationFailure", new AtomicReference<>());
|
||||
set(method, "regionPending", new ConcurrentHashMap<>());
|
||||
set(method, "lastFailedReleaseLogAt", new AtomicLong());
|
||||
set(method, "slowRequestWarnIntervalMs", 15_000);
|
||||
set(method, "inFlight", inFlight);
|
||||
set(method, "submitted", new AtomicLong(outstanding));
|
||||
set(method, "completed", completed);
|
||||
set(method, "failed", failed);
|
||||
set(method, "lastProgressAt", new AtomicLong());
|
||||
set(method, "adaptiveInFlightLimit", new AtomicInteger(outstanding));
|
||||
set(method, "permitMonitor", new Object());
|
||||
set(method, "threads", outstanding);
|
||||
set(method, "closing", new AtomicBoolean());
|
||||
return new Harness(method, semaphore, requests, listener, inFlight, failed, completed);
|
||||
}
|
||||
|
||||
void completeChunk(int x, int z, Chunk chunk, Throwable throwable) throws Exception {
|
||||
Method complete = AsyncPregenMethod.class.getDeclaredMethod(
|
||||
"completeChunk", int.class, int.class, PregenListener.class, Chunk.class, Throwable.class);
|
||||
complete.setAccessible(true);
|
||||
complete.invoke(method, x, z, listener, chunk, throwable);
|
||||
}
|
||||
|
||||
void abandon(AsyncPregenMethod.Drain drain) throws Exception {
|
||||
Method abandon = AsyncPregenMethod.class.getDeclaredMethod(
|
||||
"abandonOutstandingRequests", AsyncPregenMethod.Drain.class);
|
||||
abandon.setAccessible(true);
|
||||
abandon.invoke(method, drain);
|
||||
}
|
||||
|
||||
private static void set(AsyncPregenMethod method, String name, Object value) throws Exception {
|
||||
Field field = AsyncPregenMethod.class.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(method, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
-2
@@ -90,10 +90,11 @@ public class AsyncPregenMethodConcurrencyCapTest {
|
||||
AtomicInteger warnings = new AtomicInteger();
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
Future<Boolean> drain = executor.submit(() -> AsyncPregenMethod.awaitDrain(
|
||||
Future<AsyncPregenMethod.Drain> drain = executor.submit(() -> AsyncPregenMethod.awaitDrain(
|
||||
semaphore,
|
||||
2,
|
||||
10L,
|
||||
5L * 1000L,
|
||||
TimeUnit.MILLISECONDS,
|
||||
warnings::incrementAndGet
|
||||
));
|
||||
@@ -103,11 +104,34 @@ public class AsyncPregenMethodConcurrencyCapTest {
|
||||
}
|
||||
semaphore.release(2);
|
||||
|
||||
assertFalse(drain.get(1L, TimeUnit.SECONDS));
|
||||
AsyncPregenMethod.Drain result = drain.get(1L, TimeUnit.SECONDS);
|
||||
assertTrue(result.drained());
|
||||
assertFalse(result.interrupted());
|
||||
assertEquals(0, result.outstanding());
|
||||
assertTrue(warnings.get() > 0);
|
||||
assertEquals(0, semaphore.availablePermits());
|
||||
} finally {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closeDrainGivesUpAtItsDeadlineWhilePermitsStayHeld() {
|
||||
Semaphore semaphore = new Semaphore(0);
|
||||
AtomicInteger warnings = new AtomicInteger();
|
||||
|
||||
AsyncPregenMethod.Drain result = AsyncPregenMethod.awaitDrain(
|
||||
semaphore,
|
||||
2,
|
||||
5L,
|
||||
80L,
|
||||
TimeUnit.MILLISECONDS,
|
||||
warnings::incrementAndGet
|
||||
);
|
||||
|
||||
assertFalse(result.drained());
|
||||
assertFalse(result.interrupted());
|
||||
assertEquals(2, result.outstanding());
|
||||
assertTrue(warnings.get() > 0);
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package art.arcane.iris.core.pregenerator.methods;
|
||||
|
||||
import art.arcane.iris.core.pregenerator.PregenListener;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class PregenInFlightRequestsTest {
|
||||
@Test
|
||||
public void settlingARequestReturnsItsListenerOnlyOnce() {
|
||||
PregenInFlightRequests requests = new PregenInFlightRequests();
|
||||
PregenListener listener = mock(PregenListener.class);
|
||||
long key = PregenInFlightRequests.key(48, -4);
|
||||
|
||||
assertTrue(requests.add(key, listener));
|
||||
assertEquals(1, requests.size());
|
||||
assertSame(listener, requests.settle(key));
|
||||
assertNull(requests.settle(key));
|
||||
assertEquals(0, requests.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aDuplicateSubmissionKeepsTheOriginalRequest() {
|
||||
PregenInFlightRequests requests = new PregenInFlightRequests();
|
||||
PregenListener first = mock(PregenListener.class);
|
||||
PregenListener second = mock(PregenListener.class);
|
||||
long key = PregenInFlightRequests.key(1, 2);
|
||||
|
||||
assertTrue(requests.add(key, first));
|
||||
assertFalse(requests.add(key, second));
|
||||
assertSame(first, requests.settle(key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void drainingHandsBackEveryOutstandingRequestExactlyOnce() {
|
||||
PregenInFlightRequests requests = new PregenInFlightRequests();
|
||||
PregenListener listener = mock(PregenListener.class);
|
||||
requests.add(PregenInFlightRequests.key(0, 0), listener);
|
||||
requests.add(PregenInFlightRequests.key(1, 0), listener);
|
||||
requests.add(PregenInFlightRequests.key(-7, 9), listener);
|
||||
|
||||
Map<Long, PregenListener> drained = requests.drain();
|
||||
|
||||
assertEquals(3, drained.size());
|
||||
assertEquals(0, requests.size());
|
||||
assertTrue(requests.drain().isEmpty());
|
||||
assertNull(requests.settle(PregenInFlightRequests.key(1, 0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void keysKeepNegativeCoordinatesApartAndRoundTrip() {
|
||||
assertNotEquals(PregenInFlightRequests.key(1, -1), PregenInFlightRequests.key(-1, 1));
|
||||
assertNotEquals(PregenInFlightRequests.key(0, -1), PregenInFlightRequests.key(-1, 0));
|
||||
long key = PregenInFlightRequests.key(-48, -4);
|
||||
assertEquals(-48, PregenInFlightRequests.chunkX(key));
|
||||
assertEquals(-4, PregenInFlightRequests.chunkZ(key));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user