mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Merged off-thread dimension restore
- Saved dimensions prepare in parallel during world load - Server start only creates levels and publishes engines
This commit is contained in:
commit
a0cf3d79bf
13 files changed
+1118
-223
No files matched your search
+276
@@ -0,0 +1,276 @@
|
||||
package art.arcane.iris.modded;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
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.assertTrue;
|
||||
|
||||
public class ModdedDimensionRestoreTest {
|
||||
private static final long STEP_TIMEOUT_SECONDS = 10L;
|
||||
|
||||
@Test
|
||||
public void everyDimensionPreparesConcurrentlyOffTheCallingThread() {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = dimensions(4);
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
steps.prepareBarrier = new CountDownLatch(dimensions.size());
|
||||
|
||||
List<ModdedDimensionRestore.Result> results;
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
results = restore.finish();
|
||||
}
|
||||
|
||||
assertEquals(dimensions.size(), results.size());
|
||||
for (ModdedDimensionRestore.Result result : results) {
|
||||
assertNull(result.failure());
|
||||
}
|
||||
assertFalse(steps.prepareThreads.contains(Thread.currentThread()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void engineBuildsRunConcurrentlyWhileLevelMutationStaysOnTheCallingThread() {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = dimensions(3);
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
steps.buildBarrier = new CountDownLatch(dimensions.size());
|
||||
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
restore.finish();
|
||||
}
|
||||
|
||||
assertFalse(steps.buildThreads.contains(Thread.currentThread()));
|
||||
assertEquals(Set.of(Thread.currentThread()), steps.serverThreads);
|
||||
assertEquals(List.of("level-0:engine-0", "level-1:engine-1", "level-2:engine-2"), steps.completed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aFailedPreparationIsReportedWhileTheOtherDimensionsStillRestore() {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = dimensions(3);
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
IllegalStateException failure = new IllegalStateException("generation history is unusable");
|
||||
steps.prepareFailure = failure;
|
||||
steps.failingIndex = 1;
|
||||
|
||||
List<ModdedDimensionRestore.Result> results;
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
results = restore.finish();
|
||||
}
|
||||
|
||||
assertNull(results.get(0).failure());
|
||||
assertSame(failure, results.get(1).failure());
|
||||
assertNull(results.get(2).failure());
|
||||
assertSame(dimensions.get(1), results.get(1).dimension());
|
||||
assertEquals(List.of("level-0:engine-0", "level-2:engine-2"), steps.completed);
|
||||
assertTrue(steps.begun.stream().noneMatch((String level) -> level.equals("level-1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aFailedEngineBuildAbandonsItsLevelWithoutCompletingIt() {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = dimensions(2);
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
IllegalStateException failure = new IllegalStateException("engine refused");
|
||||
steps.buildFailure = failure;
|
||||
steps.failingIndex = 0;
|
||||
|
||||
List<ModdedDimensionRestore.Result> results;
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
results = restore.finish();
|
||||
}
|
||||
|
||||
assertSame(failure, results.get(0).failure());
|
||||
assertNull(results.get(1).failure());
|
||||
assertEquals(List.of("level-0"), steps.abandoned);
|
||||
assertEquals(List.of("level-1:engine-1"), steps.completed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aLevelThatCannotBeCreatedDiscardsItsPreparation() {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = dimensions(2);
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
IllegalStateException failure = new IllegalStateException("dimension type missing");
|
||||
steps.beginFailure = failure;
|
||||
steps.failingIndex = 1;
|
||||
|
||||
List<ModdedDimensionRestore.Result> results;
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
results = restore.finish();
|
||||
}
|
||||
|
||||
assertSame(failure, results.get(1).failure());
|
||||
assertEquals(List.of("prepared-1"), steps.discarded);
|
||||
assertEquals(List.of("level-0:engine-0"), steps.completed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void awaitingPreparationsReturnsOnlyOnceEveryDimensionIsPreparedAndCreatesNoLevel() {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = dimensions(3);
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
steps.prepareGate = release;
|
||||
Thread.ofPlatform().daemon(true).start(() -> {
|
||||
try {
|
||||
Thread.sleep(200L);
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
release.countDown();
|
||||
});
|
||||
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
restore.awaitPreparations();
|
||||
|
||||
assertEquals(dimensions.size(), steps.prepared.get());
|
||||
assertTrue(steps.begun.isEmpty());
|
||||
restore.finish();
|
||||
}
|
||||
|
||||
assertEquals(List.of("level-0:engine-0", "level-1:engine-1", "level-2:engine-2"), steps.completed);
|
||||
assertEquals(0L, release.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closingAnUnfinishedRestoreDiscardsEveryPreparation() throws InterruptedException {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = dimensions(3);
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
|
||||
ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps);
|
||||
restore.close();
|
||||
|
||||
assertTrue(steps.discardedAll.await(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS));
|
||||
assertTrue(steps.begun.isEmpty());
|
||||
assertEquals(Set.of("prepared-0", "prepared-1", "prepared-2"), Set.copyOf(steps.discarded));
|
||||
}
|
||||
|
||||
private static List<ModdedDimensionRegistryStore.PersistentDimension> dimensions(int count) {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = new ArrayList<>(count);
|
||||
for (int index = 0; index < count; index++) {
|
||||
dimensions.add(new ModdedDimensionRegistryStore.PersistentDimension(
|
||||
"iris:world" + index, "overworld", "overworld", 1000L + index));
|
||||
}
|
||||
return dimensions;
|
||||
}
|
||||
|
||||
private static int index(String value) {
|
||||
return Integer.parseInt(value.substring(value.lastIndexOf('-') + 1));
|
||||
}
|
||||
|
||||
private static int index(ModdedDimensionRegistryStore.PersistentDimension dimension) {
|
||||
return Integer.parseInt(dimension.id().substring("iris:world".length()));
|
||||
}
|
||||
|
||||
private static void await(CountDownLatch barrier) {
|
||||
if (barrier == null) {
|
||||
return;
|
||||
}
|
||||
barrier.countDown();
|
||||
try {
|
||||
if (!barrier.await(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
|
||||
throw new IllegalStateException("Restore steps did not run concurrently");
|
||||
}
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while waiting for concurrent restore steps", interrupted);
|
||||
}
|
||||
}
|
||||
|
||||
private static void awaitGate(CountDownLatch gate) {
|
||||
if (gate == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (!gate.await(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
|
||||
throw new IllegalStateException("Preparation gate never opened");
|
||||
}
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while waiting for the preparation gate", interrupted);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RecordingSteps implements ModdedDimensionRestore.Steps<String, String, String> {
|
||||
private final Set<Thread> prepareThreads = ConcurrentHashMap.newKeySet();
|
||||
private final Set<Thread> buildThreads = ConcurrentHashMap.newKeySet();
|
||||
private final Set<Thread> serverThreads = ConcurrentHashMap.newKeySet();
|
||||
private final List<String> begun = Collections.synchronizedList(new ArrayList<>());
|
||||
private final List<String> completed = Collections.synchronizedList(new ArrayList<>());
|
||||
private final List<String> abandoned = Collections.synchronizedList(new ArrayList<>());
|
||||
private final List<String> discarded = Collections.synchronizedList(new ArrayList<>());
|
||||
private final CountDownLatch discardedAll;
|
||||
private final AtomicInteger prepared = new AtomicInteger();
|
||||
private CountDownLatch prepareGate;
|
||||
private CountDownLatch prepareBarrier;
|
||||
private CountDownLatch buildBarrier;
|
||||
private RuntimeException prepareFailure;
|
||||
private RuntimeException beginFailure;
|
||||
private RuntimeException buildFailure;
|
||||
private int failingIndex = -1;
|
||||
|
||||
private RecordingSteps(int dimensions) {
|
||||
this.discardedAll = new CountDownLatch(dimensions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String prepare(ModdedDimensionRegistryStore.PersistentDimension dimension) {
|
||||
prepareThreads.add(Thread.currentThread());
|
||||
await(prepareBarrier);
|
||||
awaitGate(prepareGate);
|
||||
int index = index(dimension);
|
||||
if (prepareFailure != null && index == failingIndex) {
|
||||
throw prepareFailure;
|
||||
}
|
||||
prepared.incrementAndGet();
|
||||
return "prepared-" + index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String begin(ModdedDimensionRegistryStore.PersistentDimension dimension, String prepared) {
|
||||
serverThreads.add(Thread.currentThread());
|
||||
int index = index(dimension);
|
||||
if (beginFailure != null && index == failingIndex) {
|
||||
throw beginFailure;
|
||||
}
|
||||
String level = "level-" + index;
|
||||
begun.add(level);
|
||||
return level;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String build(String level, String prepared) {
|
||||
buildThreads.add(Thread.currentThread());
|
||||
await(buildBarrier);
|
||||
int index = index(level);
|
||||
if (buildFailure != null && index == failingIndex) {
|
||||
throw buildFailure;
|
||||
}
|
||||
return "engine-" + index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void complete(String level, String engine) {
|
||||
serverThreads.add(Thread.currentThread());
|
||||
completed.add(level + ":" + engine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abandon(String level, Throwable failure) {
|
||||
serverThreads.add(Thread.currentThread());
|
||||
abandoned.add(level);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discard(String prepared) {
|
||||
discarded.add(prepared);
|
||||
discardedAll.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
+44
-7
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
public class ModdedPackValidationPublicationTest {
|
||||
@@ -41,8 +42,8 @@ public class ModdedPackValidationPublicationTest {
|
||||
PackValidationResult result = loadable(root);
|
||||
try (MockedStatic<PackValidator> validator = mockStatic(PackValidator.class)) {
|
||||
validator.when(() -> PackValidator.validate(root)).thenReturn(result);
|
||||
assertSame(result, ModdedGenerationHistoryStorage.validatePack(root.toPath()));
|
||||
assertSame(result, ModdedGenerationHistoryStorage.validatePack(root.toPath()));
|
||||
assertSame(result, ModdedGenerationHistoryStorage.validatePack(root.toPath(), null));
|
||||
assertSame(result, ModdedGenerationHistoryStorage.validatePack(root.toPath(), null));
|
||||
validator.verify(() -> PackValidator.validate(root), times(1));
|
||||
}
|
||||
}
|
||||
@@ -56,13 +57,13 @@ public class ModdedPackValidationPublicationTest {
|
||||
PackValidationResult broken = new PackValidationResult(root.getName(), List.of("invalid"), List.of(), 1L);
|
||||
try (MockedStatic<PackValidator> validator = mockStatic(PackValidator.class)) {
|
||||
validator.when(() -> PackValidator.validate(root)).thenReturn(loadable(root), broken);
|
||||
ModdedGenerationHistoryStorage.validatePack(root.toPath());
|
||||
ModdedGenerationHistoryStorage.validatePack(root.toPath(), null);
|
||||
Files.writeString(content.toPath(), "wrong");
|
||||
Files.setLastModifiedTime(content.toPath(), timestamp);
|
||||
assertThrows(BrokenPackException.class,
|
||||
() -> ModdedGenerationHistoryStorage.validatePack(root.toPath()));
|
||||
() -> ModdedGenerationHistoryStorage.validatePack(root.toPath(), null));
|
||||
assertThrows(BrokenPackException.class,
|
||||
() -> ModdedGenerationHistoryStorage.validatePack(root.toPath()));
|
||||
() -> ModdedGenerationHistoryStorage.validatePack(root.toPath(), null));
|
||||
validator.verify(() -> PackValidator.validate(root), times(2));
|
||||
}
|
||||
}
|
||||
@@ -77,9 +78,9 @@ public class ModdedPackValidationPublicationTest {
|
||||
MockedStatic<PackValidationCache> cache = mockStatic(PackValidationCache.class)) {
|
||||
cache.when(PackValidationCache::contextFingerprint).thenAnswer(ignored -> context.get());
|
||||
validator.when(() -> PackValidator.validate(root)).thenReturn(first, second);
|
||||
assertSame(first, ModdedGenerationHistoryStorage.validatePack(root.toPath()));
|
||||
assertSame(first, ModdedGenerationHistoryStorage.validatePack(root.toPath(), null));
|
||||
context.set("second");
|
||||
assertSame(second, ModdedGenerationHistoryStorage.validatePack(root.toPath()));
|
||||
assertSame(second, ModdedGenerationHistoryStorage.validatePack(root.toPath(), null));
|
||||
validator.verify(() -> PackValidator.validate(root), times(2));
|
||||
}
|
||||
}
|
||||
@@ -189,6 +190,42 @@ public class ModdedPackValidationPublicationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snapshotReusesTheValidationOfAnIdenticalValidatedSource() throws Exception {
|
||||
File source = temporaryFolder.newFolder("overworld");
|
||||
Files.writeString(new File(source, "dimension.json").toPath(), "terrain");
|
||||
File snapshot = temporaryFolder.newFolder("epoch", "pack");
|
||||
Files.writeString(new File(snapshot, "dimension.json").toPath(), "terrain");
|
||||
PackValidationResult sourceResult = loadable(source);
|
||||
try (MockedStatic<PackValidator> validator = mockStatic(PackValidator.class)) {
|
||||
validator.when(() -> PackValidator.validate(source)).thenReturn(sourceResult);
|
||||
ModdedStartup.validatePack(source);
|
||||
|
||||
assertSame(sourceResult, ModdedGenerationHistoryStorage.validatePack(snapshot.toPath(), source.toPath()));
|
||||
|
||||
validator.verify(() -> PackValidator.validate(snapshot), never());
|
||||
assertSame(sourceResult, PackValidationRegistry.requireLoadable(snapshot.toPath()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snapshotThatDiffersFromItsSourceIsValidatedOnItsOwn() throws Exception {
|
||||
File source = temporaryFolder.newFolder("overworld");
|
||||
Files.writeString(new File(source, "dimension.json").toPath(), "terrain");
|
||||
File snapshot = temporaryFolder.newFolder("epoch", "pack");
|
||||
Files.writeString(new File(snapshot, "dimension.json").toPath(), "older terrain");
|
||||
PackValidationResult snapshotResult = loadable(snapshot);
|
||||
try (MockedStatic<PackValidator> validator = mockStatic(PackValidator.class)) {
|
||||
validator.when(() -> PackValidator.validate(source)).thenReturn(loadable(source));
|
||||
validator.when(() -> PackValidator.validate(snapshot)).thenReturn(snapshotResult);
|
||||
ModdedStartup.validatePack(source);
|
||||
|
||||
assertSame(snapshotResult, ModdedGenerationHistoryStorage.validatePack(snapshot.toPath(), source.toPath()));
|
||||
|
||||
validator.verify(() -> PackValidator.validate(snapshot), times(1));
|
||||
}
|
||||
}
|
||||
|
||||
private static PackValidationResult loadable(File root) {
|
||||
return new PackValidationResult(root.getName(), List.of(), List.of(), System.currentTimeMillis());
|
||||
}
|
||||
|
||||
+33
-10
@@ -1,7 +1,6 @@
|
||||
package art.arcane.iris.modded;
|
||||
|
||||
import art.arcane.iris.pack.PackValidationRegistry;
|
||||
import art.arcane.volmlib.nativelib.minecraft26_2.modded.NativeModdedServer;
|
||||
import art.arcane.volmlib.nativelib.minecraft26_2.modded.NativeProtocolPlayer;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
@@ -32,19 +31,12 @@ public class ModdedStartupReinjectFailureTest {
|
||||
|
||||
@Test
|
||||
public void aDimensionThatFailsToReinjectIsBanneredAndReportedToOperatorsOnJoin() {
|
||||
NativeModdedServer server = mock(NativeModdedServer.class);
|
||||
ModdedDimensionRegistryStore.PersistentDimension moon =
|
||||
new ModdedDimensionRegistryStore.PersistentDimension("iris:moon", "overworld", "overworld", 1337L);
|
||||
IllegalStateException failure = new IllegalStateException("Iris generation history is unusable for 'iris:moon'");
|
||||
|
||||
try (MockedStatic<ModdedDimensionRegistryStore> store = mockStatic(ModdedDimensionRegistryStore.class);
|
||||
MockedStatic<ModdedDimensionManager> manager = mockStatic(ModdedDimensionManager.class);
|
||||
MockedStatic<ModdedIrisLog> log = mockStatic(ModdedIrisLog.class)) {
|
||||
store.when(() -> ModdedDimensionRegistryStore.loadForStartup(server)).thenReturn(List.of(moon));
|
||||
manager.when(() -> ModdedDimensionManager.restorePersistent(server, "iris:moon", "overworld", "overworld", 1337L))
|
||||
.thenThrow(failure);
|
||||
|
||||
ModdedStartup.reinjectPersistentDimensions(server);
|
||||
try (MockedStatic<ModdedIrisLog> log = mockStatic(ModdedIrisLog.class)) {
|
||||
ModdedStartup.finishDimensionRestore(ModdedDimensionRestore.start(List.of(moon), new FailingSteps(failure)));
|
||||
|
||||
log.verify(() -> ModdedIrisLog.error(argThat((String line) -> line != null && line.contains("iris:moon")
|
||||
&& line.startsWith("Iris refused"))), times(1));
|
||||
@@ -73,4 +65,35 @@ public class ModdedStartupReinjectFailureTest {
|
||||
ModdedStartup.warnStartupFailuresTo(laterOperator);
|
||||
verify(laterOperator, never()).sendMessage(any());
|
||||
}
|
||||
|
||||
private record FailingSteps(RuntimeException failure) implements ModdedDimensionRestore.Steps<String, String, String> {
|
||||
@Override
|
||||
public String prepare(ModdedDimensionRegistryStore.PersistentDimension dimension) {
|
||||
throw failure;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String begin(ModdedDimensionRegistryStore.PersistentDimension dimension, String prepared) {
|
||||
throw new AssertionError("A failed preparation must not create a level");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String build(String level, String prepared) {
|
||||
throw new AssertionError("A failed preparation must not build an engine");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void complete(String level, String engine) {
|
||||
throw new AssertionError("A failed preparation must not publish a level");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abandon(String level, Throwable failure) {
|
||||
throw new AssertionError("A failed preparation has no level to abandon");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discard(String prepared) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user