Skip to content

Commit

Permalink
Adjust initial world spawn radius load
Browse files Browse the repository at this point in the history
The spawn selection may actually be farther than 32 blocks,
so we need a larger load radius to prevent a deadlock on startup.

Additionally, re-insert thread checks for sync loads that were
previously in 1.20.
  • Loading branch information
Spottedleaf committed Nov 15, 2024
1 parent 5833269 commit 2e7bc07
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
40 changes: 26 additions & 14 deletions patches/server/0003-Threaded-Regions.patch
Original file line number Diff line number Diff line change
Expand Up @@ -8196,7 +8196,7 @@ index e2c24813f59c2fd075c740ac1842a38f20ed8554..fbc619a132c6ca6b1abab51ac230be29
}
}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 696d075ca2883f3c37e35f983c4d020e5db89d16..9e5c063d1371d766c9e34dc8a6758fb760c164bd 100644
index 696d075ca2883f3c37e35f983c4d020e5db89d16..84f1f7956065de039837ceeaeee30259a7df4118 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -210,7 +210,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
Expand Down Expand Up @@ -8340,10 +8340,10 @@ index 696d075ca2883f3c37e35f983c4d020e5db89d16..9e5c063d1371d766c9e34dc8a6758fb7
this.addLevel(world); // Paper - Put world into worldlist before initing the world; move up
- this.initWorld(world, worlddata, this.worldData, worldoptions);
+ // Folia start - region threading
+ // the spawn should be within ~32 blocks, so we force add ticket levels to ensure the first thread
+ // the spawn should be within ~1024 blocks, so we force add ticket levels to ensure the first thread
+ // to init spawn will not run into any ownership issues
+ // move init to start of tickServer
+ int loadRegionRadius = ((32) >> 4);
+ int loadRegionRadius = 1024 >> 4;
+ world.randomSpawnSelection = new ChunkPos(world.getChunkSource().randomState().sampler().findSpawnPosition());
+ for (int currX = -loadRegionRadius; currX <= loadRegionRadius; ++currX) {
+ for (int currZ = -loadRegionRadius; currZ <= loadRegionRadius; ++currZ) {
Expand Down Expand Up @@ -10649,7 +10649,7 @@ index 2d2596f04f5addac38037a14a02c6e0622d0c485..ddfbdd7c07be201bbb29fa980b6ba832

public String getDebugStatus() {
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e80970ab844e6 100644
index dcb5651d1d9b10b40430fb2f713beedf68336704..b0e41b79d357f7e1b189985b4752accc78f05bbb 100644
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
@@ -57,16 +57,14 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
Expand All @@ -10671,7 +10671,19 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097
// Paper start
private final ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<net.minecraft.world.level.chunk.LevelChunk> fullChunks = new ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<>();
long chunkFutureAwaitCounter;
@@ -126,7 +124,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -89,6 +87,11 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
}

private ChunkAccess syncLoad(final int chunkX, final int chunkZ, final ChunkStatus toStatus) {
+ // Folia start - region threading
+ if (ca.spottedleaf.moonrise.common.util.TickThread.isTickThread()) {
+ ca.spottedleaf.moonrise.common.util.TickThread.ensureTickThread(this.level, chunkX, chunkZ, "Cannot asynchronously load chunks");
+ }
+ // Folia end - region threading
final ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ChunkTaskScheduler chunkTaskScheduler = ((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel)this.level).moonrise$getChunkTaskScheduler();
final CompletableFuture<ChunkAccess> completable = new CompletableFuture<>();
chunkTaskScheduler.scheduleChunkLoad(
@@ -126,7 +129,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
return load ? this.syncLoad(chunkX, chunkZ, toStatus) : null;
}
// Paper end - rewrite chunk system
Expand All @@ -10680,15 +10692,15 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097

public ServerChunkCache(ServerLevel world, LevelStorageSource.LevelStorageAccess session, DataFixer dataFixer, StructureTemplateManager structureTemplateManager, Executor workerExecutor, ChunkGenerator chunkGenerator, int viewDistance, int simulationDistance, boolean dsync, ChunkProgressListener worldGenerationProgressListener, ChunkStatusUpdateListener chunkStatusChangeListener, Supplier<DimensionDataStorage> persistentStateManagerFactory) {
this.level = world;
@@ -252,6 +250,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -252,6 +255,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
}

public CompletableFuture<ChunkResult<ChunkAccess>> getChunkFuture(int chunkX, int chunkZ, ChunkStatus leastStatus, boolean create) {
+ if (true) throw new UnsupportedOperationException(); // Folia - region threading
boolean flag1 = Thread.currentThread() == this.mainThread;
CompletableFuture completablefuture;

@@ -419,11 +418,12 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -419,11 +423,12 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
}

private void tickChunks() {
Expand All @@ -10704,7 +10716,7 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097
if (!this.level.isDebug()) {
ProfilerFiller gameprofilerfiller = this.level.getProfiler();

@@ -438,13 +438,13 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -438,13 +443,13 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
final ServerChunkCache.ChunkAndHolder[] raw = tickingChunks.getRawDataUnchecked();
final int size = tickingChunks.size();

Expand All @@ -10722,7 +10734,7 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097
);
}
// Paper end - chunk tick iteration optimisations
@@ -462,7 +462,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -462,7 +467,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
NaturalSpawner.SpawnState spawnercreature_d; // moved down
if ((this.spawnFriendlies || this.spawnEnemies) && this.level.paperConfig().entities.spawning.perPlayerMobSpawns) { // don't count mobs when animals and monsters are disabled
// re-set mob counts
Expand All @@ -10731,7 +10743,7 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097
// Paper start - per player mob spawning backoff
for (int ii = 0; ii < ServerPlayer.MOBCATEGORY_TOTAL_ENUMS; ii++) {
player.mobCounts[ii] = 0;
@@ -475,23 +475,23 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -475,23 +480,23 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
}
// Paper end - per player mob spawning backoff
}
Expand Down Expand Up @@ -10760,7 +10772,7 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097
entityPlayer.playerNaturallySpawnedEvent = new com.destroystokyo.paper.event.entity.PlayerNaturallySpawnCreaturesEvent(entityPlayer.getBukkitEntity(), (byte) chunkRange);
entityPlayer.playerNaturallySpawnedEvent.callEvent();
}
@@ -604,14 +604,19 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -604,14 +609,19 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon

@Override
public void onLightUpdate(LightLayer type, SectionPos pos) {
Expand All @@ -10782,7 +10794,7 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097
}

public <T> void addRegionTicket(TicketType<T> ticketType, ChunkPos pos, int radius, T argument) {
@@ -689,7 +694,8 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -689,7 +699,8 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@Nullable
@VisibleForDebug
public NaturalSpawner.SpawnState getLastSpawnState() {
Expand All @@ -10792,7 +10804,7 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097
}

public void removeTicketsOnClosing() {
@@ -729,8 +735,43 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -729,8 +740,43 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
return ServerChunkCache.this.mainThread;
}

Expand Down Expand Up @@ -10836,7 +10848,7 @@ index dcb5651d1d9b10b40430fb2f713beedf68336704..e13ccac27fa4f4c23185f12c776e8097
ServerChunkCache.this.level.getProfiler().incrementCounter("runTask");
super.doRunTask(task);
}
@@ -738,12 +779,17 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -738,12 +784,17 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@Override
// CraftBukkit start - process pending Chunk loadCallback() and unloadCallback() after each run task
public boolean pollTask() {
Expand Down
24 changes: 12 additions & 12 deletions patches/server/0017-Region-profiler.patch
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ index fbc619a132c6ca6b1abab51ac230be29367e9c6e..97817400b70b2579f3a8750f7f33197a
if (exception instanceof ReportedException) {
ReportedException reportedexception = (ReportedException) exception;
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 9e5c063d1371d766c9e34dc8a6758fb760c164bd..511992c31d70e806bba68737048ca760bef1def0 100644
index 84f1f7956065de039837ceeaeee30259a7df4118..4ed79a51c89d54a7b2ab65a99b5b09cc807f4d4e 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1654,6 +1654,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
Expand Down Expand Up @@ -1513,10 +1513,10 @@ index 6f746f5ce9751dce4912f7633c126788eb684bd0..74524dcd4539a31e9d0037cd607ffbfc
// Paper end - optimise entity tracker

diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
index e13ccac27fa4f4c23185f12c776e80970ab844e6..135fa024d81b962761f0edc6896a2a507b6981f9 100644
index b0e41b79d357f7e1b189985b4752accc78f05bbb..f7311d6efc2119b9dbe062a439850a890c9bf6b8 100644
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
@@ -392,19 +392,26 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -397,19 +397,26 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon

@Override
public void tick(BooleanSupplier shouldKeepTicking, boolean tickChunks) {
Expand All @@ -1543,47 +1543,47 @@ index e13ccac27fa4f4c23185f12c776e80970ab844e6..135fa024d81b962761f0edc6896a2a50
this.level.timings.chunks.stopTiming(); // Paper - timings
this.chunkMap.tick();
}
@@ -419,6 +426,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -424,6 +431,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon

private void tickChunks() {
io.papermc.paper.threadedregions.RegionizedWorldData regionizedWorldData = this.level.getCurrentWorldData(); // Folia - region threading
+ final ca.spottedleaf.leafprofiler.RegionizedProfiler.Handle profiler = io.papermc.paper.threadedregions.TickRegionScheduler.getProfiler(); // Folia - profiler
long chunksTicked = 0; // Paper - rewrite chunk system
//long i = this.level.getGameTime(); // Folia - region threading
long j = 1L; // Folia - region threading
@@ -431,6 +439,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -436,6 +444,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
gameprofilerfiller.push("filteringLoadedChunks");
// Paper start - chunk tick iteration optimisations
List<ServerChunkCache.ChunkAndHolder> list;
+ profiler.startTimer(ca.spottedleaf.leafprofiler.LProfilerRegistry.CHUNK_TICK_COLLECT_CHUNKS); try { // Folia - profiler
{
final ca.spottedleaf.moonrise.common.list.ReferenceList<net.minecraft.server.level.ServerChunkCache.ChunkAndHolder> tickingChunks =
((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel) this.level).moonrise$getTickingChunks();
@@ -447,6 +456,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -452,6 +461,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
regionizedWorldData.iterationCopy, size // Folia - region threading
);
}
+ } finally { profiler.stopTimer(ca.spottedleaf.leafprofiler.LProfilerRegistry.CHUNK_TICK_COLLECT_CHUNKS); } // Folia - profiler
// Paper end - chunk tick iteration optimisations
Iterator iterator = null; // Paper - chunk tick iteration optimisations
if (this.level.getServer().tickRateManager().runsNormally()) this.level.timings.chunkTicks.startTiming(); // Paper
@@ -460,6 +470,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -465,6 +475,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
// Paper start - Optional per player mob spawns
int naturalSpawnChunkCount = k;
NaturalSpawner.SpawnState spawnercreature_d; // moved down
+ profiler.startTimer(ca.spottedleaf.leafprofiler.LProfilerRegistry.MOB_SPAWN_ENTITY_COUNT); try { // Folia - profiler
if ((this.spawnFriendlies || this.spawnEnemies) && this.level.paperConfig().entities.spawning.perPlayerMobSpawns) { // don't count mobs when animals and monsters are disabled
// re-set mob counts
for (ServerPlayer player : this.level.getLocalPlayers()) { // Folia - region threading
@@ -479,6 +490,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -484,6 +495,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
} else {
spawnercreature_d = NaturalSpawner.createState(naturalSpawnChunkCount, regionizedWorldData.getLoadedEntities(), this::getFullChunk, !this.level.paperConfig().entities.spawning.perPlayerMobSpawns ? new LocalMobCapCalculator(this.chunkMap) : null, false); // Folia - region threading - note: function only cares about loaded entities, doesn't need all
}
+ } finally { profiler.stopTimer(ca.spottedleaf.leafprofiler.LProfilerRegistry.MOB_SPAWN_ENTITY_COUNT); } // Folia - profiler
// Paper end - Optional per player mob spawns
this.level.timings.countNaturalMobs.stopTiming(); // Paper - timings

@@ -500,6 +512,9 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -505,6 +517,9 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
boolean flag1 = this.level.ticksPerSpawnCategory.getLong(org.bukkit.entity.SpawnCategory.ANIMAL) != 0L && this.level.getLevelData().getGameTime() % this.level.ticksPerSpawnCategory.getLong(org.bukkit.entity.SpawnCategory.ANIMAL) == 0L; // CraftBukkit
Iterator iterator1 = list.iterator();

Expand All @@ -1593,7 +1593,7 @@ index e13ccac27fa4f4c23185f12c776e80970ab844e6..135fa024d81b962761f0edc6896a2a50
while (iterator1.hasNext()) {
ServerChunkCache.ChunkAndHolder chunkproviderserver_a = (ServerChunkCache.ChunkAndHolder) iterator1.next();
LevelChunk chunk1 = chunkproviderserver_a.chunk;
@@ -508,10 +523,12 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -513,10 +528,12 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
if (true && this.chunkMap.anyPlayerCloseEnoughForSpawning(chunkcoordintpair)) { // Paper - rewrite chunk system
chunk1.incrementInhabitedTime(j);
if (flag && (this.spawnEnemies || this.spawnFriendlies) && this.level.getWorldBorder().isWithinBounds(chunkcoordintpair) && this.chunkMap.anyPlayerCloseEnoughForSpawning(chunkcoordintpair, true)) { // Spigot
Expand All @@ -1606,7 +1606,7 @@ index e13ccac27fa4f4c23185f12c776e80970ab844e6..135fa024d81b962761f0edc6896a2a50
this.level.tickChunk(chunk1, l);
// Paper start - rewrite chunk system
if ((++chunksTicked & 7L) == 0L) {
@@ -521,19 +538,25 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -526,19 +543,25 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
}
}
}
Expand All @@ -1632,7 +1632,7 @@ index e13ccac27fa4f4c23185f12c776e80970ab844e6..135fa024d81b962761f0edc6896a2a50
{
final it.unimi.dsi.fastutil.objects.ObjectArrayList<net.minecraft.server.level.ServerChunkCache.ChunkAndHolder> chunks = (it.unimi.dsi.fastutil.objects.ObjectArrayList<net.minecraft.server.level.ServerChunkCache.ChunkAndHolder>)list;
final ServerChunkCache.ChunkAndHolder[] raw = chunks.elements();
@@ -547,6 +570,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
@@ -552,6 +575,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
holder.holder().broadcastChanges(holder.chunk());
}
}
Expand Down

0 comments on commit 2e7bc07

Please sign in to comment.