Add support for multiple slave connections for load balancing.

This commit is contained in:
Ty Sayers 2015-03-08 19:17:28 -04:00
parent 2b74e774f1
commit 7dbf970df7
5 changed files with 36 additions and 10 deletions

View File

@ -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)

View File

@ -56,7 +56,7 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
public RedisDataRepository(Region region, Class<T> elementType, String elementLabel)
{
this(ServerManager.DEFAULT_MASTER_CONNECTION, ServerManager.DEFAULT_SLAVE_CONNECTION, region,
this(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(), region,
elementType, elementLabel);
}

View File

@ -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<String, CommandType>();

View File

@ -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<Region, ServerRepository> repositories = new HashMap<Region, ServerRepository>();
@ -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);
}
}

View File

@ -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;