Refactored statmanager to only upload stats on disconnect and only one player per tick.

Fixed derp json crap with redis treating a name with only numbers as a number and adding .0 on it.
This commit is contained in:
Jonathan Williams 2015-01-03 01:58:04 -06:00
parent 2a29def670
commit cc06b0ad33
5 changed files with 127 additions and 79 deletions

View File

@ -22,10 +22,11 @@ public class ServerConfiguration extends MiniPlugin
super("Server Configuration", plugin);
Region region = plugin.getConfig().getBoolean("serverstatus.us") ? Region.US : Region.EU;
String groupName = plugin.getConfig().getString("serverstatus.group").replace(".0", "");
for (ServerGroup serverGroup : ServerManager.getServerRepository(region).getServerGroups(null))
{
if (serverGroup.getName().equalsIgnoreCase(plugin.getConfig().getString("serverstatus.group")))
if (serverGroup.getName().equalsIgnoreCase(groupName))
{
_serverGroup = serverGroup;
break;

View File

@ -2,6 +2,7 @@ package mineplex.core.stats;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -22,7 +23,7 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
private StatsRepository _repository;
private NautHashMap<String, Integer> _stats = new NautHashMap<String, Integer>();
private NautHashMap<String, NautHashMap<String, Integer>> _statUploadQueue = new NautHashMap<String, NautHashMap<String, Integer>>();
private NautHashMap<Player, NautHashMap<String, Integer>> _statUploadQueue = new NautHashMap<Player, NautHashMap<String, Integer>>();
private Runnable _saveRunnable;
public StatsManager(JavaPlugin plugin, CoreClientManager clientManager)
@ -41,7 +42,7 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
}
};
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, _saveRunnable, 40L, 40L);
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, _saveRunnable, 1L, 1L);
}
for (Stat stat : _repository.retrieveStats())
@ -50,80 +51,13 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
}
}
public void incrementStat(Player player, final String statName, final int value)
public void incrementStat(final Player player, final String statName, final int value)
{
int newValue = Get(player).addStat(statName, value);
//Event
UtilServer.getServer().getPluginManager().callEvent(new StatChangeEvent(player.getName(), statName, newValue - value, newValue));
final String uuidString = player.getUniqueId().toString();
incrementStat(uuidString, statName, value);
}
private void addToQueue(String statName, String uuidString, int value)
{
if (!_statUploadQueue.containsKey(uuidString))
{
_statUploadQueue.put(uuidString, new NautHashMap<String, Integer>());
}
if (!_statUploadQueue.get(uuidString).containsKey(statName))
{
_statUploadQueue.get(uuidString).put(statName, 0);
}
_statUploadQueue.get(uuidString).put(statName, value);
System.out.println("added to stat upload queue.");
}
protected void saveStats()
{
if (_statUploadQueue.isEmpty())
return;
try
{
NautHashMap<String, NautHashMap<Integer, Integer>> uploadQueue = new NautHashMap<String, NautHashMap<Integer, Integer>>();
synchronized (_statSync)
{
for (String key : _statUploadQueue.keySet())
{
uploadQueue.put(key, new NautHashMap<Integer, Integer>());
for (String statName : _statUploadQueue.get(key).keySet())
{
int statId = _stats.get(statName);
uploadQueue.get(key).put(statId, _statUploadQueue.get(key).get(statName));
}
}
_statUploadQueue.clear();
}
_repository.saveStats(uploadQueue);
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
@Override
protected PlayerStats AddPlayer(String player)
{
return new PlayerStats();
}
public PlayerStats getOfflinePlayerStats(String playerName) throws SQLException
{
return _repository.loadOfflinePlayerStats(playerName);
}
public void incrementStat(final String uuidString, final String statName, final int value)
{
// Verify stat is in our local cache, if not add it remotely.
if (!_stats.containsKey(statName))
{
@ -138,7 +72,7 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
// Then it pops into queue and forgets adding the new stat to db.
if (_stats.containsKey(statName))
{
addToQueue(statName, uuidString, value);
addToQueue(statName, player, value);
return;
}
@ -151,7 +85,7 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
_stats.put(stat.Name, stat.Id);
}
addToQueue(statName, uuidString, value);
addToQueue(statName, player, value);
}
}
});
@ -160,10 +94,100 @@ public class StatsManager extends MiniDbClientPlugin<PlayerStats>
{
synchronized (_statSync)
{
addToQueue(statName, uuidString, value);
addToQueue(statName, player, value);
}
}
}
private void addToQueue(String statName, Player player, int value)
{
if (!_statUploadQueue.containsKey(player))
{
_statUploadQueue.put(player, new NautHashMap<String, Integer>());
}
if (!_statUploadQueue.get(player).containsKey(statName))
{
_statUploadQueue.get(player).put(statName, 0);
}
_statUploadQueue.get(player).put(statName, value);
}
protected void saveStats()
{
if (_statUploadQueue.isEmpty())
return;
try
{
NautHashMap<String, NautHashMap<Integer, Integer>> uploadQueue = new NautHashMap<String, NautHashMap<Integer, Integer>>();
synchronized (_statSync)
{
for (Iterator<Player> statIterator = _statUploadQueue.keySet().iterator(); statIterator.hasNext();)
{
Player player = statIterator.next();
if (player.isOnline())
continue;
String uploadKey = player.getUniqueId().toString();
uploadQueue.put(uploadKey, new NautHashMap<Integer, Integer>());
for (String statName : _statUploadQueue.get(player).keySet())
{
int statId = _stats.get(statName);
uploadQueue.get(uploadKey).put(statId, _statUploadQueue.get(player).get(statName));
System.out.println(player.getName() + " saving stat : " + statName + " adding " + _statUploadQueue.get(player).get(statName));
}
statIterator.remove();
// Only process one player per tick/call.
break;
}
}
_repository.saveStats(uploadQueue);
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
public boolean incrementStat(final String uuidString, final String statName, final int value)
{
if (_stats.containsKey(statName))
return false;
final NautHashMap<String, NautHashMap<Integer, Integer>> uploadQueue = new NautHashMap<String, NautHashMap<Integer, Integer>>();
uploadQueue.put(uuidString, new NautHashMap<Integer, Integer>());
uploadQueue.get(uuidString).put(_stats.get(statName), value);
runAsync(new Runnable()
{
public void run()
{
_repository.saveStats(uploadQueue);
}
});
return true;
}
@Override
protected PlayerStats AddPlayer(String player)
{
return new PlayerStats();
}
public PlayerStats getOfflinePlayerStats(String playerName) throws SQLException
{
return _repository.loadOfflinePlayerStats(playerName);
}
@Override
public void AddCommands()

View File

@ -152,7 +152,7 @@ public class ServerStatusManager extends MiniPlugin
int tps = (int) _lagMeter.getTicksPerSecond();
String address = Bukkit.getServer().getIp().isEmpty() ? "localhost" : Bukkit.getServer().getIp();
int port = _plugin.getServer().getPort();
String group = _plugin.getConfig().getString("serverstatus.group");
String group = _plugin.getConfig().getString("serverstatus.group") + "";
int ram = (int) ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().freeMemory()) / 1048576);
int maxRam = (int) (Runtime.getRuntime().maxMemory() / 1048576);

