mirror of
https://hub.spigotmc.org/stash/scm/spigot/spigot.git
synced 2025-09-18 21:33:01 +00:00

Previously we would write to header before writing our chunk data, which opens a window for corruption (or we would overwrite entirely). Now the saving process has been changed to follow this chain of events: 1. We always allocate a new space to write so we do not potentially overwrite and corrupt the current data 2. Write the chunk data first (the order of the fields in the chunk data isn't relevant though) 3. Flush to disk (if the launch flag is used) 4. Write to the region header last 5. Flush to disk (if the launch flag is used) 6. Then we free the previous space allocated With this chain of events it is impossible for a chunk write to corrupt a region file, unless the operating system has messed around with the order of our writes or if it has lied about us flushing (if applicable). However server administrators are still recommended to continue performing regular backups. If the spigot.flush-on-save startup flag is set to true, then the steps 3 and 5 will make a call to sync() on the region file's fd, effectively flushing to disk. For people running on harddrives this is not recommended since you will incur a very significant performance hit. As such the option is disabled by default.
210 lines
9 KiB
Diff
210 lines
9 KiB
Diff
From 3818b2f1b3df645965b1359fbebce0a8635b0933 Mon Sep 17 00:00:00 2001
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
Date: Tue, 9 Jul 2019 02:40:33 -0700
|
|
Subject: [PATCH] Make region files more reliable to write to
|
|
|
|
Previously we would write to header before writing our chunk data,
|
|
which opens a window for corruption (or we would overwrite entirely).
|
|
Now the saving process has been changed to follow this chain of events:
|
|
|
|
1. We always allocate a new space to write so we do not potentially
|
|
overwrite and corrupt the current data
|
|
2. Write the chunk data first (the order of the fields in
|
|
the chunk data isn't relevant though)
|
|
3. Flush to disk (if the launch flag is used)
|
|
4. Write to the region header last
|
|
5. Flush to disk (if the launch flag is used)
|
|
6. Then we free the previous space allocated
|
|
|
|
With this chain of events it is impossible for a chunk write to corrupt
|
|
a region file, unless the operating system has messed around with the order
|
|
of our writes or if it has lied about us flushing (if applicable).
|
|
|
|
However server administrators are still recommended to continue performing
|
|
regular backups.
|
|
|
|
If the spigot.flush-on-save startup flag is set to true, then the
|
|
steps 3 and 5 will make a call to sync() on the region file's fd,
|
|
effectively flushing to disk. For people running on harddrives this
|
|
is not recommended since you will incur a very significant performance
|
|
hit. As such the option is disabled by default.
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/RegionFile.java b/src/main/java/net/minecraft/server/RegionFile.java
|
|
index d4a9af975..8b17dde50 100644
|
|
--- a/src/main/java/net/minecraft/server/RegionFile.java
|
|
+++ b/src/main/java/net/minecraft/server/RegionFile.java
|
|
@@ -25,10 +25,10 @@ public class RegionFile implements AutoCloseable {
|
|
private final File file;
|
|
// Spigot end
|
|
private static final byte[] a = new byte[4096];
|
|
- private final RandomAccessFile b;
|
|
+ private final RandomAccessFile b; // PAIL dataFile
|
|
private final int[] c = new int[1024];
|
|
private final int[] d = new int[1024];
|
|
- private final List<Boolean> e;
|
|
+ private final List<Boolean> e; // PAIL freeSectors
|
|
|
|
public RegionFile(File file) throws IOException {
|
|
this.b = new RandomAccessFile(file, "rw");
|
|
@@ -177,8 +177,8 @@ public class RegionFile implements AutoCloseable {
|
|
protected synchronized void a(ChunkCoordIntPair chunkcoordintpair, byte[] abyte, int i) {
|
|
try {
|
|
int j = this.getOffset(chunkcoordintpair);
|
|
- int k = j >> 8;
|
|
- int l = j & 255;
|
|
+ int k = j >> 8; final int oldSectorOffset = k; // Spigot - store variable for later
|
|
+ int l = j & 255; final int oldSectorCount; // Spigot - store variable for later
|
|
// Spigot start
|
|
if (l == 255) {
|
|
this.b.seek(k * 4096);
|
|
@@ -186,6 +186,7 @@ public class RegionFile implements AutoCloseable {
|
|
}
|
|
// Spigot end
|
|
int i1 = (i + 5) / 4096 + 1;
|
|
+ oldSectorCount = l; // Spigot - store variable for later (watch out for re-assignments of l)
|
|
|
|
if (i1 >= 256) {
|
|
// Spigot start
|
|
@@ -194,14 +195,18 @@ public class RegionFile implements AutoCloseable {
|
|
// Spigot end
|
|
}
|
|
|
|
- if (k != 0 && l == i1) {
|
|
+ if (false && k != 0 && l == i1) { // Spigot - We never want to overrite old data
|
|
this.a(k, abyte, i);
|
|
} else {
|
|
int j1;
|
|
|
|
+ // Spigot start - We do not free old sectors until we are done writing the new chunk data
|
|
+ /*
|
|
for (j1 = 0; j1 < l; ++j1) {
|
|
this.e.set(k + j1, true);
|
|
}
|
|
+ */
|
|
+ // Spigot end
|
|
|
|
j1 = this.e.indexOf(true);
|
|
int k1 = 0;
|
|
@@ -228,13 +233,13 @@ public class RegionFile implements AutoCloseable {
|
|
|
|
if (k1 >= i1) {
|
|
k = j1;
|
|
- this.a(chunkcoordintpair, j1 << 8 | (i1 > 255 ? 255 : i1)); // Spigot
|
|
+ // this.a(chunkcoordintpair, j1 << 8 | (i1 > 255 ? 255 : i1)); // Spigot // Spigot - We only write to header after we've written chunk data
|
|
|
|
for (l1 = 0; l1 < i1; ++l1) {
|
|
this.e.set(k + l1, false);
|
|
}
|
|
|
|
- this.a(k, abyte, i);
|
|
+ this.writeChunk(chunkcoordintpair, j1 << 8 | (i1 > 255 ? 255 : i1), k, abyte, i); // Spigot - Ensure we do not corrupt region files
|
|
} else {
|
|
this.b.seek(this.b.length());
|
|
k = this.e.size();
|
|
@@ -244,22 +249,26 @@ public class RegionFile implements AutoCloseable {
|
|
this.e.add(false);
|
|
}
|
|
|
|
- this.a(k, abyte, i);
|
|
- this.a(chunkcoordintpair, k << 8 | (i1 > 255 ? 255 : i1)); // Spigot
|
|
+ this.writeChunk(chunkcoordintpair, k << 8 | (i1 > 255 ? 255 : i1), k, abyte, i); // Spigot - Ensure we do not corrupt region files
|
|
+ }
|
|
+
|
|
+ // Spigot start - Now that we've written the new chunk we can free the old data
|
|
+ for (int off = 0; off < oldSectorCount; ++off) {
|
|
+ this.e.set(oldSectorOffset + off, true);
|
|
}
|
|
+ // Spigot end
|
|
}
|
|
|
|
- this.b(chunkcoordintpair, (int) (SystemUtils.getTimeMillis() / 1000L));
|
|
+ // this.b(chunkcoordintpair, (int) (SystemUtils.getTimeMillis() / 1000L)); // Spigot - move this into writeChunk
|
|
} catch (IOException ioexception) {
|
|
ioexception.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
- private void a(int i, byte[] abyte, int j) throws IOException {
|
|
+ private void a(int i, byte[] abyte, int j) throws IOException { // PAIL writeChunkData
|
|
this.b.seek((long) (i * 4096));
|
|
- this.b.writeInt(j + 1);
|
|
- this.b.writeByte(2);
|
|
+ this.writeIntAndByte(j + 1, (byte)2); // Spigot - Avoid 4 io write calls
|
|
this.b.write(abyte, 0, j);
|
|
}
|
|
|
|
@@ -271,30 +280,64 @@ public class RegionFile implements AutoCloseable {
|
|
return this.getOffset(chunkcoordintpair) != 0;
|
|
}
|
|
|
|
- private void a(ChunkCoordIntPair chunkcoordintpair, int i) throws IOException {
|
|
+ private void a(ChunkCoordIntPair chunkcoordintpair, int i) throws IOException { // PAIL updateChunkHeader
|
|
int j = this.f(chunkcoordintpair);
|
|
|
|
- this.c[j] = i;
|
|
+ //this.c[j] = i; // Spigot - move this to after the write
|
|
this.b.seek((long) (j * 4));
|
|
- this.b.writeInt(i);
|
|
+ this.writeInt(i); // Spigot - Avoid 3 io write calls
|
|
+ this.c[j] = i; // Spigot - move this to after the write
|
|
}
|
|
|
|
private int f(ChunkCoordIntPair chunkcoordintpair) {
|
|
return chunkcoordintpair.j() + chunkcoordintpair.k() * 32;
|
|
}
|
|
|
|
- private void b(ChunkCoordIntPair chunkcoordintpair, int i) throws IOException {
|
|
+ private void b(ChunkCoordIntPair chunkcoordintpair, int i) throws IOException { // PAIL updateChunkTime
|
|
int j = this.f(chunkcoordintpair);
|
|
|
|
- this.d[j] = i;
|
|
+ // this.d[j] = i; // Spigot - move this to after the write
|
|
this.b.seek((long) (4096 + j * 4));
|
|
- this.b.writeInt(i);
|
|
+ this.writeInt(i); // Spigot - Avoid 3 io write calls
|
|
+ this.d[j] = i; // Spigot - move this to after the write
|
|
}
|
|
|
|
public void close() throws IOException {
|
|
this.b.close();
|
|
}
|
|
|
|
+ // Spigot start - Make region files reliable
|
|
+ private static final boolean FLUSH_ON_SAVE = Boolean.getBoolean("spigot.flush-on-save");
|
|
+ private void syncRegionFile() throws IOException {
|
|
+ if (!FLUSH_ON_SAVE) {
|
|
+ return;
|
|
+ }
|
|
+ this.b.getFD().sync(); // rethrow exception as we want to avoid corrupting a regionfile
|
|
+ }
|
|
+
|
|
+ private final java.nio.ByteBuffer scratchBuffer = java.nio.ByteBuffer.allocate(8);
|
|
+
|
|
+ private void writeInt(final int value) throws IOException {
|
|
+ this.scratchBuffer.putInt(0, value);
|
|
+ this.b.write(this.scratchBuffer.array(), 0, 4);
|
|
+ }
|
|
+
|
|
+ // writes v1 then v2
|
|
+ private void writeIntAndByte(final int v1, final byte v2) throws IOException {
|
|
+ this.scratchBuffer.putInt(0, v1);
|
|
+ this.scratchBuffer.put(4, v2);
|
|
+ this.b.write(this.scratchBuffer.array(), 0, 5);
|
|
+ }
|
|
+
|
|
+ private void writeChunk(final ChunkCoordIntPair chunk, final int chunkHeaderData, final int chunkOffset, final byte[] chunkData, final int chunkDataLength) throws IOException {
|
|
+ this.a(chunkOffset, chunkData, chunkDataLength); // chunk data
|
|
+ this.syncRegionFile(); // Sync is required to ensure the previous data is written successfully
|
|
+ this.b(chunk, (int) (SystemUtils.getTimeMillis() / 1000L)); // chunk time
|
|
+ this.a(chunk, chunkHeaderData); // chunk header
|
|
+ this.syncRegionFile(); // Ensure header changes go through
|
|
+ }
|
|
+ // Spigot end
|
|
+
|
|
class ChunkBuffer extends ByteArrayOutputStream {
|
|
|
|
private final ChunkCoordIntPair b;
|
|
--
|
|
2.20.1
|
|
|