2020-05-06 11:48:49 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2019-06-09 21:22:44 +02:00
From: Shane Freeder <theboyetronic@gmail.com>
Date: Sun, 9 Jun 2019 03:53:22 +0100
Subject: [PATCH] incremental chunk saving
diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
2021-01-11 02:44:06 +01:00
index ffe9b1a63d78925e1d77b9e730aef42fed6d58fa..1278d09f70c1e97607ef20d87a178dc252c7f723 100644
2019-06-09 21:22:44 +02:00
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
2021-01-11 02:44:06 +01:00
@@ -446,4 +446,19 @@ public class PaperWorldConfig {
2019-06-09 21:22:44 +02:00
keepLoadedRange = (short) (getInt("keep-spawn-loaded-range", Math.min(spigotConfig.viewDistance, 10)) * 16);
log( "Keep Spawn Loaded Range: " + (keepLoadedRange/16));
}
+
+ public int autoSavePeriod = -1;
+ private void autoSavePeriod() {
+ autoSavePeriod = getInt("auto-save-interval", -1);
+ if (autoSavePeriod > 0) {
+ log("Auto Save Interval: " +autoSavePeriod + " (" + (autoSavePeriod / 20) + "s)");
+ } else if (autoSavePeriod < 0) {
+ autoSavePeriod = net.minecraft.server.MinecraftServer.getServer().autosavePeriod;
+ }
+ }
+
+ public int maxAutoSaveChunksPerTick = 24;
+ private void maxAutoSaveChunksPerTick() {
+ maxAutoSaveChunksPerTick = getInt("max-auto-save-chunks-per-tick", 24);
+ }
}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-04-16 02:47:34 +02:00
index 4605b26cedbd0478b41e976f4b48ad78f12e37ff..4da16809f8f084fc4af4c50b5232914ea8aee04e 100644
2019-06-09 21:22:44 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-03-16 16:50:45 +01:00
@@ -262,6 +262,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2019-06-09 21:22:44 +02:00
public static int currentTick = 0; // Paper - Further improve tick loop
public java.util.Queue<Runnable> processQueue = new java.util.concurrent.ConcurrentLinkedQueue<Runnable>();
public int autosavePeriod;
+ public boolean serverAutoSave = false; // Paper
public CommandDispatcher vanillaCommandDispatcher;
private boolean forceTicks;
2020-06-26 18:20:03 +02:00
// CraftBukkit end
2021-04-16 02:47:34 +02:00
@@ -1257,14 +1258,24 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2019-06-09 21:22:44 +02:00
this.serverPing.b().a(agameprofile);
}
- if (autosavePeriod > 0 && this.ticks % autosavePeriod == 0) { // CraftBukkit
2019-09-30 03:14:40 +02:00
- MinecraftServer.LOGGER.debug("Autosave started");
2019-06-09 21:22:44 +02:00
+ //if (autosavePeriod > 0 && this.ticks % autosavePeriod == 0) { // CraftBukkit // Paper - move down
2019-09-30 03:14:40 +02:00
+ //MinecraftServer.LOGGER.debug("Autosave started"); // Paper
2019-06-09 21:22:44 +02:00
+ serverAutoSave = (autosavePeriod > 0 && this.ticks % autosavePeriod == 0); // Paper
this.methodProfiler.enter("save");
+ if (autosavePeriod > 0 && this.ticks % autosavePeriod == 0) { // Paper
this.playerList.savePlayers();
- this.saveChunks(true, false, false);
+ }// Paper
+ // Paper start
2019-06-10 07:39:04 +02:00
+ for (WorldServer world : getWorlds()) {
+ if (world.paperConfig.autoSavePeriod > 0) {
2020-06-26 08:29:44 +02:00
+ world.saveIncrementally(serverAutoSave);
2019-06-10 07:39:04 +02:00
+ }
2019-06-09 21:22:44 +02:00
+ }
+ // Paper end
+
this.methodProfiler.exit();
2019-09-30 03:14:40 +02:00
- MinecraftServer.LOGGER.debug("Autosave finished");
2019-06-09 21:22:44 +02:00
- }
2019-09-30 03:14:40 +02:00
+ //MinecraftServer.LOGGER.debug("Autosave finished"); // Paper
2019-06-09 21:22:44 +02:00
+ //} // Paper
this.methodProfiler.enter("snooper");
if (((DedicatedServer) this).getDedicatedServerProperties().snooperEnabled && !this.snooper.d() && this.ticks > 100) { // Spigot
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/server/level/ChunkProviderServer.java b/src/main/java/net/minecraft/server/level/ChunkProviderServer.java
2021-03-16 16:50:45 +01:00
index f2d48659fdb9f030dbeec12ed820062d4d066e48..5122afbd51c87c27efa82d7d9393f252efa848d4 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/server/level/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/level/ChunkProviderServer.java
@@ -557,6 +557,15 @@ public class ChunkProviderServer extends IChunkProvider {
} // Paper - Timings
}
+ // Paper start - duplicate save, but call incremental
+ public void saveIncrementally() {
+ this.tickDistanceManager();
+ try (co.aikar.timings.Timing timed = world.timings.chunkSaveData.startTiming()) { // Paper - Timings
+ this.playerChunkMap.saveIncrementally();
+ } // Paper - Timings
+ }
+ // Paper end
+
@Override
public void close() throws IOException {
// CraftBukkit start
diff --git a/src/main/java/net/minecraft/server/level/PlayerChunk.java b/src/main/java/net/minecraft/server/level/PlayerChunk.java
index 6bced8533df49d7bfdb32dfa0caad9d788ffc2c8..75d4a8fc394449ccc006fe67a8842edcd9f36854 100644
--- a/src/main/java/net/minecraft/server/level/PlayerChunk.java
+++ b/src/main/java/net/minecraft/server/level/PlayerChunk.java
@@ -67,6 +67,9 @@ public class PlayerChunk {
2020-01-28 01:16:53 +01:00
private final PlayerChunkMap chunkMap; // Paper
+ long lastAutoSaveTime; // Paper - incremental autosave
+ long inactiveTimeStart; // Paper - incremental autosave
+
public PlayerChunk(ChunkCoordIntPair chunkcoordintpair, int i, LightEngine lightengine, PlayerChunk.c playerchunk_c, PlayerChunk.d playerchunk_d) {
this.statusFutures = new AtomicReferenceArray(PlayerChunk.CHUNK_STATUSES.size());
this.fullChunkFuture = PlayerChunk.UNLOADED_CHUNK_FUTURE;
2021-03-16 08:19:45 +01:00
@@ -422,7 +425,19 @@ public class PlayerChunk {
2020-01-28 01:16:53 +01:00
boolean flag2 = playerchunk_state.isAtLeast(PlayerChunk.State.BORDER);
boolean flag3 = playerchunk_state1.isAtLeast(PlayerChunk.State.BORDER);
+ boolean prevHasBeenLoaded = this.hasBeenLoaded; // Paper
this.hasBeenLoaded |= flag3;
+ // Paper start - incremental autosave
+ if (this.hasBeenLoaded & !prevHasBeenLoaded) {
+ long timeSinceAutoSave = this.inactiveTimeStart - this.lastAutoSaveTime;
+ if (timeSinceAutoSave < 0) {
+ // safest bet is to assume autosave is needed here
+ timeSinceAutoSave = this.chunkMap.world.paperConfig.autoSavePeriod;
+ }
+ this.lastAutoSaveTime = this.chunkMap.world.getTime() - timeSinceAutoSave;
+ this.chunkMap.autoSaveQueue.add(this);
+ }
+ // Paper end
if (!flag2 && flag3) {
// Paper start - cache ticking ready status
int expectCreateCount = ++this.fullChunkCreateCount;
2021-03-16 08:19:45 +01:00
@@ -542,8 +557,32 @@ public class PlayerChunk {
2020-01-28 01:16:53 +01:00
}
public void m() {
+ boolean prev = this.hasBeenLoaded; // Paper
+ this.hasBeenLoaded = getChunkState(this.ticketLevel).isAtLeast(PlayerChunk.State.BORDER);
+ // Paper start - incremental autosave
+ if (prev != this.hasBeenLoaded) {
+ if (this.hasBeenLoaded) {
+ long timeSinceAutoSave = this.inactiveTimeStart - this.lastAutoSaveTime;
+ if (timeSinceAutoSave < 0) {
+ // safest bet is to assume autosave is needed here
+ timeSinceAutoSave = this.chunkMap.world.paperConfig.autoSavePeriod;
+ }
+ this.lastAutoSaveTime = this.chunkMap.world.getTime() - timeSinceAutoSave;
+ this.chunkMap.autoSaveQueue.add(this);
+ } else {
+ this.inactiveTimeStart = this.chunkMap.world.getTime();
+ this.chunkMap.autoSaveQueue.remove(this);
+ }
+ }
+ // Paper end
+ }
+
+ // Paper start - incremental autosave
+ public boolean setHasBeenLoaded() {
this.hasBeenLoaded = getChunkState(this.ticketLevel).isAtLeast(PlayerChunk.State.BORDER);
+ return this.hasBeenLoaded;
}
+ // Paper end
public void a(ProtoChunkExtension protochunkextension) {
for (int i = 0; i < this.statusFutures.length(); ++i) {
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
2021-03-16 14:04:28 +01:00
index ccfde274edfe1b611ccf8c583c92b16d52e4518d..1f32ab230d650bb5f652efbacdd5e4b90dc4de89 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
2021-03-16 14:04:28 +01:00
@@ -93,6 +93,7 @@ import net.minecraft.world.level.levelgen.structure.templatesystem.DefinedStruct
2021-03-16 08:19:45 +01:00
import net.minecraft.world.level.storage.Convertable;
import net.minecraft.world.level.storage.WorldPersistentData;
import net.minecraft.world.phys.Vec3D;
2020-08-25 04:22:08 +02:00
+import it.unimi.dsi.fastutil.objects.ObjectRBTreeSet; // Paper
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-03-16 14:04:28 +01:00
@@ -380,6 +381,64 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2019-12-17 23:39:07 +01:00
2019-07-28 02:38:29 +02:00
}
2019-06-09 21:22:44 +02:00
2020-01-28 01:16:53 +01:00
+ // Paper start - incremental autosave
2020-08-25 04:22:08 +02:00
+ final ObjectRBTreeSet<PlayerChunk> autoSaveQueue = new ObjectRBTreeSet<>((playerchunk1, playerchunk2) -> {
2020-01-28 01:16:53 +01:00
+ int timeCompare = Long.compare(playerchunk1.lastAutoSaveTime, playerchunk2.lastAutoSaveTime);
+ if (timeCompare != 0) {
+ return timeCompare;
+ }
+
+ return Long.compare(MCUtil.getCoordinateKey(playerchunk1.location), MCUtil.getCoordinateKey(playerchunk2.location));
+ });
+
2019-07-28 02:38:29 +02:00
+ protected void saveIncrementally() {
+ int savedThisTick = 0;
2020-01-28 01:16:53 +01:00
+ // optimized since we search far less chunks to hit ones that need to be saved
2020-06-26 01:38:24 +02:00
+ List<PlayerChunk> reschedule = new java.util.ArrayList<>(this.world.paperConfig.maxAutoSaveChunksPerTick);
2020-01-28 01:16:53 +01:00
+ long currentTick = this.world.getTime();
+ long maxSaveTime = currentTick - this.world.paperConfig.autoSavePeriod;
2019-07-28 02:38:29 +02:00
+
2020-01-28 01:16:53 +01:00
+ for (Iterator<PlayerChunk> iterator = this.autoSaveQueue.iterator(); iterator.hasNext();) {
+ PlayerChunk playerchunk = iterator.next();
+ if (playerchunk.lastAutoSaveTime > maxSaveTime) {
+ break;
+ }
2019-07-28 02:38:29 +02:00
+
2020-01-28 01:16:53 +01:00
+ iterator.remove();
2019-07-28 02:38:29 +02:00
+
2020-01-28 01:16:53 +01:00
+ IChunkAccess ichunkaccess = playerchunk.getChunkSave().getNow(null);
+ if (ichunkaccess instanceof Chunk) {
+ boolean shouldSave = ((Chunk)ichunkaccess).lastSaved <= maxSaveTime;
2019-06-25 03:47:58 +02:00
+
2020-01-28 01:16:53 +01:00
+ if (shouldSave && this.saveChunk(ichunkaccess)) {
+ ++savedThisTick;
2019-06-09 21:22:44 +02:00
+
2020-01-28 01:16:53 +01:00
+ if (!playerchunk.setHasBeenLoaded()) {
+ // do not fall through to reschedule logic
+ playerchunk.inactiveTimeStart = currentTick;
+ if (savedThisTick >= this.world.paperConfig.maxAutoSaveChunksPerTick) {
+ break;
+ }
+ continue;
2019-06-09 21:22:44 +02:00
+ }
2019-06-17 09:47:51 +02:00
+ }
2020-01-28 01:16:53 +01:00
+ }
2019-07-28 02:38:29 +02:00
+
2020-01-28 01:16:53 +01:00
+ reschedule.add(playerchunk);
+
+ if (savedThisTick >= this.world.paperConfig.maxAutoSaveChunksPerTick) {
+ break;
2019-07-28 02:38:29 +02:00
+ }
+ }
2020-01-28 01:16:53 +01:00
+
+ for (int i = 0, len = reschedule.size(); i < len; ++i) {
+ PlayerChunk playerchunk = reschedule.get(i);
+ playerchunk.lastAutoSaveTime = this.world.getTime();
+ this.autoSaveQueue.add(playerchunk);
+ }
2019-07-28 02:38:29 +02:00
+ }
2020-01-28 01:16:53 +01:00
+ // Paper end
2019-07-28 02:38:29 +02:00
+
protected void save(boolean flag) {
if (flag) {
List<PlayerChunk> list = (List) this.visibleChunks.values().stream().filter(PlayerChunk::hasBeenLoaded).peek(PlayerChunk::m).collect(Collectors.toList());
2021-03-16 14:04:28 +01:00
@@ -490,6 +549,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-01-28 01:16:53 +01:00
this.world.unloadChunk(chunk);
}
+ this.autoSaveQueue.remove(playerchunk); // Paper
this.lightEngine.a(ichunkaccess.getPos());
this.lightEngine.queueUpdate();
2021-03-16 14:04:28 +01:00
@@ -682,6 +742,8 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2020-01-28 01:16:53 +01:00
playerchunk.a(new ProtoChunkExtension(chunk));
}
+ chunk.setLastSaved(this.world.getTime() - 1); // Paper - avoid autosaving newly generated/loaded chunks
+
chunk.a(() -> {
return PlayerChunk.getChunkState(playerchunk.getTicketLevel());
});
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/server/level/WorldServer.java b/src/main/java/net/minecraft/server/level/WorldServer.java
2021-04-07 07:17:32 +02:00
index a9c0d3fc4aa07d9d580a31106169796b7bde4e63..735da5729c16940e3d8877f32a40342b9d1e989d 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/server/level/WorldServer.java
+++ b/src/main/java/net/minecraft/server/level/WorldServer.java
2021-04-07 07:17:32 +02:00
@@ -888,11 +888,43 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2020-06-26 01:38:24 +02:00
return !this.server.a(this, blockposition, entityhuman) && this.getWorldBorder().a(blockposition);
2019-07-28 02:38:29 +02:00
}
+ // Paper start - derived from below
2020-06-26 01:38:24 +02:00
+ public void saveIncrementally(boolean doFull) {
2019-07-28 02:38:29 +02:00
+ ChunkProviderServer chunkproviderserver = this.getChunkProvider();
+
+ if (doFull) {
+ org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld()));
+ }
+
+ try (co.aikar.timings.Timing ignored = timings.worldSave.startTiming()) {
+ if (doFull) {
2019-12-13 02:18:18 +01:00
+ this.saveData();
2019-07-28 02:38:29 +02:00
+ }
+
+ timings.worldSaveChunks.startTiming(); // Paper
+ if (!this.isSavingDisabled()) chunkproviderserver.saveIncrementally();
+ timings.worldSaveChunks.stopTiming(); // Paper
+
+
2020-06-26 01:38:24 +02:00
+ // Copied from save()
2019-07-28 02:38:29 +02:00
+ // CraftBukkit start - moved from MinecraftServer.saveChunks
2020-06-26 01:38:24 +02:00
+ if (doFull) { // Paper
2019-07-28 02:38:29 +02:00
+ WorldServer worldserver1 = this;
+
2020-06-26 01:38:24 +02:00
+ worldDataServer.a(worldserver1.getWorldBorder().t());
+ worldDataServer.setCustomBossEvents(this.server.getBossBattleCustomData().save());
2020-09-11 01:56:58 +02:00
+ convertable.a(this.server.customRegistry, this.worldDataServer, this.server.getPlayerList().save());
2019-07-28 02:38:29 +02:00
+ }
2020-06-26 01:38:24 +02:00
+ // CraftBukkit end
2019-07-28 02:38:29 +02:00
+ }
+ }
+ // Paper end
+
2020-06-26 01:38:24 +02:00
public void save(@Nullable IProgressUpdate iprogressupdate, boolean flag, boolean flag1) {
2019-06-10 22:45:17 +02:00
ChunkProviderServer chunkproviderserver = this.getChunkProvider();
2019-06-09 21:22:44 +02:00
if (!flag1) {
2019-06-10 22:45:17 +02:00
- org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit
2020-06-26 01:38:24 +02:00
+ if (flag) org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit // Paper
2019-06-09 21:22:44 +02:00
try (co.aikar.timings.Timing ignored = timings.worldSave.startTiming()) { // Paper
if (iprogressupdate != null) {
2020-06-26 01:38:24 +02:00
iprogressupdate.a(new ChatMessage("menu.savingLevel"));
2021-04-07 07:17:32 +02:00
@@ -918,6 +950,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2019-12-13 02:18:18 +01:00
// CraftBukkit end
}
2020-11-03 03:22:15 +01:00
+ private void saveData() { this.aj(); } // Paper - OBFHELPER
private void aj() {
2020-06-26 01:38:24 +02:00
if (this.dragonBattle != null) {
2020-07-17 01:07:15 +02:00
this.worldDataServer.a(this.dragonBattle.a()); // CraftBukkit
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/Chunk.java b/src/main/java/net/minecraft/world/level/chunk/Chunk.java
2021-03-16 16:50:45 +01:00
index 3f926ed8e2b2c9dbf1e2493870af7eff3b6db019..2690c44eaae193a259fe195c95e59d07d5e1cc5a 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/world/level/chunk/Chunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/Chunk.java
2021-03-16 16:50:45 +01:00
@@ -81,7 +81,7 @@ public class Chunk implements IChunkAccess {
2021-03-16 08:19:45 +01:00
private TickList<Block> o;
private TickList<FluidType> p;
private boolean q;
- private long lastSaved;
+ public long lastSaved; // Paper
private volatile boolean s;
private long inhabitedTime;
@Nullable