Merge pull request #387 from CivMC/meta-log

Add logging for ChunkMeta
This commit is contained in:
okx-code
2024-03-25 21:30:19 +00:00
committed by GitHub
6 changed files with 55 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.ipvp.canvas.MenuFunctionListener;
import vg.civcraft.mc.civmodcore.chat.dialog.DialogManager;
import vg.civcraft.mc.civmodcore.commands.ChunkMetaCommand;
import vg.civcraft.mc.civmodcore.commands.CommandManager;
import vg.civcraft.mc.civmodcore.commands.StatCommand;
import vg.civcraft.mc.civmodcore.dao.DatabaseCredentials;
@@ -95,6 +96,7 @@ public class CivModCorePlugin extends ACivMod {
this.commands.init();
this.commands.registerCommand(new ConfigCommand());
this.commands.registerCommand(new StatCommand());
this.commands.registerCommand(new ChunkMetaCommand());
// Load APIs
EnchantUtils.loadEnchantAbbreviations(this);
MoreTags.init();

View File

@@ -0,0 +1,33 @@
package vg.civcraft.mc.civmodcore.commands;
import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Subcommand;
import net.kyori.adventure.text.Component;
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;
@CommandAlias("cmc")
public class ChunkMetaCommand extends BaseCommand {
private static final Logger CHUNK_META_LOGGER = LogManager.getLogger("Chunk meta");
@Subcommand("togglechunkmeta")
@Description("Toggle showing chunk meta logs")
@CommandPermission("cmc.debug")
public void chunkmeta(CommandSender sender) {
if (!CHUNK_META_LOGGER.getLevel().isMoreSpecificThan(Level.INFO)) {
Configurator.setLevel("Chunk meta", Level.INFO);
sender.sendMessage(Component.text("Chunk meta logs disabled.", NamedTextColor.RED));
} else {
Configurator.setLevel("Chunk meta", Level.DEBUG);
sender.sendMessage(Component.text("Chunk meta logs enabled.", NamedTextColor.GREEN));
}
}
}

View File

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

View File

@@ -1,6 +1,7 @@
package vg.civcraft.mc.civmodcore.world.locations.chunkmeta;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.logging.log4j.LogManager;
import org.bukkit.Chunk;
import org.bukkit.event.EventHandler;
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;
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 ChunkMetaViewTracker viewTracker;
@@ -52,6 +54,7 @@ public class ChunkMetaListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void chunkUnload(ChunkUnloadEvent e) {
CHUNK_META_LOGGER.debug("World " + CivModCorePlugin.getInstance().getWorldIdManager().getInternalWorldId(e.getWorld()) + ": Adding " + e.getChunk() + " to unload queue - length " + unloadQueue.size());
unloadQueue.add(e.getChunk());
viewTracker.applyToAllSingleBlockViews(s -> s.handleChunkUnload(e.getChunk()));
}

View File

@@ -10,6 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import java.util.logging.Logger;
import org.apache.logging.log4j.LogManager;
import org.bukkit.World;
/**
@@ -20,6 +21,7 @@ import org.bukkit.World;
*
*/
public class WorldChunkMetaManager {
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
@@ -218,26 +220,30 @@ public class WorldChunkMetaManager {
if (unloadingQueue.isEmpty()) {
return;
}
CHUNK_META_LOGGER.debug("World " + worldID + ": Processing unloading queue");
Set<ChunkCoord> readdList = null;
ChunkCoord coord;
while((coord = unloadingQueue.poll()) != null) {
if (!coord.isUnloaded()) {
CHUNK_META_LOGGER.debug("World " + worldID + ": Skipping chunk " + coord + " because it is loaded at " + coord.getLastLoadedTime() + " after " + coord.getLastUnloadedTime());
continue;
}
if (System.currentTimeMillis() - coord.getLastUnloadedTime() > UNLOAD_DELAY) {
CHUNK_META_LOGGER.debug("World " + worldID + ": Unloading chunk " + coord + " - unloaded: " + coord.getLastUnloadedTime());
unloadChunkCoord(coord);
} else {
if (readdList == null) {
readdList = new HashSet<>();
}
readdList.add(coord);
}
}
if (readdList != null) {
CHUNK_META_LOGGER.debug("World " + worldID + ": Unloaded chunks remaining unsaved: " + readdList);
unloadingQueue.addAll(readdList);
}
}, UNLOAD_CHECK_INTERVAL, UNLOAD_CHECK_INTERVAL, TimeUnit.MILLISECONDS);
@@ -267,6 +273,7 @@ public class WorldChunkMetaManager {
// written to the db
synchronized (metas) {
if (coord.isUnloaded()) {
CHUNK_META_LOGGER.debug("Chunk no longer being tracked: " + coord);
metas.remove(coord);
coord.clearUnloaded();
}
@@ -313,6 +320,7 @@ public class WorldChunkMetaManager {
*/
void unloadChunk(int x, int z) {
ChunkCoord chunkCoord = getChunkCoord(x, z, false, false);
CHUNK_META_LOGGER.debug("World " + worldID + ": Add to unloading queue: " + chunkCoord);
// chunkCoord can never be null here, otherwise our data structure would be
// broken, in which case we'd want to know
chunkCoord.minecraftChunkUnloaded();

View File

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