2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shane Freeder <theboyetronic@gmail.com>
Date: Sun, 9 Jun 2019 03:53:22 +0100
Subject: [PATCH] incremental chunk saving
2021-06-14 07:34:40 +02:00
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
2021-06-17 10:37:27 +02:00
index 9e5810eb0085ad956f0bd1cd69fa88909d9d638a..77e90a6b7d29ad989fd961e00a6fd97c7e5ec4fe 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
2021-06-15 15:20:52 +02:00
@@ -44,6 +44,21 @@ public class PaperWorldConfig {
2021-06-11 14:02:28 +02:00
log( "Keep Spawn Loaded Range: " + (keepLoadedRange/16));
}
2021-06-14 07:34:40 +02:00
2021-06-11 14:02:28 +02:00
+ 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);
+ }
2021-06-14 07:34:40 +02:00
+
private boolean getBoolean(String path, boolean def) {
config.addDefault("world-settings.default." + path, def);
return config.getBoolean("world-settings." + worldName + "." + path, config.getBoolean("world-settings.default." + path));
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-06-17 10:37:27 +02:00
index e3d6a4f64948d5b793fd82262b08753d8a60609e..04531db7c26b9defe76d2584d960132ad79dc0fa 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-06-14 07:34:40 +02:00
@@ -300,6 +300,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +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 Commands vanillaCommandDispatcher;
2021-06-14 07:34:40 +02:00
public boolean forceTicks; // Paper
2021-06-11 14:02:28 +02:00
// CraftBukkit end
2021-06-14 07:34:40 +02:00
@@ -1411,14 +1412,23 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
this.status.getPlayers().setSample(agameprofile);
}
2021-06-14 07:34:40 +02:00
- if (this.autosavePeriod > 0 && this.tickCount % this.autosavePeriod == 0) { // CraftBukkit
2021-06-11 14:02:28 +02:00
- MinecraftServer.LOGGER.debug("Autosave started");
2021-06-14 07:34:40 +02:00
+ // if (this.autosavePeriod > 0 && this.tickCount % this.autosavePeriod == 0) { // CraftBukkit // Paper - move down
+ // MinecraftServer.LOGGER.debug("Autosave started"); // Paper
+ serverAutoSave = (autosavePeriod > 0 && this.tickCount % autosavePeriod == 0); // Paper
2021-06-11 14:02:28 +02:00
this.profiler.push("save");
2021-06-14 07:34:40 +02:00
+ if (this.autosavePeriod > 0 && this.tickCount % this.autosavePeriod == 0) { // Paper - moved from above
2021-06-11 14:02:28 +02:00
this.playerList.saveAll();
- this.saveAllChunks(true, false, false);
2021-06-14 07:34:40 +02:00
- this.profiler.pop();
2021-06-11 14:02:28 +02:00
- MinecraftServer.LOGGER.debug("Autosave finished");
2021-06-14 07:34:40 +02:00
+ // this.saveAllChunks(true, false, false); // Paper - saved incrementally below
+ } // Paper start
+ for (ServerLevel level : this.getAllLevels()) {
+ if (level.paperConfig.autoSavePeriod > 0) {
+ level.saveIncrementally(this.serverAutoSave);
+ }
}
+ // Paper end
+ this.profiler.pop();
+ // MinecraftServer.LOGGER.debug("Autosave finished"); // Paper
2021-06-11 14:02:28 +02:00
+ //} // Paper
this.profiler.push("snooper");
if (((DedicatedServer) this).getProperties().snooperEnabled && !this.snooper.isStarted() && this.tickCount > 100) { // Spigot
diff --git a/src/main/java/net/minecraft/server/level/ChunkHolder.java b/src/main/java/net/minecraft/server/level/ChunkHolder.java
2021-06-17 10:37:27 +02:00
index 948a817a1d6f4435655931357aa094b889771e82..88c7ae09e29a32309bf2a63ac0593b42d3fe6a42 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ChunkHolder.java
+++ b/src/main/java/net/minecraft/server/level/ChunkHolder.java
2021-06-14 07:34:40 +02:00
@@ -111,6 +111,8 @@ public class ChunkHolder {
this.playersInChunkTickRange = this.chunkMap.playerChunkTickRangeMap.getObjectsInRange(key);
}
// Paper end - optimise isOutsideOfRange
2021-06-11 14:02:28 +02:00
+ long lastAutoSaveTime; // Paper - incremental autosave
+ long inactiveTimeStart; // Paper - incremental autosave
2021-06-14 07:34:40 +02:00
public ChunkHolder(ChunkPos pos, int level, LevelHeightAccessor world, LevelLightEngine lightingProvider, ChunkHolder.LevelChangeListener levelUpdateListener, ChunkHolder.PlayerProvider playersWatchingChunkProvider) {
2021-06-11 14:02:28 +02:00
this.futures = new AtomicReferenceArray(ChunkHolder.CHUNK_STATUSES.size());
2021-06-14 07:34:40 +02:00
@@ -533,7 +535,19 @@ public class ChunkHolder {
2021-06-11 14:02:28 +02:00
boolean flag2 = playerchunk_state.isOrAfter(ChunkHolder.FullChunkStatus.BORDER);
boolean flag3 = playerchunk_state1.isOrAfter(ChunkHolder.FullChunkStatus.BORDER);
+ boolean prevHasBeenLoaded = this.wasAccessibleSinceLastSave; // Paper
this.wasAccessibleSinceLastSave |= flag3;
+ // Paper start - incremental autosave
+ if (this.wasAccessibleSinceLastSave & !prevHasBeenLoaded) {
+ long timeSinceAutoSave = this.inactiveTimeStart - this.lastAutoSaveTime;
+ if (timeSinceAutoSave < 0) {
+ // safest bet is to assume autosave is needed here
+ timeSinceAutoSave = this.chunkMap.level.paperConfig.autoSavePeriod;
+ }
+ this.lastAutoSaveTime = this.chunkMap.level.getGameTime() - timeSinceAutoSave;
+ this.chunkMap.autoSaveQueue.add(this);
+ }
+ // Paper end
if (!flag2 && flag3) {
2021-06-14 07:34:40 +02:00
int expectCreateCount = ++this.fullChunkCreateCount; // Paper
this.fullChunkFuture = chunkStorage.prepareAccessibleChunk(this);
@@ -654,9 +668,33 @@ public class ChunkHolder {
2021-06-11 14:02:28 +02:00
}
public void refreshAccessibility() {
+ boolean prev = this.wasAccessibleSinceLastSave; // Paper
2021-06-14 07:34:40 +02:00
this.wasAccessibleSinceLastSave = ChunkHolder.getFullChunkStatus(this.ticketLevel).isOrAfter(ChunkHolder.FullChunkStatus.BORDER);
2021-06-11 14:02:28 +02:00
+ // Paper start - incremental autosave
+ if (prev != this.wasAccessibleSinceLastSave) {
+ if (this.wasAccessibleSinceLastSave) {
+ long timeSinceAutoSave = this.inactiveTimeStart - this.lastAutoSaveTime;
+ if (timeSinceAutoSave < 0) {
+ // safest bet is to assume autosave is needed here
+ timeSinceAutoSave = this.chunkMap.level.paperConfig.autoSavePeriod;
+ }
+ this.lastAutoSaveTime = this.chunkMap.level.getGameTime() - timeSinceAutoSave;
+ this.chunkMap.autoSaveQueue.add(this);
+ } else {
+ this.inactiveTimeStart = this.chunkMap.level.getGameTime();
+ this.chunkMap.autoSaveQueue.remove(this);
+ }
+ }
+ // Paper end
2021-06-14 07:34:40 +02:00
}
2021-06-11 14:02:28 +02:00
+ // Paper start - incremental autosave
+ public boolean setHasBeenLoaded() {
2021-06-14 07:34:40 +02:00
+ this.wasAccessibleSinceLastSave = getFullChunkStatus(this.ticketLevel).isOrAfter(ChunkHolder.FullChunkStatus.BORDER);
2021-06-11 14:02:28 +02:00
+ return this.wasAccessibleSinceLastSave;
2021-06-14 07:34:40 +02:00
+ }
2021-06-11 14:02:28 +02:00
+ // Paper end
2021-06-14 07:34:40 +02:00
+
public void replaceProtoChunk(ImposterProtoChunk chunk) {
2021-06-11 14:02:28 +02:00
for (int i = 0; i < this.futures.length(); ++i) {
2021-06-14 07:34:40 +02:00
CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> completablefuture = (CompletableFuture) this.futures.get(i);
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
2021-06-17 10:37:27 +02:00
index 39c7ffe656b6e1217d1e7bcf45f68501ed7e015d..07ecd8fd4d0806309c2ac3a465f1ddae3b164e60 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
2021-06-16 08:25:38 +02:00
@@ -97,6 +97,7 @@ import net.minecraft.world.level.levelgen.structure.templatesystem.StructureMana
2021-06-11 14:02:28 +02:00
import net.minecraft.world.level.storage.DimensionDataStorage;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.world.phys.Vec3;
+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-06-16 08:25:38 +02:00
@@ -748,6 +749,64 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2021-06-11 14:02:28 +02:00
}
+ // Paper start - incremental autosave
+ final ObjectRBTreeSet<ChunkHolder> autoSaveQueue = new ObjectRBTreeSet<>((playerchunk1, playerchunk2) -> {
+ int timeCompare = Long.compare(playerchunk1.lastAutoSaveTime, playerchunk2.lastAutoSaveTime);
+ if (timeCompare != 0) {
+ return timeCompare;
+ }
+
+ return Long.compare(MCUtil.getCoordinateKey(playerchunk1.pos), MCUtil.getCoordinateKey(playerchunk2.pos));
+ });
+
+ protected void saveIncrementally() {
+ int savedThisTick = 0;
+ // optimized since we search far less chunks to hit ones that need to be saved
+ List<ChunkHolder> reschedule = new java.util.ArrayList<>(this.level.paperConfig.maxAutoSaveChunksPerTick);
+ long currentTick = this.level.getGameTime();
+ long maxSaveTime = currentTick - this.level.paperConfig.autoSavePeriod;
+
+ for (Iterator<ChunkHolder> iterator = this.autoSaveQueue.iterator(); iterator.hasNext();) {
+ ChunkHolder playerchunk = iterator.next();
+ if (playerchunk.lastAutoSaveTime > maxSaveTime) {
+ break;
+ }
+
+ iterator.remove();
+
+ ChunkAccess ichunkaccess = playerchunk.getChunkToSave().getNow(null);
+ if (ichunkaccess instanceof LevelChunk) {
+ boolean shouldSave = ((LevelChunk)ichunkaccess).lastSaveTime <= maxSaveTime;
+
2021-06-17 10:37:27 +02:00
+ if (shouldSave && this.save(ichunkaccess) && this.level.entityManager.storeChunkSections(playerchunk.pos.toLong(), entity -> {})) {
2021-06-11 14:02:28 +02:00
+ ++savedThisTick;
+
+ if (!playerchunk.setHasBeenLoaded()) {
+ // do not fall through to reschedule logic
+ playerchunk.inactiveTimeStart = currentTick;
+ if (savedThisTick >= this.level.paperConfig.maxAutoSaveChunksPerTick) {
+ break;
+ }
+ continue;
+ }
+ }
+ }
+
+ reschedule.add(playerchunk);
+
+ if (savedThisTick >= this.level.paperConfig.maxAutoSaveChunksPerTick) {
+ break;
+ }
+ }
+
+ for (int i = 0, len = reschedule.size(); i < len; ++i) {
+ ChunkHolder playerchunk = reschedule.get(i);
+ playerchunk.lastAutoSaveTime = this.level.getGameTime();
+ this.autoSaveQueue.add(playerchunk);
+ }
+ }
+ // Paper end
+
protected void saveAllChunks(boolean flush) {
2021-06-14 07:34:40 +02:00
Long2ObjectLinkedOpenHashMap<ChunkHolder> visibleChunks = this.getVisibleChunks(); // Paper remove clone of visible Chunks unless saving off main thread (watchdog kill)
2021-06-11 14:02:28 +02:00
if (flush) {
2021-06-16 08:25:38 +02:00
@@ -887,6 +946,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2021-06-14 07:34:40 +02:00
asyncSaveData, chunk);
chunk.setUnsaved(false);
+ chunk.setLastSaved(this.level.getGameTime()); // Paper - track last saved time
}
// Paper end
2021-06-17 10:37:27 +02:00
@@ -918,6 +978,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2021-06-11 14:02:28 +02:00
this.level.unload(chunk);
}
2021-06-14 07:34:40 +02:00
+ this.autoSaveQueue.remove(holder); // Paper
2021-06-11 14:02:28 +02:00
2021-06-17 10:37:27 +02:00
this.lightEngine.updateChunkStatus(ichunkaccess.getPos());
this.lightEngine.tryScheduleUpdate();
@@ -1271,6 +1332,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2021-06-14 07:34:40 +02:00
if (!chunk.isUnsaved()) {
return false;
} else {
+ chunk.setLastSaved(this.level.getGameTime()); // Paper - track save time
chunk.setUnsaved(false);
ChunkPos chunkcoordintpair = chunk.getPos();
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2021-06-17 10:37:27 +02:00
index 135d162a926bd258d2aa001b7d0ef880fd3f2fb9..334f1ad2a5ce236b58a82b1ef2e143eaa3909f12 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2021-06-16 02:50:38 +02:00
@@ -672,6 +672,15 @@ public class ServerChunkCache extends ChunkSource {
2021-06-11 14:02:28 +02:00
} // Paper - Timings
}
+ // Paper start - duplicate save, but call incremental
+ public void saveIncrementally() {
+ this.runDistanceManagerUpdates();
+ try (co.aikar.timings.Timing timed = level.timings.chunkSaveData.startTiming()) { // Paper - Timings
+ this.chunkMap.saveIncrementally();
+ } // Paper - Timings
+ }
+ // Paper end
+
@Override
public void close() throws IOException {
// CraftBukkit start
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
2021-06-17 10:37:27 +02:00
index d724ce8cf79fe7194e79da2e8e369777bbe638d2..1ffb6c12a2eadc0fee0081f980594ac8b5bc4277 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
2021-06-17 10:37:27 +02:00
@@ -1003,6 +1003,37 @@ public class ServerLevel extends net.minecraft.world.level.Level implements Worl
2021-06-11 14:02:28 +02:00
return !this.server.isUnderSpawnProtection(this, pos, player) && this.getWorldBorder().isWithinBounds(pos);
}
+ // Paper start - derived from below
+ public void saveIncrementally(boolean doFull) {
+ ServerChunkCache chunkproviderserver = this.getChunkSource();
+
+ if (doFull) {
+ org.bukkit.Bukkit.getPluginManager().callEvent(new org.bukkit.event.world.WorldSaveEvent(getWorld()));
+ }
+
2021-06-14 07:34:40 +02:00
+ try (co.aikar.timings.Timing ignored = this.timings.worldSave.startTiming()) {
2021-06-11 14:02:28 +02:00
+ if (doFull) {
2021-06-14 07:34:40 +02:00
+ this.saveLevelData();
2021-06-11 14:02:28 +02:00
+ }
+
2021-06-14 07:34:40 +02:00
+ this.timings.worldSaveChunks.startTiming(); // Paper
2021-06-11 14:02:28 +02:00
+ if (!this.noSave()) chunkproviderserver.saveIncrementally();
2021-06-14 07:34:40 +02:00
+ this.timings.worldSaveChunks.stopTiming(); // Paper
2021-06-11 14:02:28 +02:00
+
+ // Copied from save()
+ // CraftBukkit start - moved from MinecraftServer.saveChunks
+ if (doFull) { // Paper
+ ServerLevel worldserver1 = this;
+
2021-06-14 07:34:40 +02:00
+ this.serverLevelData.setWorldBorder(worldserver1.getWorldBorder().createSettings());
+ this.serverLevelData.setCustomBossEvents(this.server.getCustomBossEvents().save());
+ this.convertable.saveDataTag(this.server.registryHolder, this.serverLevelData, this.server.getPlayerList().getSingleplayerData());
2021-06-11 14:02:28 +02:00
+ }
+ // CraftBukkit end
+ }
+ }
+ // Paper end
+
public void save(@Nullable ProgressListener progressListener, boolean flush, boolean flag1) {
ServerChunkCache chunkproviderserver = this.getChunkSource();
2021-06-14 07:34:40 +02:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java b/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
2021-06-16 02:50:38 +02:00
index a857953f3488e79fd601ac63881bc4d87708afa7..3cf3b0486f786d7d043cce75767753e1cdacf781 100644
2021-06-14 07:34:40 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
+++ b/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
@@ -29,6 +29,7 @@ public interface ChunkAccess extends BlockGetter, FeatureAccess {
return GameEventDispatcher.NOOP;
2021-06-11 14:02:28 +02:00
}
2021-06-14 07:34:40 +02:00
+ default void setLastSaved(long ticks) {}
// Paper start
default boolean generateFlatBedrock() {
if (this instanceof ProtoChunk) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
2021-06-17 10:37:27 +02:00
index cc72c5c7e63794974080648abc94646819ea0924..199efaefef890d2695682cc695b02c6ca3f971e2 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
2021-06-14 07:34:40 +02:00
@@ -108,6 +108,13 @@ public class LevelChunk implements ChunkAccess {
private final ShortList[] postProcessing;
2021-06-11 14:02:28 +02:00
private TickList<Block> blockTicks;
private TickList<Fluid> liquidTicks;
2021-06-14 07:34:40 +02:00
+ // Paper start - track last save time
+ public long lastSaveTime;
+ @Override
+ public void setLastSaved(long ticks) {
+ this.lastSaveTime = ticks;
+ }
+ // Paper end
2021-06-11 14:02:28 +02:00
private volatile boolean unsaved;
private long inhabitedTime;
@Nullable