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
2020-05-06 11:48:49 +02:00
index 071e5e7f729d6c3ffb70506e7ef32eebee1e9118..48676152152faf7a7b9524ac37d8b4a8c32c4e2c 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
2020-01-18 18:28:32 +01:00
@@ -487,4 +487,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/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
2020-06-02 05:15:47 +02:00
index 4f05468590e30f4b11599d3841418c76519255e4..820033289cbeb782429b26b6bcabc3835b0101a3 100644
2019-06-09 21:22:44 +02:00
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -42,7 +42,7 @@ public class Chunk implements IChunkAccess {
private TickList<Block> o;
private TickList<FluidType> p;
private boolean q;
- private long lastSaved;
+ public long lastSaved; // Paper
private volatile boolean s;
2019-12-12 17:20:43 +01:00
private long inhabitedTime;
2019-06-09 21:22:44 +02:00
@Nullable
2019-07-28 02:38:29 +02:00
diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
2020-05-06 11:48:49 +02:00
index e6d08756f76360b29b29f18305e5ec84d09f2d54..6713b7667ae4fe3f1f555a71321832b4a9492162 100644
2019-07-28 02:38:29 +02:00
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
2020-04-24 11:33:33 +02:00
@@ -515,6 +515,15 @@ public class ChunkProviderServer extends IChunkProvider {
2019-07-28 02:38:29 +02:00
} // 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 {
2019-08-05 18:35:40 +02:00
// CraftBukkit start
2019-06-09 21:22:44 +02:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2020-05-17 03:38:19 +02:00
index 8b499c815c77bf5b356d4216ba6cbf2a329c9aca..cfed5f51431ec5aecb538a321327bfb6e8a0bd88 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
2019-12-12 17:20:43 +01:00
@@ -168,6 +168,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 File bukkitDataPackFolder;
public CommandDispatcher vanillaCommandDispatcher;
private boolean forceTicks;
2020-05-17 03:38:19 +02:00
@@ -1116,14 +1117,28 @@ 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) {
+ try {
2019-07-28 02:38:29 +02:00
+ world.saveIncrementally(serverAutoSave);
2019-06-10 07:39:04 +02:00
+ } catch (ExceptionWorldConflict exceptionWorldConflict) {
+ MinecraftServer.LOGGER.warn(exceptionWorldConflict.getMessage());
+ }
+ }
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
2020-01-28 01:16:53 +01:00
diff --git a/src/main/java/net/minecraft/server/PlayerChunk.java b/src/main/java/net/minecraft/server/PlayerChunk.java
2020-05-20 11:11:57 +02:00
index 47e3e618c9e683e6975fb64e1094dc7078574dae..ed9ada49c7cc1131691bd6e005b2380274ef23e3 100644
2020-01-28 01:16:53 +01:00
--- a/src/main/java/net/minecraft/server/PlayerChunk.java
+++ b/src/main/java/net/minecraft/server/PlayerChunk.java
2020-05-20 11:11:57 +02:00
@@ -41,6 +41,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;
2020-05-20 11:11:57 +02:00
@@ -386,7 +389,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;
2020-05-20 11:11:57 +02:00
@@ -506,8 +521,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) {
2019-06-09 21:22:44 +02:00
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
2020-05-06 11:48:49 +02:00
index 34f470779fa5d1cf9638431253024481236c073b..4f5b516144829a7ae11f21a56789ac7a1f256250 100644
2019-06-09 21:22:44 +02:00
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
2020-05-06 09:44:47 +02:00
@@ -332,6 +332,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
+ final it.unimi.dsi.fastutil.objects.ObjectRBTreeSet<PlayerChunk> autoSaveQueue = new it.unimi.dsi.fastutil.objects.ObjectRBTreeSet<>((playerchunk1, playerchunk2) -> {
+ 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
+ List<PlayerChunk> reschedule = new ArrayList<>(this.world.paperConfig.maxAutoSaveChunksPerTick);
+ 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());
2020-05-06 09:44:47 +02:00
@@ -442,6 +500,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();
2020-05-06 09:44:47 +02:00
@@ -623,6 +682,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());
});
2019-06-09 21:22:44 +02:00
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
2020-05-17 03:38:19 +02:00
index ca0a23be27ee8174204867d463eb89a10931ff84..a0484d8062ecfb817cfd5b996915dc8f9a4eb2bd 100644
2019-06-09 21:22:44 +02:00
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
2020-05-10 11:52:31 +02:00
@@ -817,11 +817,44 @@ public class WorldServer extends World {
2019-12-12 17:20:43 +01:00
return this.worldProvider.c();
2019-07-28 02:38:29 +02:00
}
+ // Paper start - derived from below
+ public void saveIncrementally(boolean doFull) throws ExceptionWorldConflict {
+ 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
+
+
+ // CraftBukkit start - moved from MinecraftServer.saveChunks
+ // PAIL - rename
+ if (doFull) {
+ WorldServer worldserver1 = this;
+ WorldData worlddata = worldserver1.getWorldData();
+
2019-12-13 02:18:18 +01:00
+ worldserver1.getWorldBorder().save(worlddata);
+ worlddata.setCustomBossEvents(this.server.getBossBattleCustomData().save());
+ worldserver1.getDataManager().saveWorldData(worlddata, this.server.getPlayerList().save());
2019-07-28 02:38:29 +02:00
+ // CraftBukkit end
+ }
+ }
+ }
+ // Paper end
+
public void save(@Nullable IProgressUpdate iprogressupdate, boolean flag, boolean flag1) throws ExceptionWorldConflict {
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
2019-07-28 02:38:29 +02:00
+ if (flag) org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit
2019-06-09 21:22:44 +02:00
try (co.aikar.timings.Timing ignored = timings.worldSave.startTiming()) { // Paper
if (iprogressupdate != null) {
iprogressupdate.a(new ChatMessage("menu.savingLevel", new Object[0]));
2020-05-10 11:52:31 +02:00
@@ -848,6 +881,7 @@ public class WorldServer extends World {
2019-12-13 02:18:18 +01:00
// CraftBukkit end
}
+ protected void saveData() throws ExceptionWorldConflict { this.m_(); } // Paper - OBFHELPER
protected void m_() throws ExceptionWorldConflict {
this.checkSession();
this.worldProvider.i();