Add support for multiple slave connections for load balancing.
This commit is contained in:
parent
2b74e774f1
commit
7dbf970df7
@ -16,6 +16,7 @@ import mineplex.serverdata.RedisDataRepository;
|
|||||||
import mineplex.serverdata.Region;
|
import mineplex.serverdata.Region;
|
||||||
import mineplex.serverdata.ServerManager;
|
import mineplex.serverdata.ServerManager;
|
||||||
import mineplex.serverdata.ServerRepository;
|
import mineplex.serverdata.ServerRepository;
|
||||||
|
import mineplex.serverdata.Utility;
|
||||||
import mineplex.serverdata.transfers.ServerTransfer;
|
import mineplex.serverdata.transfers.ServerTransfer;
|
||||||
import mineplex.serverdata.transfers.TransferCommand;
|
import mineplex.serverdata.transfers.TransferCommand;
|
||||||
|
|
||||||
@ -44,7 +45,7 @@ public class QueueRepository
|
|||||||
*/
|
*/
|
||||||
public QueueRepository(Region region)
|
public QueueRepository(Region region)
|
||||||
{
|
{
|
||||||
this(ServerManager.DEFAULT_MASTER_CONNECTION, region);
|
this(ServerManager.getMasterConnection(), region);
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueueParty getQueueParty(int partyId)
|
public QueueParty getQueueParty(int partyId)
|
||||||
|
@ -56,7 +56,7 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
|
|||||||
|
|
||||||
public RedisDataRepository(Region region, Class<T> elementType, String elementLabel)
|
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);
|
elementType, elementLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ public class ServerCommandManager
|
|||||||
*/
|
*/
|
||||||
private 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;
|
_readPool = _writePool;
|
||||||
_commandTypes = new HashMap<String, CommandType>();
|
_commandTypes = new HashMap<String, CommandType>();
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package mineplex.serverdata;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ServerManager handles the creation/management of {@link ServerRepository}s for use.
|
* ServerManager handles the creation/management of {@link ServerRepository}s for use.
|
||||||
@ -10,10 +11,9 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class ServerManager
|
public class ServerManager
|
||||||
{
|
{
|
||||||
|
// Ports associated with slave redis instances
|
||||||
// The default port used by redis databases
|
private static final int[] SLAVE_PORTS = {6378, 6380, 6381};
|
||||||
public final static ConnectionData DEFAULT_MASTER_CONNECTION = new ConnectionData("10.33.53.16", 6379);
|
private static Random random = new Random();
|
||||||
public final static ConnectionData DEFAULT_SLAVE_CONNECTION = new ConnectionData("10.33.53.16", 6378);
|
|
||||||
|
|
||||||
// The cached repository instances
|
// The cached repository instances
|
||||||
private static Map<Region, ServerRepository> repositories = new HashMap<Region, ServerRepository>();
|
private static Map<Region, ServerRepository> repositories = new HashMap<Region, ServerRepository>();
|
||||||
@ -41,7 +41,32 @@ public class ServerManager
|
|||||||
*/
|
*/
|
||||||
public static ServerRepository getServerRepository(Region region)
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ public class Utility
|
|||||||
{
|
{
|
||||||
if (_masterPool == null)
|
if (_masterPool == null)
|
||||||
{
|
{
|
||||||
_masterPool = generatePool(ServerManager.DEFAULT_MASTER_CONNECTION);
|
_masterPool = generatePool(ServerManager.getMasterConnection());
|
||||||
}
|
}
|
||||||
|
|
||||||
return _masterPool;
|
return _masterPool;
|
||||||
@ -132,7 +132,7 @@ public class Utility
|
|||||||
{
|
{
|
||||||
if (_slavePool == null)
|
if (_slavePool == null)
|
||||||
{
|
{
|
||||||
_slavePool = generatePool(ServerManager.DEFAULT_SLAVE_CONNECTION);
|
_slavePool = generatePool(ServerManager.getSlaveConnection());
|
||||||
}
|
}
|
||||||
|
|
||||||
return _slavePool;
|
return _slavePool;
|
||||||
|
Loading…
Reference in New Issue
Block a user