From 61b8600730ca84e823f863f58ccb4e179caa463b Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Sun, 21 Jul 2019 14:36:27 -0700 Subject: [PATCH] Only write to cached header in RegionFile if write potentially succeeds (#2294) --- .../Fix-World-isChunkGenerated-calls.patch | 4 +-- ...gion-files-more-reliable-to-write-to.patch | 25 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Spigot-Server-Patches/Fix-World-isChunkGenerated-calls.patch b/Spigot-Server-Patches/Fix-World-isChunkGenerated-calls.patch index c925971ac5..af85e3c665 100644 --- a/Spigot-Server-Patches/Fix-World-isChunkGenerated-calls.patch +++ b/Spigot-Server-Patches/Fix-World-isChunkGenerated-calls.patch @@ -197,7 +197,7 @@ index 0911880c9d..86fb51b90a 100644 boolean isOutsideOfRange(ChunkCoordIntPair chunkcoordintpair) { // Spigot start diff --git a/src/main/java/net/minecraft/server/RegionFile.java b/src/main/java/net/minecraft/server/RegionFile.java -index 2e14d84657..66c8b0307f 100644 +index b0ec9edf67..41f1e15cb0 100644 --- a/src/main/java/net/minecraft/server/RegionFile.java +++ b/src/main/java/net/minecraft/server/RegionFile.java @@ -0,0 +0,0 @@ public class RegionFile implements AutoCloseable { @@ -240,7 +240,7 @@ index 2e14d84657..66c8b0307f 100644 return this.getOffset(chunkcoordintpair) != 0; } @@ -0,0 +0,0 @@ public class RegionFile implements AutoCloseable { - this.writeInt(i); // Paper - Avoid 3 io write calls + this.c[j] = i; // Paper - move this to after the write } + private final int getChunkLocation(ChunkCoordIntPair chunkcoordintpair) { return this.f(chunkcoordintpair); } // Paper - OBFHELPER diff --git a/Spigot-Server-Patches/Make-region-files-more-reliable-to-write-to.patch b/Spigot-Server-Patches/Make-region-files-more-reliable-to-write-to.patch index 76865ad6e6..be93503750 100644 --- a/Spigot-Server-Patches/Make-region-files-more-reliable-to-write-to.patch +++ b/Spigot-Server-Patches/Make-region-files-more-reliable-to-write-to.patch @@ -37,7 +37,7 @@ affect save performance if the startup flag is used (especially on HDDs). diff --git a/src/main/java/net/minecraft/server/RegionFile.java b/src/main/java/net/minecraft/server/RegionFile.java -index ed2ccebb23..2e14d84657 100644 +index ed2ccebb23..b0ec9edf67 100644 --- a/src/main/java/net/minecraft/server/RegionFile.java +++ b/src/main/java/net/minecraft/server/RegionFile.java @@ -0,0 +0,0 @@ public class RegionFile implements AutoCloseable { @@ -117,8 +117,11 @@ index ed2ccebb23..2e14d84657 100644 + // Paper end } - this.b(chunkcoordintpair, (int) (SystemUtils.getTimeMillis() / 1000L)); -@@ -0,0 +0,0 @@ public class RegionFile implements AutoCloseable { +- this.b(chunkcoordintpair, (int) (SystemUtils.getTimeMillis() / 1000L)); ++ // Paper - move this into writeChunkData + } catch (IOException ioexception) { + com.destroystokyo.paper.util.SneakyThrow.sneaky(ioexception); // Paper - we want the upper try/catch to retry this + } } @@ -139,19 +142,28 @@ index ed2ccebb23..2e14d84657 100644 private void a(ChunkCoordIntPair chunkcoordintpair, int i) throws IOException { int j = this.f(chunkcoordintpair); - this.c[j] = i; +- this.c[j] = i; ++ //this.c[j] = i; // Paper - move this to after the write this.b.seek((long) (j * 4)); - this.b.writeInt(i); + this.writeInt(i); // Paper - Avoid 3 io write calls ++ this.c[j] = i; // Paper - move this to after the write } private int f(ChunkCoordIntPair chunkcoordintpair) { -@@ -0,0 +0,0 @@ public class RegionFile implements AutoCloseable { + return chunkcoordintpair.j() + chunkcoordintpair.k() * 32; + } - this.d[j] = i; ++ private final void updateChunkTime(ChunkCoordIntPair chunkPos, final int time) throws IOException { this.b(chunkPos, time); } // Paper - OBFHELPER + private void b(ChunkCoordIntPair chunkcoordintpair, int i) throws IOException { + int j = this.f(chunkcoordintpair); + +- this.d[j] = i; ++ //this.d[j] = i; // Paper - move this to after the write this.b.seek((long) (4096 + j * 4)); - this.b.writeInt(i); + this.writeInt(i); // Paper - Avoid 3 io write calls ++ this.d[j] = i; // Paper - move this to after the write } public void close() throws IOException { @@ -189,6 +201,7 @@ index ed2ccebb23..2e14d84657 100644 + final int chunkOffset, final byte[] chunkData, final int chunkDataLength) throws IOException { + this.writeChunkData(chunkOffset, chunkData, chunkDataLength); + this.syncRegionFile(); // Sync is required to ensure the previous data is written successfully ++ this.updateChunkTime(chunk, (int)(SystemUtils.getTimeMillis() / 1000L)); + this.updateChunkHeader(chunk, chunkHeaderData); + this.syncRegionFile(); // Ensure header changes go through + }