From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: tr7zw Date: Fri, 31 Jul 2020 22:39:45 -0500 Subject: [PATCH] Player-saving-async-FileIO diff --git a/src/main/java/net/minecraft/server/AdvancementDataPlayer.java b/src/main/java/net/minecraft/server/AdvancementDataPlayer.java index 2e88442e13546e9005e3783b09feaddbed7cea16..0f05c8887f5ecd639021e90389e53891b6ad80fc 100644 --- a/src/main/java/net/minecraft/server/AdvancementDataPlayer.java +++ b/src/main/java/net/minecraft/server/AdvancementDataPlayer.java @@ -27,6 +27,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nullable; @@ -50,6 +52,7 @@ public class AdvancementDataPlayer { @Nullable private Advancement l; private boolean m = true; + public static ExecutorService saveThread = Executors.newSingleThreadExecutor(); // Yatopia // Paper start - fix advancement data player leakage final Map> criterionData = Maps.newIdentityHashMap(); @@ -228,53 +231,66 @@ public class AdvancementDataPlayer { jsonelement.getAsJsonObject().addProperty("DataVersion", SharedConstants.getGameVersion().getWorldVersion()); + // Yatopia start + String gson = null; try { - FileOutputStream fileoutputstream = new FileOutputStream(this.f); - Throwable throwable = null; - - try { - OutputStreamWriter outputstreamwriter = new OutputStreamWriter(fileoutputstream, Charsets.UTF_8.newEncoder()); - Throwable throwable1 = null; - - try { - AdvancementDataPlayer.b.toJson(jsonelement, outputstreamwriter); - } catch (Throwable throwable2) { - throwable1 = throwable2; - throw throwable2; - } finally { - if (outputstreamwriter != null) { - if (throwable1 != null) { - try { - outputstreamwriter.close(); - } catch (Throwable throwable3) { - throwable1.addSuppressed(throwable3); - } - } else { - outputstreamwriter.close(); - } - } - - } - } catch (Throwable throwable4) { - throwable = throwable4; - throw throwable4; - } finally { - if (fileoutputstream != null) { - if (throwable != null) { - try { - fileoutputstream.close(); - } catch (Throwable throwable5) { - throwable.addSuppressed(throwable5); - } - } else { - fileoutputstream.close(); - } - } - - } - } catch (IOException ioexception) { - AdvancementDataPlayer.LOGGER.error("Couldn't save player advancements to {}", this.f, ioexception); + gson = AdvancementDataPlayer.b.toJson(jsonelement); + } catch (Throwable throwable) { + throw throwable; + } + if(gson != null) { + final String fGson = gson; + saveThread.submit(() -> { + try { + FileOutputStream fileoutputstream = new FileOutputStream(this.f); + Throwable throwable = null; + + try { + OutputStreamWriter outputstreamwriter = new OutputStreamWriter(fileoutputstream, Charsets.UTF_8.newEncoder()); + Throwable throwable1 = null; + + try { + outputstreamwriter.write(fGson); + } catch (Throwable throwable2) { + throwable1 = throwable2; + throw throwable2; + } finally { + if (outputstreamwriter != null) { + if (throwable1 != null) { + try { + outputstreamwriter.close(); + } catch (Throwable throwable3) { + throwable1.addSuppressed(throwable3); + } + } else { + outputstreamwriter.close(); + } + } + + } + } catch (Throwable throwable4) { + throwable = throwable4; + throw throwable4; + } finally { + if (fileoutputstream != null) { + if (throwable != null) { + try { + fileoutputstream.close(); + } catch (Throwable throwable5) { + throwable.addSuppressed(throwable5); + } + } else { + fileoutputstream.close(); + } + } + + } + } catch (IOException ioexception) { + AdvancementDataPlayer.LOGGER.error("Couldn't save player advancements to {}", this.f, ioexception); + } + }); } + // Yatopia end } diff --git a/src/main/java/net/minecraft/server/EntityHuman.java b/src/main/java/net/minecraft/server/EntityHuman.java index 2cada09ced1660526e9c112c2c8d92bbf9d6ea98..767ecf5fe63d86522ef8de4c0b4cda2803a5145c 100644 --- a/src/main/java/net/minecraft/server/EntityHuman.java +++ b/src/main/java/net/minecraft/server/EntityHuman.java @@ -700,11 +700,23 @@ public abstract class EntityHuman extends EntityLiving { } + // Yatopia start + private NBTTagList inventorySnapshot = null; + private NBTTagList enderchestSnapshot = null; + + + public void takeInventorySnapshot() { + inventorySnapshot = this.inventory.a(new NBTTagList()); + enderchestSnapshot = this.enderChest.g(); + } + + // Yatopia end + @Override public void saveData(NBTTagCompound nbttagcompound) { super.saveData(nbttagcompound); nbttagcompound.setInt("DataVersion", SharedConstants.getGameVersion().getWorldVersion()); - nbttagcompound.set("Inventory", this.inventory.a(new NBTTagList())); + nbttagcompound.set("Inventory", inventorySnapshot != null ? inventorySnapshot : this.inventory.a(new NBTTagList())); inventorySnapshot = null;// Yatopia nbttagcompound.setInt("SelectedItemSlot", this.inventory.itemInHandIndex); nbttagcompound.setShort("SleepTimer", (short) this.sleepTicks); nbttagcompound.setFloat("XpP", this.exp); @@ -714,7 +726,7 @@ public abstract class EntityHuman extends EntityLiving { nbttagcompound.setInt("Score", this.getScore()); this.foodData.b(nbttagcompound); this.abilities.a(nbttagcompound); - nbttagcompound.set("EnderItems", this.enderChest.g()); + nbttagcompound.set("EnderItems", enderchestSnapshot != null ? enderchestSnapshot : this.enderChest.g()); enderchestSnapshot = null;// Yatopia if (!this.getShoulderEntityLeft().isEmpty()) { nbttagcompound.set("ShoulderEntityLeft", this.getShoulderEntityLeft()); } diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java index 8b64720f4acf5deb341074cd75f99ca7816f1e41..2e24e2210f91e69ead4f21f2426e5230fb887e53 100644 --- a/src/main/java/net/minecraft/server/PlayerList.java +++ b/src/main/java/net/minecraft/server/PlayerList.java @@ -20,6 +20,7 @@ import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -1262,6 +1263,28 @@ public abstract class PlayerList { if (team != null) scoreboard.removeTeam(team); } // Paper end + + // Yatopia start - make sure all saves are done + try { + ((WorldNBTStorage)playerFileData).saveThread.shutdown(); + boolean done = ((WorldNBTStorage)playerFileData).saveThread.awaitTermination(60, TimeUnit.SECONDS); + if(!done) { + LOGGER.error("Players did not save completly!"); + } + ServerStatisticManager.saveThread.shutdown(); + done = ServerStatisticManager.saveThread.awaitTermination(60, TimeUnit.SECONDS); + if(!done) { + LOGGER.error("Stats did not save completly!"); + } + AdvancementDataPlayer.saveThread.shutdown(); + done = AdvancementDataPlayer.saveThread.awaitTermination(60, TimeUnit.SECONDS); + if(!done) { + LOGGER.error("Advancements did not save completly!"); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + // Yatopia end } // Paper end @@ -1299,13 +1322,13 @@ public abstract class PlayerList { File file = this.server.a(SavedFile.STATS).toFile(); File file1 = new File(file, uuid + ".json"); - if (!file1.exists()) { + /*if (!file1.exists()) { // Yatopia dont check for old stats files with sync File IO File file2 = new File(file, displayName + ".json"); // CraftBukkit if (file2.exists() && file2.isFile()) { file2.renameTo(file1); } - } + }*/ serverstatisticmanager = new ServerStatisticManager(this.server, file1); // this.o.put(uuid, serverstatisticmanager); // CraftBukkit diff --git a/src/main/java/net/minecraft/server/ServerStatisticManager.java b/src/main/java/net/minecraft/server/ServerStatisticManager.java index 18725272f920eafb655de8b923a8a85dbdc704cf..5f222d8b90951c61e77ab7032f8728772ba8ab9d 100644 --- a/src/main/java/net/minecraft/server/ServerStatisticManager.java +++ b/src/main/java/net/minecraft/server/ServerStatisticManager.java @@ -20,6 +20,10 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; import java.util.Set; +import java.util.Map.Entry; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -30,6 +34,7 @@ public class ServerStatisticManager extends StatisticManager { private final File d; private final Set> e = Sets.newHashSet(); private int f = -300; + public static ExecutorService saveThread = Executors.newSingleThreadExecutor(); // Yatopia public ServerStatisticManager(MinecraftServer minecraftserver, File file) { this.c = minecraftserver; @@ -40,7 +45,9 @@ public class ServerStatisticManager extends StatisticManager { Statistic wrapper = StatisticList.CUSTOM.b( entry.getKey() ); this.a.put( wrapper, entry.getValue().intValue() ); } - // Spigot end + // Spigot ends + // Yatopia start + saveThread.submit(() -> { if (file.isFile()) { try { this.a(minecraftserver.getDataFixer(), org.apache.commons.io.FileUtils.readFileToString(file)); @@ -50,17 +57,21 @@ public class ServerStatisticManager extends StatisticManager { ServerStatisticManager.LOGGER.error("Couldn't parse statistics file {}", file, jsonparseexception); } } - + }); } public void save() { if ( org.spigotmc.SpigotConfig.disableStatSaving ) return; // Spigot + // Yatopia start + String str = this.b(); + saveThread.submit(() -> { try { - org.apache.commons.io.FileUtils.writeStringToFile(this.d, this.b()); + org.apache.commons.io.FileUtils.writeStringToFile(this.d, str); } catch (IOException ioexception) { ServerStatisticManager.LOGGER.error("Couldn't save stats", ioexception); } - + }); + // Yatopia end } @Override @@ -111,7 +122,7 @@ public class ServerStatisticManager extends StatisticManager { if (nbttagcompound2.hasKeyOfType(s2, 99)) { SystemUtils.a(this.a(statisticwrapper, s2), (statistic) -> { - this.a.put(statistic, nbttagcompound2.getInt(s2)); + this.a.put(statistic, nbttagcompound2.getInt(s2) + this.a.getOrDefault(statistic, 0)); // Yatopia fix async load }, () -> { ServerStatisticManager.LOGGER.warn("Invalid statistic in {}: Don't know what {} is", this.d, s2); }); diff --git a/src/main/java/net/minecraft/server/WorldNBTStorage.java b/src/main/java/net/minecraft/server/WorldNBTStorage.java index be060f602f53344cf51c22eafacb4dc9219c8856..b14f3d44a76ccac791b7be353bb2899fc59f70ec 100644 --- a/src/main/java/net/minecraft/server/WorldNBTStorage.java +++ b/src/main/java/net/minecraft/server/WorldNBTStorage.java @@ -11,6 +11,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; // CraftBukkit start +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + import org.bukkit.craftbukkit.entity.CraftPlayer; // CraftBukkit end @@ -19,6 +23,7 @@ public class WorldNBTStorage { private static final Logger LOGGER = LogManager.getLogger(); private final File playerDir; protected final DataFixer a; + public ExecutorService saveThread = Executors.newSingleThreadExecutor(); // Yatopia public WorldNBTStorage(Convertable.ConversionSession convertable_conversionsession, DataFixer datafixer) { this.a = datafixer; @@ -29,14 +34,22 @@ public class WorldNBTStorage { public void save(EntityHuman entityhuman) { if(!com.destroystokyo.paper.PaperConfig.savePlayerData) return; // Paper - Make player data saving configurable try { - NBTTagCompound nbttagcompound = entityhuman.save(new NBTTagCompound()); - File file = File.createTempFile(entityhuman.getUniqueIDString() + "-", ".dat", this.playerDir); - - NBTCompressedStreamTools.a(nbttagcompound, (OutputStream) (new FileOutputStream(file))); - File file1 = new File(this.playerDir, entityhuman.getUniqueIDString() + ".dat"); - File file2 = new File(this.playerDir, entityhuman.getUniqueIDString() + ".dat_old"); - - SystemUtils.a(file1, file, file2); + // Yatopia start + entityhuman.takeInventorySnapshot(); // Take a sync inventory/enderchest snapshot to prevent duping + saveThread.submit(() -> { // Save the tag async + try { + NBTTagCompound nbttagcompound = entityhuman.save(new NBTTagCompound()); + File file = File.createTempFile(entityhuman.getUniqueIDString() + "-", ".dat", this.playerDir); + File file1 = new File(this.playerDir, entityhuman.getUniqueIDString() + ".dat"); + File file2 = new File(this.playerDir, entityhuman.getUniqueIDString() + ".dat_old"); + + NBTCompressedStreamTools.a(nbttagcompound, (OutputStream) (new FileOutputStream(file))); + SystemUtils.a(file1, file, file2); + } catch (Exception exception) { + WorldNBTStorage.LOGGER.error("Failed to save player data for {}", entityhuman.getName(), exception); // Paper + } + }); + // Yatopia end } catch (Exception exception) { WorldNBTStorage.LOGGER.error("Failed to save player data for {}", entityhuman.getName(), exception); // Paper }