Update to Redis 2.8.1 and clean up repositories

This commit is contained in:
Shaun Bennett 2016-04-17 12:58:44 +10:00
parent a730b81feb
commit 16dacbe3b3
8 changed files with 131 additions and 466 deletions

View File

@ -191,27 +191,13 @@ public class ReportManager {
public int generateReportId()
{
JedisPool pool = Utility.getPool(true);
Jedis jedis = pool.getResource();
long uniqueReportId = -1;
try
try (Jedis jedis = pool.getResource())
{
uniqueReportId = jedis.incr("reports.unique-id");
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
pool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
pool.returnResource(jedis);
}
}
return (int) uniqueReportId;
}

View File

@ -163,25 +163,11 @@ public class Utility
{
long currentTime = 0;
JedisPool pool = getPool(false);
Jedis jedis = pool.getResource();
try
try(Jedis jedis = pool.getResource())
{
currentTime = Long.parseLong(jedis.time().get(0));
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
pool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (pool != null)
{
pool.returnResource(jedis);
}
}
_millisTimeDifference = (currentTime * 1000) - System.currentTimeMillis();
}

View File

@ -45,29 +45,15 @@ public class ServerCommandManager
*/
private void initialize()
{
final Jedis jedis = _readPool.getResource();
// Spin up a new thread and subscribe to the Redis pubsub network
Thread thread = new Thread("Redis Manager")
{
public void run()
{
try
try (Jedis jedis = _readPool.getResource())
{
jedis.psubscribe(new ServerCommandListener(), SERVER_COMMANDS_CHANNEL + ":*");
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
}
finally
{
if (_readPool != null)
{
_readPool.returnResource(jedis);
}
}
}
};
@ -86,25 +72,11 @@ public class ServerCommandManager
{
String commandType = serverCommand.getClass().getSimpleName();
String serializedCommand = Utility.serialize(serverCommand);
Jedis jedis = _writePool.getResource();
try
try(Jedis jedis = _writePool.getResource())
{
jedis.publish(SERVER_COMMANDS_CHANNEL + ":" + commandType, serializedCommand);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (_writePool != null)
{
_writePool.returnResource(jedis);
}
}
}
}).start();
}

View File