View File

@ -257,7 +257,7 @@ public class ServerGroup
for (MinecraftServer server : servers)
{
if (_name.equalsIgnoreCase(server.getGroup()))
if (_name.equalsIgnoreCase(server.getGroup().replace(".0", "")))
{
_servers.add(server);
}

View File

@ -47,7 +47,7 @@ public class ServerMonitor
private static Region _region;
private static boolean _debug = false;
public static void main (String args[])
{
_region = !new File("eu.dat").exists() ? Region.US : Region.EU;
@ -112,6 +112,9 @@ public class ServerMonitor
}
}
double totalCPU = 0.0;
double totalRAM = 0.0;
if (_count % 15 == 0)
{
_badServers.clear();
@ -123,6 +126,11 @@ public class ServerMonitor
log("------=[OFFLINE]=------=[" + serverData.getName() + ":" + serverData.getPublicAddress() + "]=------=[OFFLINE]=------");
_badServers.put(serverData.getName(), true);
}
else
{
totalCPU += serverData.getAvailableCpu();
totalRAM += serverData.getAvailableRam();
}
}
log(_badServers.size() + " bad servers.");
@ -155,6 +163,14 @@ public class ServerMonitor
serverMap.put(serverNum, server);
}
}
/*
if (groupStatus.getHost() == null || groupStatus.getHost().isEmpty())
{
int serverCount = groupStatus.getServers().size();
log(groupStatus.getName() + " : " + groupStatus.getPlayerCount() + " players on " + serverCount + " servers " + String.format("%.2f", ((double)serverCount * (double)groupStatus.getRequiredCpu() / totalCPU)) + "% CPU," + String.format("%.2f", ((double)serverCount * (double)groupStatus.getRequiredRam() / totalRAM)) + "% RAM", true);
}
*/
}
HashSet<String> onlineServers = new HashSet<String>();
@ -418,7 +434,7 @@ public class ServerMonitor
boolean success = false;
if (_debug)
return true;
return false;
Process process = null;
String cmd = "/home/mineplex/isServerOnline.sh";
@ -515,8 +531,15 @@ public class ServerMonitor
}
private static void log(String message)
{
log(message, false);
}
private static void log(String message, boolean fileOnly)
{
_logger.info("[" + _dateFormat.format(new Date()) + "] " + message);
System.out.println("[" + _dateFormat.format(new Date()) + "] " + message);
if (!fileOnly)
System.out.println("[" + _dateFormat.format(new Date()) + "] " + message);
}
}