Fixes to DynamicServer Monitor

Fixed SSM disguise clunky movement.
This commit is contained in:
Jonathan Williams 2013-11-08 10:52:56 -08:00
parent 79b739fc14
commit cae10fe7b4
7 changed files with 171 additions and 135 deletions

View File

@ -27,7 +27,7 @@ public class LobbyBalancer implements Listener, Runnable
{ {
_plugin = plugin; _plugin = plugin;
_repository = new LobbyBalancerRepository(); _repository = new LobbyBalancerRepository();
_repository.initialize(true); _repository.initialize(false);
loadLobbyServers(); loadLobbyServers();

View File

@ -74,6 +74,7 @@ public class PlayerCountRepository
throw new SQLException("Creating bungee server failed, no rows affected."); throw new SQLException("Creating bungee server failed, no rows affected.");
} }
resultSet.close();
resultSet = preparedStatementInsert.getGeneratedKeys(); resultSet = preparedStatementInsert.getGeneratedKeys();
if (resultSet.next()) if (resultSet.next())

View File

@ -390,7 +390,7 @@ public class DisguiseManager extends MiniPlugin implements IPacketRunnable
_movePacketMap.put(movePacket.a, velocityPacket); _movePacketMap.put(movePacket.a, velocityPacket);
//XXX packetList.forceAdd(velocityPacket); packetList.forceAdd(velocityPacket);
if (_goingUp.contains(movePacket.a) && movePacket.c != 0 && movePacket.c > 20) if (_goingUp.contains(movePacket.a) && movePacket.c != 0 && movePacket.c > 20)
{ {
@ -398,7 +398,7 @@ public class DisguiseManager extends MiniPlugin implements IPacketRunnable
{ {
public void run() public void run()
{ {
//XXX packetList.forceAdd(velocityPacket); packetList.forceAdd(velocityPacket);
} }
}); });
} }
@ -437,7 +437,7 @@ public class DisguiseManager extends MiniPlugin implements IPacketRunnable
_movePacketMap.put(movePacket.a, velocityPacket); _movePacketMap.put(movePacket.a, velocityPacket);
//XXX packetList.forceAdd(velocityPacket); packetList.forceAdd(velocityPacket);
if (_goingUp.contains(movePacket.a) && movePacket.c != 0 && movePacket.c > 20) if (_goingUp.contains(movePacket.a) && movePacket.c != 0 && movePacket.c > 20)
{ {
@ -445,7 +445,7 @@ public class DisguiseManager extends MiniPlugin implements IPacketRunnable
{ {
public void run() public void run()
{ {
//XXX packetList.forceAdd(velocityPacket); packetList.forceAdd(velocityPacket);
} }
}); });
} }

View File

@ -88,6 +88,8 @@ public class ServerStatusRepository
throw new SQLException("Creating server status failed, no rows affected."); throw new SQLException("Creating server status failed, no rows affected.");
} }
resultSet.close();
resultSet = preparedStatementInsert.getGeneratedKeys(); resultSet = preparedStatementInsert.getGeneratedKeys();
if (resultSet.next()) if (resultSet.next())

View File

@ -1,18 +1,21 @@
package mineplex.servermonitor; package mineplex.servermonitor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class GroupStatusData public class GroupStatusData
{ {
private int _serverNum = 0;
private int _totalCount = 0; private int _totalCount = 0;
private int _joinableCount = 0; private int _joinableCount = 0;
public int Players; public int Players;
public int MaxPlayers; public int MaxPlayers;
public int MaxServerNumber;
public List<ServerStatusData> EmptyServers = new ArrayList<ServerStatusData>(); public List<ServerStatusData> EmptyServers = new ArrayList<ServerStatusData>();
public List<ServerStatusData> Servers = new ArrayList<ServerStatusData>(); public HashMap<Integer, ServerStatusData> Servers = new HashMap<Integer, ServerStatusData>();
public void addServer(ServerStatusData serverStatusData) public void addServer(ServerStatusData serverStatusData)
{ {
@ -43,7 +46,7 @@ public class GroupStatusData
EmptyServers.add(serverStatusData); EmptyServers.add(serverStatusData);
} }
Servers.add(serverStatusData); Servers.put(Integer.parseInt(serverStatusData.Name.split("-")[1]), serverStatusData);
} }
public int getTotalServers() public int getTotalServers()
@ -55,4 +58,19 @@ public class GroupStatusData
{ {
return _joinableCount; return _joinableCount;
} }
public int getNextServerNumber()
{
_serverNum++;
while (true)
{
if (!Servers.containsKey(_serverNum))
{
return _serverNum;
}
_serverNum++;
}
}
} }

