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 _userName = "root";
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 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 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_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_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 = ?";
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 = ?;";
public static Connection connection;
public void initialize()
public void initialize(boolean us)
{
_us = us;
PreparedStatement preparedStatement = null;
try
@ -105,6 +108,8 @@ public class Repository
connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(RETRIEVE_OLD_SERVER_STATUSES);
preparedStatement.setBoolean(1, _us);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
@ -174,6 +179,8 @@ public class Repository
connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(RETRIEVE_SERVERGROUP_STATUSES);
preparedStatement.setBoolean(1, _us);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
@ -271,6 +278,8 @@ public class Repository
resultSet.close();
preparedStatement = connection.prepareStatement(RETRIEVE_AVAILABLE_SERVERS);
preparedStatement.setBoolean(1, _us);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())

View File

@ -1,7 +1,9 @@
package mineplex.servermonitor;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -9,6 +11,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
public class ServerMonitor
{
@ -19,17 +22,49 @@ public class ServerMonitor
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)
{
Collection<ServerGroupData> serverGroups = _repository.retrieveServerGroups();
HashMap<String, GroupStatusData> groupStatusList = _repository.retrieveGroupStatusData();
for (ServerStatusData statusData : _repository.retrieveOldServerStatuses())
{
System.out.println("----Old Server Status----> " + statusData.Address + ", " + statusData.Name);
if (us)
{
if (!serverTracker.containsKey(statusData.Name))
{
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);
//restartServer(statusData);
}
if (us)
{
// Remove successfully restarted US servers
for (GroupStatusData groupStatus : groupStatusList.values())
{
for (ServerStatusData serverToKill : groupStatus.Servers.values())
{
if (serverTracker.containsKey(serverToKill.Name))
serverTracker.remove(serverToKill.Name);
}
}
}
else
{
List<DynamicServerData> dynamicServers = new ArrayList<DynamicServerData>(_repository.retrieveDynamicServers());
if (_count % 15 == 0)
@ -56,9 +91,6 @@ public class ServerMonitor
iterator.remove();
}
Collection<ServerGroupData> serverGroups = _repository.retrieveServerGroups();
HashMap<String, GroupStatusData> groupStatusList = _repository.retrieveGroupStatusData();
for (GroupStatusData groupStatus : groupStatusList.values())
{
for (ServerStatusData serverToKill : groupStatus.KillServers)
@ -66,6 +98,29 @@ public class ServerMonitor
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)
@ -92,9 +147,13 @@ public class ServerMonitor
break;
}
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());
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--;
}
@ -106,6 +165,7 @@ public class ServerMonitor
serversToKill--;
}
}
}
int processWaits = 0;