
When a player triggers a chunk load via walking around or teleporting there is no need to stop everything and get this chunk on the main thread. The client is used to having to wait some time for this chunk and the server doesn't immediately do anything with it except send it to the player. At the same time chunk loading is the last major source of file IO that still runs on the main thread. These two facts make it possible to offload chunks loaded for this reason to another thread. However, not all parts of chunk loading can happen off the main thread. For this we use the new AsynchronousExecutor system to split chunk loading in to three pieces. The first is loading data from disk, decompressing it, and parsing it in to an NBT structure. The second piece is creating entities and tile entities in the chunk and adding them to the world, this is still done on the main thread. The third piece is informing everyone who requested a chunk load that the load is finished. For this we register callbacks and then run them on the main thread once the previous two stages are finished. There are still cases where a chunk is needed immediately and these will still trigger chunk loading entirely on the main thread. The most obvious case is plugins using the API to request a chunk load. We also must load the chunk immediately when something in the world tries to access it. In these cases we ignore any possibly pending or in progress chunk loading that is happening asynchronously as we will have the chunk loaded by the time they are finished. The hope is that overall this system will result in less CPU time and pauses due to blocking file IO on the main thread thus giving more consistent performance. Testing so far has shown that this also speeds up chunk loading client side although some of this is likely to be because we are sending less chunks at once for the client to process. Thanks for @ammaraskar for help with the implementation of this feature.
336 lines
12 KiB
Java
336 lines
12 KiB
Java
package net.minecraft.server;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
// CraftBukkit start
|
|
import java.util.Random;
|
|
|
|
import org.bukkit.Server;
|
|
import org.bukkit.craftbukkit.chunkio.ChunkIOExecutor;
|
|
import org.bukkit.craftbukkit.util.LongHash;
|
|
import org.bukkit.craftbukkit.util.LongHashSet;
|
|
import org.bukkit.craftbukkit.util.LongObjectHashMap;
|
|
import org.bukkit.event.world.ChunkUnloadEvent;
|
|
// CraftBukkit end
|
|
|
|
public class ChunkProviderServer implements IChunkProvider {
|
|
|
|
// CraftBukkit start
|
|
public LongHashSet unloadQueue = new LongHashSet();
|
|
public Chunk emptyChunk;
|
|
public IChunkProvider chunkProvider; // CraftBukkit
|
|
private IChunkLoader e;
|
|
public boolean forceChunkLoad = false; // true -> false
|
|
public LongObjectHashMap<Chunk> chunks = new LongObjectHashMap<Chunk>();
|
|
public WorldServer world;
|
|
// CraftBukkit end
|
|
|
|
public ChunkProviderServer(WorldServer worldserver, IChunkLoader ichunkloader, IChunkProvider ichunkprovider) {
|
|
this.emptyChunk = new EmptyChunk(worldserver, 0, 0);
|
|
this.world = worldserver;
|
|
this.e = ichunkloader;
|
|
this.chunkProvider = ichunkprovider;
|
|
}
|
|
|
|
public boolean isChunkLoaded(int i, int j) {
|
|
return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
|
|
}
|
|
|
|
public void queueUnload(int i, int j) {
|
|
if (this.world.worldProvider.e()) {
|
|
ChunkCoordinates chunkcoordinates = this.world.getSpawn();
|
|
int k = i * 16 + 8 - chunkcoordinates.x;
|
|
int l = j * 16 + 8 - chunkcoordinates.z;
|
|
short short1 = 128;
|
|
|
|
// CraftBukkit start
|
|
if (k < -short1 || k > short1 || l < -short1 || l > short1 || !(this.world.keepSpawnInMemory)) { // Added 'this.world.keepSpawnInMemory'
|
|
this.unloadQueue.add(i, j);
|
|
|
|
Chunk c = this.chunks.get(LongHash.toLong(i, j));
|
|
if (c != null) {
|
|
c.mustSave = true;
|
|
}
|
|
}
|
|
// CraftBukkit end
|
|
} else {
|
|
// CraftBukkit start
|
|
this.unloadQueue.add(i, j);
|
|
|
|
Chunk c = this.chunks.get(LongHash.toLong(i, j));
|
|
if (c != null) {
|
|
c.mustSave = true;
|
|
}
|
|
// CraftBukkit end
|
|
}
|
|
}
|
|
|
|
public void a() {
|
|
Iterator iterator = this.chunks.values().iterator(); // CraftBukkit
|
|
|
|
while (iterator.hasNext()) {
|
|
Chunk chunk = (Chunk) iterator.next();
|
|
|
|
this.queueUnload(chunk.x, chunk.z);
|
|
}
|
|
}
|
|
|
|
// CraftBukkit start - add async variant, provide compatibility
|
|
public Chunk getChunkAt(int i, int j) {
|
|
return getChunkAt(i, j, null);
|
|
}
|
|
|
|
public Chunk getChunkAt(int i, int j, Runnable runnable) {
|
|
this.unloadQueue.remove(i, j);
|
|
Chunk chunk = (Chunk) this.chunks.get(LongHash.toLong(i, j));
|
|
boolean newChunk = false;
|
|
ChunkRegionLoader loader = null;
|
|
|
|
if (this.e instanceof ChunkRegionLoader) {
|
|
loader = (ChunkRegionLoader) this.e;
|
|
}
|
|
|
|
// If the chunk exists but isn't loaded do it async
|
|
if (chunk == null && runnable != null && loader != null && loader.chunkExists(this.world, i, j)) {
|
|
ChunkIOExecutor.queueChunkLoad(this.world, loader, this, i, j, runnable);
|
|
return null;
|
|
}
|
|
// CraftBukkit end
|
|
|
|
if (chunk == null) {
|
|
chunk = this.loadChunk(i, j);
|
|
if (chunk == null) {
|
|
if (this.chunkProvider == null) {
|
|
chunk = this.emptyChunk;
|
|
} else {
|
|
try {
|
|
chunk = this.chunkProvider.getOrCreateChunk(i, j);
|
|
} catch (Throwable throwable) {
|
|
CrashReport crashreport = CrashReport.a(throwable, "Exception generating new chunk");
|
|
CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Chunk to be generated");
|
|
|
|
crashreportsystemdetails.a("Location", String.format("%d,%d", new Object[] { Integer.valueOf(i), Integer.valueOf(j)}));
|
|
crashreportsystemdetails.a("Position hash", Long.valueOf(LongHash.toLong(i, j)));
|
|
crashreportsystemdetails.a("Generator", this.chunkProvider.getName());
|
|
throw new ReportedException(crashreport);
|
|
}
|
|
}
|
|
newChunk = true; // CraftBukkit
|
|
}
|
|
|
|
this.chunks.put(LongHash.toLong(i, j), chunk); // CraftBukkit
|
|
if (chunk != null) {
|
|
chunk.addEntities();
|
|
}
|
|
|
|
// CraftBukkit start
|
|
Server server = this.world.getServer();
|
|
if (server != null) {
|
|
/*
|
|
* If it's a new world, the first few chunks are generated inside
|
|
* the World constructor. We can't reliably alter that, so we have
|
|
* no way of creating a CraftWorld/CraftServer at that point.
|
|
*/
|
|
server.getPluginManager().callEvent(new org.bukkit.event.world.ChunkLoadEvent(chunk.bukkitChunk, newChunk));
|
|
}
|
|
// CraftBukkit end
|
|
|
|
chunk.a(this, this, i, j);
|
|
}
|
|
|
|
// CraftBukkit start - If we didn't need to load the chunk run the callback now
|
|
if (runnable != null) {
|
|
runnable.run();
|
|
}
|
|
// CraftBukkit end
|
|
|
|
return chunk;
|
|
}
|
|
|
|
public Chunk getOrCreateChunk(int i, int j) {
|
|
// CraftBukkit start
|
|
Chunk chunk = (Chunk) this.chunks.get(LongHash.toLong(i, j));
|
|
|
|
chunk = chunk == null ? (!this.world.isLoading && !this.forceChunkLoad ? this.emptyChunk : this.getChunkAt(i, j)) : chunk;
|
|
if (chunk == this.emptyChunk) return chunk;
|
|
if (i != chunk.x || j != chunk.z) {
|
|
MinecraftServer.log.severe("Chunk (" + chunk.x + ", " + chunk.z + ") stored at (" + i + ", " + j + ") in world '" + world.getWorld().getName() + "'");
|
|
MinecraftServer.log.severe(chunk.getClass().getName());
|
|
Throwable ex = new Throwable();
|
|
ex.fillInStackTrace();
|
|
ex.printStackTrace();
|
|
}
|
|
return chunk;
|
|
// CraftBukkit end
|
|
}
|
|
|
|
public Chunk loadChunk(int i, int j) { // CraftBukkit - private -> public
|
|
if (this.e == null) {
|
|
return null;
|
|
} else {
|
|
try {
|
|
Chunk chunk = this.e.a(this.world, i, j);
|
|
|
|
if (chunk != null) {
|
|
chunk.n = this.world.getTime();
|
|
if (this.chunkProvider != null) {
|
|
this.chunkProvider.recreateStructures(i, j);
|
|
}
|
|
}
|
|
|
|
return chunk;
|
|
} catch (Exception exception) {
|
|
exception.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void saveChunkNOP(Chunk chunk) { // CraftBukkit - private -> public
|
|
if (this.e != null) {
|
|
try {
|
|
this.e.b(this.world, chunk);
|
|
} catch (Exception exception) {
|
|
exception.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void saveChunk(Chunk chunk) { // CraftBukkit - private -> public
|
|
if (this.e != null) {
|
|
try {
|
|
chunk.n = this.world.getTime();
|
|
this.e.a(this.world, chunk);
|
|
} catch (Exception ioexception) { // CraftBukkit - IOException -> Exception
|
|
ioexception.printStackTrace();
|
|
// CraftBukkit start - remove extra exception
|
|
}
|
|
// } catch (ExceptionWorldConflict exceptionworldconflict) {
|
|
// exceptionworldconflict.printStackTrace();
|
|
// }
|
|
// CraftBukkit end
|
|
}
|
|
}
|
|
|
|
public void getChunkAt(IChunkProvider ichunkprovider, int i, int j) {
|
|
Chunk chunk = this.getOrCreateChunk(i, j);
|
|
|
|
if (!chunk.done) {
|
|
chunk.done = true;
|
|
if (this.chunkProvider != null) {
|
|
this.chunkProvider.getChunkAt(ichunkprovider, i, j);
|
|
|
|
// CraftBukkit start
|
|
BlockSand.instaFall = true;
|
|
Random random = new Random();
|
|
random.setSeed(world.getSeed());
|
|
long xRand = random.nextLong() / 2L * 2L + 1L;
|
|
long zRand = random.nextLong() / 2L * 2L + 1L;
|
|
random.setSeed((long) i * xRand + (long) j * zRand ^ world.getSeed());
|
|
|
|
org.bukkit.World world = this.world.getWorld();
|
|
if (world != null) {
|
|
for (org.bukkit.generator.BlockPopulator populator : world.getPopulators()) {
|
|
populator.populate(world, random, chunk.bukkitChunk);
|
|
}
|
|
}
|
|
BlockSand.instaFall = false;
|
|
this.world.getServer().getPluginManager().callEvent(new org.bukkit.event.world.ChunkPopulateEvent(chunk.bukkitChunk));
|
|
// CraftBukkit end
|
|
|
|
chunk.e();
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean saveChunks(boolean flag, IProgressUpdate iprogressupdate) {
|
|
int i = 0;
|
|
// CraftBukkit start
|
|
Iterator iterator = this.chunks.values().iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
Chunk chunk = (Chunk) iterator.next();
|
|
// CraftBukkit end
|
|
|
|
if (flag) {
|
|
this.saveChunkNOP(chunk);
|
|
}
|
|
|
|
if (chunk.a(flag)) {
|
|
this.saveChunk(chunk);
|
|
chunk.l = false;
|
|
++i;
|
|
if (i == 24 && !flag) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (flag) {
|
|
if (this.e == null) {
|
|
return true;
|
|
}
|
|
|
|
this.e.b();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public boolean unloadChunks() {
|
|
if (!this.world.savingDisabled) {
|
|
// CraftBukkit start
|
|
Server server = this.world.getServer();
|
|
for (int i = 0; i < 100 && !this.unloadQueue.isEmpty(); i++) {
|
|
long chunkcoordinates = this.unloadQueue.popFirst();
|
|
Chunk chunk = this.chunks.get(chunkcoordinates);
|
|
if (chunk == null) continue;
|
|
|
|
ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk);
|
|
server.getPluginManager().callEvent(event);
|
|
if (!event.isCancelled()) {
|
|
chunk.removeEntities();
|
|
this.saveChunk(chunk);
|
|
this.saveChunkNOP(chunk);
|
|
// this.unloadQueue.remove(integer);
|
|
this.chunks.remove(chunkcoordinates); // CraftBukkit
|
|
}
|
|
}
|
|
// CraftBukkit end
|
|
|
|
if (this.e != null) {
|
|
this.e.a();
|
|
}
|
|
}
|
|
|
|
return this.chunkProvider.unloadChunks();
|
|
}
|
|
|
|
public boolean canSave() {
|
|
return !this.world.savingDisabled;
|
|
}
|
|
|
|
public String getName() {
|
|
return "ServerChunkCache: " + this.chunks.values().size() + " Drop: " + this.unloadQueue.size(); // CraftBukkit
|
|
}
|
|
|
|
public List getMobsFor(EnumCreatureType enumcreaturetype, int i, int j, int k) {
|
|
return this.chunkProvider.getMobsFor(enumcreaturetype, i, j, k);
|
|
}
|
|
|
|
public ChunkPosition findNearestMapFeature(World world, String s, int i, int j, int k) {
|
|
return this.chunkProvider.findNearestMapFeature(world, s, i, j, k);
|
|
}
|
|
|
|
public int getLoadedChunks() {
|
|
return this.chunks.values().size(); // CraftBukkit
|
|
}
|
|
|
|
public void recreateStructures(int i, int j) {}
|
|
}
|