View File

@ -17,13 +17,13 @@ public class Repository
private String _password = "y2D4atu3Pene2asw"; private String _password = "y2D4atu3Pene2asw";
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), 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), 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 serverName, address, motd, players, maxPlayers FROM ServerStatus WHERE TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) > 10;"; private static String RETRIEVE_OLD_SERVER_STATUSES = "SELECT ServerStatus.serverName, ServerStatus.address, motd, players, maxPlayers FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address LIKE CONCAT(DynamicServers.address, '%') WHERE DynamicServers.US = false AND TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) > 10;";
private static String CREATE_DYNAMIC_TABLE = "CREATE TABLE IF NOT EXISTS DynamicServers (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), address 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), US BOOLEAN NOT NULL DEFAULT 'true', PRIMARY KEY (id));";
private static String RETRIEVE_AVAILABLE_SERVERS = "SELECT DynamicServers.serverName, DynamicServers.address, DynamicServers.US, ServerStatus.serverGroup, COUNT(*) As serverCount FROM DynamicServers LEFT JOIN ServerStatus ON ServerStatus.address LIKE CONCAT(DynamicServers.address, '%') WHERE DynamicServers.US = false GROUP BY DynamicServers.address, ServerStatus.serverGroup;"; private static String RETRIEVE_AVAILABLE_SERVERS = "SELECT DynamicServers.serverName, DynamicServers.address, DynamicServers.US, ServerStatus.serverGroup, COUNT(*) As serverCount FROM DynamicServers LEFT JOIN ServerStatus ON ServerStatus.address LIKE CONCAT(DynamicServers.address, '%') WHERE DynamicServers.US = false GROUP BY DynamicServers.address, ServerStatus.serverGroup;";
private static String RETRIEVE_SERVERGROUP_STATUSES = "SELECT ServerStatus.serverName, serverGroup, motd, ServerStatus.address, players, maxPlayers, case when TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.lastTimeWithPlayers)) > 300 then 1 else 0 end as empty FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address LIKE CONCAT(DynamicServers.address, '%') WHERE DynamicServers.US = false AND TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) <= 10"; private static String RETRIEVE_SERVERGROUP_STATUSES = "SELECT ServerStatus.serverName, serverGroup, motd, ServerStatus.address, players, maxPlayers, case when TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.lastTimeWithPlayers)) > 300 then 1 else 0 end as empty FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address LIKE CONCAT(DynamicServers.address, '%') WHERE DynamicServers.US = false AND TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) <= 10";
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 serverName = ?;";
public void initialize() public void initialize()
{ {
@ -448,4 +448,50 @@ public class Repository
return serverGroupMap.values(); return serverGroupMap.values();
} }
public void removeServerRecord(ServerStatusData serverToKill)
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(DELETE_SERVER_STATUS);
preparedStatement.setString(1, serverToKill.Address + ":" + serverToKill.Port);
preparedStatement.setString(2, serverToKill.Name);
preparedStatement.execute();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
} }

View File

