From e630116ff8559da64406c76d9d7fd3c170ea713c Mon Sep 17 00:00:00 2001 From: Tux Date: Wed, 26 Nov 2014 17:26:51 -0500 Subject: [PATCH] Better handling of improperly-fetched Jedis resources. --- .../minecraft/redisbungee/DataManager.java | 12 ++- .../minecraft/redisbungee/RedisBungee.java | 24 +++--- .../redisbungee/util/UUIDTranslator.java | 73 +++++++++---------- 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java index 74813fb..e915c57 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java @@ -68,7 +68,8 @@ public class DataManager implements Listener { } catch (JedisConnectionException e) { // Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - plugin.getPool().returnBrokenResource(tmpRsc); + if (tmpRsc != null) + plugin.getPool().returnBrokenResource(tmpRsc); throw new RuntimeException("Unable to get server for " + uuid, e); } finally { plugin.getPool().returnResource(tmpRsc); @@ -98,7 +99,8 @@ public class DataManager implements Listener { } catch (JedisConnectionException e) { // Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - plugin.getPool().returnBrokenResource(tmpRsc); + if (tmpRsc != null) + plugin.getPool().returnBrokenResource(tmpRsc); throw new RuntimeException("Unable to get server for " + uuid, e); } finally { plugin.getPool().returnResource(tmpRsc); @@ -132,7 +134,8 @@ public class DataManager implements Listener { } catch (JedisConnectionException e) { // Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - plugin.getPool().returnBrokenResource(tmpRsc); + if (tmpRsc != null) + plugin.getPool().returnBrokenResource(tmpRsc); throw new RuntimeException("Unable to get server for " + uuid, e); } catch (UnknownHostException e) { return null; @@ -191,7 +194,8 @@ public class DataManager implements Listener { } catch (JedisConnectionException e) { // Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - plugin.getPool().returnBrokenResource(tmpRsc); + if (tmpRsc != null) + plugin.getPool().returnBrokenResource(tmpRsc); throw new RuntimeException("Unable to get server for " + uuid, e); } finally { plugin.getPool().returnResource(tmpRsc); diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index c21fcb3..8a5f006 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -106,7 +106,8 @@ public final class RedisBungee extends Plugin { return servers.build(); } catch (JedisConnectionException e) { getLogger().log(Level.SEVERE, "Unable to fetch all server IDs", e); - pool.returnBrokenResource(jedis); + if (jedis != null) + pool.returnBrokenResource(jedis); return Collections.singletonList(serverId); } finally { pool.returnResource(jedis); @@ -135,7 +136,8 @@ public final class RedisBungee extends Plugin { } catch (JedisConnectionException e) { // Redis server has disappeared! getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - pool.returnBrokenResource(rsc); + if (rsc != null) + pool.returnBrokenResource(rsc); throw new RuntimeException("Unable to get total player count", e); } finally { pool.returnResource(rsc); @@ -181,7 +183,8 @@ public final class RedisBungee extends Plugin { } catch (JedisConnectionException e) { // Redis server has disappeared! getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - pool.returnBrokenResource(rsc); + if (rsc != null) + pool.returnBrokenResource(rsc); throw new RuntimeException("Unable to get all players online", e); } finally { pool.returnResource(rsc); @@ -197,17 +200,7 @@ public final class RedisBungee extends Plugin { final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) { checkArgument(getServerIds().contains(proxyId) || proxyId.equals("allservers"), "proxyId is invalid"); - Jedis jedis = pool.getResource(); - try { - jedis.publish("redisbungee-" + proxyId, command); - } catch (JedisConnectionException e) { - // Redis server has disappeared! - getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - pool.returnBrokenResource(jedis); - throw new RuntimeException("Unable to publish command", e); - } finally { - pool.returnResource(jedis); - } + sendChannelMessage("redisbungee-" + proxyId, command); } final void sendChannelMessage(String channel, String message) { @@ -217,7 +210,8 @@ public final class RedisBungee extends Plugin { } catch (JedisConnectionException e) { // Redis server has disappeared! getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); - pool.returnBrokenResource(jedis); + if (jedis != null) + pool.returnBrokenResource(jedis); throw new RuntimeException("Unable to publish channel message", e); } finally { pool.returnResource(jedis); diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDTranslator.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDTranslator.java index c72f847..516197c 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDTranslator.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDTranslator.java @@ -13,6 +13,7 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import net.md_5.bungee.api.ProxyServer; import redis.clients.jedis.Jedis; +import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import java.util.Calendar; @@ -79,46 +80,44 @@ public final class UUIDTranslator { // Let's try Redis. Jedis jedis = plugin.getPool().getResource(); try { + String stored = jedis.hget("uuid-cache", player.toLowerCase()); + if (stored != null) { + // Found an entry value. Deserialize it. + CachedUUIDEntry entry = RedisBungee.getGson().fromJson(stored, CachedUUIDEntry.class); + + // Check for expiry: + if (entry.expired()) { + jedis.hdel("uuid-cache", player.toLowerCase()); + } else { + nameToUuidMap.put(player.toLowerCase(), entry); + uuidToNameMap.put(entry.getUuid(), entry); + return entry.getUuid(); + } + } + + // That didn't work. Let's ask Mojang. + if (!expensiveLookups) + return null; + + Map uuidMap1; try { - String stored = jedis.hget("uuid-cache", player.toLowerCase()); - if (stored != null) { - // Found an entry value. Deserialize it. - CachedUUIDEntry entry = RedisBungee.getGson().fromJson(stored, CachedUUIDEntry.class); - - // Check for expiry: - if (entry.expired()) { - jedis.hdel("uuid-cache", player.toLowerCase()); - } else { - nameToUuidMap.put(player.toLowerCase(), entry); - uuidToNameMap.put(entry.getUuid(), entry); - return entry.getUuid(); - } - } - - // That didn't work. Let's ask Mojang. - if (!expensiveLookups) - return null; - - Map uuidMap1; - try { - uuidMap1 = new UUIDFetcher(Collections.singletonList(player)).call(); - } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "Unable to fetch UUID from Mojang for " + player, e); - return null; - } - for (Map.Entry entry : uuidMap1.entrySet()) { - if (entry.getKey().equalsIgnoreCase(player)) { - persistInfo(entry.getKey(), entry.getValue(), jedis); - return entry.getValue(); - } - } - } catch (JedisException e) { - plugin.getLogger().log(Level.SEVERE, "Unable to fetch UUID for " + player, e); - // Go ahead and give them what we have. + uuidMap1 = new UUIDFetcher(Collections.singletonList(player)).call(); + } catch (Exception e) { + plugin.getLogger().log(Level.SEVERE, "Unable to fetch UUID from Mojang for " + player, e); return null; } - } finally { - plugin.getPool().returnResource(jedis); + for (Map.Entry entry : uuidMap1.entrySet()) { + if (entry.getKey().equalsIgnoreCase(player)) { + persistInfo(entry.getKey(), entry.getValue(), jedis); + return entry.getValue(); + } + } + } catch (JedisException e) { + plugin.getLogger().log(Level.SEVERE, "Unable to fetch UUID for " + player, e); + if (jedis != null) + plugin.getPool().returnBrokenResource(jedis); + // Go ahead and give them what we have. + return null; } return null; // Nope, game over!