2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-22 20:28:00 +00:00

Introduce a set of performance tweaks.

The most notable changes are a refactor of DataManager to make the caching system easier to tweak, and the ability to use Lua scripts that run on the Redis server to optimize certain batch operations (currently, only getServersToPlayers() uses this).

This also paves the way for me to deprecate old versions of Redis. Anything older than 2.6 is ancient, and even 2.6 is ancient, so it ends up being a net overall benefit.
This commit is contained in:
Tux 2015-06-21 17:32:28 -04:00
parent 27bf52f13d
commit 8b5eacec40
7 changed files with 321 additions and 114 deletions

View File

@ -26,6 +26,8 @@
*/ */
package com.imaginarycode.minecraft.redisbungee; package com.imaginarycode.minecraft.redisbungee;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.net.InetAddresses; import com.google.common.net.InetAddresses;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
@ -34,6 +36,7 @@ import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetwork
import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.util.InternalCache;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
@ -42,12 +45,11 @@ import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler; import net.md_5.bungee.event.EventHandler;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
@ -58,149 +60,108 @@ import java.util.logging.Level;
@RequiredArgsConstructor @RequiredArgsConstructor
public class DataManager implements Listener { public class DataManager implements Listener {
private final RedisBungee plugin; private final RedisBungee plugin;
private final ConcurrentMap<UUID, String> serverCache = new ConcurrentHashMap<>(192, 0.65f, 4); private final InternalCache<UUID, String> serverCache = createCache();
private final ConcurrentMap<UUID, String> proxyCache = new ConcurrentHashMap<>(192, 0.65f, 4); private final InternalCache<UUID, String> proxyCache = createCache();
private final ConcurrentMap<UUID, InetAddress> ipCache = new ConcurrentHashMap<>(192, 0.65f, 4); private final InternalCache<UUID, InetAddress> ipCache = createCache();
private final ConcurrentMap<UUID, Long> lastOnlineCache = new ConcurrentHashMap<>(192, 0.65f, 4); private final InternalCache<UUID, Long> lastOnlineCache = createCache();
public static <K, V> InternalCache<K, V> createCache() {
return new InternalCache<>();
}
private final JsonParser parser = new JsonParser(); private final JsonParser parser = new JsonParser();
public String getServer(UUID uuid) { public String getServer(final UUID uuid) {
ProxiedPlayer player = plugin.getProxy().getPlayer(uuid); ProxiedPlayer player = plugin.getProxy().getPlayer(uuid);
if (player != null) if (player != null)
return player.getServer() != null ? player.getServer().getInfo().getName() : null; return player.getServer() != null ? player.getServer().getInfo().getName() : null;
String server = serverCache.get(uuid); try {
return serverCache.get(uuid, new Callable<String>() {
if (server != null) @Override
return server; public String call() throws Exception {
try (Jedis tmpRsc = plugin.getPool().getResource()) {
try (Jedis tmpRsc = plugin.getPool().getResource()) { return tmpRsc.hget("player:" + uuid, "server");
server = tmpRsc.hget("player:" + uuid, "server"); }
}
if (server == null) });
return null; } catch (ExecutionException e) {
plugin.getLogger().log(Level.SEVERE, "Unable to get server", e);
serverCache.put(uuid, server);
return server;
} 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);
throw new RuntimeException("Unable to get server for " + uuid, e); throw new RuntimeException("Unable to get server for " + uuid, e);
} }
} }
public String getProxy(UUID uuid) { public String getProxy(final UUID uuid) {
ProxiedPlayer player = plugin.getProxy().getPlayer(uuid); ProxiedPlayer player = plugin.getProxy().getPlayer(uuid);
if (player != null) if (player != null)
return RedisBungee.getConfiguration().getServerId(); return RedisBungee.getConfiguration().getServerId();
String server = proxyCache.get(uuid); try {
return proxyCache.get(uuid, new Callable<String>() {
if (server != null) @Override
return server; public String call() throws Exception {
try (Jedis tmpRsc = plugin.getPool().getResource()) {
try (Jedis tmpRsc = plugin.getPool().getResource()) { return tmpRsc.hget("player:" + uuid, "proxy");
server = tmpRsc.hget("player:" + uuid, "proxy"); }
}
if (server == null) });
return null; } catch (ExecutionException e) {
plugin.getLogger().log(Level.SEVERE, "Unable to get proxy", e);
proxyCache.put(uuid, server); throw new RuntimeException("Unable to get proxy for " + uuid, e);
return server;
} 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);
throw new RuntimeException("Unable to get server for " + uuid, e);
} }
} }
public InetAddress getIp(UUID uuid) { public InetAddress getIp(final UUID uuid) {
ProxiedPlayer player = plugin.getProxy().getPlayer(uuid); ProxiedPlayer player = plugin.getProxy().getPlayer(uuid);
if (player != null) if (player != null)
return player.getAddress().getAddress(); return player.getAddress().getAddress();
InetAddress address = ipCache.get(uuid); try {
return ipCache.get(uuid, new Callable<InetAddress>() {
if (address != null) @Override
return address; public InetAddress call() throws Exception {
try (Jedis tmpRsc = plugin.getPool().getResource()) {
try (Jedis tmpRsc = plugin.getPool().getResource()) { String result = tmpRsc.hget("player:" + uuid, "ip");
String result = tmpRsc.hget("player:" + uuid, "ip"); return result == null ? null : InetAddresses.forString(result);
if (result != null) { }
address = InetAddresses.forString(result); }
ipCache.put(uuid, address); });
return address; } catch (ExecutionException e) {
} plugin.getLogger().log(Level.SEVERE, "Unable to get IP", e);
return null; throw new RuntimeException("Unable to get IP for " + uuid, e);
} 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);
throw new RuntimeException("Unable to get server for " + uuid, e);
} }
} }
public long getLastOnline(UUID uuid) { public long getLastOnline(final UUID uuid) {
ProxiedPlayer player = plugin.getProxy().getPlayer(uuid); ProxiedPlayer player = plugin.getProxy().getPlayer(uuid);
if (player != null) if (player != null)
return 0; return 0;
Long time = lastOnlineCache.get(uuid); try {
return lastOnlineCache.get(uuid, new Callable<Long>() {
if (time != null) @Override
return time; public Long call() throws Exception {
try (Jedis tmpRsc = plugin.getPool().getResource()) {
try (Jedis tmpRsc = plugin.getPool().getResource()) { String result = tmpRsc.hget("player:" + uuid, "online");
String result = tmpRsc.hget("player:" + uuid, "online"); return result == null ? -1 : Long.valueOf(result);
if (result != null)
try {
time = Long.valueOf(result);
if (time == null)
return -1;
lastOnlineCache.put(uuid, time);
return time;
} catch (NumberFormatException e) {
plugin.getLogger().info("I found a funny number for when " + uuid + " was last online!");
boolean found = false;
for (String proxyId : plugin.getServerIds()) {
if (proxyId.equals(RedisBungee.getConfiguration().getServerId())) continue;
if (tmpRsc.sismember("proxy:" + proxyId + ":usersOnline", uuid.toString())) {
found = true;
break;
}
} }
long value = 0;
if (!found) {
value = System.currentTimeMillis();
plugin.getLogger().info(uuid + " isn't online. Setting to current time.");
} else {
plugin.getLogger().info(uuid + " is online. Setting to 0. Please check your BungeeCord instances.");
plugin.getLogger().info("If they are working properly, and this error does not resolve in a few minutes, please let Tux know!");
}
tmpRsc.hset("player:" + uuid, "online", Long.toString(value));
return value;
} }
return (long) -1; });
} catch (JedisConnectionException e) { } catch (ExecutionException e) {
// Redis server has disappeared! plugin.getLogger().log(Level.SEVERE, "Unable to get last time online", e);
plugin.getLogger().log(Level.SEVERE, "Unable to get connection from pool - did your Redis server go away?", e); throw new RuntimeException("Unable to get last time online for " + uuid, e);
throw new RuntimeException("Unable to get server for " + uuid, e);
} }
} }
private void invalidate(UUID uuid) { private void invalidate(UUID uuid) {
ipCache.remove(uuid); ipCache.invalidate(uuid);
lastOnlineCache.remove(uuid); lastOnlineCache.invalidate(uuid);
serverCache.remove(uuid); serverCache.invalidate(uuid);
proxyCache.remove(uuid); proxyCache.invalidate(uuid);
} }
@EventHandler @EventHandler

