From 1992205393b3c69791bd2754a05bae84c88b8b6f Mon Sep 17 00:00:00 2001 From: Ty Sayers Date: Mon, 29 Jun 2015 19:27:05 -0400 Subject: [PATCH] Modify RedisConfiguration deserialization options to allow for more dynamic distribution of redis server types and data handling. Servers are now defined individually with metadata representing server type (master/slave) and custom connection names for data redirection on server start up. --- Plugins/Mineplex.Queue.Core/.project | 17 +++++++ .../serverdata/redis/RedisConfig.java | 45 ++++++++++++------ .../serverdata/servers/ConnectionData.java | 32 +++++++++++-- .../serverdata/servers/ServerManager.java | 46 ++++++++++++------- Plugins/Mineplex.ServerProcesses/.project | 17 +++++++ 5 files changed, 122 insertions(+), 35 deletions(-) create mode 100644 Plugins/Mineplex.Queue.Core/.project create mode 100644 Plugins/Mineplex.ServerProcesses/.project diff --git a/Plugins/Mineplex.Queue.Core/.project b/Plugins/Mineplex.Queue.Core/.project new file mode 100644 index 000000000..669fb734b --- /dev/null +++ b/Plugins/Mineplex.Queue.Core/.project @@ -0,0 +1,17 @@ + + + Mineplex.Queue.Core + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisConfig.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisConfig.java index 99e055c8e..cd841fc6f 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisConfig.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisConfig.java @@ -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 _slaveConnections; + private List _connections; /** * Class constructor - * @param master - * @param slaves + * @param connections */ - public RedisConfig(ConnectionData master, List slaves) + public RedisConfig(List 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()); + _connections = new ArrayList(); + _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 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 getConnections(boolean writeable, String name) + { + List connections = new ArrayList(); + 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; } } diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ConnectionData.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ConnectionData.java index efb265fc1..e7c8676dd 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ConnectionData.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ConnectionData.java @@ -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)); } } diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerManager.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerManager.java index dd84ee13a..44b62e013 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerManager.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/servers/ServerManager.java @@ -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 connections = new ArrayList(); List lines = Files.readAllLines(configFile.toPath(), Charset.defaultCharset()); - ConnectionData master = deserializeConnection(lines.get(0)); - List slaves = new ArrayList(); - - 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)); + } } diff --git a/Plugins/Mineplex.ServerProcesses/.project b/Plugins/Mineplex.ServerProcesses/.project new file mode 100644 index 000000000..66b31cde0 --- /dev/null +++ b/Plugins/Mineplex.ServerProcesses/.project @@ -0,0 +1,17 @@ + + + Mineplex.ServerProcesses + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + +