diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java index a6f7fd59b..1de16eec2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java @@ -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; } diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Utility.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Utility.java index f80fe0b7c..8d8f2daf3 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Utility.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/Utility.java @@ -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(); } diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/ServerCommandManager.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/ServerCommandManager.java index 29abc70ce..0b0779035 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/ServerCommandManager.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/ServerCommandManager.java @@ -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(); } diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java index caea6188b..5ccfd291a 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java @@ -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 implements DataRepository +public class RedisDataRepository extends RedisRepository implements DataRepository { - - // 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 _elementType; - // A unique label designating the elements and this repository. private String _elementLabel; @@ -43,19 +28,14 @@ public class RedisDataRepository implements DataRepository * Class constructor * @param writeConn * @param readConn - * @param host - * @param port * @param region */ public RedisDataRepository(ConnectionData writeConn, ConnectionData readConn, Region region, Class 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 elementType, String elementLabel) @@ -71,7 +51,7 @@ public class RedisDataRepository implements DataRepository 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 implements DataRepository public Collection getElements(Collection dataIds) { Collection elements = new HashSet(); - Jedis jedis = _readPool.getResource(); - try + try(Jedis jedis = getResource(false)) { Pipeline pipeline = jedis.pipelined(); @@ -120,20 +99,7 @@ public class RedisDataRepository implements DataRepository } } } - 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 implements DataRepository 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 implements DataRepository 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 implements DataRepository @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 implements DataRepository 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 implements DataRepository @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 implements DataRepository transaction.exec(); } } - catch (JedisConnectionException exception) - { - exception.printStackTrace(); - _writePool.returnBrokenResource(jedis); - jedis = null; - } - finally - { - if (jedis != null) - { - _writePool.returnResource(jedis); - } - } - + return 0; } protected Set getActiveElements() { Set dataIds = new HashSet(); - 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 getDeadElements() { Set dataIds = new HashSet(); - 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 implements DataRepository { 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); - } - - } diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisRepository.java new file mode 100644 index 000000000..dd43e1fb0 --- /dev/null +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisRepository.java @@ -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); + } +} diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisServerRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisServerRepository.java index 50b3bee6a..15120076e 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisServerRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisServerRepository.java @@ -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 getServerStatusesByPrefix(String prefix) { Collection servers = new HashSet(); - 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> responses = new ArrayList>(); @@ -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 getDedicatedServers() { Collection servers = new HashSet(); - Jedis jedis = _readPool.getResource(); - - try + + try(Jedis jedis = getResource(false)) { String key = concatenate("serverstatus", "dedicated"); Set 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 getServerGroups(Collection serverStatuses) { Collection servers = new HashSet(); - Jedis jedis = _readPool.getResource(); - try + + try(Jedis jedis = getResource(false)) { - String key = "servergroups"; Set names = jedis.smembers(key); Set>> serverDatas = new HashSet>>(); @@ -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 getActiveNames(String key) { Set names = new HashSet(); - 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 getDeadNames(String key) { Set names = new HashSet(); - 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 getDeadServers() { Set servers = new HashSet(); - 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 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 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; } diff --git a/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java b/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java index 23801e02d..de1e9e7c7 100644 --- a/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java +++ b/Plugins/Mineplex.Votifier/src/mineplex/votifier/VotifierManager.java @@ -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(); } diff --git a/Plugins/pom.xml b/Plugins/pom.xml index ed927b9ac..fc85e9849 100644 --- a/Plugins/pom.xml +++ b/Plugins/pom.xml @@ -1,4 +1,3 @@ - 4.0.0 @@ -140,7 +139,7 @@ redis.clients jedis - 2.6.2 + 2.8.1 compile