@ -13,29 +13,14 @@ import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.servers.ConnectionData;
import mineplex.serverdata.servers.ServerManager;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.exceptions.JedisConnectionException;
public class RedisDataRepository<T extends Data> implements DataRepository<T>
public class RedisDataRepository<T extends Data> extends RedisRepository implements DataRepository<T>
{
// The delimiter character used for redis key paths
public final char KEY_DELIMITER = '.';
// The pools used to retrieve jedis instances.
private JedisPool _writePool;
private JedisPool _readPool;
// The geographical region of the servers stored by this ServerRepository
private Region _region;
// The class type of the elements stored in this repository
private Class<T> _elementType;
// A unique label designating the elements and this repository.
private String _elementLabel;
@ -43,19 +28,14 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
* Class constructor
* @param writeConn
* @param readConn
* @param host
* @param port
* @param region
*/
public RedisDataRepository(ConnectionData writeConn, ConnectionData readConn, Region region,
Class<T> elementType, String elementLabel)
{
_writePool = Utility.generatePool(writeConn);
_readPool = (writeConn == readConn) ? _writePool : Utility.generatePool(readConn);
_region = region;
super(writeConn, readConn, region);
_elementType = elementType;
_elementLabel = elementLabel;
}
public RedisDataRepository(ConnectionData conn, Region region, Class<T> elementType, String elementLabel)
@ -71,7 +51,7 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
public String getElementSetKey()
{
return concatenate("data", _elementLabel, _region.toString());
return concatenate("data", _elementLabel, getRegion().toString());
}
public String generateKey(T element)
@ -94,9 +74,8 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
public Collection<T> getElements(Collection<String> dataIds)
{
Collection<T> elements = new HashSet<T>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
Pipeline pipeline = jedis.pipelined();
@ -120,20 +99,7 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
}
}
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return elements;
}
@ -141,37 +107,21 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
public T getElement(String dataId)
{
T element = null;
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String key = generateKey(dataId);
String serializedData = jedis.get(key);
element = deserialize(serializedData);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return element;
}
@Override
public void addElement(T element, int timeout)
{
Jedis jedis = _writePool.getResource();
try
try(Jedis jedis = getResource(true))
{
String serializedData = serialize(element);
String dataId = element.getDataId();
@ -184,19 +134,6 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
transaction.zadd(setKey, expiry, dataId.toString());
transaction.exec();
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_writePool.returnResource(jedis);
}
}
}
@Override
@ -214,9 +151,7 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
@Override
public void removeElement(String dataId)
{
Jedis jedis = _writePool.getResource();
try
try(Jedis jedis = getResource(true))
{
String setKey = getElementSetKey();
String dataKey = generateKey(dataId);
@ -226,19 +161,6 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
transaction.zrem(setKey, dataId);
transaction.exec();
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_writePool.returnResource(jedis);
}
}
}
@Override
@ -250,9 +172,7 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
@Override
public int clean()
{
Jedis jedis = _writePool.getResource();
try
try(Jedis jedis = getResource(true))
{
for (String dataId : getDeadElements())
{
@ -264,76 +184,35 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
transaction.exec();
}
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_writePool.returnResource(jedis);
}
}
return 0;
}
protected Set<String> getActiveElements()
{
Set<String> dataIds = new HashSet<String>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String min = "(" + currentTime();
String max = "+inf";
dataIds = jedis.zrangeByScore(getElementSetKey(), min, max);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return dataIds;
}
protected Set<String> getDeadElements()
{
Set<String> dataIds = new HashSet<String>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String min = "-inf";
String max = currentTime() + "";
dataIds = jedis.zrangeByScore(getElementSetKey(), min, max);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return dataIds;
}
@ -351,16 +230,4 @@ public class RedisDataRepository<T extends Data> implements DataRepository<T>
{
return Utility.currentTimeSeconds();
}
/**
* @param elements - the elements to concatenate together
* @return the concatenated form of all {@code elements}
* separated by the delimiter {@value KEY_DELIMITER}.
*/
protected String concatenate(String... elements)
{
return Utility.concatenate(KEY_DELIMITER, elements);
}
}

View File

@ -0,0 +1,70 @@
package mineplex.serverdata.redis;
import mineplex.serverdata.Region;
import mineplex.serverdata.Utility;
import mineplex.serverdata.servers.ConnectionData;
import mineplex.serverdata.servers.ServerManager;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* Repository for managing Redis Connections
* @author Shaun Bennett
*/
public class RedisRepository
{
protected static final char KEY_DELIMITER = '.';
private JedisPool _writePool;
private JedisPool _readPool;
private Region _region;
public RedisRepository(ConnectionData writeConn, ConnectionData readConn, Region region)
{
_writePool = Utility.generatePool(writeConn);
_readPool = (writeConn == readConn) ? _writePool : Utility.generatePool(readConn);
_region = region;
}
public RedisRepository(Region region)
{
this(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(), region);
}
/**
* Get a Jedis Resource from the pool. This Jedis instance needs to be closed when you are done with using it.
* Call jedis.close() or use try with resources when using getResource()
*
* @param writeable If we need to be able to write to redis. Trying to write to a non writeable jedis instance will
* throw an error.
* @return {@link Jedis} instance from pool
*/
protected Jedis getResource(boolean writeable)
{
return (writeable ? _writePool : _readPool).getResource();
}
/**
* Get the server region that this redis repository is for. The region will affect the keys for redis
* @return server region
*/
public Region getRegion()
{
return _region;
}
protected String getKey(String dataKey)
{
return concatenate("minecraft", "data", _region.name(), dataKey);
}
/**
* @param elements - the elements to concatenate together
* @return the concatenated form of all {@code elements}
* separated by the delimiter {@value KEY_DELIMITER}.
*/
protected String concatenate(String... elements)
{
return Utility.concatenate(KEY_DELIMITER, elements);
}
}

View File

