mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-11-22 20:28:00 +00:00
BLEEDING: New data caching mechanism for RedisBungee, which will enable greater performance as soon as it has been fleshed out.
This commit is contained in:
parent
befd4d0d26
commit
b37566b47f
@ -0,0 +1,264 @@
|
||||
/**
|
||||
* Copyright © 2013 tuxed <write@imaginarycode.com>
|
||||
* This work is free. You can redistribute it and/or modify it under the
|
||||
* terms of the Do What The Fuck You Want To Public License, Version 2,
|
||||
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
|
||||
*/
|
||||
package com.imaginarycode.minecraft.redisbungee;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
|
||||
import net.md_5.bungee.api.event.PostLoginEvent;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* This class manages all the data that RedisBungee fetches from Redis, along with updates to that data.
|
||||
*
|
||||
* @since 0.3.3
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class DataManager implements Listener {
|
||||
private final RedisBungee plugin;
|
||||
|
||||
/*
|
||||
* Caches of player data in Redis.
|
||||
*
|
||||
* Most of these are purged only based on size limits but are also invalidated on certain actions.
|
||||
*/
|
||||
private final Cache<UUID, String> serverCache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(5, TimeUnit.SECONDS)
|
||||
.maximumSize(2000)
|
||||
.concurrencyLevel(2)
|
||||
.build();
|
||||
|
||||
private final Cache<UUID, String> proxyCache = CacheBuilder.newBuilder()
|
||||
.maximumSize(2000)
|
||||
.concurrencyLevel(2)
|
||||
.build();
|
||||
|
||||
private final Cache<UUID, InetAddress> ipCache = CacheBuilder.newBuilder()
|
||||
.maximumSize(2000)
|
||||
.concurrencyLevel(2)
|
||||
.build();
|
||||
|
||||
private final Cache<UUID, Long> lastOnlineCache = CacheBuilder.newBuilder()
|
||||
.maximumSize(2000)
|
||||
.concurrencyLevel(2)
|
||||
.build();
|
||||
|
||||
static final UUID source = UUID.randomUUID();
|
||||
|
||||
public String getServer(UUID uuid) {
|
||||
ProxiedPlayer player = plugin.getProxy().getPlayer(uuid);
|
||||
|
||||
if (player != null)
|
||||
return player.getServer() != null ? player.getServer().getInfo().getName() : null;
|
||||
|
||||
String server = serverCache.getIfPresent(uuid);
|
||||
|
||||
if (server != null)
|
||||
return server;
|
||||
|
||||
Jedis tmpRsc = plugin.getPool().getResource();
|
||||
try {
|
||||
server = tmpRsc.hget("player:" + uuid, "server");
|
||||
|
||||
if (server == null)
|
||||
return null;
|
||||
|
||||
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);
|
||||
plugin.getPool().returnBrokenResource(tmpRsc);
|
||||
throw new RuntimeException("Unable to get server for " + uuid, e);
|
||||
} finally {
|
||||
plugin.getPool().returnResource(tmpRsc);
|
||||
}
|
||||
}
|
||||
|
||||
public String getProxy(UUID uuid) {
|
||||
ProxiedPlayer player = plugin.getProxy().getPlayer(uuid);
|
||||
|
||||
if (player != null)
|
||||
return plugin.getServerId();
|
||||
|
||||
String server = proxyCache.getIfPresent(uuid);
|
||||
|
||||
if (server != null)
|
||||
return server;
|
||||
|
||||
Jedis tmpRsc = plugin.getPool().getResource();
|
||||
try {
|
||||
server = tmpRsc.hget("player:" + uuid, "proxy");
|
||||
|
||||
if (server == null)
|
||||
return null;
|
||||
|
||||
proxyCache.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);
|
||||
plugin.getPool().returnBrokenResource(tmpRsc);
|
||||
throw new RuntimeException("Unable to get server for " + uuid, e);
|
||||
} finally {
|
||||
plugin.getPool().returnResource(tmpRsc);
|
||||
}
|
||||
}
|
||||
|
||||
public InetAddress getIp(UUID uuid) {
|
||||
ProxiedPlayer player = plugin.getProxy().getPlayer(uuid);
|
||||
|
||||
if (player != null)
|
||||
return player.getAddress().getAddress();
|
||||
|
||||
InetAddress address = ipCache.getIfPresent(uuid);
|
||||
|
||||
if (address != null)
|
||||
return address;
|
||||
|
||||
Jedis tmpRsc = plugin.getPool().getResource();
|
||||
try {
|
||||
String result = tmpRsc.hget("player:" + uuid, "ip");
|
||||
if (result != null) {
|
||||
address = InetAddress.getByName(result);
|
||||
|
||||
if (address == null)
|
||||
return null;
|
||||
|
||||
ipCache.put(uuid, address);
|
||||
return address;
|
||||
}
|
||||
return null;
|
||||
} 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);
|
||||
throw new RuntimeException("Unable to get server for " + uuid, e);
|
||||
} catch (UnknownHostException e) {
|
||||
return null;
|
||||
} finally {
|
||||
plugin.getPool().returnResource(tmpRsc);
|
||||
}
|
||||
}
|
||||
|
||||
public long getLastOnline(UUID uuid) {
|
||||
ProxiedPlayer player = plugin.getProxy().getPlayer(uuid);
|
||||
|
||||
if (player != null)
|
||||
return 0;
|
||||
|
||||
Long time = lastOnlineCache.getIfPresent(uuid);
|
||||
|
||||
if (time != null)
|
||||
return time;
|
||||
|
||||
Jedis tmpRsc = plugin.getPool().getResource();
|
||||
try {
|
||||
String result = tmpRsc.hget("player:" + uuid, "online");
|
||||
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(plugin.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) {
|
||||
// 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);
|
||||
throw new RuntimeException("Unable to get server for " + uuid, e);
|
||||
} finally {
|
||||
plugin.getPool().returnResource(tmpRsc);
|
||||
}
|
||||
}
|
||||
|
||||
private void invalidate(UUID uuid) {
|
||||
ipCache.invalidate(uuid);
|
||||
lastOnlineCache.invalidate(uuid);
|
||||
serverCache.invalidate(uuid);
|
||||
proxyCache.invalidate(uuid);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPostLogin(PostLoginEvent event) {
|
||||
// Invalidate all entries related to this player, since they now lie.
|
||||
invalidate(event.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDisconnect(PlayerDisconnectEvent event) {
|
||||
// Invalidate all entries related to this player, since they now lie.
|
||||
invalidate(event.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPubSubMessage(PubSubMessageEvent event) {
|
||||
if (!event.getChannel().equals("redisbungee-data"))
|
||||
return;
|
||||
|
||||
DataManagerMessage message = RedisBungee.getGson().fromJson(event.getMessage(), DataManagerMessage.class);
|
||||
|
||||
if (message.getSource().equals(source))
|
||||
return;
|
||||
|
||||
// For now we will just invalidate the caches. In a future version the action scope will be expanded ;)
|
||||
plugin.getLogger().info("Invaliding player " + message.getTarget() + " from PubSub");
|
||||
invalidate(message.getTarget());
|
||||
}
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
static class DataManagerMessage {
|
||||
enum Action {
|
||||
JOIN,
|
||||
LEAVE
|
||||
}
|
||||
|
||||
private final UUID target;
|
||||
private final UUID source = DataManager.source;
|
||||
private final Action action; // for future use!
|
||||
}
|
||||
}
|
@ -55,6 +55,8 @@ public final class RedisBungee extends Plugin {
|
||||
private static Gson gson = new Gson();
|
||||
@Getter
|
||||
private String serverId;
|
||||
@Getter
|
||||
private DataManager dataManager;
|
||||
|
||||
private static RedisBungeeAPI api;
|
||||
private static PubSubListener psl = null;
|
||||
@ -116,9 +118,7 @@ public final class RedisBungee extends Plugin {
|
||||
final Multimap<String, UUID> serversToPlayers() {
|
||||
ImmutableMultimap.Builder<String, UUID> multimapBuilder = ImmutableMultimap.builder();
|
||||
for (UUID p : getPlayers()) {
|
||||
ServerInfo si = getServerFor(p);
|
||||
if (si != null)
|
||||
multimapBuilder = multimapBuilder.put(si.getName(), p);
|
||||
multimapBuilder.put(dataManager.getServer(p), p);
|
||||
}
|
||||
return multimapBuilder.build();
|
||||
}
|
||||
@ -209,93 +209,6 @@ public final class RedisBungee extends Plugin {
|
||||
return ImmutableSet.copyOf(serversToPlayers().get(server));
|
||||
}
|
||||
|
||||
final ServerInfo getServerFor(@NonNull UUID uuid) {
|
||||
ServerInfo server = null;
|
||||
if (getProxy().getPlayer(uuid) != null) return getProxy().getPlayer(uuid).getServer().getInfo();
|
||||
if (pool != null) {
|
||||
Jedis tmpRsc = pool.getResource();
|
||||
try {
|
||||
String result = tmpRsc.hget("player:" + uuid, "server");
|
||||
if (result != null)
|
||||
server = getProxy().getServerInfo(result);
|
||||
} 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(tmpRsc);
|
||||
throw new RuntimeException("Unable to get server for " + uuid, e);
|
||||
} finally {
|
||||
pool.returnResource(tmpRsc);
|
||||
}
|
||||
}
|
||||
return server;
|
||||
}
|
||||
|
||||
final long getLastOnline(@NonNull UUID uuid) {
|
||||
long time = -1L;
|
||||
if (getProxy().getPlayer(uuid) != null) return 0;
|
||||
if (pool != null) {
|
||||
Jedis tmpRsc = pool.getResource();
|
||||
try {
|
||||
String result = tmpRsc.hget("player:" + uuid, "online");
|
||||
if (result != null)
|
||||
try {
|
||||
time = Long.valueOf(result);
|
||||
} catch (NumberFormatException e) {
|
||||
getLogger().info("I found a funny number for when " + uuid + " was last online!");
|
||||
boolean found = false;
|
||||
for (String proxyId : getServerIds()) {
|
||||
if (proxyId.equals(serverId)) continue;
|
||||
if (tmpRsc.sismember("proxy:" + proxyId + ":usersOnline", uuid.toString())) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
String value = "0";
|
||||
if (!found) {
|
||||
value = String.valueOf(System.currentTimeMillis());
|
||||
getLogger().info(uuid + " isn't online. Setting to current time.");
|
||||
} else {
|
||||
getLogger().info(uuid + " is online. Setting to 0. Please check your BungeeCord instances.");
|
||||
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", value);
|
||||
}
|
||||
} 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(tmpRsc);
|
||||
throw new RuntimeException("Unable to get last time online for " + uuid, e);
|
||||
} finally {
|
||||
pool.returnResource(tmpRsc);
|
||||
}
|
||||
}
|
||||
return time;
|
||||
}
|
||||
|
||||
final InetAddress getIpAddress(@NonNull UUID uuid) {
|
||||
if (getProxy().getPlayer(uuid) != null)
|
||||
return getProxy().getPlayer(uuid).getAddress().getAddress();
|
||||
InetAddress ia = null;
|
||||
if (pool != null) {
|
||||
Jedis tmpRsc = pool.getResource();
|
||||
try {
|
||||
String result = tmpRsc.hget("player:" + uuid, "ip");
|
||||
if (result != null)
|
||||
ia = InetAddress.getByName(result);
|
||||
} 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(tmpRsc);
|
||||
throw new RuntimeException("Unable to fetch IP address for " + uuid, e);
|
||||
} catch (UnknownHostException ignored) {
|
||||
// Best to just return null
|
||||
} finally {
|
||||
pool.returnResource(tmpRsc);
|
||||
}
|
||||
}
|
||||
return ia;
|
||||
}
|
||||
|
||||
final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
|
||||
checkArgument(getServerIds().contains(proxyId) || proxyId.equals("allservers"), "proxyId is invalid");
|
||||
Jedis jedis = pool.getResource();
|
||||
@ -366,6 +279,7 @@ public final class RedisBungee extends Plugin {
|
||||
}
|
||||
}, 0, 3, TimeUnit.SECONDS);
|
||||
consumer = new RedisBungeeConsumer(this);
|
||||
dataManager = new DataManager(this);
|
||||
getProxy().getScheduler().runAsync(this, consumer);
|
||||
if (configuration.getBoolean("register-bungee-commands", true)) {
|
||||
getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.GlistCommand(this));
|
||||
@ -378,6 +292,7 @@ public final class RedisBungee extends Plugin {
|
||||
getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.ServerIds());
|
||||
api = new RedisBungeeAPI(this);
|
||||
getProxy().getPluginManager().registerListener(this, new RedisBungeeListener(this));
|
||||
getProxy().getPluginManager().registerListener(this, dataManager);
|
||||
psl = new PubSubListener();
|
||||
getProxy().getScheduler().runAsync(this, psl);
|
||||
getProxy().getScheduler().schedule(this, new Runnable() {
|
||||
@ -543,7 +458,7 @@ public final class RedisBungee extends Plugin {
|
||||
try {
|
||||
rsc = pool.getResource();
|
||||
jpsh = new JedisPubSubHandler();
|
||||
rsc.subscribe(jpsh, "redisbungee-" + serverId, "redisbungee-allservers");
|
||||
rsc.subscribe(jpsh, "redisbungee-" + serverId, "redisbungee-allservers", "redisbungee-data");
|
||||
} catch (JedisException | ClassCastException ignored) {
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class RedisBungeeAPI {
|
||||
* @return the last time a player was on, if online returns a 0
|
||||
*/
|
||||
public final long getLastOnline(@NonNull UUID player) {
|
||||
return plugin.getLastOnline(player);
|
||||
return plugin.getDataManager().getLastOnline(player);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,7 +59,8 @@ public class RedisBungeeAPI {
|
||||
* @return a {@link net.md_5.bungee.api.config.ServerInfo} for the server the player is on.
|
||||
*/
|
||||
public final ServerInfo getServerFor(@NonNull UUID player) {
|
||||
return plugin.getServerFor(player);
|
||||
String server = plugin.getDataManager().getServer(player);
|
||||
return plugin.getProxy().getServerInfo(server);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -130,7 +131,7 @@ public class RedisBungeeAPI {
|
||||
* @since 0.2.4
|
||||
*/
|
||||
public final InetAddress getPlayerIp(@NonNull UUID player) {
|
||||
return plugin.getIpAddress(player);
|
||||
return plugin.getDataManager().getIp(player);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -53,12 +53,14 @@ public class RedisBungeeConsumer implements Runnable {
|
||||
pipeline.hset("player:" + event1.getPlayer().getUniqueId().toString(), "ip", event1.getPlayer().getAddress().getAddress().getHostAddress());
|
||||
plugin.getUuidTranslator().persistInfo(event1.getPlayer().getName(), event1.getPlayer().getUniqueId(), pipeline);
|
||||
pipeline.hset("player:" + event1.getPlayer().getUniqueId().toString(), "proxy", plugin.getServerId());
|
||||
pipeline.publish("redisbungee-data", RedisBungee.getGson().toJson(new DataManager.DataManagerMessage(event1.getPlayer().getUniqueId(), DataManager.DataManagerMessage.Action.JOIN)));
|
||||
pipeline.sync();
|
||||
} else if (event instanceof PlayerLoggedOffConsumerEvent) {
|
||||
PlayerLoggedOffConsumerEvent event1 = (PlayerLoggedOffConsumerEvent) event;
|
||||
Pipeline pipeline = jedis.pipelined();
|
||||
jedis.hset("player:" + event1.getPlayer().getUniqueId().toString(), "online", String.valueOf(System.currentTimeMillis()));
|
||||
RedisUtil.cleanUpPlayer(event1.getPlayer().getUniqueId().toString(), pipeline);
|
||||
pipeline.publish("redisbungee-data", RedisBungee.getGson().toJson(new DataManager.DataManagerMessage(event1.getPlayer().getUniqueId(), DataManager.DataManagerMessage.Action.LEAVE)));
|
||||
pipeline.sync();
|
||||
} else if (event instanceof PlayerChangedServerConsumerEvent) {
|
||||
PlayerChangedServerConsumerEvent event1 = (PlayerChangedServerConsumerEvent) event;
|
||||
|
@ -120,7 +120,7 @@ public class RedisBungeeListener implements Listener {
|
||||
String user = in.readUTF();
|
||||
out.writeUTF("LastOnline");
|
||||
out.writeUTF(user);
|
||||
out.writeLong(plugin.getLastOnline(plugin.getUuidTranslator().getTranslatedUuid(user, true)));
|
||||
out.writeLong(RedisBungee.getApi().getLastOnline(plugin.getUuidTranslator().getTranslatedUuid(user, true)));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user