Skip to content

Commit

Permalink
Merge pull request #226 from radiant-ai/fix_lag_on_player_and_chunk_t…
Browse files Browse the repository at this point in the history
…otal

Improve performance of chunk loaded metric and offline player count
  • Loading branch information
sladkoff authored Feb 28, 2024
2 parents e864767 + e7de226 commit f04c2f1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
57 changes: 57 additions & 0 deletions src/main/java/de/sldk/mc/collectors/LoadedChunksCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package de.sldk.mc.collectors;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.WorldUnloadEvent;

import java.util.HashMap;
import java.util.Map;

public class LoadedChunksCollector implements Listener {

private final Map<String, Integer> loadedChunksTotalMap = new HashMap<>();

@EventHandler
public void onChunkLoad(ChunkLoadEvent event) {
World world = event.getWorld();
Integer currentCount = loadedChunksTotalMap.get(world.getName());
if (currentCount == null) {
loadedChunksTotalMap.put(world.getName(), world.getLoadedChunks().length);
}
else {
loadedChunksTotalMap.put(world.getName(), currentCount + 1);
}
}

@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
World world = event.getWorld();
Integer currentCount = loadedChunksTotalMap.get(world.getName());
if (currentCount == null) {
loadedChunksTotalMap.put(world.getName(), world.getLoadedChunks().length);
}
else {
loadedChunksTotalMap.put(world.getName(), currentCount - 1);
}
}

@EventHandler
public void onWorldUnload(WorldUnloadEvent event) {
World world = event.getWorld();
loadedChunksTotalMap.remove(world.getName());
}

public int getLoadedChunkTotal(String worldName) {
return loadedChunksTotalMap.computeIfAbsent(worldName, k -> {
World world = Bukkit.getWorld(worldName);
if (world == null) {
return 0;
}
return world.getLoadedChunks().length;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.sldk.mc.tps;
package de.sldk.mc.collectors;

import java.util.LinkedList;
import java.util.function.Supplier;
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/de/sldk/mc/metrics/LoadedChunks.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.sldk.mc.metrics;

import de.sldk.mc.collectors.LoadedChunksCollector;
import io.prometheus.client.Gauge;
import org.bukkit.World;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;

public class LoadedChunks extends WorldMetric {
Expand All @@ -12,17 +14,31 @@ public class LoadedChunks extends WorldMetric {
.labelNames("world")
.create();

private final LoadedChunksCollector loadedChunksCollector = new LoadedChunksCollector();

public LoadedChunks(Plugin plugin) {
super(plugin, LOADED_CHUNKS);
}

@Override
public void enable() {
super.enable();
getPlugin().getServer().getPluginManager().registerEvents(loadedChunksCollector, getPlugin());
}

@Override
public void disable() {
super.disable();
HandlerList.unregisterAll(loadedChunksCollector);
}

@Override
protected void clear() {
LOADED_CHUNKS.clear();
}

@Override
public void collect(World world) {
LOADED_CHUNKS.labels(world.getName()).set(world.getLoadedChunks().length);
LOADED_CHUNKS.labels(world.getName()).set(loadedChunksCollector.getLoadedChunkTotal(world.getName()));
}
}
5 changes: 5 additions & 0 deletions src/main/java/de/sldk/mc/metrics/PlayersTotal.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public PlayersTotal(Plugin plugin) {
public void doCollect() {
PLAYERS.set(Bukkit.getOfflinePlayers().length);
}

@Override
public boolean isAsyncCapable() {
return true;
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/sldk/mc/metrics/Tps.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.sldk.mc.metrics;

import de.sldk.mc.tps.TpsCollector;
import de.sldk.mc.collectors.TpsCollector;
import io.prometheus.client.Gauge;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.sldk.mc.tps;
package de.sldk.mc.collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
Expand Down

0 comments on commit f04c2f1

Please # to comment.