mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-03 03:31:32 +00:00
Skip manifest fsyncs for studio histories
- Pass the epoch policy into the generation history store - Skip epoch metadata and manifest fsyncs when the policy does not sync - Test that only syncing policies force the store's files and directories
This commit is contained in:
1 parent
84e2d7b02d
commit
1c7dec2439
4 files changed
+105
-23
No files matched your search
@@ -3,8 +3,8 @@ package art.arcane.iris.world.history;
|
|||||||
import art.arcane.iris.world.storage.Durability;
|
import art.arcane.iris.world.storage.Durability;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How a history publishes and re-checks its generation epochs. Relaxed and Studio epochs skip fsyncs, so a crash
|
* How a history publishes and re-checks its generation epochs. Relaxed and Studio histories skip the fsyncs of epoch
|
||||||
* can lose or tear them. Studio epochs are also trusted after this process hashed them once, because Studio worlds
|
* packs, epoch metadata and the manifest, so a crash can lose or tear them. Studio epochs are also trusted after this process hashed them once, because Studio worlds
|
||||||
* are private to the server and discarded on restart.
|
* are private to the server and discarded on restart.
|
||||||
*/
|
*/
|
||||||
public enum EpochPolicy {
|
public enum EpochPolicy {
|
||||||
|
|||||||
@@ -204,7 +204,7 @@ public final class GenerationHistory {
|
|||||||
);
|
);
|
||||||
forceEpochPublication(paths, epoch.epochId(), epochPolicy);
|
forceEpochPublication(paths, epoch.epochId(), epochPolicy);
|
||||||
GenerationSemanticIndex.initialize(paths.dimensionRoot());
|
GenerationSemanticIndex.initialize(paths.dimensionRoot());
|
||||||
GenerationHistoryStore store = GenerationHistoryStore.initialize(paths.generationRoot(), epoch);
|
GenerationHistoryStore store = GenerationHistoryStore.initialize(paths.generationRoot(), epoch, epochPolicy);
|
||||||
return new GenerationHistory(paths, packs, store, ownership, kernels,
|
return new GenerationHistory(paths, packs, store, ownership, kernels,
|
||||||
Optional.of(new FreshPublication(publishedPack, epoch)), unpublished);
|
Optional.of(new FreshPublication(publishedPack, epoch)), unpublished);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,11 @@ public final class GenerationHistoryStore {
|
|||||||
private final Path generationDirectory;
|
private final Path generationDirectory;
|
||||||
private final Path manifestPath;
|
private final Path manifestPath;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
|
private static final DirectorySync NO_DIRECTORY_SYNC = directory -> {
|
||||||
|
};
|
||||||
|
|
||||||
private final DirectorySync directorySync;
|
private final DirectorySync directorySync;
|
||||||
|
private final EpochPolicy policy;
|
||||||
private GenerationManifest manifest;
|
private GenerationManifest manifest;
|
||||||
private IOException failure;
|
private IOException failure;
|
||||||
|
|
||||||
@@ -56,20 +60,34 @@ public final class GenerationHistoryStore {
|
|||||||
Path generationDirectory,
|
Path generationDirectory,
|
||||||
GenerationManifest manifest,
|
GenerationManifest manifest,
|
||||||
Clock clock,
|
Clock clock,
|
||||||
DirectorySync directorySync
|
DirectorySync directorySync,
|
||||||
|
EpochPolicy policy
|
||||||
) {
|
) {
|
||||||
this.generationDirectory = generationDirectory;
|
this.generationDirectory = generationDirectory;
|
||||||
this.manifestPath = generationDirectory.resolve(MANIFEST_FILE_NAME);
|
this.manifestPath = generationDirectory.resolve(MANIFEST_FILE_NAME);
|
||||||
this.manifest = Objects.requireNonNull(manifest, "manifest");
|
this.manifest = Objects.requireNonNull(manifest, "manifest");
|
||||||
this.clock = Objects.requireNonNull(clock, "clock");
|
this.clock = Objects.requireNonNull(clock, "clock");
|
||||||
this.directorySync = Objects.requireNonNull(directorySync, "directorySync");
|
this.directorySync = Objects.requireNonNull(directorySync, "directorySync");
|
||||||
|
this.policy = Objects.requireNonNull(policy, "policy");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GenerationHistoryStore initialize(
|
public static GenerationHistoryStore initialize(
|
||||||
Path generationDirectory,
|
Path generationDirectory,
|
||||||
GenerationEpoch initialEpoch
|
GenerationEpoch initialEpoch
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
return initialize(generationDirectory, initialEpoch, Clock.systemUTC());
|
return initialize(generationDirectory, initialEpoch, EpochPolicy.configured());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Histories whose policy does not sync skip every manifest and epoch metadata fsync, so a crash can lose the
|
||||||
|
* latest manifest update.
|
||||||
|
*/
|
||||||
|
public static GenerationHistoryStore initialize(
|
||||||
|
Path generationDirectory,
|
||||||
|
GenerationEpoch initialEpoch,
|
||||||
|
EpochPolicy policy
|
||||||
|
) throws IOException {
|
||||||
|
return initialize(generationDirectory, initialEpoch, Clock.systemUTC(), manifestDirectorySync(policy), policy);
|
||||||
}
|
}
|
||||||
|
|
||||||
static GenerationHistoryStore initialize(
|
static GenerationHistoryStore initialize(
|
||||||
@@ -85,6 +103,16 @@ public final class GenerationHistoryStore {
|
|||||||
GenerationEpoch initialEpoch,
|
GenerationEpoch initialEpoch,
|
||||||
Clock clock,
|
Clock clock,
|
||||||
DirectorySync directorySync
|
DirectorySync directorySync
|
||||||
|
) throws IOException {
|
||||||
|
return initialize(generationDirectory, initialEpoch, clock, directorySync, EpochPolicy.configured());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static GenerationHistoryStore initialize(
|
||||||
|
Path generationDirectory,
|
||||||
|
GenerationEpoch initialEpoch,
|
||||||
|
Clock clock,
|
||||||
|
DirectorySync directorySync,
|
||||||
|
EpochPolicy policy
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
Path directory = normalizeDirectory(generationDirectory);
|
Path directory = normalizeDirectory(generationDirectory);
|
||||||
requireSafeParent(directory);
|
requireSafeParent(directory);
|
||||||
@@ -92,7 +120,7 @@ public final class GenerationHistoryStore {
|
|||||||
String lockName = "." + Objects.requireNonNull(directory.getFileName(), "Generation history directory name")
|
String lockName = "." + Objects.requireNonNull(directory.getFileName(), "Generation history directory name")
|
||||||
+ ".manifest.lock";
|
+ ".manifest.lock";
|
||||||
try (GenerationPublicationLock ignored = GenerationPublicationLock.acquire(parent, lockName)) {
|
try (GenerationPublicationLock ignored = GenerationPublicationLock.acquire(parent, lockName)) {
|
||||||
ensureDirectory(directory);
|
ensureDirectory(directory, policy);
|
||||||
Path manifestPath = directory.resolve(MANIFEST_FILE_NAME);
|
Path manifestPath = directory.resolve(MANIFEST_FILE_NAME);
|
||||||
if (Files.exists(manifestPath, LinkOption.NOFOLLOW_LINKS)) {
|
if (Files.exists(manifestPath, LinkOption.NOFOLLOW_LINKS)) {
|
||||||
throw new FileAlreadyExistsException(manifestPath.toString());
|
throw new FileAlreadyExistsException(manifestPath.toString());
|
||||||
@@ -103,16 +131,25 @@ public final class GenerationHistoryStore {
|
|||||||
requiredClock.millis()
|
requiredClock.millis()
|
||||||
);
|
);
|
||||||
DirectorySync requiredDirectorySync = Objects.requireNonNull(directorySync, "directorySync");
|
DirectorySync requiredDirectorySync = Objects.requireNonNull(directorySync, "directorySync");
|
||||||
writeManifestAtomically(directory, manifestPath, manifest, null, false, requiredDirectorySync);
|
writeManifestAtomically(directory, manifestPath, manifest, null, false, requiredDirectorySync, policy);
|
||||||
return new GenerationHistoryStore(directory, manifest, requiredClock, requiredDirectorySync);
|
return new GenerationHistoryStore(directory, manifest, requiredClock, requiredDirectorySync, policy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GenerationHistoryStore open(Path generationDirectory) throws IOException {
|
public static GenerationHistoryStore open(Path generationDirectory) throws IOException {
|
||||||
return open(generationDirectory, Clock.systemUTC());
|
return open(generationDirectory, EpochPolicy.configured());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GenerationHistoryStore open(Path generationDirectory, EpochPolicy policy) throws IOException {
|
||||||
|
return open(generationDirectory, Clock.systemUTC(), policy);
|
||||||
}
|
}
|
||||||
|
|
||||||
static GenerationHistoryStore open(Path generationDirectory, Clock clock) throws IOException {
|
static GenerationHistoryStore open(Path generationDirectory, Clock clock) throws IOException {
|
||||||
|
return open(generationDirectory, clock, EpochPolicy.configured());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static GenerationHistoryStore open(Path generationDirectory, Clock clock, EpochPolicy policy)
|
||||||
|
throws IOException {
|
||||||
Path directory = normalizeDirectory(generationDirectory);
|
Path directory = normalizeDirectory(generationDirectory);
|
||||||
requireDirectory(directory);
|
requireDirectory(directory);
|
||||||
Path manifestPath = directory.resolve(MANIFEST_FILE_NAME);
|
Path manifestPath = directory.resolve(MANIFEST_FILE_NAME);
|
||||||
@@ -122,10 +159,15 @@ public final class GenerationHistoryStore {
|
|||||||
directory,
|
directory,
|
||||||
manifest,
|
manifest,
|
||||||
clock,
|
clock,
|
||||||
GenerationHistoryStore::forceDirectory
|
manifestDirectorySync(policy),
|
||||||
|
policy
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static DirectorySync manifestDirectorySync(EpochPolicy policy) {
|
||||||
|
return policy.syncs() ? GenerationHistoryStore::forceDirectory : NO_DIRECTORY_SYNC;
|
||||||
|
}
|
||||||
|
|
||||||
public Path generationDirectory() {
|
public Path generationDirectory() {
|
||||||
return generationDirectory;
|
return generationDirectory;
|
||||||
}
|
}
|
||||||
@@ -213,7 +255,7 @@ public final class GenerationHistoryStore {
|
|||||||
|
|
||||||
private void writeUpdatedManifest(GenerationManifest updated) throws IOException {
|
private void writeUpdatedManifest(GenerationManifest updated) throws IOException {
|
||||||
try {
|
try {
|
||||||
writeManifestAtomically(generationDirectory, manifestPath, updated, manifest, true, directorySync);
|
writeManifestAtomically(generationDirectory, manifestPath, updated, manifest, true, directorySync, policy);
|
||||||
} catch (IOException error) {
|
} catch (IOException error) {
|
||||||
reconcileAfterWriteFailure(error);
|
reconcileAfterWriteFailure(error);
|
||||||
throw error;
|
throw error;
|
||||||
@@ -261,7 +303,8 @@ public final class GenerationHistoryStore {
|
|||||||
GenerationManifest manifest,
|
GenerationManifest manifest,
|
||||||
GenerationManifest previous,
|
GenerationManifest previous,
|
||||||
boolean replaceExisting,
|
boolean replaceExisting,
|
||||||
DirectorySync directorySync
|
DirectorySync directorySync,
|
||||||
|
EpochPolicy policy
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
requireSafeParent(generationDirectory);
|
requireSafeParent(generationDirectory);
|
||||||
requireDirectory(generationDirectory);
|
requireDirectory(generationDirectory);
|
||||||
@@ -271,7 +314,7 @@ public final class GenerationHistoryStore {
|
|||||||
}
|
}
|
||||||
for (GenerationEpoch epoch : manifest.epochs()) {
|
for (GenerationEpoch epoch : manifest.epochs()) {
|
||||||
if (previous == null || previous.epoch(epoch.epochId()).isEmpty()) {
|
if (previous == null || previous.epoch(epoch.epochId()).isEmpty()) {
|
||||||
publishEpoch(generationDirectory, epoch);
|
publishEpoch(generationDirectory, epoch, policy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Path temporary = Files.createTempFile(generationDirectory, ".manifest-", ".tmp");
|
Path temporary = Files.createTempFile(generationDirectory, ".manifest-", ".tmp");
|
||||||
@@ -285,7 +328,7 @@ public final class GenerationHistoryStore {
|
|||||||
while (buffer.hasRemaining()) {
|
while (buffer.hasRemaining()) {
|
||||||
channel.write(buffer);
|
channel.write(buffer);
|
||||||
}
|
}
|
||||||
Durability.force(channel);
|
forceFile(channel, policy);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (replaceExisting) {
|
if (replaceExisting) {
|
||||||
@@ -332,11 +375,12 @@ public final class GenerationHistoryStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void publishEpoch(Path generationDirectory, GenerationEpoch epoch) throws IOException {
|
private static void publishEpoch(Path generationDirectory, GenerationEpoch epoch, EpochPolicy policy)
|
||||||
|
throws IOException {
|
||||||
Path epochsDirectory = generationDirectory.resolve("epochs");
|
Path epochsDirectory = generationDirectory.resolve("epochs");
|
||||||
ensureDirectory(epochsDirectory);
|
ensureDirectory(epochsDirectory, policy);
|
||||||
Path epochDirectory = epochDirectory(generationDirectory, epoch.epochId());
|
Path epochDirectory = epochDirectory(generationDirectory, epoch.epochId());
|
||||||
ensureDirectory(epochDirectory);
|
ensureDirectory(epochDirectory, policy);
|
||||||
Path metadata = epochDirectory.resolve("epoch.json");
|
Path metadata = epochDirectory.resolve("epoch.json");
|
||||||
if (Files.exists(metadata, LinkOption.NOFOLLOW_LINKS)) {
|
if (Files.exists(metadata, LinkOption.NOFOLLOW_LINKS)) {
|
||||||
if (!readEpoch(generationDirectory, epoch.epochId()).equals(epoch)) {
|
if (!readEpoch(generationDirectory, epoch.epochId()).equals(epoch)) {
|
||||||
@@ -355,14 +399,14 @@ public final class GenerationHistoryStore {
|
|||||||
while (buffer.hasRemaining()) {
|
while (buffer.hasRemaining()) {
|
||||||
channel.write(buffer);
|
channel.write(buffer);
|
||||||
}
|
}
|
||||||
Durability.force(channel);
|
forceFile(channel, policy);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Files.move(temporary, metadata, StandardCopyOption.ATOMIC_MOVE);
|
Files.move(temporary, metadata, StandardCopyOption.ATOMIC_MOVE);
|
||||||
} catch (AtomicMoveNotSupportedException exception) {
|
} catch (AtomicMoveNotSupportedException exception) {
|
||||||
throw new IOException("Generation history requires atomic epoch publication.", exception);
|
throw new IOException("Generation history requires atomic epoch publication.", exception);
|
||||||
}
|
}
|
||||||
forceDirectory(epochDirectory);
|
forceDirectory(epochDirectory, policy);
|
||||||
} finally {
|
} finally {
|
||||||
Files.deleteIfExists(temporary);
|
Files.deleteIfExists(temporary);
|
||||||
}
|
}
|
||||||
@@ -379,24 +423,37 @@ public final class GenerationHistoryStore {
|
|||||||
return Objects.requireNonNull(directory, "generationDirectory").toAbsolutePath().normalize();
|
return Objects.requireNonNull(directory, "generationDirectory").toAbsolutePath().normalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ensureDirectory(Path directory) throws IOException {
|
private static void ensureDirectory(Path directory, EpochPolicy policy) throws IOException {
|
||||||
requireSafeParent(directory);
|
requireSafeParent(directory);
|
||||||
|
Path parent = Objects.requireNonNull(directory.getParent(), "Generation history parent");
|
||||||
if (Files.exists(directory, LinkOption.NOFOLLOW_LINKS)) {
|
if (Files.exists(directory, LinkOption.NOFOLLOW_LINKS)) {
|
||||||
requireDirectory(directory);
|
requireDirectory(directory);
|
||||||
forceDirectory(Objects.requireNonNull(directory.getParent(), "Generation history parent"));
|
forceDirectory(parent, policy);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Files.createDirectory(directory);
|
Files.createDirectory(directory);
|
||||||
} catch (FileAlreadyExistsException race) {
|
} catch (FileAlreadyExistsException race) {
|
||||||
requireDirectory(directory);
|
requireDirectory(directory);
|
||||||
forceDirectory(Objects.requireNonNull(directory.getParent(), "Generation history parent"));
|
forceDirectory(parent, policy);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
forceDirectory(Objects.requireNonNull(directory.getParent(), "Generation history parent"));
|
forceDirectory(parent, policy);
|
||||||
requireDirectory(directory);
|
requireDirectory(directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void forceFile(FileChannel channel, EpochPolicy policy) throws IOException {
|
||||||
|
if (policy.syncs()) {
|
||||||
|
Durability.force(channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void forceDirectory(Path directory, EpochPolicy policy) throws IOException {
|
||||||
|
if (policy.syncs()) {
|
||||||
|
forceDirectory(directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void requireDirectory(Path directory) throws IOException {
|
private static void requireDirectory(Path directory) throws IOException {
|
||||||
requireSafeParent(directory);
|
requireSafeParent(directory);
|
||||||
if (!Files.exists(directory, LinkOption.NOFOLLOW_LINKS)) {
|
if (!Files.exists(directory, LinkOption.NOFOLLOW_LINKS)) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package art.arcane.iris.world.history;
|
package art.arcane.iris.world.history;
|
||||||
|
|
||||||
import art.arcane.iris.testsupport.DurabilityMode;
|
import art.arcane.iris.testsupport.DurabilityMode;
|
||||||
|
import art.arcane.iris.world.storage.Durability;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import org.junit.Assume;
|
import org.junit.Assume;
|
||||||
@@ -43,6 +44,30 @@ public class GenerationHistoryStoreTest {
|
|||||||
@Rule
|
@Rule
|
||||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onlySyncingPoliciesForceManifestAndEpochMetadata() throws Exception {
|
||||||
|
String previous = System.getProperty(Durability.MODE_PROPERTY);
|
||||||
|
System.clearProperty(Durability.MODE_PROPERTY);
|
||||||
|
try {
|
||||||
|
assertTrue(storeDirectorySyncs("durable-store", EpochPolicy.DURABLE) > 0);
|
||||||
|
assertEquals(0L, storeDirectorySyncs("studio-store", EpochPolicy.STUDIO));
|
||||||
|
} finally {
|
||||||
|
if (previous != null) {
|
||||||
|
System.setProperty(Durability.MODE_PROPERTY, previous);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private long storeDirectorySyncs(String name, EpochPolicy policy) throws Exception {
|
||||||
|
Path directory = temporaryFolder.getRoot().toPath().resolve(name).resolve("generation");
|
||||||
|
Files.createDirectories(directory.getParent());
|
||||||
|
List<Path> opened = DirectorySyncCounter.openedDirectories(() -> {
|
||||||
|
GenerationHistoryStore store = GenerationHistoryStore.initialize(directory, epoch(PACK_A), policy);
|
||||||
|
store.preparePendingActivation(epoch(PACK_B), 256);
|
||||||
|
});
|
||||||
|
return DirectorySyncCounter.countUnder(directory.getParent(), opened);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void persistsActiveAndPendingHistoryAcrossReopen() throws Exception {
|
public void persistsActiveAndPendingHistoryAcrossReopen() throws Exception {
|
||||||
Path directory = temporaryFolder.getRoot().toPath().resolve("generation");
|
Path directory = temporaryFolder.getRoot().toPath().resolve("generation");
|
||||||
|
|||||||
Reference in new issue
Block a user