This commit is contained in:
okx-code
2024-03-25 21:27:08 +00:00
parent f4bc6fb407
commit da67fa4b14
5 changed files with 27 additions and 15 deletions

View File

@@ -7,24 +7,26 @@ import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Subcommand;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.logging.Level;
import java.util.logging.Logger;
@CommandAlias("cmc") @CommandAlias("cmc")
public class ChunkMetaCommand extends BaseCommand { public class ChunkMetaCommand extends BaseCommand {
private static final Logger CHUNK_META_LOGGER = Logger.getLogger("Chunk meta"); private static final Logger CHUNK_META_LOGGER = LogManager.getLogger("Chunk meta");
@Subcommand("togglechunkmeta") @Subcommand("togglechunkmeta")
@Description("Toggle showing chunk meta logs") @Description("Toggle showing chunk meta logs")
@CommandPermission("cmc.debug") @CommandPermission("cmc.debug")
public void chunkmeta(CommandSender sender) { public void chunkmeta(CommandSender sender) {
if (CHUNK_META_LOGGER.getLevel().intValue() < Level.INFO.intValue()) { if (!CHUNK_META_LOGGER.getLevel().isMoreSpecificThan(Level.INFO)) {
CHUNK_META_LOGGER.setLevel(Level.INFO); Configurator.setLevel("Chunk meta", Level.INFO);
sender.sendMessage(Component.text("Chunk meta logs disabled.", NamedTextColor.RED)); sender.sendMessage(Component.text("Chunk meta logs disabled.", NamedTextColor.RED));
} else { } else {
CHUNK_META_LOGGER.setLevel(Level.FINE); Configurator.setLevel("Chunk meta", Level.DEBUG);
sender.sendMessage(Component.text("Chunk meta logs enabled.", NamedTextColor.GREEN)); sender.sendMessage(Component.text("Chunk meta logs enabled.", NamedTextColor.GREEN));
} }
} }

View File

@@ -115,6 +115,10 @@ public class ChunkCoord extends XZWCoord {
return lastUnloadedTime; return lastUnloadedTime;
} }
public long getLastLoadedTime() {
return lastLoadedTime;
}
void clearUnloaded() { void clearUnloaded() {
lastUnloadedTime = INVALID_TIME; lastUnloadedTime = INVALID_TIME;
} }

View File

@@ -1,6 +1,7 @@
package vg.civcraft.mc.civmodcore.world.locations.chunkmeta; package vg.civcraft.mc.civmodcore.world.locations.chunkmeta;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import org.apache.logging.log4j.LogManager;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@@ -13,6 +14,7 @@ import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.api.ChunkMetaViewTrac
import vg.civcraft.mc.civmodcore.world.locations.global.WorldIDManager; import vg.civcraft.mc.civmodcore.world.locations.global.WorldIDManager;
public class ChunkMetaListener implements Listener { public class ChunkMetaListener implements Listener {
private static final org.apache.logging.log4j.Logger CHUNK_META_LOGGER = LogManager.getLogger("Chunk meta");
private final GlobalChunkMetaManager manager; private final GlobalChunkMetaManager manager;
private final ChunkMetaViewTracker viewTracker; private final ChunkMetaViewTracker viewTracker;
@@ -52,6 +54,7 @@ public class ChunkMetaListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void chunkUnload(ChunkUnloadEvent e) { public void chunkUnload(ChunkUnloadEvent e) {
CHUNK_META_LOGGER.debug("Adding " + e.getChunk() + " to unload queue - length " + unloadQueue.size());
unloadQueue.add(e.getChunk()); unloadQueue.add(e.getChunk());
viewTracker.applyToAllSingleBlockViews(s -> s.handleChunkUnload(e.getChunk())); viewTracker.applyToAllSingleBlockViews(s -> s.handleChunkUnload(e.getChunk()));
} }

View File

@@ -10,8 +10,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.apache.logging.log4j.LogManager;
import org.bukkit.World; import org.bukkit.World;
import vg.civcraft.mc.civmodcore.commands.ChunkMetaCommand;
/** /**
* Stores Chunk metadata for all plugins for one specific world. Metadata is * Stores Chunk metadata for all plugins for one specific world. Metadata is
@@ -21,7 +21,7 @@ import vg.civcraft.mc.civmodcore.commands.ChunkMetaCommand;
* *
*/ */
public class WorldChunkMetaManager { public class WorldChunkMetaManager {
private static final Logger CHUNK_META_LOGGER = Logger.getLogger("Chunk meta"); private static final org.apache.logging.log4j.Logger CHUNK_META_LOGGER = LogManager.getLogger("Chunk meta");
/** /**
* How long should chunk data be kept in memory after the chunk is unloaded? 5 * How long should chunk data be kept in memory after the chunk is unloaded? 5
@@ -220,17 +220,19 @@ public class WorldChunkMetaManager {
if (unloadingQueue.isEmpty()) { if (unloadingQueue.isEmpty()) {
return; return;
} }
CHUNK_META_LOGGER.debug("World " + worldID + ": Processing unloading queue");
Set<ChunkCoord> readdList = null; Set<ChunkCoord> readdList = null;
ChunkCoord coord; ChunkCoord coord;
while((coord = unloadingQueue.poll()) != null) { while((coord = unloadingQueue.poll()) != null) {
if (!coord.isUnloaded()) { if (!coord.isUnloaded()) {
CHUNK_META_LOGGER.debug("World " + worldID + ": Skipping chunk " + coord + " because it is loaded at " + coord.getLastLoadedTime() + " after " + coord.getLastUnloadedTime());
continue; continue;
} }
if (System.currentTimeMillis() - coord.getLastUnloadedTime() > UNLOAD_DELAY) { if (System.currentTimeMillis() - coord.getLastUnloadedTime() > UNLOAD_DELAY) {
CHUNK_META_LOGGER.fine("Unloading chunk " + coord + " - unloaded: " + coord.getLastUnloadedTime()); CHUNK_META_LOGGER.debug("World " + worldID + ": Unloading chunk " + coord + " - unloaded: " + coord.getLastUnloadedTime());
unloadChunkCoord(coord); unloadChunkCoord(coord);
} else { } else {
if (readdList == null) { if (readdList == null) {
@@ -241,7 +243,7 @@ public class WorldChunkMetaManager {
} }
} }
if (readdList != null) { if (readdList != null) {
CHUNK_META_LOGGER.fine("Unloaded chunks remaining unsaved: " + readdList); CHUNK_META_LOGGER.debug("World " + worldID + ": Unloaded chunks remaining unsaved: " + readdList);
unloadingQueue.addAll(readdList); unloadingQueue.addAll(readdList);
} }
}, UNLOAD_CHECK_INTERVAL, UNLOAD_CHECK_INTERVAL, TimeUnit.MILLISECONDS); }, UNLOAD_CHECK_INTERVAL, UNLOAD_CHECK_INTERVAL, TimeUnit.MILLISECONDS);
@@ -271,7 +273,7 @@ public class WorldChunkMetaManager {
// written to the db // written to the db
synchronized (metas) { synchronized (metas) {
if (coord.isUnloaded()) { if (coord.isUnloaded()) {
CHUNK_META_LOGGER.fine("Chunk no longer being tracked: " + coord); CHUNK_META_LOGGER.debug("Chunk no longer being tracked: " + coord);
metas.remove(coord); metas.remove(coord);
coord.clearUnloaded(); coord.clearUnloaded();
} }
@@ -318,7 +320,7 @@ public class WorldChunkMetaManager {
*/ */
void unloadChunk(int x, int z) { void unloadChunk(int x, int z) {
ChunkCoord chunkCoord = getChunkCoord(x, z, false, false); ChunkCoord chunkCoord = getChunkCoord(x, z, false, false);
CHUNK_META_LOGGER.fine("Add to unloading queue: " + chunkCoord); CHUNK_META_LOGGER.debug("World " + worldID + ": Add to unloading queue: " + chunkCoord);
// chunkCoord can never be null here, otherwise our data structure would be // chunkCoord can never be null here, otherwise our data structure would be
// broken, in which case we'd want to know // broken, in which case we'd want to know
chunkCoord.minecraftChunkUnloaded(); chunkCoord.minecraftChunkUnloaded();

View File

@@ -2,14 +2,15 @@ package vg.civcraft.mc.civmodcore.world.locations.chunkmeta.block.table;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Location; import org.bukkit.Location;
import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.CacheState; import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.CacheState;
import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.block.BlockBasedChunkMeta; import vg.civcraft.mc.civmodcore.world.locations.chunkmeta.block.BlockBasedChunkMeta;
public abstract class TableBasedBlockChunkMeta<D extends TableBasedDataObject> public abstract class TableBasedBlockChunkMeta<D extends TableBasedDataObject>
extends BlockBasedChunkMeta<TableBasedDataObject, TableStorageEngine<D>> { extends BlockBasedChunkMeta<TableBasedDataObject, TableStorageEngine<D>> {
private static final Logger CHUNK_META_LOGGER = Logger.getLogger("Chunk meta"); private static final Logger CHUNK_META_LOGGER = LogManager.getLogger("Chunk meta");
private List<D> modifiedEntries; private List<D> modifiedEntries;
@@ -54,7 +55,7 @@ public abstract class TableBasedBlockChunkMeta<D extends TableBasedDataObject>
@Override @Override
public void insert() { public void insert() {
CHUNK_META_LOGGER.fine("Inserting at " + chunkCoord); CHUNK_META_LOGGER.debug("Inserting at " + chunkCoord);
for (D data : modifiedEntries) { for (D data : modifiedEntries) {
switch (data.getCacheState()) { switch (data.getCacheState()) {
case NORMAL: case NORMAL: