Better handling of improperly-fetched Jedis resources.

This commit is contained in:
Tux 2014-11-26 17:26:51 -05:00
parent cb61d90d6c
commit e630116ff8
3 changed files with 53 additions and 56 deletions

View File

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

View File

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

View File

@ -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<String, UUID> 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<String, UUID> 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<String, UUID> 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<String, UUID> 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!