@ -7,8 +7,6 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
public class ServerMonitor public class ServerMonitor
{ {
@ -21,55 +19,20 @@ public class ServerMonitor
while (true) while (true)
{ {
/* if (_count % 10 == 0)
if (_count % 20 == 0)
{ {
for (ServerStatusData statusData : _repository.retrieveOldServerStatuses()) for (ServerStatusData statusData : _repository.retrieveOldServerStatuses())
{ {
String key = statusData.Address + " " + statusData.Name; System.out.println("----Old Server Status----");
killServer(statusData);
String cmd = "/home/mineplex/restartServer.sh";
Process process = null;
try
{
process = new ProcessBuilder(new String[] {"/bin/sh", "-x", cmd, statusData.Address, statusData.Name}).start();
process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while(line != null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (process != null)
{
process.destroy();
}
} }
System.out.println("Sent restart command to " + key + "");
}
}
*/
if (_count % 10 == 0)
{
List<DynamicServerData> dynamicServers = new ArrayList<DynamicServerData>(_repository.retrieveDynamicServers()); List<DynamicServerData> dynamicServers = new ArrayList<DynamicServerData>(_repository.retrieveDynamicServers());
Collection<ServerGroupData> serverGroups = _repository.retrieveServerGroups(); Collection<ServerGroupData> serverGroups = _repository.retrieveServerGroups();
HashMap<String, GroupStatusData> groupStatusList = _repository.retrieveGroupStatusData(); HashMap<String, GroupStatusData> groupStatusList = _repository.retrieveGroupStatusData();
for (ServerGroupData serverGroup : serverGroups) for (ServerGroupData serverGroup : serverGroups)
{ {
System.out.println("Checking Server Group " + serverGroup.Name);
if (!groupStatusList.containsKey(serverGroup.Name)) if (!groupStatusList.containsKey(serverGroup.Name))
{ {
groupStatusList.put(serverGroup.Name, new GroupStatusData()); groupStatusList.put(serverGroup.Name, new GroupStatusData());
@ -77,14 +40,14 @@ public class ServerMonitor
GroupStatusData groupStatus = groupStatusList.get(serverGroup.Name); GroupStatusData groupStatus = groupStatusList.get(serverGroup.Name);
int serversToAdd = Math.max(serverGroup.RequiredTotalServers - groupStatus.getTotalServers(), serverGroup.RequiredJoinableServers - groupStatus.getJoinableCount());
int serversToKill = groupStatus.EmptyServers.size() - serverGroup.RequiredJoinableServers;
int serverNum = groupStatus.getTotalServers() + 1;
if (serversToAdd > 0)
{ 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) while (serversToAdd > 0)
{ {
int serverNum = groupStatus.getNextServerNumber();
Collections.sort(dynamicServers, new DynamicServerSorter()); Collections.sort(dynamicServers, new DynamicServerSorter());
DynamicServerData bestServer = getBestDynamicServer(dynamicServers, serverGroup); DynamicServerData bestServer = getBestDynamicServer(dynamicServers, serverGroup);
@ -94,49 +57,66 @@ public class ServerMonitor
break; break;
} }
String cmd = "/home/mineplex/easyRemoteStartServer.sh"; System.out.println("Adding server for Server Group " + serverGroup.Name + " Req Total: " + serverGroup.RequiredTotalServers + " Req Joinable: " + serverGroup.RequiredJoinableServers + " | Actual Total: " + groupStatus.getTotalServers() + " Actual Joinable: " + groupStatus.getJoinableCount());
Process process = null; startServer(bestServer, serverGroup, serverNum);
serversToAdd--;
}
while (serversToKill > 0)
{
System.out.println("Killing excess server for Server Group " + serverGroup.Name + " Req Total: " + serverGroup.RequiredTotalServers + " Req Joinable: " + serverGroup.RequiredJoinableServers + " | Actual Total: " + groupStatus.getTotalServers() + " Actual Joinable: " + groupStatus.getJoinableCount());
killServer(groupStatus.EmptyServers.get(0));
serversToKill--;
}
}
}
try try
{ {
process = new ProcessBuilder(new String[] {"/bin/sh", "-x", cmd, bestServer.Address, serverGroup.ScriptName, serverGroup.Prefix + "-" + serverNum, "1", bestServer.US + ""}).start(); Thread.sleep(1000);
process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while(line != null)
{
System.out.println(line);
line=reader.readLine();
} }
} catch (InterruptedException e)
catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
finally
{ _count++;
if (process != null) _count %= 10000;
{
process.destroy();
} }
} }
bestServer.setServerGroupCount(serverGroup, bestServer.ServerGroupCount.containsKey(serverGroup.Name) ? (bestServer.ServerGroupCount.get(serverGroup.Name) + 1) : 1); private static DynamicServerData getBestDynamicServer(Collection<DynamicServerData> dynamicServers, ServerGroupData serverGroup)
System.out.println("Sent start command to " + bestServer.Address + " for " + serverGroup.Prefix + "-" + (groupStatus.getTotalServers() + 1));
serversToAdd--;
serverNum++;
}
}
else if (serversToKill > 0)
{ {
while (serversToKill > 0) DynamicServerData bestServer = null;
for (DynamicServerData serverData : dynamicServers)
{
if (serverData.AvailableRAM > serverGroup.RequiredRAM && serverData.AvailableCPU > serverGroup.RequiredCPU)
{
if (bestServer == null)
{
bestServer = serverData;
if (!serverData.ServerGroupCount.containsKey(serverGroup.Name))
break;
}
else if (serverData.ServerGroupCount.containsKey(serverGroup.Name))
{
if (serverData.ServerGroupCount.get(serverGroup.Name) < bestServer.ServerGroupCount.get(serverGroup.Name))
bestServer = serverData;
}
}
}
return bestServer;
}
private static void killServer(ServerStatusData serverToKill)
{ {
String cmd = "/home/mineplex/easyRemoteKillServer.sh"; String cmd = "/home/mineplex/easyRemoteKillServer.sh";
Process process = null; Process process = null;
ServerStatusData serverToKill = groupStatus.EmptyServers.get(0);
try try
{ {
process = new ProcessBuilder(new String[] {"/bin/sh", "-x", cmd, serverToKill.Address, serverToKill.Name}).start(); process = new ProcessBuilder(new String[] {"/bin/sh", "-x", cmd, serverToKill.Address, serverToKill.Name}).start();
@ -162,52 +142,41 @@ public class ServerMonitor
} }
} }
System.out.println("Sent start command to " + serverToKill.Address + " for " + serverToKill.Name); _repository.removeServerRecord(serverToKill);
serversToKill--; System.out.println("Sent kill command to " + serverToKill.Address + " for " + serverToKill.Name);
}
}
}
} }
private static void startServer(DynamicServerData serverSpace, ServerGroupData serverGroup, int serverNum)
{
String cmd = "/home/mineplex/easyRemoteStartServer.sh";
Process process = null;
try try
{ {
Thread.sleep(1000); process = new ProcessBuilder(new String[] {"/bin/sh", cmd, serverSpace.Address, serverGroup.ScriptName, serverGroup.Prefix + "-" + serverNum, "1", serverSpace.US ? "us" : "eu"}).start();
process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while(line != null)
{
System.out.println(line);
line=reader.readLine();
} }
catch (InterruptedException e) }
catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
finally
_count++;
_count %= 20;
}
}
private static DynamicServerData getBestDynamicServer(Collection<DynamicServerData> dynamicServers, ServerGroupData serverGroup)
{ {
DynamicServerData bestServer = null; if (process != null)
for (DynamicServerData serverData : dynamicServers)
{ {
System.out.println("Checking Dynamic Server " + serverData.Name); process.destroy();
if (serverData.AvailableRAM > serverGroup.RequiredRAM && serverData.AvailableCPU > serverGroup.RequiredCPU)
{
if (bestServer == null)
{
bestServer = serverData;
if (!serverData.ServerGroupCount.containsKey(serverGroup.Name))
break;
}
else if (serverData.ServerGroupCount.containsKey(serverGroup.Name))
{
if (serverData.ServerGroupCount.get(serverGroup.Name) < bestServer.ServerGroupCount.get(serverGroup.Name))
bestServer = serverData;
}
} }
} }
return bestServer; serverSpace.setServerGroupCount(serverGroup, serverSpace.ServerGroupCount.containsKey(serverGroup.Name) ? (serverSpace.ServerGroupCount.get(serverGroup.Name) + 1) : 1);
System.out.println("Sent start command to " + serverSpace.Name + " for " + serverGroup.Prefix + "-" + serverNum);
} }
} }