2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 9 Aug 2020 08:59:25 +0300
Subject: [PATCH] Incremental player saving
diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
2021-11-25 12:20:13 +01:00
index 9a66cddbf9863aa6ff566a337153883c07c08e41..b6c7aec1516a4db4fde8c9ec2772cd982ed5e304 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
2021-11-25 12:20:13 +01:00
@@ -468,4 +468,15 @@ public class PaperConfig {
config.set("settings.unsupported-settings.allow-headless-pistons-readme", "This setting controls if players should be able to create headless pistons.");
allowHeadlessPistons = getBoolean("settings.unsupported-settings.allow-headless-pistons", false);
2021-06-11 14:02:28 +02:00
}
2021-11-25 12:20:13 +01:00
+
2021-06-11 14:02:28 +02:00
+ public static int playerAutoSaveRate = -1;
+ public static int maxPlayerAutoSavePerTick = 10;
+ private static void playerAutoSaveRate() {
+ playerAutoSaveRate = getInt("settings.player-auto-save-rate", -1);
+ maxPlayerAutoSavePerTick = getInt("settings.max-player-auto-save-per-tick", -1);
+ if (maxPlayerAutoSavePerTick == -1) { // -1 Automatic / "Recommended"
+ // 10 should be safe for everyone unless you mass spamming player auto save
+ maxPlayerAutoSavePerTick = (playerAutoSaveRate == -1 || playerAutoSaveRate > 100) ? 10 : 20;
+ }
+ }
}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-11-25 12:20:13 +01:00
index 4351dd9c293d1db48c87c5df28bcb2b9a59583f7..7f733bd666e42f8ba80bfda233f662564bfc0e24 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-11-25 12:20:13 +01:00
@@ -896,12 +896,17 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
return flag3;
}
+ // Paper start
public boolean saveEverything(boolean suppressLogs, boolean flush, boolean force) {
+ return saveEverything(suppressLogs, flush, force, -1);
+ }
+ public boolean saveEverything(boolean suppressLogs, boolean flush, boolean force, int playerSaveInterval) {
+ // Paper end
boolean flag3;
2021-06-14 12:42:08 +02:00
2021-11-25 12:20:13 +01:00
try {
this.isSaving = true;
- this.getPlayerList().saveAll();
+ this.getPlayerList().saveAll(playerSaveInterval); // Paper
flag3 = this.saveAllChunks(suppressLogs, flush, force);
} finally {
this.isSaving = false;
@@ -963,7 +968,6 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
this.isSaving = true;
2021-06-14 12:42:08 +02:00
if (this.playerList != null) {
MinecraftServer.LOGGER.info("Saving players");
- this.playerList.saveAll();
this.playerList.removeAll(this.isRestarting); // Paper
try { Thread.sleep(100); } catch (InterruptedException ex) {} // CraftBukkit - SPIGOT-625 - give server at least a chance to send packets
}
2021-11-25 12:20:13 +01:00
@@ -1408,13 +1412,19 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
}
}
2021-06-14 12:42:08 +02:00
+ // Paper start
+ int playerSaveInterval = com.destroystokyo.paper.PaperConfig.playerAutoSaveRate;
+ if (playerSaveInterval < 0) {
+ playerSaveInterval = autosavePeriod;
+ }
+ // Paper end
2021-11-25 12:20:13 +01:00
if (this.autosavePeriod > 0 && this.tickCount % this.autosavePeriod == 0) { // CraftBukkit
MinecraftServer.LOGGER.debug("Autosave started");
2021-06-11 14:02:28 +02:00
this.profiler.push("save");
2021-11-25 12:20:13 +01:00
- this.saveEverything(true, false, false);
+ this.saveEverything(true, false, false, playerSaveInterval); // Paper
this.profiler.pop();
MinecraftServer.LOGGER.debug("Autosave finished");
- }
+ } else this.getPlayerList().saveAll(playerSaveInterval); // Paper
io.papermc.paper.util.CachedLists.reset(); // Paper
// Paper start - move executeAll() into full server tick timing
try (co.aikar.timings.Timing ignored = MinecraftTimings.processTasksTimer.startTiming()) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2021-11-25 12:20:13 +01:00
index 84f78e2b3bf51eae1d486852464df032861c5a0b..968b8180f92066a43f06bff8dd1d49b03bd08f5b 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2021-11-25 12:20:13 +01:00
@@ -171,6 +171,7 @@ public class ServerPlayer extends Player {
2021-06-14 12:42:08 +02:00
public final int getViewDistance() { return this.getLevel().getChunkSource().chunkMap.viewDistance - 1; } // Paper - placeholder
2021-06-11 14:02:28 +02:00
private static final Logger LOGGER = LogManager.getLogger();
+ public long lastSave = MinecraftServer.currentTick; // Paper
2021-06-14 12:42:08 +02:00
private static final int NEUTRAL_MOB_DEATH_NOTIFICATION_RADII_XZ = 32;
private static final int NEUTRAL_MOB_DEATH_NOTIFICATION_RADII_Y = 10;
2021-06-11 14:02:28 +02:00
public ServerGamePacketListenerImpl connection;
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
2021-11-25 12:20:13 +01:00
index 310bed27c04ef812aa8607c9dca789db13341d25..b1f93551ce3e4d12e5cbe424c1ceaf3da20d0ffd 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
2021-11-25 12:20:13 +01:00
@@ -569,6 +569,7 @@ public abstract class PlayerList {
2021-06-11 14:02:28 +02:00
protected void save(ServerPlayer player) {
if (!player.getBukkitEntity().isPersistent()) return; // CraftBukkit
if (!player.didPlayerJoinEvent) return; // Paper - If we never fired PJE, we disconnected during login. Data has not changed, and additionally, our saved vehicle is not loaded! If we save now, we will lose our vehicle (CraftBukkit bug)
+ player.lastSave = MinecraftServer.currentTick; // Paper
this.playerIo.save(player);
ServerStatsCounter serverstatisticmanager = (ServerStatsCounter) player.getStats(); // CraftBukkit
2021-11-25 12:20:13 +01:00
@@ -1181,10 +1182,21 @@ public abstract class PlayerList {
2021-06-11 14:02:28 +02:00
}
public void saveAll() {
+ // Paper start - incremental player saving
2021-11-25 12:20:13 +01:00
+ saveAll(-1);
2021-06-11 14:02:28 +02:00
+ }
2021-11-25 12:20:13 +01:00
+ public void saveAll(int interval) {
net.minecraft.server.MCUtil.ensureMain("Save Players" , () -> { // Paper - Ensure main
2021-06-11 14:02:28 +02:00
MinecraftTimings.savePlayers.startTiming(); // Paper
+ int numSaved = 0;
+ long now = MinecraftServer.currentTick;
for (int i = 0; i < this.players.size(); ++i) {
2021-06-14 12:42:08 +02:00
- this.save(this.players.get(i));
2021-06-11 14:02:28 +02:00
+ ServerPlayer entityplayer = this.players.get(i);
2021-11-25 12:20:13 +01:00
+ if (interval != -1 || now - entityplayer.lastSave >= interval) {
2021-06-11 14:02:28 +02:00
+ this.save(entityplayer);
2021-11-25 12:20:13 +01:00
+ if (interval != -1 && ++numSaved <= com.destroystokyo.paper.PaperConfig.maxPlayerAutoSavePerTick) { break; }
2021-06-11 14:02:28 +02:00
+ }
+ // Paper end
}
MinecraftTimings.savePlayers.stopTiming(); // Paper
return null; }); // Paper - ensure main