Merge branch 'master' of ssh://184.154.0.242:7999/min/mineplex

This commit is contained in:
Chiss 2014-08-15 09:49:12 +10:00
commit 697897b967
8 changed files with 87 additions and 105 deletions

View File

@ -174,6 +174,10 @@
<include name="**/*.class"/>
</fileset>
<fileset dir="../Mineplex.ServerData/bin">
<include name="**/*.class"/>
</fileset>
<zipfileset src="../Libraries/mysql.zip" />
<manifest>
@ -188,6 +192,8 @@
<zipfileset src="../Libraries/gson-2.2.1.jar" />
<zipfileset src="../Libraries/commons-logging-1.1.1.jar" />
<zipfileset src="../Libraries/commons-codec-1.6.jar" />
<zipfileset src="../Libraries/jedis-2.4.2.jar" />
<zipfileset src="../Libraries/commons-pool2-2.2.jar" />
</jar>
<copy file="../bin/ServerMonitor.jar" todir="../../Testing/ServerMonitor/"/>
</target>

View File

@ -2,7 +2,6 @@ package mineplex.core.status;
import java.io.File;
import java.util.Collection;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
@ -21,8 +20,8 @@ import mineplex.serverdata.ServerRepository;
public class ServerStatusManager extends MiniPlugin
{
// The default timeout (in milliseconds) before the ServerStatus expires.
public final int DEFAULT_SERVER_TIMEOUT = 15000;
// The default timeout (in seconds) before the ServerStatus expires.
public final int DEFAULT_SERVER_TIMEOUT = 15;
private ServerRepository _repository;
private LagMeter _lagMeter;
@ -30,7 +29,6 @@ public class ServerStatusManager extends MiniPlugin
private String _name;
private boolean _us;
private boolean _alternateSeconds;
private boolean _enabled = true;
public ServerStatusManager(JavaPlugin plugin, LagMeter lagMeter)
@ -41,20 +39,15 @@ public class ServerStatusManager extends MiniPlugin
if (new File("IgnoreUpdates.dat").exists())
_enabled = false;
ServerListPingEvent event = new ServerListPingEvent(null, plugin.getServer().getMotd(), plugin.getServer().getOnlinePlayers().length, plugin.getServer().getMaxPlayers());
GetPluginManager().callEvent(event);
setupConfigValues();
String address = Bukkit.getServer().getIp().isEmpty() ? "localhost" : Bukkit.getServer().getIp();
_name = plugin.getConfig().getString("serverstatus.name");
_us = plugin.getConfig().getBoolean("serverstatus.us");
Region region = _us ? Region.US : Region.EU;
_repository = ServerManager.getServerRepository(region);
saveServerStatus();
}
private void setupConfigValues()
@ -112,11 +105,6 @@ public class ServerStatusManager extends MiniPlugin
if (!_enabled)
return;
_alternateSeconds = !_alternateSeconds;
if (!_alternateSeconds)
return;
saveServerStatus();
}
@ -132,7 +120,7 @@ public class ServerStatusManager extends MiniPlugin
{
public void run()
{
_repository.updataServerStatus(serverSnapshot, 15000);
_repository.updataServerStatus(serverSnapshot, DEFAULT_SERVER_TIMEOUT);
}
});
}

View File

@ -55,6 +55,7 @@ public class LobbyMenu extends ShopPageBase<ServerManager, LobbyShop> implements
Material status = Material.IRON_BLOCK;
List<String> lore = new ArrayList<String>();
slot = Integer.parseInt(serverInfo.Name.split("-")[1]) - 1;
if (slot >= 54)
break;
@ -79,19 +80,12 @@ public class LobbyMenu extends ShopPageBase<ServerManager, LobbyShop> implements
AddButton(slot, new ShopItem(status, ChatColor.UNDERLINE + "" + ChatColor.BOLD + "" + ChatColor.WHITE + "Server " + serverInfo.Name.substring(serverInfo.Name.indexOf('-') + 1), lore.toArray(new String[lore.size()]), Integer.parseInt(serverInfo.Name.substring(serverInfo.Name.indexOf('-') + 1)), false), new JoinServerButton(this, serverInfo));
else
AddItem(slot, new ShopItem(status, ChatColor.UNDERLINE + "" + ChatColor.BOLD + "" + ChatColor.WHITE + "Server " + serverInfo.Name.substring(serverInfo.Name.indexOf('-') + 1), lore.toArray(new String[lore.size()]), Integer.parseInt(serverInfo.Name.substring(serverInfo.Name.indexOf('-') + 1)), false));
slot += 1;
}
while (slot < 54)
{
setItem(slot, null);
slot++;
}
}
public void Update()
{
clear();
BuildPage();
}

