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

This commit is contained in:
Jonathan Williams 2015-06-29 19:43:58 -04:00
commit 78c5b63e85
5 changed files with 122 additions and 35 deletions

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Mineplex.Queue.Core</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.Random;
import mineplex.serverdata.servers.ConnectionData;
import mineplex.serverdata.servers.ConnectionData.ConnectionType;
public class RedisConfig
{
@ -14,18 +15,15 @@ public class RedisConfig
private static Random random = new Random(); // Utility random
// The connections managed by this configuration
private ConnectionData _masterConnection;
private List<ConnectionData> _slaveConnections;
private List<ConnectionData> _connections;
/**
* Class constructor
* @param master
* @param slaves
* @param connections
*/
public RedisConfig(ConnectionData master, List<ConnectionData> slaves)
public RedisConfig(List<ConnectionData> connections)
{
_masterConnection = master;
_slaveConnections = slaves;
_connections = connections;
}
/**
@ -34,7 +32,8 @@ public class RedisConfig
*/
public RedisConfig()
{
this(new ConnectionData(DEFAULT_IP, DEFAULT_PORT), new ArrayList<ConnectionData>());
_connections = new ArrayList<ConnectionData>();
_connections.add(new ConnectionData(DEFAULT_IP, DEFAULT_PORT, ConnectionType.MASTER, "DefaultConnection"));
}
/**
@ -43,23 +42,39 @@ public class RedisConfig
*/
public ConnectionData getConnection()
{
return getConnection(true);
return getConnection(true, null);
}
/**
* @param writeable - whether the returned connection reference can receive write-requests.
* @return a {@link ConnectionData} referencing a valid redis-connection from this configuration.
*/
public ConnectionData getConnection(boolean writeable)
public ConnectionData getConnection(boolean writeable, String name)
{
if (writeable || _slaveConnections.size() == 0)
List<ConnectionData> connections = getConnections(writeable, name);
if (connections.size() > 0)
{
return _masterConnection;
int index = random.nextInt(connections.size());
return connections.get(index);
}
else
return null;
}
public List<ConnectionData> getConnections(boolean writeable, String name)
{
List<ConnectionData> connections = new ArrayList<ConnectionData>();
ConnectionType type = (writeable) ? ConnectionType.MASTER : ConnectionType.SLAVE;
for (ConnectionData connection : _connections)
{
int index = random.nextInt(_slaveConnections.size());
return _slaveConnections.get(index);
if (connection.getType() == type && connection.nameMatches(name))
{
connections.add(connection);
}
}
return connections;
}
}

View File

@ -7,22 +7,48 @@ package mineplex.serverdata.servers;
*/
public class ConnectionData
{
public enum ConnectionType
{
MASTER,
SLAVE;
}
private ConnectionType _type; // The type of connection available
public ConnectionType getType() { return _type; }
private String _name; // The name associated with this connection
public String getName() { return _name; }
private String _host; // The host URL to connect to repository
private String _host; // The host URL to connect to repository
public String getHost() { return _host; }
private int _port; // The port to connect to repository
private int _port; // The port to connect to repository
public int getPort() { return _port; }
/**
* Constructor
* @param host - the host URL defining the repository
* @param port - the port used for connection to repository
* @param type - the type of connection referenced by this ConnectionData
* @param name - the name associated with ConnectionData
*/
public ConnectionData(String host, int port)
public ConnectionData(String host, int port, ConnectionType type, String name)
{
_host = host;
_port = port;
_type = type;
_name = name;
}
/**
* @param name
* @return true, if {@code name} is null or it matches (case-insensitive) the {@code _name} associated
* with this ConnectionData, false otherwise.
*/
public boolean nameMatches(String name)
{
return (name == null || name.equalsIgnoreCase(_name));
}
}

View File

