From 7dbf970df7e80681e748ac6692586057adc5fc2b Mon Sep 17 00:00:00 2001 From: Ty Sayers Date: Sun, 8 Mar 2015 19:17:28 -0400 Subject: [PATCH] Add support for multiple slave connections for load balancing. --- .../src/mineplex/queuer/QueueRepository.java | 3 +- .../serverdata/RedisDataRepository.java | 2 +- .../serverdata/ServerCommandManager.java | 2 +- .../mineplex/serverdata/ServerManager.java | 35 ++++++++++++++++--- .../src/mineplex/serverdata/Utility.java | 4 +-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Plugins/Mineplex.Queuer/src/mineplex/queuer/QueueRepository.java b/Plugins/Mineplex.Queuer/src/mineplex/queuer/QueueRepository.java index 12f056d62..e7226c5c8 100644 --- a/Plugins/Mineplex.Queuer/src/mineplex/queuer/QueueRepository.java +++ b/Plugins/Mineplex.Queuer/src/mineplex/queuer/QueueRepository.java @@ -16,6 +16,7 @@ import mineplex.serverdata.RedisDataRepository; import mineplex.serverdata.Region; import mineplex.serverdata.ServerManager; import mineplex.serverdata.ServerRepository; +import mineplex.serverdata.Utility; import mineplex.serverdata.transfers.ServerTransfer; import mineplex.serverdata.transfers.TransferCommand; @@ -44,7 +45,7 @@ public class QueueRepository */ public QueueRepository(Region region) { - this(ServerManager.DEFAULT_MASTER_CONNECTION, region); + this(ServerManager.getMasterConnection(), region); } public QueueParty getQueueParty(int partyId) diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/RedisDataRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/RedisDataRepository.java index a8f7e3d86..0c12c0797 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/RedisDataRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/RedisDataRepository.java @@ -56,7 +56,7 @@ public class RedisDataRepository implements DataRepository public RedisDataRepository(Region region, Class elementType, String elementLabel) { - this(ServerManager.DEFAULT_MASTER_CONNECTION, ServerManager.DEFAULT_SLAVE_CONNECTION, region, + this(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(), region, elementType, elementLabel); } diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerCommandManager.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerCommandManager.java index 111163f7e..a66ebb936 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerCommandManager.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerCommandManager.java @@ -28,7 +28,7 @@ public class ServerCommandManager */ private ServerCommandManager() { - _writePool = Utility.generatePool(new ConnectionData("10.33.53.16", 6377)); // Hardcoded connection to standalone redis instance + _writePool = Utility.generatePool(ServerManager.getPubSubConnection()); // Hardcoded connection to standalone redis instance _readPool = _writePool; _commandTypes = new HashMap(); diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerManager.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerManager.java index 188d93d5f..ce9f54e49 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerManager.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/ServerManager.java @@ -2,6 +2,7 @@ package mineplex.serverdata; import java.util.HashMap; import java.util.Map; +import java.util.Random; /** * ServerManager handles the creation/management of {@link ServerRepository}s for use. @@ -10,10 +11,9 @@ import java.util.Map; */ public class ServerManager { - - // The default port used by redis databases - public final static ConnectionData DEFAULT_MASTER_CONNECTION = new ConnectionData("10.33.53.16", 6379); - public final static ConnectionData DEFAULT_SLAVE_CONNECTION = new ConnectionData("10.33.53.16", 6378); + // Ports associated with slave redis instances + private static final int[] SLAVE_PORTS = {6378, 6380, 6381}; + private static Random random = new Random(); // The cached repository instances private static Map repositories = new HashMap(); @@ -41,7 +41,32 @@ public class ServerManager */ public static ServerRepository getServerRepository(Region region) { - return getServerRepository(DEFAULT_MASTER_CONNECTION, DEFAULT_SLAVE_CONNECTION, region); + return getServerRepository(getMasterConnection(), getSlaveConnection(), region); } + /** + * @return the {@link ConnectionData} associated with the master instance connection. + */ + public static ConnectionData getMasterConnection() + { + return new ConnectionData("10.33.53.16", 6379); + } + + /** + * Non-Deterministic: Generates random slave instance connection. + * @return the {@link ConnectionData} associated with a random slave connection. + */ + public static ConnectionData getSlaveConnection() + { + int port = SLAVE_PORTS[random.nextInt(SLAVE_PORTS.length)]; + return new ConnectionData("10.33.53.16", port); + } + + /** + * @return the {@link ConnectionData} associated with the dedicated PubSub instance. + */ + public static ConnectionData getPubSubConnection() + { + return new ConnectionData("10.33.53.16", 6377); + } } diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Utility.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Utility.java index 8c9fcaf5b..480172436 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Utility.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Utility.java @@ -123,7 +123,7 @@ public class Utility { if (_masterPool == null) { - _masterPool = generatePool(ServerManager.DEFAULT_MASTER_CONNECTION); + _masterPool = generatePool(ServerManager.getMasterConnection()); } return _masterPool; @@ -132,7 +132,7 @@ public class Utility { if (_slavePool == null) { - _slavePool = generatePool(ServerManager.DEFAULT_SLAVE_CONNECTION); + _slavePool = generatePool(ServerManager.getSlaveConnection()); } return _slavePool;