mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Skip epoch fsyncs for studio histories
- Add EpochDurability so each history and pack repository decides whether epoch publication syncs to disk - Create studio world histories unsynced; other worlds still follow iris.durability - Skip the pack copy, epoch directory and epoch tree fsyncs for unsynced histories - Test that only unsynced histories skip the syncs
This commit is contained in:
1 parent
81293f2d4b
commit
e6fa0a49b6
8 files changed
+213
-28
No files matched your search
@@ -52,6 +52,7 @@ import art.arcane.iris.world.runtime.WorldDeletionQueue;
|
||||
import art.arcane.iris.studio.jigsaw.JigsawStudioActivation;
|
||||
import art.arcane.iris.world.IrisToolbelt;
|
||||
import art.arcane.volmlib.util.cache.AtomicCache;
|
||||
import art.arcane.iris.world.history.EpochDurability;
|
||||
import art.arcane.iris.world.history.GenerationActivation;
|
||||
import art.arcane.iris.world.history.GenerationEpoch;
|
||||
import art.arcane.iris.world.history.GenerationEpochContractFactory;
|
||||
@@ -214,7 +215,17 @@ public class StudioSVC implements IrisService {
|
||||
File dimensionRoot,
|
||||
long worldSeed
|
||||
) {
|
||||
return publishGenerationHistory(sender, dimension, dimensionRoot, worldSeed, false);
|
||||
return installIntoWorld(sender, dimension, dimensionRoot, worldSeed, EpochDurability.configured());
|
||||
}
|
||||
|
||||
public GenerationPublication installIntoWorld(
|
||||
VolmitSender sender,
|
||||
IrisDimension dimension,
|
||||
File dimensionRoot,
|
||||
long worldSeed,
|
||||
EpochDurability durability
|
||||
) {
|
||||
return publishGenerationHistory(sender, dimension, dimensionRoot, worldSeed, false, durability);
|
||||
}
|
||||
|
||||
public IrisDimension replaceIntoWorld(
|
||||
@@ -223,7 +234,8 @@ public class StudioSVC implements IrisService {
|
||||
File dimensionRoot,
|
||||
long worldSeed
|
||||
) {
|
||||
GenerationPublication publication = publishGenerationHistory(sender, dimension, dimensionRoot, worldSeed, true);
|
||||
GenerationPublication publication = publishGenerationHistory(
|
||||
sender, dimension, dimensionRoot, worldSeed, true, EpochDurability.configured());
|
||||
return publication == null ? null : publication.dimension();
|
||||
}
|
||||
|
||||
@@ -244,7 +256,8 @@ public class StudioSVC implements IrisService {
|
||||
IrisDimension dimension,
|
||||
File dimensionRoot,
|
||||
long worldSeed,
|
||||
boolean stageUpdate
|
||||
boolean stageUpdate,
|
||||
EpochDurability durability
|
||||
) {
|
||||
if (J.isPrimaryThread()) {
|
||||
sender.sendMessage(IrisLanguage.text(
|
||||
@@ -286,7 +299,8 @@ public class StudioSVC implements IrisService {
|
||||
candidate.packFingerprint(),
|
||||
worldSeed,
|
||||
candidate.dimensionContract(),
|
||||
candidate.registryContract()
|
||||
candidate.registryContract(),
|
||||
durability
|
||||
);
|
||||
}
|
||||
Path installedPack = stageUpdate && history.pendingActivation().isPresent()
|
||||
|
||||
@@ -22,6 +22,7 @@ import art.arcane.volmlib.nativelib.NativeAdapters;
|
||||
import art.arcane.volmlib.nativelib.server.NativeServerDiagnostics;
|
||||
import art.arcane.iris.world.runtime.TransientWorldCleanupSupport;
|
||||
import art.arcane.iris.world.runtime.WorldRuntimeControlService;
|
||||
import art.arcane.iris.world.history.EpochDurability;
|
||||
import art.arcane.iris.world.history.GenerationHistory;
|
||||
import com.google.common.util.concurrent.AtomicDouble;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
@@ -270,8 +271,9 @@ public class IrisCreator {
|
||||
if (benchmark && !studio) {
|
||||
installedDimension = studioService.installIntoTransientWorld(sender, resolvedDimension, dimensionRoot);
|
||||
} else {
|
||||
EpochDurability durability = studio ? EpochDurability.UNSYNCED : EpochDurability.configured();
|
||||
StudioSVC.GenerationPublication publication = studioService.installIntoWorld(
|
||||
sender, resolvedDimension, dimensionRoot, seed);
|
||||
sender, resolvedDimension, dimensionRoot, seed, durability);
|
||||
installedDimension = publication == null ? null : publication.dimension();
|
||||
if (publication != null) {
|
||||
publishedHistory = publication.history();
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package art.arcane.iris.world.history;
|
||||
|
||||
import art.arcane.iris.world.storage.Durability;
|
||||
|
||||
/**
|
||||
* Whether publishing a generation epoch forces its pack files and directories to disk. Unsynced epochs can be
|
||||
* lost or torn by a crash, so they are only for histories that are discarded on restart, such as Studio worlds.
|
||||
*/
|
||||
public enum EpochDurability {
|
||||
SYNCED,
|
||||
UNSYNCED;
|
||||
|
||||
public static EpochDurability configured() {
|
||||
return Durability.enabled() ? SYNCED : UNSYNCED;
|
||||
}
|
||||
|
||||
public boolean syncs() {
|
||||
return this == SYNCED;
|
||||
}
|
||||
}
|
||||
@@ -140,18 +140,33 @@ public final class GenerationHistory {
|
||||
GenerationKernelRegistry kernels
|
||||
) throws IOException {
|
||||
return create(new FreshCreation(dimensionRoot, packSource, packFingerprint, worldSeed, dimensionContract, registryContract),
|
||||
version, kernels, false);
|
||||
version, kernels, false, EpochDurability.configured());
|
||||
}
|
||||
|
||||
public static GenerationHistory create(
|
||||
Path dimensionRoot,
|
||||
Path packSource,
|
||||
String packFingerprint,
|
||||
long worldSeed,
|
||||
GenerationEpoch.DimensionContract dimensionContract,
|
||||
GenerationRegistryContract registryContract,
|
||||
EpochDurability durability
|
||||
) throws IOException {
|
||||
GenerationKernelRegistry kernels = GenerationKernelRegistry.standard();
|
||||
return create(new FreshCreation(dimensionRoot, packSource, packFingerprint, worldSeed, dimensionContract, registryContract),
|
||||
kernels.current(), kernels, false, durability);
|
||||
}
|
||||
|
||||
public static GenerationHistory createUnpublished(FreshCreation creation) throws IOException {
|
||||
Objects.requireNonNull(creation, "creation");
|
||||
Files.createDirectory(creation.dimensionRoot());
|
||||
GenerationKernelRegistry kernels = GenerationKernelRegistry.standard();
|
||||
return create(creation, kernels.current(), kernels, true);
|
||||
return create(creation, kernels.current(), kernels, true, EpochDurability.configured());
|
||||
}
|
||||
|
||||
private static GenerationHistory create(FreshCreation creation, GenerationKernelRegistry.Version version,
|
||||
GenerationKernelRegistry kernels, boolean unpublished) throws IOException {
|
||||
GenerationKernelRegistry kernels, boolean unpublished,
|
||||
EpochDurability durability) throws IOException {
|
||||
Path dimensionRoot = creation.dimensionRoot();
|
||||
Path packSource = creation.packSource();
|
||||
String packFingerprint = creation.packFingerprint();
|
||||
@@ -180,14 +195,14 @@ public final class GenerationHistory {
|
||||
kernels
|
||||
);
|
||||
requireSourceFingerprint(packSource, epoch.packFingerprint(), epoch.packFingerprintVersion());
|
||||
GenerationPackRepository packs = new GenerationPackRepository(paths.dimensionRoot());
|
||||
GenerationPackRepository packs = new GenerationPackRepository(paths.dimensionRoot(), durability);
|
||||
Path publishedPack = packs.publish(
|
||||
epoch.epochId(),
|
||||
epoch.packFingerprint(),
|
||||
epoch.packFingerprintVersion(),
|
||||
packSource
|
||||
);
|
||||
forceEpochPublication(paths, epoch.epochId());
|
||||
forceEpochPublication(paths, epoch.epochId(), durability);
|
||||
GenerationSemanticIndex.initialize(paths.dimensionRoot());
|
||||
GenerationHistoryStore store = GenerationHistoryStore.initialize(paths.generationRoot(), epoch);
|
||||
return new GenerationHistory(paths, packs, store, ownership, kernels,
|
||||
@@ -315,7 +330,7 @@ public final class GenerationHistory {
|
||||
expectedEpoch.packFingerprint(),
|
||||
expectedEpoch.packFingerprintVersion()
|
||||
);
|
||||
forceEpochPublication(paths, expectedEpoch.epochId());
|
||||
forceEpochPublication(paths, expectedEpoch.epochId(), history.packs.durability());
|
||||
}
|
||||
migrateLegacyMantle(paths, history.activeActivation().activationId());
|
||||
if (!legacyExists) {
|
||||
@@ -432,7 +447,7 @@ public final class GenerationHistory {
|
||||
packSource,
|
||||
activePackAsLinkBasis()
|
||||
);
|
||||
forceEpochPublication(paths, candidate.epochId());
|
||||
forceEpochPublication(paths, candidate.epochId(), packs.durability());
|
||||
return store.preparePendingActivation(candidate, transitionWidthBlocks);
|
||||
}
|
||||
|
||||
@@ -1339,20 +1354,20 @@ public final class GenerationHistory {
|
||||
|
||||
private static void forceEpochPublication(
|
||||
GenerationHistoryPaths paths,
|
||||
String epochId
|
||||
String epochId,
|
||||
EpochDurability durability
|
||||
) throws IOException {
|
||||
Path epochRoot = paths.epochRoot(epochId);
|
||||
if (!Files.isDirectory(epochRoot, LinkOption.NOFOLLOW_LINKS)
|
||||
|| Files.isSymbolicLink(epochRoot)) {
|
||||
throw new IOException("Generation epoch is not a safe directory: " + epochRoot);
|
||||
}
|
||||
if (File.separatorChar != '\\') {
|
||||
if (Durability.enabled()) {
|
||||
GenerationPackRepository.forceDirectoryTree(epochRoot, GenerationHistory::forceDirectory);
|
||||
}
|
||||
forceDirectory(paths.epochsRoot());
|
||||
forceDirectory(paths.generationRoot());
|
||||
if (!durability.syncs() || File.separatorChar == '\\') {
|
||||
return;
|
||||
}
|
||||
GenerationPackRepository.forceDirectoryTree(epochRoot, GenerationHistory::forceDirectory);
|
||||
forceDirectory(paths.epochsRoot());
|
||||
forceDirectory(paths.generationRoot());
|
||||
}
|
||||
|
||||
private static void forceDirectory(Path directory) throws IOException {
|
||||
|
||||
@@ -41,14 +41,24 @@ public final class GenerationPackRepository {
|
||||
private final Path dimensionRoot;
|
||||
private final Path generationRoot;
|
||||
private final Path epochsRoot;
|
||||
private final EpochDurability durability;
|
||||
|
||||
public GenerationPackRepository(Path dimensionRoot) {
|
||||
this(dimensionRoot, EpochDurability.configured());
|
||||
}
|
||||
|
||||
public GenerationPackRepository(Path dimensionRoot, EpochDurability durability) {
|
||||
Path root = Objects.requireNonNull(dimensionRoot, "dimensionRoot")
|
||||
.toAbsolutePath()
|
||||
.normalize();
|
||||
this.dimensionRoot = root;
|
||||
generationRoot = root.resolve("iris").resolve("generation");
|
||||
epochsRoot = generationRoot.resolve("epochs");
|
||||
this.durability = Objects.requireNonNull(durability, "durability");
|
||||
}
|
||||
|
||||
public EpochDurability durability() {
|
||||
return durability;
|
||||
}
|
||||
|
||||
public Path generationRoot() {
|
||||
@@ -74,7 +84,8 @@ public final class GenerationPackRepository {
|
||||
|
||||
/**
|
||||
* Files identical to the published {@code basisPack} are hard-linked instead of copied. Published packs are
|
||||
* immutable and were already synced, so only new or changed files pay for a copy and an fsync.
|
||||
* immutable and were already synced, so only new or changed files pay for a copy and an fsync. Unsynced
|
||||
* repositories skip every fsync.
|
||||
*/
|
||||
public synchronized Path publish(
|
||||
String epochId,
|
||||
@@ -111,7 +122,7 @@ public final class GenerationPackRepository {
|
||||
Path stage = Files.createTempDirectory(epochDirectory, ".pack-");
|
||||
try {
|
||||
Files.delete(stage);
|
||||
copyPackTree(source, stage, defaultExecutor(), defaultWorkers(), PackCopyMode.DURABLE, basisPack);
|
||||
copyPackTree(source, stage, defaultExecutor(), defaultWorkers(), publicationCopyMode(), basisPack);
|
||||
requireFingerprint(stage, packFingerprint, packFingerprintVersion);
|
||||
try (AtomicDirectoryPublisher.Publication publication =
|
||||
AtomicDirectoryPublisher.publishAbsent(stage, target)) {
|
||||
@@ -119,7 +130,7 @@ public final class GenerationPackRepository {
|
||||
} catch (FileAlreadyExistsException race) {
|
||||
return requireExactPack(epochId, packFingerprint, packFingerprintVersion);
|
||||
}
|
||||
forceDirectory(epochDirectory);
|
||||
syncDirectory(epochDirectory);
|
||||
validateExistingAncestors(epochId);
|
||||
requireSafeDirectory(target, "Generation epoch pack is missing or unsafe");
|
||||
return target;
|
||||
@@ -174,21 +185,25 @@ public final class GenerationPackRepository {
|
||||
requireSafeDirectory(epoch, "Generation epoch path is not a safe directory");
|
||||
}
|
||||
|
||||
private static Path ensureChildDirectory(Path parent, String name) throws IOException {
|
||||
private PackCopyMode publicationCopyMode() {
|
||||
return durability.syncs() ? PackCopyMode.DURABLE : PackCopyMode.SCRATCH;
|
||||
}
|
||||
|
||||
private Path ensureChildDirectory(Path parent, String name) throws IOException {
|
||||
Path child = parent.resolve(name);
|
||||
if (Files.exists(child, LinkOption.NOFOLLOW_LINKS)) {
|
||||
requireSafeDirectory(child, "Generation epoch ancestor is not a safe directory");
|
||||
forceDirectory(parent);
|
||||
syncDirectory(parent);
|
||||
return child;
|
||||
}
|
||||
try {
|
||||
Files.createDirectory(child);
|
||||
} catch (FileAlreadyExistsException race) {
|
||||
requireSafeDirectory(child, "Generation epoch ancestor is not a safe directory");
|
||||
forceDirectory(parent);
|
||||
syncDirectory(parent);
|
||||
return child;
|
||||
}
|
||||
forceDirectory(parent);
|
||||
syncDirectory(parent);
|
||||
requireSafeDirectory(child, "Generation epoch ancestor is not a safe directory");
|
||||
return child;
|
||||
}
|
||||
@@ -207,8 +222,8 @@ public final class GenerationPackRepository {
|
||||
}
|
||||
}
|
||||
|
||||
private static void forceDirectory(Path directory) throws IOException {
|
||||
if (File.separatorChar == '\\') {
|
||||
private void syncDirectory(Path directory) throws IOException {
|
||||
if (!durability.syncs() || File.separatorChar == '\\') {
|
||||
return;
|
||||
}
|
||||
try (FileChannel channel = FileChannel.open(directory, StandardOpenOption.READ)) {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package art.arcane.iris.world.history;
|
||||
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
|
||||
/**
|
||||
* Records directory channels opened on the calling thread, which is how directory fsyncs are issued.
|
||||
*/
|
||||
final class DirectorySyncCounter {
|
||||
private DirectorySyncCounter() {
|
||||
}
|
||||
|
||||
static List<Path> openedDirectories(IoAction action) throws Exception {
|
||||
List<Path> opened = new CopyOnWriteArrayList<>();
|
||||
try (MockedStatic<FileChannel> ignored = mockStatic(FileChannel.class, invocation -> {
|
||||
if (invocation.getMethod().getName().equals("open")
|
||||
&& invocation.getArgument(0) instanceof Path path
|
||||
&& Files.isDirectory(path)) {
|
||||
opened.add(path.toAbsolutePath().normalize());
|
||||
}
|
||||
return invocation.callRealMethod();
|
||||
})) {
|
||||
action.run();
|
||||
}
|
||||
return opened;
|
||||
}
|
||||
|
||||
static long countUnder(Path root, List<Path> opened) {
|
||||
Path normalizedRoot = root.toAbsolutePath().normalize();
|
||||
return opened.stream().filter(path -> path.startsWith(normalizedRoot)).count();
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface IoAction {
|
||||
void run() throws Exception;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package art.arcane.iris.world.history;
|
||||
|
||||
import art.arcane.iris.world.storage.Durability;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -65,6 +67,41 @@ public class GenerationHistoryTest extends GenerationHistorySupport {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onlyUnsyncedHistoriesSkipTheEpochTreeSyncWhenStaging() throws Exception {
|
||||
String previous = System.getProperty(Durability.MODE_PROPERTY);
|
||||
System.clearProperty(Durability.MODE_PROPERTY);
|
||||
try {
|
||||
assertEquals(1, stagingEpochTreeSyncs("synced", EpochDurability.SYNCED));
|
||||
assertEquals(0, stagingEpochTreeSyncs("unsynced", EpochDurability.UNSYNCED));
|
||||
} finally {
|
||||
if (previous != null) {
|
||||
System.setProperty(Durability.MODE_PROPERTY, previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int stagingEpochTreeSyncs(String name, EpochDurability durability) throws Exception {
|
||||
Path world = temporaryFolder.newFolder(name + "-staging-world").toPath();
|
||||
Path packA = createPack(name + "-staging-a", "alpha");
|
||||
Path packB = createPack(name + "-staging-b", "beta");
|
||||
GenerationHistory history = GenerationHistory.create(world, packA, fingerprint(packA), 42L, contract(),
|
||||
GenerationRegistryContract.empty(), durability);
|
||||
AtomicInteger treeSyncs = new AtomicInteger();
|
||||
try (MockedStatic<GenerationPackRepository> ignored = mockStatic(GenerationPackRepository.class, invocation -> {
|
||||
if (invocation.getMethod().getName().equals("forceDirectoryTree") && invocation.getArguments().length == 2) {
|
||||
treeSyncs.incrementAndGet();
|
||||
}
|
||||
return invocation.callRealMethod();
|
||||
})) {
|
||||
stage(history, packB);
|
||||
}
|
||||
|
||||
Path stagedPack = history.packRoot(history.pendingActivation().orElseThrow().activationId());
|
||||
assertEquals(fingerprint(packB), fingerprint(stagedPack));
|
||||
return treeSyncs.get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void firstPackAccessRejectsTamperingAfterCreation() throws Exception {
|
||||
Path world = temporaryFolder.newFolder("tampered-publication-world").toPath();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package art.arcane.iris.world.history;
|
||||
|
||||
import art.arcane.iris.world.storage.Durability;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -9,6 +10,7 @@ import org.mockito.MockedStatic;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -73,6 +75,42 @@ public class GenerationPackRepositoryTest {
|
||||
assertEquals(fingerprint(second), fingerprint(published));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsyncedRepositoryPublishesTheSamePackWithoutDirectorySyncs() throws Exception {
|
||||
Path source = createPack("unsynced-source", "{}");
|
||||
Path syncedWorld = temporaryFolder.newFolder("synced-world").toPath();
|
||||
Path unsyncedWorld = temporaryFolder.newFolder("unsynced-world").toPath();
|
||||
GenerationPackRepository synced = new GenerationPackRepository(syncedWorld, EpochDurability.SYNCED);
|
||||
GenerationPackRepository unsynced = new GenerationPackRepository(unsyncedWorld, EpochDurability.UNSYNCED);
|
||||
|
||||
List<Path> syncedOpens = DirectorySyncCounter.openedDirectories(() -> synced.publish(
|
||||
digest('a'), fingerprint(source), GenerationPackFingerprint.CURRENT_VERSION, source));
|
||||
List<Path> unsyncedOpens = DirectorySyncCounter.openedDirectories(() -> unsynced.publish(
|
||||
digest('a'), fingerprint(source), GenerationPackFingerprint.CURRENT_VERSION, source));
|
||||
|
||||
assertTrue(DirectorySyncCounter.countUnder(syncedWorld, syncedOpens) > 0);
|
||||
assertEquals(0L, DirectorySyncCounter.countUnder(unsyncedWorld, unsyncedOpens));
|
||||
assertEquals(fingerprint(source), fingerprint(unsynced.packRoot(digest('a'))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultRepositoryFollowsTheGlobalDurabilityProperty() throws Exception {
|
||||
Path dimensionRoot = temporaryFolder.newFolder("configured-world").toPath();
|
||||
String previous = System.getProperty(Durability.MODE_PROPERTY);
|
||||
try {
|
||||
System.clearProperty(Durability.MODE_PROPERTY);
|
||||
assertEquals(EpochDurability.SYNCED, new GenerationPackRepository(dimensionRoot).durability());
|
||||
System.setProperty(Durability.MODE_PROPERTY, "relaxed");
|
||||
assertEquals(EpochDurability.UNSYNCED, new GenerationPackRepository(dimensionRoot).durability());
|
||||
} finally {
|
||||
if (previous == null) {
|
||||
System.clearProperty(Durability.MODE_PROPERTY);
|
||||
} else {
|
||||
System.setProperty(Durability.MODE_PROPERTY, previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scratchSnapshotCopiesThePackWithoutLinkingTheSource() throws Exception {
|
||||
Path source = createPack("snapshot-source", "{}");
|
||||
|
||||
Reference in new issue
Block a user