From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Shane Freeder 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 index ffe9b1a63d78925e1d77b9e730aef42fed6d58fa..1278d09f70c1e97607ef20d87a178dc252c7f723 100644 --- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java +++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java @@ -446,4 +446,19 @@ public class PaperWorldConfig { 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 index e4b7bd30117e0a3af50ec28aada604df837ebb02..1d8695ac6692194c9a1cfd46c9d735120ca75245 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -261,6 +261,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant processQueue = new java.util.concurrent.ConcurrentLinkedQueue(); public int autosavePeriod; + public boolean serverAutoSave = false; // Paper public CommandDispatcher vanillaCommandDispatcher; private boolean forceTicks; // CraftBukkit end @@ -1246,14 +1247,24 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant 0 && this.ticks % autosavePeriod == 0) { // CraftBukkit - MinecraftServer.LOGGER.debug("Autosave started"); + //if (autosavePeriod > 0 && this.ticks % autosavePeriod == 0) { // CraftBukkit // Paper - move down + //MinecraftServer.LOGGER.debug("Autosave started"); // Paper + 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 + for (WorldServer world : getWorlds()) { + if (world.paperConfig.autoSavePeriod > 0) { + world.saveIncrementally(serverAutoSave); + } + } + // Paper end + this.methodProfiler.exit(); - MinecraftServer.LOGGER.debug("Autosave finished"); - } + //MinecraftServer.LOGGER.debug("Autosave finished"); // Paper + //} // Paper this.methodProfiler.enter("snooper"); if (((DedicatedServer) this).getDedicatedServerProperties().snooperEnabled && !this.snooper.d() && this.ticks > 100) { // Spigot diff --git a/src/main/java/net/minecraft/server/level/ChunkProviderServer.java b/src/main/java/net/minecraft/server/level/ChunkProviderServer.java index 24b7b76e11cf2b389c6f40fb4046294f356164c5..0b967cc5a86b357ff808621abcb96239bd09d077 100644 --- 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 { 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; @@ -422,7 +425,19 @@ public class PlayerChunk { 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; @@ -542,8 +557,32 @@ public class PlayerChunk { } 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) { diff --git a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java index c8b2013791ce0ac4438243998b43bbd781405254..ec9b765a09500c4e4e5dde8b7856e2b752e7fdc2 100644 --- a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java +++ b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java @@ -91,6 +91,7 @@ import net.minecraft.world.level.levelgen.structure.templatesystem.DefinedStruct import net.minecraft.world.level.storage.Convertable; import net.minecraft.world.level.storage.WorldPersistentData; import net.minecraft.world.phys.Vec3D; +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; @@ -378,6 +379,64 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d { } + // Paper start - incremental autosave + final ObjectRBTreeSet autoSaveQueue = new 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)); + }); + + protected void saveIncrementally() { + int savedThisTick = 0; + // optimized since we search far less chunks to hit ones that need to be saved + List reschedule = new java.util.ArrayList<>(this.world.paperConfig.maxAutoSaveChunksPerTick); + long currentTick = this.world.getTime(); + long maxSaveTime = currentTick - this.world.paperConfig.autoSavePeriod; + + for (Iterator iterator = this.autoSaveQueue.iterator(); iterator.hasNext();) { + PlayerChunk playerchunk = iterator.next(); + if (playerchunk.lastAutoSaveTime > maxSaveTime) { + break; + } + + iterator.remove(); + + IChunkAccess ichunkaccess = playerchunk.getChunkSave().getNow(null); + if (ichunkaccess instanceof Chunk) { + boolean shouldSave = ((Chunk)ichunkaccess).lastSaved <= maxSaveTime; + + if (shouldSave && this.saveChunk(ichunkaccess)) { + ++savedThisTick; + + if (!playerchunk.setHasBeenLoaded()) { + // do not fall through to reschedule logic + playerchunk.inactiveTimeStart = currentTick; + if (savedThisTick >= this.world.paperConfig.maxAutoSaveChunksPerTick) { + break; + } + continue; + } + } + } + + reschedule.add(playerchunk); + + if (savedThisTick >= this.world.paperConfig.maxAutoSaveChunksPerTick) { + break; + } + } + + for (int i = 0, len = reschedule.size(); i < len; ++i) { + PlayerChunk playerchunk = reschedule.get(i); + playerchunk.lastAutoSaveTime = this.world.getTime(); + this.autoSaveQueue.add(playerchunk); + } + } + // Paper end + protected void save(boolean flag) { if (flag) { List list = (List) this.visibleChunks.values().stream().filter(PlayerChunk::hasBeenLoaded).peek(PlayerChunk::m).collect(Collectors.toList()); @@ -488,6 +547,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d { this.world.unloadChunk(chunk); } + this.autoSaveQueue.remove(playerchunk); // Paper this.lightEngine.a(ichunkaccess.getPos()); this.lightEngine.queueUpdate(); @@ -680,6 +740,8 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d { 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()); }); diff --git a/src/main/java/net/minecraft/server/level/WorldServer.java b/src/main/java/net/minecraft/server/level/WorldServer.java index bfcd40c0cf7f3b41601d4c422e75690a20860bf0..2d2857b301377d45ca5c63e6d855304b3e605f78 100644 --- a/src/main/java/net/minecraft/server/level/WorldServer.java +++ b/src/main/java/net/minecraft/server/level/WorldServer.java @@ -885,11 +885,43 @@ public class WorldServer extends World implements GeneratorAccessSeed { return !this.server.a(this, blockposition, entityhuman) && this.getWorldBorder().a(blockposition); } + // Paper start - derived from below + public void saveIncrementally(boolean doFull) { + 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) { + this.saveData(); + } + + timings.worldSaveChunks.startTiming(); // Paper + if (!this.isSavingDisabled()) chunkproviderserver.saveIncrementally(); + timings.worldSaveChunks.stopTiming(); // Paper + + + // Copied from save() + // CraftBukkit start - moved from MinecraftServer.saveChunks + if (doFull) { // Paper + WorldServer worldserver1 = this; + + worldDataServer.a(worldserver1.getWorldBorder().t()); + worldDataServer.setCustomBossEvents(this.server.getBossBattleCustomData().save()); + convertable.a(this.server.customRegistry, this.worldDataServer, this.server.getPlayerList().save()); + } + // CraftBukkit end + } + } + // Paper end + public void save(@Nullable IProgressUpdate iprogressupdate, boolean flag, boolean flag1) { ChunkProviderServer chunkproviderserver = this.getChunkProvider(); if (!flag1) { - org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit + if (flag) org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld())); // CraftBukkit // Paper try (co.aikar.timings.Timing ignored = timings.worldSave.startTiming()) { // Paper if (iprogressupdate != null) { iprogressupdate.a(new ChatMessage("menu.savingLevel")); @@ -915,6 +947,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { // CraftBukkit end } + private void saveData() { this.aj(); } // Paper - OBFHELPER private void aj() { if (this.dragonBattle != null) { this.worldDataServer.a(this.dragonBattle.a()); // CraftBukkit diff --git a/src/main/java/net/minecraft/world/level/chunk/Chunk.java b/src/main/java/net/minecraft/world/level/chunk/Chunk.java index 5539ee8552c4bcfa718a63dbd4c25bd232048bff..f036cc45e7b1f3aedd8c63293b9b5436968a42c3 100644 --- a/src/main/java/net/minecraft/world/level/chunk/Chunk.java +++ b/src/main/java/net/minecraft/world/level/chunk/Chunk.java @@ -77,7 +77,7 @@ public class Chunk implements IChunkAccess { private TickList o; private TickList p; private boolean q; - private long lastSaved; + public long lastSaved; // Paper private volatile boolean s; private long inhabitedTime; @Nullable