Tweaked ServerMonitor to keep track of servers started/restarted and to handle us/eu in same build.

This commit is contained in:
Jonathan Williams 2014-04-26 23:26:56 -07:00
parent 0e72d1c1eb
commit 62740b6f5b
2 changed files with 142 additions and 73 deletions

View File

@ -16,20 +16,23 @@ public class Repository
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/ServerStatus"; private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/ServerStatus";
private String _userName = "root"; private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh"; private String _password = "tAbechAk3wR7tuTh";
private boolean _us = true;
private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ServerStatus (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), serverGroup VARCHAR(256), address VARCHAR(256), port VARCHAR(11), updated LONG, lastTimeWithPlayers LONG, motd VARCHAR(256), players INT, maxPlayers INT, tps INT, ram INT, maxRam INT, PRIMARY KEY (id));"; private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ServerStatus (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), serverGroup VARCHAR(256), address VARCHAR(256), port VARCHAR(11), updated LONG, lastTimeWithPlayers LONG, motd VARCHAR(256), players INT, maxPlayers INT, tps INT, ram INT, maxRam INT, PRIMARY KEY (id));";
private static String RETRIEVE_OLD_SERVER_STATUSES = "SELECT ServerStatus.serverName, DynamicServers.address, ServerStatus.address, ServerStatus.port, motd, players, maxPlayers, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = false;"; private static String RETRIEVE_OLD_SERVER_STATUSES = "SELECT ServerStatus.serverName, DynamicServers.address, ServerStatus.address, ServerStatus.port, motd, players, maxPlayers, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = ?;";
private static String CREATE_DYNAMIC_TABLE = "CREATE TABLE IF NOT EXISTS DynamicServers (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), address VARCHAR(256), privateAddress VARCHAR(256), US BOOLEAN NOT NULL DEFAULT 'true', PRIMARY KEY (id));"; private static String CREATE_DYNAMIC_TABLE = "CREATE TABLE IF NOT EXISTS DynamicServers (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), address VARCHAR(256), privateAddress VARCHAR(256), US BOOLEAN NOT NULL DEFAULT 'true', PRIMARY KEY (id));";
private static String RETRIEVE_AVAILABLE_SERVERS = "SELECT DynamicServers.serverName, DynamicServers.address, DynamicServers.privateAddress, DynamicServers.US, DynamicServers.availableCpu, DynamicServers.availableRam, ServerStatus.serverGroup, COUNT(*) As serverCount FROM DynamicServers LEFT JOIN ServerStatus ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = false GROUP BY DynamicServers.address, ServerStatus.serverGroup;"; private static String RETRIEVE_AVAILABLE_SERVERS = "SELECT DynamicServers.serverName, DynamicServers.address, DynamicServers.privateAddress, DynamicServers.US, DynamicServers.availableCpu, DynamicServers.availableRam, ServerStatus.serverGroup, COUNT(*) As serverCount FROM DynamicServers LEFT JOIN ServerStatus ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = ? GROUP BY DynamicServers.address, ServerStatus.serverGroup;";
private static String RETRIEVE_SERVERGROUP_STATUSES = "SELECT ServerStatus.serverName, serverGroup, motd, DynamicServers.address, ServerStatus.address, ServerStatus.port, players, maxPlayers, case when TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.lastTimeWithPlayers)) > 300 then 1 else 0 end as empty, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = false"; private static String RETRIEVE_SERVERGROUP_STATUSES = "SELECT ServerStatus.serverName, serverGroup, motd, DynamicServers.address, ServerStatus.address, ServerStatus.port, players, maxPlayers, case when TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.lastTimeWithPlayers)) > 300 then 1 else 0 end as empty, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = ?";
private static String RETRIEVE_SERVER_GROUP_DATA = "SELECT groupName, prefix, scriptName, requiredRam, cpuRequired, requiredTotal, requiredJoinable FROM ServerGroups;"; private static String RETRIEVE_SERVER_GROUP_DATA = "SELECT groupName, prefix, scriptName, requiredRam, cpuRequired, requiredTotal, requiredJoinable FROM ServerGroups;";
private static String DELETE_SERVER_STATUS = "DELETE FROM ServerStatus WHERE address = ? AND port = ? AND serverName = ?;"; private static String DELETE_SERVER_STATUS = "DELETE FROM ServerStatus WHERE address = ? AND port = ? AND serverName = ?;";
public static Connection connection; public static Connection connection;
public void initialize() public void initialize(boolean us)
{ {
_us = us;
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try try
@ -105,6 +108,8 @@ public class Repository
connection = DriverManager.getConnection(_connectionString, _userName, _password); connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(RETRIEVE_OLD_SERVER_STATUSES); preparedStatement = connection.prepareStatement(RETRIEVE_OLD_SERVER_STATUSES);
preparedStatement.setBoolean(1, _us);
resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
while (resultSet.next()) while (resultSet.next())
@ -174,6 +179,8 @@ public class Repository
connection = DriverManager.getConnection(_connectionString, _userName, _password); connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(RETRIEVE_SERVERGROUP_STATUSES); preparedStatement = connection.prepareStatement(RETRIEVE_SERVERGROUP_STATUSES);
preparedStatement.setBoolean(1, _us);
resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
while (resultSet.next()) while (resultSet.next())
@ -271,6 +278,8 @@ public class Repository
resultSet.close(); resultSet.close();
preparedStatement = connection.prepareStatement(RETRIEVE_AVAILABLE_SERVERS); preparedStatement = connection.prepareStatement(RETRIEVE_AVAILABLE_SERVERS);
preparedStatement.setBoolean(1, _us);
resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
while (resultSet.next()) while (resultSet.next())

View File

@ -1,7 +1,9 @@
package mineplex.servermonitor; package mineplex.servermonitor;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.AbstractMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -9,6 +11,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
public class ServerMonitor public class ServerMonitor
{ {
@ -18,92 +21,149 @@ public class ServerMonitor
private static HashMap<String, Boolean> _badServers = new HashMap<String, Boolean>(); private static HashMap<String, Boolean> _badServers = new HashMap<String, Boolean>();
public static void main (String args[]) public static void main (String args[])
{ {
_repository.initialize(); boolean us = !new File("eu.dat").exists();
_repository.initialize(us);
HashMap<String, Entry<String, Long>> serverTracker = new HashMap<String, Entry<String, Long>>();
while (true) while (true)
{ {
for (ServerStatusData statusData : _repository.retrieveOldServerStatuses())
{
System.out.println("----Old Server Status----> " + statusData.Address + ", " + statusData.Name);
killServer(statusData);
//restartServer(statusData);
}
List<DynamicServerData> dynamicServers = new ArrayList<DynamicServerData>(_repository.retrieveDynamicServers());
if (_count % 15 == 0)
{
_badServers.clear();
for (DynamicServerData serverData : dynamicServers)
{
if (isServerOffline(serverData))
{
System.out.println("------=[OFFLINE]=------=[" + serverData.Name + ":" + serverData.Address + "]=------=[OFFLINE]=------");
_badServers.put(serverData.Name, true);
}
}
System.out.println(_badServers.size() + " bad servers.");
}
for (Iterator<DynamicServerData> iterator = dynamicServers.iterator(); iterator.hasNext();)
{
DynamicServerData serverData = iterator.next();
if (_badServers.containsKey(serverData.Name))
iterator.remove();
}
Collection<ServerGroupData> serverGroups = _repository.retrieveServerGroups(); Collection<ServerGroupData> serverGroups = _repository.retrieveServerGroups();
HashMap<String, GroupStatusData> groupStatusList = _repository.retrieveGroupStatusData(); HashMap<String, GroupStatusData> groupStatusList = _repository.retrieveGroupStatusData();
for (GroupStatusData groupStatus : groupStatusList.values()) for (ServerStatusData statusData : _repository.retrieveOldServerStatuses())
{ {
for (ServerStatusData serverToKill : groupStatus.KillServers) if (us)
{ {
System.out.println("----DUPLICATE SERVER----> " + serverToKill.Address + ", " + serverToKill.Name); if (!serverTracker.containsKey(statusData.Name))
killServer(serverToKill); {
restartServer(statusData);
serverTracker.put(statusData.Name, new AbstractMap.SimpleEntry<String, Long>(statusData.Address, System.currentTimeMillis()));
}
else if (System.currentTimeMillis() - serverTracker.get(statusData.Name).getValue() > 15000)
{
serverTracker.remove(statusData.Name);
System.out.println("-=[SERVER RESTART TOO SLOW]=- " + statusData.Name + " (Putting back in restart queue)");
}
} }
else
killServer(statusData);
} }
for (ServerGroupData serverGroup : serverGroups) if (us)
{ {
if (!groupStatusList.containsKey(serverGroup.Name)) // Remove successfully restarted US servers
for (GroupStatusData groupStatus : groupStatusList.values())
{ {
groupStatusList.put(serverGroup.Name, new GroupStatusData()); for (ServerStatusData serverToKill : groupStatus.Servers.values())
}
GroupStatusData groupStatus = groupStatusList.get(serverGroup.Name);
int serversToAdd = Math.max(serverGroup.RequiredTotalServers - groupStatus.getTotalServers(), serverGroup.RequiredJoinableServers - groupStatus.getJoinableCount());
int serversToKill = (groupStatus.getTotalServers() > serverGroup.RequiredTotalServers && groupStatus.getJoinableCount() > serverGroup.RequiredJoinableServers) ? Math.min(groupStatus.getJoinableCount() - serverGroup.RequiredJoinableServers, groupStatus.EmptyServers.size()) : 0;
while (serversToAdd > 0)
{
int serverNum = groupStatus.getNextServerNumber();
Collections.sort(dynamicServers, new DynamicServerSorter());
DynamicServerData bestServer = getBestDynamicServer(dynamicServers, serverGroup);
if (bestServer == null)
{ {
System.out.println("No best dynamic server available for group " + serverGroup.Name); if (serverTracker.containsKey(serverToKill.Name))
break; serverTracker.remove(serverToKill.Name);
} }
}
System.out.println("[" + bestServer.Name + ":" + bestServer.Address + "] Adding " + serverGroup.Name + " Req Total: " + serverGroup.RequiredTotalServers + " Req Joinable: " + serverGroup.RequiredJoinableServers + " | Actual Total: " + groupStatus.getTotalServers() + " Actual Joinable: " + groupStatus.getJoinableCount()); }
else
startServer(bestServer, serverGroup, serverNum); {
List<DynamicServerData> dynamicServers = new ArrayList<DynamicServerData>(_repository.retrieveDynamicServers());
if (_count % 15 == 0)
{
_badServers.clear();
serversToAdd--; for (DynamicServerData serverData : dynamicServers)
{
if (isServerOffline(serverData))
{
System.out.println("------=[OFFLINE]=------=[" + serverData.Name + ":" + serverData.Address + "]=------=[OFFLINE]=------");
_badServers.put(serverData.Name, true);
}
}
System.out.println(_badServers.size() + " bad servers.");
} }
while (serversToKill > 0) for (Iterator<DynamicServerData> iterator = dynamicServers.iterator(); iterator.hasNext();)
{ {
System.out.println("[" + groupStatus.EmptyServers.get(0).Name + ":" + groupStatus.EmptyServers.get(0).Address + "] Killing " + serverGroup.Name + " Req Total: " + serverGroup.RequiredTotalServers + " Req Joinable: " + serverGroup.RequiredJoinableServers + " | Actual Total: " + groupStatus.getTotalServers() + " Actual Joinable: " + groupStatus.getJoinableCount()); DynamicServerData serverData = iterator.next();
killServer(groupStatus.EmptyServers.remove(0));
serversToKill--; if (_badServers.containsKey(serverData.Name))
iterator.remove();
}
for (GroupStatusData groupStatus : groupStatusList.values())
{
for (ServerStatusData serverToKill : groupStatus.KillServers)
{
System.out.println("----DUPLICATE SERVER----> " + serverToKill.Address + ", " + serverToKill.Name);
killServer(serverToKill);
}
for (ServerStatusData serverToKill : groupStatus.Servers.values())
{
if (serverTracker.containsKey(serverToKill.Name))
serverTracker.remove(serverToKill.Name);
}
}
for (Iterator<Entry<String, Entry<String, Long>>> iterator = serverTracker.entrySet().iterator(); iterator.hasNext();)
{
Entry<String, Entry<String, Long>> entry = iterator.next();
if (System.currentTimeMillis() - entry.getValue().getValue() > 15000)
{
System.out.println("-=[SERVER STARTUP TOO SLOW]=- " + entry.getKey());
ServerStatusData serverToKill = new ServerStatusData();
serverToKill.Name = entry.getKey();
serverToKill.Address = entry.getValue().getKey();
killServer(serverToKill);
iterator.remove();
}
}
for (ServerGroupData serverGroup : serverGroups)
{
if (!groupStatusList.containsKey(serverGroup.Name))
{
groupStatusList.put(serverGroup.Name, new GroupStatusData());
}
GroupStatusData groupStatus = groupStatusList.get(serverGroup.Name);
int serversToAdd = Math.max(serverGroup.RequiredTotalServers - groupStatus.getTotalServers(), serverGroup.RequiredJoinableServers - groupStatus.getJoinableCount());
int serversToKill = (groupStatus.getTotalServers() > serverGroup.RequiredTotalServers && groupStatus.getJoinableCount() > serverGroup.RequiredJoinableServers) ? Math.min(groupStatus.getJoinableCount() - serverGroup.RequiredJoinableServers, groupStatus.EmptyServers.size()) : 0;
while (serversToAdd > 0)
{
int serverNum = groupStatus.getNextServerNumber();
Collections.sort(dynamicServers, new DynamicServerSorter());
DynamicServerData bestServer = getBestDynamicServer(dynamicServers, serverGroup);
if (bestServer == null)
{
System.out.println("No best dynamic server available for group " + serverGroup.Name);
break;
}
if (serverTracker.containsKey(serverGroup.Prefix + "-" + serverNum))
System.out.println("[WAITING] On " + serverGroup.Prefix + "-" + serverNum + " to finish starting...");
else
{
startServer(bestServer, serverGroup, serverNum);
serverTracker.put(serverGroup.Prefix + "-" + serverNum, new AbstractMap.SimpleEntry<String, Long>(bestServer.Address, System.currentTimeMillis()));
}
serversToAdd--;
}
while (serversToKill > 0)
{
System.out.println("[" + groupStatus.EmptyServers.get(0).Name + ":" + groupStatus.EmptyServers.get(0).Address + "] Killing " + serverGroup.Name + " Req Total: " + serverGroup.RequiredTotalServers + " Req Joinable: " + serverGroup.RequiredJoinableServers + " | Actual Total: " + groupStatus.getTotalServers() + " Actual Joinable: " + groupStatus.getJoinableCount());
killServer(groupStatus.EmptyServers.remove(0));
serversToKill--;
}
} }
} }