@ -12,6 +12,7 @@ import java.util.Random;
import mineplex.serverdata.Region;
import mineplex.serverdata.redis.RedisConfig;
import mineplex.serverdata.redis.RedisServerRepository;
import mineplex.serverdata.servers.ConnectionData.ConnectionType;
/**
* ServerManager handles the creation/management of {@link ServerRepository}s for use.
@ -20,6 +21,8 @@ import mineplex.serverdata.redis.RedisServerRepository;
*/
public class ServerManager
{
public static final String SERVER_STATUS_LABEL = "ServerStatus"; // Label differentiating ServerStatus related servers
// Configuration determining connection information
private static RedisConfig _config;
@ -32,7 +35,7 @@ public class ServerManager
* @param region - the geographical region of the {@link ServerRepository}.
* @return a newly instanced (or cached) {@link ServerRepository} for the specified {@code region}.
*/
public static ServerRepository getServerRepository(ConnectionData writeConn, ConnectionData readConn, Region region)
private static ServerRepository getServerRepository(ConnectionData writeConn, ConnectionData readConn, Region region)
{
if (repositories.containsKey(region)) return repositories.get(region);
@ -49,7 +52,7 @@ public class ServerManager
*/
public static ServerRepository getServerRepository(Region region)
{
return getServerRepository(getMasterConnection(), getSlaveConnection(), region);
return getServerRepository(getConnection(true, SERVER_STATUS_LABEL), getConnection(false, SERVER_STATUS_LABEL), region);
}
/**
@ -69,13 +72,18 @@ public class ServerManager
return getConnection(false);
}
public static ConnectionData getConnection(boolean writeable, String name)
{
return getConfig().getConnection(writeable, name);
}
/**
* @param writeable - whether the connection referenced in return can receive write-requests
* @return a newly generated {@code ConnectionData} pointing to a valid connection.
*/
public static ConnectionData getConnection(boolean writeable)
{
return getConfig().getConnection(writeable);
return getConnection(writeable, null);
}
/**
@ -91,32 +99,28 @@ public class ServerManager
if (configFile.exists())
{
List<ConnectionData> connections = new ArrayList<ConnectionData>();
List<String> lines = Files.readAllLines(configFile.toPath(), Charset.defaultCharset());
ConnectionData master = deserializeConnection(lines.get(0));
List<ConnectionData> slaves = new ArrayList<ConnectionData>();
for (int i = 1; i < lines.size(); i++)
for (String line : lines)
{
ConnectionData slave = deserializeConnection(lines.get(i));
slaves.add(slave);
ConnectionData connection = deserializeConnection(line);
connections.add(connection);
}
System.out.println("LOADED " + (master != null) + " " + slaves.size());
_config = new RedisConfig(master, slaves);
System.out.println("Master connection " + master.getHost() + " port " + master.getPort());
_config = new RedisConfig(connections);
}
else
{
System.out.println("redis-config.dat not found at " + configFile.toPath().toString());
log("redis-config.dat not found at " + configFile.toPath().toString());
_config = new RedisConfig();
}
}
catch (Exception exception)
{
exception.printStackTrace();
System.out.println("---Unable To Parse Redis Configuration File---");
log("---Unable To Parse Redis Configuration File---");
}
}
@ -132,14 +136,22 @@ public class ServerManager
{
String[] args = line.split(" ");
if (args.length == 2)
if (args.length >= 2)
{
String ip = args[0];
int port = Integer.parseInt(args[1]);
String typeName = (args.length >= 3) ? args[2].toUpperCase() : "MASTER"; // Defaults to MASTER if omitted.
ConnectionType type = ConnectionType.valueOf(typeName);
String name = (args.length >= 4) ? args[3] : "DefaultConnection"; // Defaults to DefaultConnection if omitted.
return new ConnectionData(ip, port);
return new ConnectionData(ip, port, type, name);
}
return null;
}
private static void log(String message)
{
System.out.println(String.format("[ServerManager] %s", message));
}
}

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Mineplex.ServerProcesses</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>