View File

@ -0,0 +1,45 @@
/**
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
package com.imaginarycode.minecraft.redisbungee;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class IOUtil {
public static String readInputStreamAsString(InputStream is) {
String string;
try {
string = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new AssertionError(e);
}
return string;
}
}

View File

@ -30,7 +30,9 @@ import com.google.common.base.Functions;
import com.google.common.collect.*; import com.google.common.collect.*;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.util.LuaManager;
import com.imaginarycode.minecraft.redisbungee.util.NameFetcher; import com.imaginarycode.minecraft.redisbungee.util.NameFetcher;
import com.imaginarycode.minecraft.redisbungee.util.UUIDFetcher; import com.imaginarycode.minecraft.redisbungee.util.UUIDFetcher;
import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator; import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator;
@ -53,6 +55,7 @@ import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.exceptions.JedisException;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -87,7 +90,8 @@ public final class RedisBungee extends Plugin {
private AtomicInteger nagAboutServers = new AtomicInteger(); private AtomicInteger nagAboutServers = new AtomicInteger();
private ScheduledTask integrityCheck; private ScheduledTask integrityCheck;
private ScheduledTask heartbeatTask; private ScheduledTask heartbeatTask;
private boolean usingLua;
private LuaManager.Script serverToPlayersScript;
/** /**
* Fetch the {@link RedisBungeeAPI} object created on plugin start. * Fetch the {@link RedisBungeeAPI} object created on plugin start.
@ -133,13 +137,26 @@ public final class RedisBungee extends Plugin {
} }
final Multimap<String, UUID> serversToPlayers() { final Multimap<String, UUID> serversToPlayers() {
ImmutableMultimap.Builder<String, UUID> multimapBuilder = ImmutableMultimap.builder(); if (usingLua) {
for (UUID p : getPlayers()) { String string = (String) serverToPlayersScript.eval(ImmutableList.<String>of(), getServerIds());
String name = dataManager.getServer(p); Map<String, Set<UUID>> deserialized = gson.fromJson(string, new TypeToken<Map<String, Set<UUID>>>() {}.getType());
if (name != null)
multimapBuilder.put(name, p); ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
for (Map.Entry<String, Set<UUID>> entry : deserialized.entrySet()) {
builder.putAll(entry.getKey(), entry.getValue());
}
return builder.build();
} else {
ImmutableMultimap.Builder<String, UUID> multimapBuilder = ImmutableMultimap.builder();
for (UUID p : getPlayers()) {
String name = dataManager.getServer(p);
if (name != null)
multimapBuilder.put(name, p);
}
return multimapBuilder.build();
} }
return multimapBuilder.build();
} }
final int getCount() { final int getCount() {
@ -229,6 +246,22 @@ public final class RedisBungee extends Plugin {
if (pool != null) { if (pool != null) {
try (Jedis tmpRsc = pool.getResource()) { try (Jedis tmpRsc = pool.getResource()) {
tmpRsc.hset("heartbeats", configuration.getServerId(), String.valueOf(System.currentTimeMillis())); tmpRsc.hset("heartbeats", configuration.getServerId(), String.valueOf(System.currentTimeMillis()));
// This is more portable than INFO <section>
String info = tmpRsc.info();
for (String s : info.split("\r\n")) {
if (s.startsWith("redis_version:")) {
String version = s.split(":")[1];
if (!(usingLua = RedisUtil.canUseLua(version))) {
getLogger().warning("Your version of Redis (" + version + ") is below 2.6. RedisBungee will disable optimizations using Lua.");
getLogger().warning("Support for versions of Redis below version 2.6 will be removed in the future.");
} else {
getLogger().info("Using Redis >= 2.6, enabling Lua optimizations.");
LuaManager manager = new LuaManager(this);
serverToPlayersScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/server_to_players.lua")));
}
break;
}
}
} }
serverIds = getCurrentServerIds(); serverIds = getCurrentServerIds();
uuidTranslator = new UUIDTranslator(this); uuidTranslator = new UUIDTranslator(this);

View File

@ -35,4 +35,18 @@ class RedisUtil {
rsc.hdel("player:" + player, "ip"); rsc.hdel("player:" + player, "ip");
rsc.hdel("player:" + player, "proxy"); rsc.hdel("player:" + player, "proxy");
} }
public static boolean canUseLua(String redisVersion) {
// Need to use >=2.6 to use Lua optimizations.
String[] args = redisVersion.split("\\.");
if (args.length < 2) {
return false;
}
int major = Integer.parseInt(args[0]);
int minor = Integer.parseInt(args[1]);
return major >= 3 || (major == 2 && minor >= 6);
}
} }

View File

@ -0,0 +1,64 @@
/**
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
package com.imaginarycode.minecraft.redisbungee.util;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
// I would use the Guava, but can't because I need a few more properties.
public class InternalCache<K, V> {
private final ConcurrentMap<K, V> map = new ConcurrentHashMap<>(128, 0.75f, 4);
public V get(K key, Callable<V> loader) throws ExecutionException {
V value = map.get(key);
if (value == null) {
try {
value = loader.call();
} catch (Exception e) {
throw new ExecutionException(e);
}
if (value == null)
return null;
map.putIfAbsent(key, value);
}
return value;
}
public V put(K key, V value) {
return map.put(key, value);
}
public void invalidate(K key) {
map.remove(key);
}
}

View File

@ -0,0 +1,70 @@
/**
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
package com.imaginarycode.minecraft.redisbungee.util;
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import lombok.RequiredArgsConstructor;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
import java.util.List;
@RequiredArgsConstructor
public class LuaManager {
private final RedisBungee plugin;
public Script createScript(String script) {
try (Jedis jedis = plugin.getPool().getResource()) {
String hash = jedis.scriptLoad(script);
return new Script(script, hash);
}
}
@RequiredArgsConstructor
public class Script {
private final String script;
private final String hashed;
public Object eval(List<String> keys, List<String> args) {
Object data;
try (Jedis jedis = plugin.getPool().getResource()) {
try {
data = jedis.evalsha(hashed, keys, args);
} catch (JedisDataException e) {
if (e.getMessage().startsWith("NOSCRIPT")) {
data = jedis.eval(script, keys, args);
} else {
throw e;
}
}
}
return data;
}
}
}

View File

@ -0,0 +1,20 @@
-- This script needs all active proxies available specified as args.
local serverToData = {}
for _, proxy in ipairs(ARGV) do
local players = redis.call("SMEMBERS", "proxy:" .. proxy .. ":usersOnline")
for _, player in ipairs(players) do
local server = redis.call("HGET", "player:" .. player, "server")
if server then
if serverToData[server] then
local data = serverToData[server]
data[#data + 1] = player
else
serverToData[server] = {player}
end
end
end
end
-- Redis can't map a Lua table back, so we have to send it as JSON.
return cjson.encode(serverToData)