@ -30,29 +30,11 @@ import redis.clients.jedis.exceptions.JedisConnectionException;
* @author Ty
*
*/
public class RedisServerRepository implements ServerRepository
public class RedisServerRepository extends RedisRepository implements ServerRepository
{
// The delimiter character used for redis key paths
public final char KEY_DELIMITER = '.';
// The pool used to retrieve jedis instances.
private JedisPool _writePool;
private JedisPool _readPool;
// The geographical region of the servers stored by this ServerRepository
private Region _region;
/**
* Class constructor
* @param host
* @param port
*/
public RedisServerRepository(ConnectionData writeConn, ConnectionData readConn, Region region)
{
_writePool = Utility.generatePool(writeConn);
_readPool = (writeConn == readConn) ? _writePool : Utility.generatePool(readConn);
_region = region;
super(writeConn, readConn, region);
}
@Override
@ -65,11 +47,10 @@ public class RedisServerRepository implements ServerRepository
public Collection<MinecraftServer> getServerStatusesByPrefix(String prefix)
{
Collection<MinecraftServer> servers = new HashSet<MinecraftServer>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String setKey = concatenate("serverstatus", "minecraft", _region.toString());
String setKey = concatenate("serverstatus", "minecraft", getRegion().toString());
Pipeline pipeline = jedis.pipelined();
List<Response<String>> responses = new ArrayList<Response<String>>();
@ -95,20 +76,7 @@ public class RedisServerRepository implements ServerRepository
}
}
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return servers;
}
@ -132,42 +100,26 @@ public class RedisServerRepository implements ServerRepository
public MinecraftServer getServerStatus(String serverName)
{
MinecraftServer server = null;
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String setKey = concatenate("serverstatus", "minecraft", _region.toString());
String setKey = concatenate("serverstatus", "minecraft", getRegion().toString());
String dataKey = concatenate(setKey, serverName);
String serializedData = jedis.get(dataKey);
server = Utility.deserialize(serializedData, MinecraftServer.class);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return server;
}
@Override
public void updataServerStatus(MinecraftServer serverData, int timeout)
{
Jedis jedis = _writePool.getResource();
try
try(Jedis jedis = getResource(true))
{
String serializedData = Utility.serialize(serverData);
String serverName = serverData.getName();
String setKey = concatenate("serverstatus", "minecraft", _region.toString());
String setKey = concatenate("serverstatus", "minecraft", getRegion().toString());
String dataKey = concatenate(setKey, serverName);
long expiry = Utility.currentTimeSeconds() + timeout;
@ -176,30 +128,15 @@ public class RedisServerRepository implements ServerRepository
transaction.zadd(setKey, expiry, serverName);
transaction.exec();
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_writePool.returnResource(jedis);
}
}
}
@Override
public void removeServerStatus(MinecraftServer serverData)
{
Jedis jedis = _writePool.getResource();
try
try(Jedis jedis = getResource(true))
{
String serverName = serverData.getName();
String setKey = concatenate("serverstatus", "minecraft", _region.toString());
String setKey = concatenate("serverstatus", "minecraft", getRegion().toString());
String dataKey = concatenate(setKey, serverName);
Transaction transaction = jedis.multi();
@ -207,19 +144,6 @@ public class RedisServerRepository implements ServerRepository
transaction.zrem(setKey, serverName);
transaction.exec();
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_writePool.returnResource(jedis);
}
}
}
@Override
@ -232,9 +156,8 @@ public class RedisServerRepository implements ServerRepository
public Collection<DedicatedServer> getDedicatedServers()
{
Collection<DedicatedServer> servers = new HashSet<DedicatedServer>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String key = concatenate("serverstatus", "dedicated");
Set<String> serverNames = jedis.smembers(key);
@ -258,7 +181,7 @@ public class RedisServerRepository implements ServerRepository
{
DedicatedServer server = new DedicatedServer(data);
if (server.getRegion() == _region)
if (server.getRegion() == getRegion())
servers.add(server);
}
catch (Exception ex)
@ -268,20 +191,7 @@ public class RedisServerRepository implements ServerRepository
}
}
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return servers;
}
@ -289,10 +199,9 @@ public class RedisServerRepository implements ServerRepository
public Collection<ServerGroup> getServerGroups(Collection<MinecraftServer> serverStatuses)
{
Collection<ServerGroup> servers = new HashSet<ServerGroup>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String key = "servergroups";
Set<String> names = jedis.smembers(key);
Set<Response<Map<String, String>>> serverDatas = new HashSet<Response<Map<String, String>>>();
@ -321,7 +230,7 @@ public class RedisServerRepository implements ServerRepository
{
ServerGroup serverGroup = new ServerGroup(data, serverStatuses);
if (serverGroup.getRegion() == Region.ALL || serverGroup.getRegion() == _region)
if (serverGroup.getRegion() == Region.ALL || serverGroup.getRegion() == getRegion())
servers.add(serverGroup);
}
catch (Exception exception)
@ -331,20 +240,7 @@ public class RedisServerRepository implements ServerRepository
}
}
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return servers;
}
@ -356,28 +252,14 @@ public class RedisServerRepository implements ServerRepository
protected Set<String> getActiveNames(String key)
{
Set<String> names = new HashSet<String>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String min = "(" + Utility.currentTimeSeconds();
String max = "+inf";
names = jedis.zrangeByScore(key, min, max);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return names;
}
@ -388,51 +270,26 @@ public class RedisServerRepository implements ServerRepository
protected Set<String> getDeadNames(String key)
{
Set<String> names = new HashSet<String>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String min = "-inf";
String max = Utility.currentTimeSeconds() + "";
names = jedis.zrangeByScore(key, min, max);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return names;
}
/**
* @param elements - the elements to concatenate together
* @return the concatenated form of all {@code elements}
* separated by the delimiter {@value KEY_DELIMITER}.
*/
protected String concatenate(String... elements)
{
return Utility.concatenate(KEY_DELIMITER, elements);
}
@Override
public Collection<MinecraftServer> getDeadServers()
{
Set<MinecraftServer> servers = new HashSet<MinecraftServer>();
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
Pipeline pipeline = jedis.pipelined();
String setKey = concatenate("serverstatus", "minecraft", _region.toString());
String setKey = concatenate("serverstatus", "minecraft", getRegion().toString());
String min = "-inf";
String max = Utility.currentTimeSeconds() + "";
@ -454,29 +311,14 @@ public class RedisServerRepository implements ServerRepository
servers.add(server);
}
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return servers;
}
@Override
public void updateServerGroup(ServerGroup serverGroup)
{
Jedis jedis = _writePool.getResource();
try
try(Jedis jedis = getResource(true))
{
HashMap<String, String> serializedData = serverGroup.getDataMap();
System.out.println(serializedData);
@ -489,27 +331,12 @@ public class RedisServerRepository implements ServerRepository
transaction.sadd(key, serverGroupName);
transaction.exec();
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_writePool.returnResource(jedis);
}
}
}
@Override
public void removeServerGroup(ServerGroup serverGroup)
{
Jedis jedis = _writePool.getResource();
try
try(Jedis jedis = getResource(true))
{
String serverName = serverGroup.getName();
String setKey = "servergroups";
@ -520,47 +347,20 @@ public class RedisServerRepository implements ServerRepository
transaction.srem(setKey, serverName);
transaction.exec();
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_writePool.returnResource(jedis);
}
}
}
@Override
public ServerGroup getServerGroup(String serverGroup)
{
ServerGroup server = null;
Jedis jedis = _readPool.getResource();
try
try(Jedis jedis = getResource(false))
{
String key = concatenate("servergroups", serverGroup);
Map<String, String> data = jedis.hgetAll(key);
server = new ServerGroup(data, null);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
_readPool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (jedis != null)
{
_readPool.returnResource(jedis);
}
}
return server;
}

View File

@ -255,27 +255,12 @@ public class VotifierManager extends MiniPlugin
{
public void run()
{
Jedis jedis = writePool.getResource();
try
try (Jedis jedis = writePool.getResource())
{
String commandType = serverCommand.getClass().getSimpleName();
String serializedCommand = Utility.serialize(serverCommand);
jedis.publish("commands.server" + ":" + commandType, serializedCommand);
}
catch (JedisConnectionException exception)
{
exception.printStackTrace();
writePool.returnBrokenResource(jedis);
jedis = null;
}
finally
{
if (writePool != null)
{
writePool.returnResource(jedis);
}
}
}
}).start();
}

View File

@ -1,4 +1,3 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
@ -140,7 +139,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
<version>2.8.1</version>
<scope>compile</scope>
</dependency>
</dependencies>