Skip to content

Commit

Permalink
Instantly continue on world upgrade finish
Browse files Browse the repository at this point in the history
  • Loading branch information
MartijnMuijsers committed Mar 24, 2023
1 parent b4a965d commit 2cb8f3f
Show file tree
Hide file tree
Showing 27 changed files with 95 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Fri, 24 Mar 2023 09:16:33 +0100
Subject: [PATCH] Instantly continue on world upgrade finish

License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
Gale - https://galemc.org

diff --git a/src/main/java/io/papermc/paper/world/ThreadedWorldUpgrader.java b/src/main/java/io/papermc/paper/world/ThreadedWorldUpgrader.java
index 513833c2ea23df5b079d157bc5cb89d5c9754c0b..c13df3a375f416273c6a26f5f77624c1f34a918c 100644
--- a/src/main/java/io/papermc/paper/world/ThreadedWorldUpgrader.java
+++ b/src/main/java/io/papermc/paper/world/ThreadedWorldUpgrader.java
@@ -25,6 +25,7 @@ import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
@@ -88,7 +89,12 @@ public class ThreadedWorldUpgrader {
new ChunkStorage(regionFolder.toPath(), this.dataFixer, false), this.removeCaches, this.dimensionType, this.generatorKey);

long expectedChunks = (long)regionFiles.length * (32L * 32L);
+ // Gale start - instantly continue on world upgrade finish
+ final long[] finalExpectedChunks = {-1L};

+ var finishLock = new java.util.concurrent.locks.ReentrantLock();
+ var finishCondition = finishLock.newCondition();
+ // Gale end - instantly continue on world upgrade finish
for (final File regionFile : regionFiles) {
final ChunkPos regionPos = RegionFileStorage.getRegionFileCoordinates(regionFile.toPath());
if (regionPos == null) {
@@ -96,8 +102,25 @@ public class ThreadedWorldUpgrader {
continue;
}

- this.threadPool.execute(new ConvertTask(info, regionPos.x >> 5, regionPos.z >> 5));
+ // Gale start - instantly continue on world upgrade finish
+ Runnable taskWithNotification = () -> {
+ new ConvertTask(info, regionPos.x >> 5, regionPos.z >> 5).run();
+ final long current = info.convertedChunks.get();
+ if (current == finalExpectedChunks[0]) {
+ while (!finishLock.tryLock()) {
+ Thread.onSpinWait();
+ }
+ try {
+ finishCondition.signal();
+ } finally {
+ finishLock.unlock();
+ }
+ }
+ };
+ this.threadPool.execute(taskWithNotification);
+ // Gale end - instantly continue on world upgrade finish
}
+ finalExpectedChunks[0] = expectedChunks; // Gale - instantly continue on world upgrade finish
this.threadPool.shutdown();

final DecimalFormat format = new DecimalFormat("#0.00");
@@ -109,9 +132,16 @@ public class ThreadedWorldUpgrader {

LOGGER.info("{}% completed ({} / {} chunks)...", format.format((double)current / (double)expectedChunks * 100.0), current, expectedChunks);

+ // Gale start - instantly continue on world upgrade finish
+ while (!finishLock.tryLock()) {
+ Thread.onSpinWait();
+ }
try {
- Thread.sleep(1000L);
- } catch (final InterruptedException ignore) {}
+ finishCondition.await(1000L, TimeUnit.MILLISECONDS);
+ } catch (final InterruptedException ignore) {} finally {
+ finishLock.unlock();
+ }
+ // Gale end - instantly continue on world upgrade finish
}

