Added try catch in BungeeRotator and ServerMonitor so they don't crash out.

This commit is contained in:
Jonathan Williams 2015-02-14 00:47:03 -08:00
parent 6383cf7ce6
commit beac0b4828
2 changed files with 374 additions and 342 deletions

View File

@ -35,148 +35,164 @@ public class BungeeRotator
while (true)
{
List<BungeeServer> bungeeServers = _repository.getBungeeServers();
HashSet<String> usServers = new HashSet<String>();
HashSet<String> euServers = new HashSet<String>();
Collections.sort(bungeeServers, bungeeSorter);
for (BungeeServer server : bungeeServers)
{
if (usServers.size() < maxRecordCount && server.US)
{
if (usServers.size() >= 2 && server.Players > 900)
continue;
System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers);
usServers.add(server.Address);
}
else if (euServers.size() < maxRecordCount && !server.US)
{
if (euServers.size() >= 2 && server.Players > 900)
continue;
System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers);
euServers.add(server.Address);
}
}
DomainRecords records = new ApiGetCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728,
"/records", "").Execute(DomainRecords.class);
List<DnsRecord> recordsToDelete = new ArrayList<DnsRecord>();
List<DnsRecord> recordsToAdd = new ArrayList<DnsRecord>();
for (DnsRecord record : records.data)
{
if (record.type.equalsIgnoreCase("A"))
{
if (record.name.equalsIgnoreCase("us"))
{
if (usServers.contains(record.value))
usServers.remove(record.value);
else
recordsToDelete.add(record);
}
else if (record.name.equalsIgnoreCase("eu"))
{
if (euServers.contains(record.value))
euServers.remove(record.value);
else
recordsToDelete.add(record);
}
}
}
for (String address : usServers)
{
recordsToAdd.add(new ARecord("us", address, 300));
System.out.println("Addding server address in DNS : " + "us " + address);
}
for (String address : euServers)
{
recordsToAdd.add(new ARecord("eu", address, 300));
System.out.println("Addding server address in DNS : " + "eu " + address);
}
if (recordsToAdd.size() > 0)
{
new ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "createMulti").Execute(recordsToAdd);
System.out.println("Created " + recordsToAdd.size() + " records.");
}
if (recordsToDelete.size() > 0)
{
StringBuilder idBuilder = new StringBuilder();
for (DnsRecord record : recordsToDelete)
{
if (idBuilder.length() != 0)
idBuilder.append("&");
idBuilder.append("ids=" + record.id);
}
new ApiDeleteCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records?" + idBuilder.toString()).Execute();
System.out.println("Deleted " + recordsToDelete.size() + " records.");
}
int processWaits = 0;
while (_processes.size() > 0)
{
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
ProcessRunner pr = iterator.next();
try
{
pr.join(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if (pr.isDone())
iterator.remove();
}
if (_processes.size() > 0)
{
try
{
Thread.sleep(6000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
if (processWaits >= 60)
{
System.out.println("Killing stale processes.");
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
iterator.next().abort();
iterator.remove();
}
}
processWaits++;
}
processWaits = 0;
try
{
Thread.sleep(30000);
List<BungeeServer> bungeeServers = _repository.getBungeeServers();
HashSet<String> usServers = new HashSet<String>();
HashSet<String> euServers = new HashSet<String>();
Collections.sort(bungeeServers, bungeeSorter);
for (BungeeServer server : bungeeServers)
{
if (usServers.size() < maxRecordCount && server.US)
{
if (usServers.size() >= 2 && server.Players > 900)
continue;
System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers);
usServers.add(server.Address);
}
else if (euServers.size() < maxRecordCount && !server.US)
{
if (euServers.size() >= 2 && server.Players > 900)
continue;
System.out.println("SELECTED " + server.Address + " " + (server.US ? "us" : "eu") + " " + server.Players + "/" + server.MaxPlayers);
euServers.add(server.Address);
}
}
DomainRecords records = new ApiGetCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728,
"/records", "").Execute(DomainRecords.class);
List<DnsRecord> recordsToDelete = new ArrayList<DnsRecord>();
List<DnsRecord> recordsToAdd = new ArrayList<DnsRecord>();
for (DnsRecord record : records.data)
{
if (record.type.equalsIgnoreCase("A"))
{
if (record.name.equalsIgnoreCase("us"))
{
if (usServers.contains(record.value))
usServers.remove(record.value);
else
recordsToDelete.add(record);
}
else if (record.name.equalsIgnoreCase("eu"))
{
if (euServers.contains(record.value))
euServers.remove(record.value);
else
recordsToDelete.add(record);
}
}
}
for (String address : usServers)
{
recordsToAdd.add(new ARecord("us", address, 300));
System.out.println("Addding server address in DNS : " + "us " + address);
}
for (String address : euServers)
{
recordsToAdd.add(new ARecord("eu", address, 300));
System.out.println("Addding server address in DNS : " + "eu " + address);
}
if (recordsToAdd.size() > 0)
{
new ApiPostCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records/", "createMulti").Execute(recordsToAdd);
System.out.println("Created " + recordsToAdd.size() + " records.");
}
if (recordsToDelete.size() > 0)
{
StringBuilder idBuilder = new StringBuilder();
for (DnsRecord record : recordsToDelete)
{
if (idBuilder.length() != 0)
idBuilder.append("&");
idBuilder.append("ids=" + record.id);
}
new ApiDeleteCall("https://api.dnsmadeeasy.com/V2.0/dns/managed/", 962728, "/records?" + idBuilder.toString()).Execute();
System.out.println("Deleted " + recordsToDelete.size() + " records.");
}
int processWaits = 0;
while (_processes.size() > 0)
{
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
ProcessRunner pr = iterator.next();
try
{
pr.join(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if (pr.isDone())
iterator.remove();
}
if (_processes.size() > 0)
{
try
{
Thread.sleep(6000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
if (processWaits >= 60)
{
System.out.println("Killing stale processes.");
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
iterator.next().abort();
iterator.remove();
}
}
processWaits++;
}
processWaits = 0;
try
{
Thread.sleep(30000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
catch (InterruptedException e)
catch (Exception ex)
{
e.printStackTrace();
System.out.println("Error doing something : " + ex.getMessage());
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

View File

@ -89,247 +89,263 @@ public class ServerMonitor
while (true)
{
_totalPlayers = 0;
_serverStatuses = _repository.getServerStatuses();
_serverGroups = _repository.getServerGroups(_serverStatuses);
_serverGroupMap = new HashMap<String, ServerGroup>();
_dedicatedServers = new ArrayList<DedicatedServer>(_repository.getDedicatedServers());
calculateTotalPlayers();
killDeadServers();
double totalCPU = 0.0;
double totalRAM = 0.0;
double availableCPU = 0.0;
double availableRAM = 0.0;
for (DedicatedServer server : _dedicatedServers)
try
{
totalCPU += server.getAvailableCpu();
totalRAM += server.getAvailableRam();
}
for (MinecraftServer minecraftServer : _serverStatuses)
{
if (minecraftServer.getMotd().contains("Finished") || (minecraftServer.getGroup().equalsIgnoreCase("UltraHardcore") && minecraftServer.getMotd().contains("Restarting") && minecraftServer.getPlayerCount() == 0))
{
killServer(minecraftServer.getName(), minecraftServer.getPublicAddress(), minecraftServer.getPlayerCount(), "[KILLED] [FINISHED] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress(), true);
handleUserServerGroup(_serverGroupMap.get(minecraftServer.getGroup()));
continue;
}
_totalPlayers = 0;
_serverStatuses = _repository.getServerStatuses();
_serverGroups = _repository.getServerGroups(_serverStatuses);
_serverGroupMap = new HashMap<String, ServerGroup>();
_dedicatedServers = new ArrayList<DedicatedServer>(_repository.getDedicatedServers());
calculateTotalPlayers();
killDeadServers();
double totalCPU = 0.0;
double totalRAM = 0.0;
double availableCPU = 0.0;
double availableRAM = 0.0;
for (DedicatedServer server : _dedicatedServers)
{
if (_serverGroupMap.containsKey(minecraftServer.getGroup()) && minecraftServer.getPublicAddress().equalsIgnoreCase(server.getPrivateAddress()))
{
ServerGroup serverGroup = _serverGroupMap.get(minecraftServer.getGroup());
server.incrementServerCount(serverGroup);
}
}
}
if (_count % 15 == 0)
{
_badServers.clear();
for (DedicatedServer serverData : _dedicatedServers)
{
if (isServerOffline(serverData))
{
log("------=[OFFLINE]=------=[" + serverData.getName() + ":" + serverData.getPublicAddress() + "]=------=[OFFLINE]=------");
_badServers.put(serverData.getName(), true);
}
totalCPU += server.getAvailableCpu();
totalRAM += server.getAvailableRam();
}
log(_badServers.size() + " bad servers.");
}
for (Iterator<DedicatedServer> iterator = _dedicatedServers.iterator(); iterator.hasNext();)
{
DedicatedServer serverData = iterator.next();
if (_badServers.containsKey(serverData.getName()))
iterator.remove();
else
for (MinecraftServer minecraftServer : _serverStatuses)
{
availableCPU += serverData.getAvailableCpu();
availableRAM += serverData.getAvailableRam();
}
}
double usedCpuPercent = Math.round((1 - availableCPU / totalCPU) * 10000.0) / 100.0;
double usedRamPercent = Math.round((1 - availableRAM / totalRAM) * 10000.0) / 100.0;
log("Using " + usedCpuPercent + "% of available CPU (" + availableCPU + " Free) and " + usedRamPercent + "% of available RAM (" + availableRAM / 1000 + "GB Free)");
_historyRepository.saveDedicatedServerStats(_dedicatedServers);
log("Saved Dedicated Server Stats.");
_historyRepository.saveServerGroupStats((int)totalCPU, (int)totalRAM, _serverGroupMap.values());
log("Saved ServerGroup Stats.");
_historyRepository.saveNetworkStats(usedCpuPercent, usedRamPercent, availableCPU, availableRAM, _region);
log("Saved Network Stats.");
for (ServerGroup groupStatus : _serverGroups)
{
NautHashMap<Integer, MinecraftServer> serverMap = new NautHashMap<Integer, MinecraftServer>();
for (Iterator<MinecraftServer> serverIterator = groupStatus.getServers().iterator(); serverIterator.hasNext();)
{
try
if (minecraftServer.getMotd().contains("Finished") || (minecraftServer.getGroup().equalsIgnoreCase("UltraHardcore") && minecraftServer.getMotd().contains("Restarting") && minecraftServer.getPlayerCount() == 0))
{
MinecraftServer server = serverIterator.next();
int serverNum = Integer.parseInt(server.getName().split("-")[1]);
killServer(minecraftServer.getName(), minecraftServer.getPublicAddress(), minecraftServer.getPlayerCount(), "[KILLED] [FINISHED] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress(), true);
if (serverMap.containsKey(serverNum))
handleUserServerGroup(_serverGroupMap.get(minecraftServer.getGroup()));
continue;
}
for (DedicatedServer server : _dedicatedServers)
{
if (_serverGroupMap.containsKey(minecraftServer.getGroup()) && minecraftServer.getPublicAddress().equalsIgnoreCase(server.getPrivateAddress()))
{
killServer(server.getName(), server.getPublicAddress(), server.getPlayerCount(), "[KILLED] [DUPLICATE] " + server.getName() + ":" + server.getPublicAddress(), true);
serverIterator.remove();
ServerGroup serverGroup = _serverGroupMap.get(minecraftServer.getGroup());
server.incrementServerCount(serverGroup);
}
}
}
if (_count % 15 == 0)
{
_badServers.clear();
for (DedicatedServer serverData : _dedicatedServers)
{
if (isServerOffline(serverData))
{
log("------=[OFFLINE]=------=[" + serverData.getName() + ":" + serverData.getPublicAddress() + "]=------=[OFFLINE]=------");
_badServers.put(serverData.getName(), true);
}
}
log(_badServers.size() + " bad servers.");
}
for (Iterator<DedicatedServer> iterator = _dedicatedServers.iterator(); iterator.hasNext();)
{
DedicatedServer serverData = iterator.next();
if (_badServers.containsKey(serverData.getName()))
iterator.remove();
else
{
availableCPU += serverData.getAvailableCpu();
availableRAM += serverData.getAvailableRam();
}
}
double usedCpuPercent = Math.round((1 - availableCPU / totalCPU) * 10000.0) / 100.0;
double usedRamPercent = Math.round((1 - availableRAM / totalRAM) * 10000.0) / 100.0;
log("Using " + usedCpuPercent + "% of available CPU (" + availableCPU + " Free) and " + usedRamPercent + "% of available RAM (" + availableRAM / 1000 + "GB Free)");
_historyRepository.saveDedicatedServerStats(_dedicatedServers);
log("Saved Dedicated Server Stats.");
_historyRepository.saveServerGroupStats((int)totalCPU, (int)totalRAM, _serverGroupMap.values());
log("Saved ServerGroup Stats.");
_historyRepository.saveNetworkStats(usedCpuPercent, usedRamPercent, availableCPU, availableRAM, _region);
log("Saved Network Stats.");
for (ServerGroup groupStatus : _serverGroups)
{
NautHashMap<Integer, MinecraftServer> serverMap = new NautHashMap<Integer, MinecraftServer>();
for (Iterator<MinecraftServer> serverIterator = groupStatus.getServers().iterator(); serverIterator.hasNext();)
{
try
{
MinecraftServer server = serverIterator.next();
int serverNum = Integer.parseInt(server.getName().split("-")[1]);
if (serverMap.containsKey(serverNum))
{
killServer(server.getName(), server.getPublicAddress(), server.getPlayerCount(), "[KILLED] [DUPLICATE] " + server.getName() + ":" + server.getPublicAddress(), true);
serverIterator.remove();
}
else
{
serverMap.put(serverNum, server);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
/*
if (groupStatus.getHost() == null || groupStatus.getHost().isEmpty())
{
if (groupStatus.getName().startsWith("0"))
{
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", false);
}
}
*/
}
HashSet<String> onlineServers = new HashSet<String>();
HashSet<String> laggyServers = new HashSet<String>();
laggyServers.addAll(_laggyServers);
_laggyServers.clear();
for (MinecraftServer minecraftServer : _serverStatuses)
{
onlineServers.add(minecraftServer.getName());
if (minecraftServer.getTps() <= 17)
{
if (minecraftServer.getTps() <= 10)
{
if (laggyServers.contains(minecraftServer.getName()))
{
new RestartCommand(minecraftServer.getName(), _region).publish();
log("[RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress());
}
else
{
_laggyServers.add(minecraftServer.getName());
log("[IMPENDING RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress());
}
}
else
{
serverMap.put(serverNum, server);
}
log("[Performance] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress() + "] Running poorly at " + minecraftServer.getTps() + " TPS");
}
}
for (Iterator<Entry<String, Entry<String, Long>>> iterator = serverTracker.entrySet().iterator(); iterator.hasNext();)
{
Entry<String, Entry<String, Long>> entry = iterator.next();
if (onlineServers.contains(entry.getKey()))
iterator.remove();
else if (System.currentTimeMillis() - entry.getValue().getValue() > 35000)
{
String serverName = entry.getKey();
String serverAddress = entry.getValue().getKey();
killServer(serverName, serverAddress, 0, "[KILLED] [SLOW-STARTUP] " + serverName + ":" + serverAddress, true);
iterator.remove();
}
}
for (ServerGroup serverGroup : _serverGroups)
{
if (serverGroup.getName().equals("Testing"))
continue;
try
{
handleGroupChanges(serverTracker, serverGroup, false);
}
catch (Exception exception)
{
exception.printStackTrace();
log(exception.getMessage());
log("Can't handle group changes for " + serverGroup.getName());
}
}
/*
if (groupStatus.getHost() == null || groupStatus.getHost().isEmpty())
{
if (groupStatus.getName().startsWith("0"))
{
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", false);
}
}
*/
}
HashSet<String> onlineServers = new HashSet<String>();
HashSet<String> laggyServers = new HashSet<String>();
laggyServers.addAll(_laggyServers);
_laggyServers.clear();
for (MinecraftServer minecraftServer : _serverStatuses)
{
onlineServers.add(minecraftServer.getName());
int processWaits = 0;
if (minecraftServer.getTps() <= 17)
while (_processes.size() > 0)
{
if (minecraftServer.getTps() <= 10)
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
if (laggyServers.contains(minecraftServer.getName()))
ProcessRunner pr = iterator.next();
try
{
new RestartCommand(minecraftServer.getName(), _region).publish();
log("[RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress());
pr.join(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
else
if (pr.isDone())
iterator.remove();
}
if (_processes.size() > 0)
{
try
{
_laggyServers.add(minecraftServer.getName());
log("[IMPENDING RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress());
log("Sleeping while processes run...");
Thread.sleep(6000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
log("[Performance] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress() + "] Running poorly at " + minecraftServer.getTps() + " TPS");
if (processWaits >= 10)
{
log("Killing stale processes.");
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
iterator.next().abort();
iterator.remove();
}
}
processWaits++;
}
}
for (Iterator<Entry<String, Entry<String, Long>>> iterator = serverTracker.entrySet().iterator(); iterator.hasNext();)
{
Entry<String, Entry<String, Long>> entry = iterator.next();
if (onlineServers.contains(entry.getKey()))
iterator.remove();
else if (System.currentTimeMillis() - entry.getValue().getValue() > 35000)
{
String serverName = entry.getKey();
String serverAddress = entry.getValue().getKey();
killServer(serverName, serverAddress, 0, "[KILLED] [SLOW-STARTUP] " + serverName + ":" + serverAddress, true);
iterator.remove();
}
}
for (ServerGroup serverGroup : _serverGroups)
{
if (serverGroup.getName().equals("Testing"))
continue;
processWaits = 0;
try
{
handleGroupChanges(serverTracker, serverGroup, false);
}
catch (Exception exception)
log("Natural sleep.");
Thread.sleep(10000);
}
catch (InterruptedException e)
{
log(exception.getMessage());
log("Can't handle group changes for " + serverGroup.getName());
}
}
int processWaits = 0;
while (_processes.size() > 0)
{
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
ProcessRunner pr = iterator.next();
try
{
pr.join(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if (pr.isDone())
iterator.remove();
e.printStackTrace();
}
if (_processes.size() > 0)
{
try
{
log("Sleeping while processes run...");
Thread.sleep(6000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
if (processWaits >= 10)
{
log("Killing stale processes.");
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
{
iterator.next().abort();
iterator.remove();
}
}
processWaits++;
_count++;
}
processWaits = 0;
try
catch (Exception ex)
{
log("Natural sleep.");
Thread.sleep(10000);
}
catch (InterruptedException e)
{
e.printStackTrace();
log("Error doing something : " + ex.getMessage());
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
_count++;
}
}