View File

@ -14,9 +14,7 @@ import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import redis.clients.jedis.Tuple;
/**
* RedisServerRepository offers a Redis-based implementation of {@link ServerRepository}
@ -113,8 +111,8 @@ public class RedisServerRepository implements ServerRepository
String serverName = serverData.getName();
String setKey = concatenate("serverstatus", "minecraft", _region.toString());
String dataKey = concatenate(setKey, serverName);
long expiry = System.currentTimeMillis() + timeout;
long expiry = Long.parseLong(jedis.time().get(0)) + timeout;
Transaction transaction = jedis.multi();
transaction.set(dataKey, serializedData);
transaction.zadd(setKey, expiry, serverName);
@ -138,7 +136,7 @@ public class RedisServerRepository implements ServerRepository
String dataKey = concatenate(setKey, serverName);
Transaction transaction = jedis.multi();
transaction.set(dataKey, null);
transaction.del(dataKey);
transaction.zrem(setKey, serverName);
transaction.exec();
}
@ -204,7 +202,7 @@ public class RedisServerRepository implements ServerRepository
{
for (MinecraftServer minecraftServer : getServerStatuses())
{
if (serverGroups.containsKey(minecraftServer.getGroup()))
if (serverGroups.containsKey(minecraftServer.getGroup()) && minecraftServer.getPublicAddress().equalsIgnoreCase(server.getPrivateAddress()))
{
ServerGroup serverGroup = serverGroups.get(minecraftServer.getGroup());
server.incrementServerCount(serverGroup);
@ -241,32 +239,6 @@ public class RedisServerRepository implements ServerRepository
return servers;
}
@Override
public int clean()
{
Jedis jedis = _jedisPool.getResource();
try
{
String setKey = concatenate("serverstatus", "minecraft", _region.toString());
for (String deadName : getDeadNames(setKey))
{
String dataKey = concatenate(setKey, deadName);
Transaction transaction = jedis.multi();
transaction.del(dataKey);
transaction.zrem(setKey, deadName);
transaction.exec();
}
}
finally
{
_jedisPool.returnResource(jedis);
}
return 0;
}
/**
* @param key - the key where the sorted set of server sessions is stored
* @return the {@link Set} of active server names stored at {@code key} for non-expired
@ -279,7 +251,7 @@ public class RedisServerRepository implements ServerRepository
try
{
String min = "(" + System.currentTimeMillis();
String min = "(" + jedis.time().get(0);
String max = "+inf";
names = jedis.zrangeByScore(key, min, max);
}
@ -291,29 +263,6 @@ public class RedisServerRepository implements ServerRepository
return names;
}
/**
* @param key - the key where the sorted set of server sessions is stored
* @return the {@link Set} of dead (expired) server names stored at {@code key}.
*/
protected Set<String> getDeadNames(String key)
{
Set<String> names = new HashSet<String>();
Jedis jedis = _jedisPool.getResource();
try
{
String min = "-inf";
String max = System.currentTimeMillis() + "";
names = jedis.zrangeByScore(key, min, max);
}
finally
{
_jedisPool.returnResource(jedis);
}
return names;
}
/**
* @param elements - the elements to concatenate together
* @return the concatenated form of all {@code elements}
@ -324,6 +273,42 @@ public class RedisServerRepository implements ServerRepository
return Utility.concatenate(KEY_DELIMITER, elements);
}
@Override
public Collection<MinecraftServer> getDeadServers()
{
Set<MinecraftServer> servers = new HashSet<MinecraftServer>();
Jedis jedis = _jedisPool.getResource();
try
{
Pipeline pipeline = jedis.pipelined();
String setKey = concatenate("serverstatus", "minecraft", _region.toString());
String min = "-inf";
String max = jedis.time().get(0);
List<Response<String>> responses = new ArrayList<Response<String>>();
for (Tuple serverName : jedis.zrangeByScoreWithScores(setKey, min, max))
{
String dataKey = concatenate(setKey, serverName.getElement());
responses.add(pipeline.get(dataKey));
}
pipeline.sync();
for (Response<String> response : responses)
{
String serializedData = response.get();
servers.add(Utility.deserialize(serializedData, MinecraftServer.class));
}
}
finally
{
_jedisPool.returnResource(jedis);
}
return servers;
}
/*
* <region> = "US" or "EU"
* serverstatus.minecraft.<region>.<name> stores the JSON encoded information of an active MinecraftServer instance.

View File

@ -9,5 +9,5 @@ package mineplex.serverdata;
public enum Region
{
US,
EU;
EU,
}

View File

@ -51,13 +51,13 @@ public class ServerGroup
public boolean getArcadeGroup() { return _arcadeGroup; }
private String _worldZip;
public String getWorldZip() { return _serverType; }
public String getWorldZip() { return _worldZip; }
private String _plugin;
public String getPlugin() { return _serverType; }
public String getPlugin() { return _plugin; }
private String _configPath;
public String getConfigPath() { return _serverType; }
public String getConfigPath() { return _configPath; }
private int _minPlayers;
public int getMinPlayers() { return _minPlayers; }
@ -208,7 +208,7 @@ public class ServerGroup
for (MinecraftServer server : repository.getServerStatuses())
{
if (_name.equals(server.getGroup()))
if (_name.equalsIgnoreCase(server.getGroup()))
{
servers.add(server);
}
@ -218,9 +218,9 @@ public class ServerGroup
/**
* @return a unique server name suffix id, unique to any servers in this ServerGroup.
*/
public int generateUniqueId()
public int generateUniqueId(int startId)
{
int id = 0;
int id = startId;
while (true)
{

View File

@ -1,6 +1,7 @@
package mineplex.serverdata;
import java.util.Collection;
import java.util.Set;
/**
* The ServerRepository is used for storing/retrieving active sessions
@ -60,12 +61,6 @@ public interface ServerRepository
* currently active {@link ServerGroup}s in the repository.
*/
public Collection<ServerGroup> getServerGroups();
/**
* Clean the repository by removing all expired server statuses that have
* passed their timeout period.
* @return the number of expired server statuses removed/cleared.
*/
public int clean();
Collection<MinecraftServer> getDeadServers();
}

View File

@ -73,8 +73,12 @@ public class ServerMonitor
while (true)
{
Collection<ServerGroup> serverGroups = _repository.getServerGroups();
_repository.clean(); // Clean out old expired server entries
for (MinecraftServer deadServer : _repository.getDeadServers())
{
killServer(deadServer.getName(), deadServer.getPublicAddress(), true);
}
List<DedicatedServer> dedicatedServers = new ArrayList<DedicatedServer>(_repository.getDedicatedServers());
if (_count % 15 == 0)
@ -82,7 +86,7 @@ public class ServerMonitor
_badServers.clear();
for (DedicatedServer serverData : dedicatedServers)
{
{
if (isServerOffline(serverData))
{
System.out.println("------=[OFFLINE]=------=[" + serverData.getName() + ":" + serverData.getPublicAddress() + "]=------=[OFFLINE]=------");
@ -117,11 +121,20 @@ public class ServerMonitor
}
}*/
HashSet<String> onlineServers = new HashSet<String>();
for (MinecraftServer minecraftServer : _repository.getServerStatuses())
{
onlineServers.add(minecraftServer.getName());
}
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)
if (onlineServers.contains(entry.getKey()))
iterator.remove();
else if (System.currentTimeMillis() - entry.getValue().getValue() > 20000)
{
System.out.println("-=[SERVER STARTUP TOO SLOW]=- " + entry.getKey());
@ -185,7 +198,7 @@ public class ServerMonitor
processWaits++;
}
processWaits = 0;
processWaits = 0;
try
{
@ -203,7 +216,7 @@ public class ServerMonitor
private static void handleGroupChanges(List<DedicatedServer> dedicatedServers, HashMap<String, Entry<String, Long>> serverTracker, ServerGroup serverGroup, boolean free)
{
int serverNum = serverGroup.generateUniqueId();
int serverNum = 0;
//GroupStatusData groupStatus = groupStatusList.get(serverGroup.Name);
int requiredTotal = serverGroup.getRequiredTotalServers();
int requiredJoinable = serverGroup.getRequiredJoinableServers();
@ -221,6 +234,7 @@ public class ServerMonitor
while (serversToAdd > 0)
{
serverNum = serverGroup.generateUniqueId(serverNum + 1);
Collections.sort(dedicatedServers, new DedicatedServerSorter());
DedicatedServer bestServer = getBestDedicatedServer(dedicatedServers, serverGroup);
@ -244,7 +258,7 @@ public class ServerMonitor
while (serversToKill > 0)
{
List<MinecraftServer> emptyServers = new ArrayList<MinecraftServer>(serverGroup.getEmptyServers());
MinecraftServer emptyServer = emptyServers.get(0);
MinecraftServer emptyServer = emptyServers.get(serversToKill - 1);
System.out.println("[" + emptyServer.getName() + ":" + emptyServer.getPublicAddress() + "] Killing " + serverGroup.getName() + " Req Total: " + serverGroup.getRequiredTotalServers() + " Req Joinable: " + serverGroup.getRequiredJoinableServers() + " | Actual Total: " + serverGroup.getServerCount() + " Actual Joinable: " + serverGroup.getJoinableCount());
killServer(emptyServer);
serversToKill--;
@ -357,12 +371,12 @@ public class ServerMonitor
private static void startServer(final DedicatedServer serverSpace, final ServerGroup serverGroup, final int serverNum, final boolean free)
{
String cmd = "/home/mineplex/easyRemoteStartServer.sh";
String cmd = "/home/mineplex/easyRemoteStartServerCustom.sh";
final String groupPrefix = serverGroup.getPrefix();
final String serverName = serverSpace.getName();
final String serverAddress = serverSpace.getPublicAddress();
ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverAddress, serverSpace.getPrivateAddress(), serverGroup.getPortSection() + serverNum + "", serverGroup.getRequiredRam() + "", serverGroup.getWorldZip(), serverGroup.getPlugin(), serverGroup.getConfigPath(), serverGroup.getName(), serverGroup.getPrefix() + "-" + serverNum, serverGroup.getMinPlayers() + "", serverGroup.getMaxPlayers() + "", serverGroup.getPvp() + "", serverGroup.getTournament() + "", free + "", serverSpace.isUsRegion() ? "true" : "false", serverGroup.getArcadeGroup() + "", serverGroup.getGames(), serverGroup.getServerType(), serverGroup.getAddNoCheat() + ""});
ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverAddress, serverSpace.getPrivateAddress(), (serverGroup.getPortSection() + serverNum) + "", serverGroup.getRequiredRam() + "", serverGroup.getWorldZip(), serverGroup.getPlugin(), serverGroup.getConfigPath(), serverGroup.getName(), serverGroup.getPrefix() + "-" + serverNum, serverGroup.getMinPlayers() + "", serverGroup.getMaxPlayers() + "", serverGroup.getPvp() + "", serverGroup.getTournament() + "", free + "", serverSpace.isUsRegion() ? "true" : "false", serverGroup.getArcadeGroup() + "", serverGroup.getGames(), serverGroup.getServerType(), serverGroup.getAddNoCheat() + ""});
pr.start(new GenericRunnable<Boolean>()
{
public void run(Boolean error)