final long end = System.nanoTime();
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ index 5a92a8f24d3a6957cc3f8404bf3458f4c4058fb4..666114daf9e9a3f9f9e0779a3a40dfac
// Attempt to detect vectorization
try {
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index d278472bf407ae7c678bccb3a061437a5642749e..931fcce46f3941af772416f39229ed4abc7f8301 100644
index 716867461631119de283e750cf14820b5630ccd5..7fe27e79b71117a3cea8fa041689ec5467dcd70f 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -249,6 +249,8 @@ import org.bukkit.structure.StructureManager;
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ index 394b5044090c1c7b00b30036e4b8be4a6ac58ed1..0f0895ca1faa1e4bbd96614b497efd6a
doFirst {
workingDir.mkdirs()
diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java
index 7c38d14671b8a9a671deece654d2f89426b26081..b3ff28d9cac66ae076179b861fd4735e4ecb279b 100644
index 185ad2527449bddfcf031ef6e3ae0ea6d39edf0e..3e97a50514f4b4ce1711c5813762782eb9da3ddf 100644
--- a/src/main/java/org/bukkit/craftbukkit/Main.java
+++ b/src/main/java/org/bukkit/craftbukkit/Main.java
@@ -4,16 +4,21 @@ import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ License: AGPL-3.0 (https://www.gnu.org/licenses/agpl-3.0.html)
Gale - https://galemc.org

diff --git a/src/main/java/io/papermc/paper/configuration/PaperConfigurations.java b/src/main/java/io/papermc/paper/configuration/PaperConfigurations.java
index 47a3580caef45ffe71446c247d4e06e332b2fda2..0b7d6bdd9d0543a4b37abfdd67c9fda4688134de 100644
index 760601de028536aec142d195aa875ae59edd8e41..4753d4b489724d5b2681ac1c9f3736f08bfafbd2 100644
--- a/src/main/java/io/papermc/paper/configuration/PaperConfigurations.java
+++ b/src/main/java/io/papermc/paper/configuration/PaperConfigurations.java
@@ -41,6 +41,7 @@ import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ index 157f74bbbac8cbbed37c4fcc339b3b3c06d06bbe..cd614c55aa3f9a014c2a073abf803888
1, 1, 0L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 30d848a13cc53bbe1ca1bcd497b2d2d5cb8a278d..d301399b47626a6196e89b76110d9305f1a4beeb 100644
index b9dfe7aa02d8ac8b254f857756385605e3a98a2c..1c2020b366d2da3a054fbdaab2dd5734e6dbb315 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1084,9 +1084,6 @@ public abstract class MinecraftServer extends MinecraftServerBlockableEventLoop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ License: AGPL-3.0 (https://www.gnu.org/licenses/agpl-3.0.html)
Gale - https://galemc.org

diff --git a/src/main/java/io/papermc/paper/world/ThreadedWorldUpgrader.java b/src/main/java/io/papermc/paper/world/ThreadedWorldUpgrader.java
index 513833c2ea23df5b079d157bc5cb89d5c9754c0b..5320a366924e2ab6b60e5563ca58eb0ae6323d21 100644
index c13df3a375f416273c6a26f5f77624c1f34a918c..ccab7e0d529a9909410da4301db037bf4b0f02f0 100644
--- a/src/main/java/io/papermc/paper/world/ThreadedWorldUpgrader.java
+++ b/src/main/java/io/papermc/paper/world/ThreadedWorldUpgrader.java
@@ -7,25 +7,21 @@ import net.minecraft.nbt.CompoundTag;
@@ -7,17 +7,16 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.worldupdate.WorldUpgrader;
import net.minecraft.world.level.ChunkPos;
Expand All @@ -30,15 +30,7 @@ index 513833c2ea23df5b079d157bc5cb89d5c9754c0b..5320a366924e2ab6b60e5563ca58eb0a
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
-import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;

@@ -46,6 +42,10 @@ public class ThreadedWorldUpgrader {
@@ -47,6 +46,10 @@ public class ThreadedWorldUpgrader {
this.dimensionType = dimensionType;
this.worldName = worldName;
this.worldDir = worldDir;
Expand All @@ -49,18 +41,18 @@ index 513833c2ea23df5b079d157bc5cb89d5c9754c0b..5320a366924e2ab6b60e5563ca58eb0a
this.threadPool = Executors.newFixedThreadPool(Math.max(1, threads), new ThreadFactory() {
private final AtomicInteger threadCounter = new AtomicInteger();

@@ -61,6 +61,7 @@ public class ThreadedWorldUpgrader {
@@ -62,6 +65,7 @@ public class ThreadedWorldUpgrader {
return ret;
}
});
+ */ // Gale - base thread pool - remove world upgrade executors
this.dataFixer = dataFixer;
this.generatorKey = generatorKey;
this.removeCaches = removeCaches;
@@ -98,17 +99,22 @@ public class ThreadedWorldUpgrader {

this.threadPool.execute(new ConvertTask(info, regionPos.x >> 5, regionPos.z >> 5));
@@ -121,17 +125,22 @@ public class ThreadedWorldUpgrader {
// Gale end - instantly continue on world upgrade finish
}
finalExpectedChunks[0] = expectedChunks; // Gale - instantly continue on world upgrade finish
- this.threadPool.shutdown();

final DecimalFormat format = new DecimalFormat("#0.00");
Expand All @@ -79,6 +71,6 @@ index 513833c2ea23df5b079d157bc5cb89d5c9754c0b..5320a366924e2ab6b60e5563ca58eb0a
+ }
+ // Gale end - base thread pool - remove world upgrade executors - wait for task completion instead of thread pool termination
+
try {
Thread.sleep(1000L);
} catch (final InterruptedException ignore) {}
// Gale start - instantly continue on world upgrade finish
while (!finishLock.tryLock()) {
Thread.onSpinWait();
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ index f5c15d40094c2ddc6220b0595597d12103fcf425..79ef41d2bb30beee2355d1de3dc99c9e
}

diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index d301399b47626a6196e89b76110d9305f1a4beeb..4f73bbb49ff96adeb3b9ba245c2f32c9cb97df1d 100644
index 1c2020b366d2da3a054fbdaab2dd5734e6dbb315..d9e07fd59e4f0bb006cadd022b2086fc65f5eacf 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1073,6 +1073,10 @@ public abstract class MinecraftServer extends MinecraftServerBlockableEventLoop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ License: AGPL-3.0 (https://www.gnu.org/licenses/agpl-3.0.html)
Gale - https://galemc.org

diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 4f73bbb49ff96adeb3b9ba245c2f32c9cb97df1d..dad347675f45922254f8b26efb2cce7461411928 100644
index d9e07fd59e4f0bb006cadd022b2086fc65f5eacf..0782da72420e6988728a699af420fd17e5828e06 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -156,7 +156,6 @@ import org.galemc.gale.executor.queue.BaseTaskQueues;
Expand Down

0 comments on commit 2cb8f3f

Please sign in to comment.