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

make velocity init on constructor than init method, change runables to lambdas and some generic removeals with supressing unchecked for eval lua

This commit is contained in:
mohammed jasem alaajel 2022-07-08 06:47:50 +04:00
parent 2c11cb179a
commit 76787455d8
2 changed files with 246 additions and 268 deletions

View File

@ -38,6 +38,7 @@ import redis.clients.jedis.exceptions.JedisConnectionException;
import java.io.*;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@ -46,8 +47,6 @@ import java.util.logging.Level;
import static com.google.common.base.Preconditions.checkArgument;
public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin<ProxiedPlayer> {
private static final Gson gson = new Gson();
private RedisBungeeAPI api;
private PubSubListener psl = null;
private JedisSummoner jedisSummoner;
@ -81,7 +80,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
@Override
public int getCurrentCount() {
Long count = (Long) getPlayerCountScript.eval(ImmutableList.<String>of(), ImmutableList.<String>of());
Long count = (Long) getPlayerCountScript.eval(ImmutableList.of(), ImmutableList.of());
return count.intValue();
}
@ -109,7 +108,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
keys.add("proxy:" + i + ":usersOnline");
}
if (!keys.isEmpty()) {
Set<String> users = rsc.sunion(keys.toArray(new String[keys.size()]));
Set<String> users = rsc.sunion(keys.toArray(new String[0]));
if (users != null && !users.isEmpty()) {
for (String user : users) {
try {
@ -149,12 +148,11 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
}
@Override
@SuppressWarnings("unchecked")
public Multimap<String, UUID> serversToPlayers() {
try {
return serverToPlayersCache.get(SERVER_TO_PLAYERS_KEY, new Callable<Multimap<String, UUID>>() {
@Override
public Multimap<String, UUID> call() throws Exception {
Collection<String> data = (Collection<String>) serverToPlayersScript.eval(ImmutableList.<String>of(), getServerIds());
return serverToPlayersCache.get(SERVER_TO_PLAYERS_KEY, () -> {
Collection<String> data = (Collection<String>) serverToPlayersScript.eval(ImmutableList.of(), getServerIds());
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
String key = null;
for (String s : data) {
@ -168,7 +166,6 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
}
return builder.build();
}
});
} catch (ExecutionException e) {
throw new RuntimeException(e);
@ -362,12 +359,13 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
String version = s.split(":")[1];
getLogger().info(version + " <- redis version");
if (!RedisUtil.isRedisVersionRight(version)) {
getLogger().warning("Your version of Redis (" + version + ") is not at least version 6.0 RedisBungee requires a newer version of Redis.");
getLogger().severe("Your version of Redis (" + version + ") is not at least version 6.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected");
} else {
LuaManager manager = new LuaManager(this);
serverToPlayersScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/server_to_players.lua")));
getPlayerCountScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_player_count.lua")));
getLogger().info("lua manager was loaded");
}
break;
}
@ -382,9 +380,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
}
serverIds = getCurrentServerIds(true, false);
uuidTranslator = new UUIDTranslator(this);
heartbeatTask = service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
heartbeatTask = service.scheduleAtFixedRate(() -> {
try (Jedis rsc = requestJedis()) {
long redisTime = getRedisTime(rsc.time());
rsc.hset("heartbeats", configuration.getServerId(), String.valueOf(redisTime));
@ -399,16 +395,13 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
} catch (Throwable e) {
getLogger().log(Level.SEVERE, "Unable to update data - did your Redis server go away?", e);
}
}
}, 0, 3, TimeUnit.SECONDS);
dataManager = new BungeeDataManager(this);
getProxy().getPluginManager().registerListener(this, new RedisBungeeListener(this, configuration.getExemptAddresses()));
getProxy().getPluginManager().registerListener(this, dataManager);
psl = new PubSubListener(this);
getProxy().getScheduler().runAsync(this, psl);
integrityCheck = service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
integrityCheck = service.scheduleAtFixedRate(() -> {
try (Jedis tmpRsc = requestJedis()) {
Set<String> players = getLocalPlayersAsUuidStrings();
Set<String> playersInRedis = tmpRsc.smembers("proxy:" + configuration.getServerId() + ":usersOnline");
@ -467,7 +460,6 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
} catch (Throwable e) {
getLogger().log(Level.SEVERE, "Unable to fix up stored player data", e);
}
}
}, 0, 1, TimeUnit.MINUTES);
}
getProxy().registerChannel("legacy:redisbungee");
@ -513,16 +505,15 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
@Override
public void loadConfig() throws IOException {
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
if (!getDataFolder().exists() && getDataFolder().mkdir()) {
getLogger().info("data folder was created");
}
File file = new File(getDataFolder(), "config.yml");
if (!file.exists()) {
file.createNewFile();
if (!file.exists() && file.createNewFile()) {
try (InputStream in = getResourceAsStream("example_config.yml");
OutputStream out = new FileOutputStream(file)) {
OutputStream out = Files.newOutputStream(file.toPath())) {
ByteStreams.copy(in, out);
}
}
@ -574,8 +565,8 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
rsc.ping();
// If that worked, now we can check for an existing, alive Bungee:
File crashFile = new File(getDataFolder(), "restarted_from_crash.txt");
if (crashFile.exists()) {
crashFile.delete();
if (crashFile.exists() && crashFile.delete()) {
getLogger().info("crash file was deleted");
} else if (rsc.hexists("heartbeats", serverId)) {
try {
long value = Long.parseLong(rsc.hget("heartbeats", serverId));

View File

@ -8,7 +8,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.inject.Inject;
import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent;
@ -40,6 +39,7 @@ import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.exceptions.JedisConnectionException;
import static com.google.common.base.Preconditions.checkArgument;
import java.io.*;
@ -52,18 +52,16 @@ import java.util.concurrent.atomic.AtomicInteger;
@Plugin(id = "redisbungee", name = "RedisBungee", version = RBUtils.VERSION, url = "https://github.com/ProxioDev/RedisBungee", authors = "ProxioDev")
public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
private static final Gson gson = new Gson();
private final ProxyServer server;
private final Logger logger;
private final File dataFolder;
private RedisBungeeAPI api;
private PubSubListener psl = null;
private final RedisBungeeAPI api;
private final PubSubListener psl;
private JedisSummoner jedisSummoner;
private UUIDTranslator uuidTranslator;
private final UUIDTranslator uuidTranslator;
private RedisBungeeConfiguration configuration;
private VelocityDataManager dataManager;
private OkHttpClient httpClient;
private final VelocityDataManager dataManager;
private final OkHttpClient httpClient;
private volatile List<String> serverIds;
private final AtomicInteger nagAboutServers = new AtomicInteger();
private final AtomicInteger globalPlayerCount = new AtomicInteger();
@ -83,6 +81,44 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
this.server = server;
this.logger = logger;
this.dataFolder = dataDirectory.toFile();
try {
loadConfig();
} catch (IOException e) {
throw new RuntimeException("Unable to load/save config", e);
} catch (JedisConnectionException e) {
throw new RuntimeException("Unable to connect to your Redis server!", e);
}
this.api = new RedisBungeeAPI(this);
uuidTranslator = new UUIDTranslator(this);
dataManager = new VelocityDataManager(this);
psl = new PubSubListener(this);
this.httpClient = new OkHttpClient();
Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(6));
this.httpClient.setDispatcher(dispatcher);
NameFetcher.setHttpClient(httpClient);
UUIDFetcher.setHttpClient(httpClient);
// keeping this lol
new RedisBungee(api);
// check if redis version compatible
try (Jedis jedis = requestJedis()) {
String info = jedis.info();
for (String s : info.split("\r\n")) {
if (s.startsWith("redis_version:")) {
String version = s.split(":")[1];
getLogger().info(version + " <- redis version");
if (!RedisUtil.isRedisVersionRight(version)) {
getLogger().error("Your version of Redis (" + version + ") is not at least version 6.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected");
} else {
LuaManager manager = new LuaManager(this);
serverToPlayersScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/server_to_players.lua")));
getPlayerCountScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_player_count.lua")));
getLogger().info("lua manager was loaded");
}
break;
}
}
}
}
@ -99,7 +135,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
@Override
public int getCurrentCount() {
Long count = (Long) getPlayerCountScript.eval(ImmutableList.<String>of(), ImmutableList.<String>of());
Long count = (Long) getPlayerCountScript.eval(ImmutableList.of(), ImmutableList.of());
return count.intValue();
}
@ -127,7 +163,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
keys.add("proxy:" + i + ":usersOnline");
}
if (!keys.isEmpty()) {
Set<String> users = rsc.sunion(keys.toArray(new String[keys.size()]));
Set<String> users = rsc.sunion(keys.toArray(new String[0]));
if (users != null && !users.isEmpty()) {
for (String user : users) {
try {
@ -167,12 +203,11 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
}
@Override
@SuppressWarnings("unchecked")
public Multimap<String, UUID> serversToPlayers() {
try {
return serverToPlayersCache.get(SERVER_TO_PLAYERS_KEY, new Callable<Multimap<String, UUID>>() {
@Override
public Multimap<String, UUID> call() throws Exception {
Collection<String> data = (Collection<String>) serverToPlayersScript.eval(ImmutableList.<String>of(), getServerIds());
return serverToPlayersCache.get(SERVER_TO_PLAYERS_KEY, () -> {
Collection<String> data = (Collection<String>) serverToPlayersScript.eval(ImmutableList.of(), getServerIds());
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
String key = null;
for (String s : data) {
@ -186,7 +221,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
}
return builder.build();
}
});
} catch (ExecutionException e) {
throw new RuntimeException(e);
@ -349,48 +383,16 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
@Override
public void initialize() {
try {
loadConfig();
} catch (IOException e) {
throw new RuntimeException("Unable to load/save config", e);
} catch (JedisConnectionException e) {
throw new RuntimeException("Unable to connect to your Redis server!", e);
}
this.api = new RedisBungeeAPI(this);
// call old plugin class to support old plugins
new RedisBungee(api);
if (isJedisAvailable()) {
// set the first heartbeat
try (Jedis tmpRsc = requestJedis()) {
// 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];
getLogger().info(version + " <- redis version");
if (!RedisUtil.isRedisVersionRight(version)) {
getLogger().warn("Your version of Redis (" + version + ") is not at least version 6.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected");
} else {
LuaManager manager = new LuaManager(this);
serverToPlayersScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/server_to_players.lua")));
getPlayerCountScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_player_count.lua")));
}
break;
}
}
tmpRsc.hset("heartbeats", configuration.getServerId(), tmpRsc.time().get(0));
long uuidCacheSize = tmpRsc.hlen("uuid-cache");
if (uuidCacheSize > 750000) {
getLogger().info("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
}
}
serverIds = getCurrentServerIds(true, false);
uuidTranslator = new UUIDTranslator(this);
heartbeatTask = getProxy().getScheduler().buildTask(this, new Runnable() {
@Override
public void run() {
heartbeatTask = getProxy().getScheduler().buildTask(this, () -> {
try (Jedis rsc = requestJedis()) {
long redisTime = getRedisTime(rsc.time());
rsc.hset("heartbeats", configuration.getServerId(), String.valueOf(redisTime));
@ -405,16 +407,12 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
} catch (Throwable e) {
getLogger().error("Unable to update data - did your Redis server go away?", e);
}
}
}).repeat(3, TimeUnit.SECONDS).schedule();
dataManager = new VelocityDataManager(this);
getProxy().getEventManager().register(this, new RedisBungeeListener(this, configuration.getExemptAddresses()));
getProxy().getEventManager().register(this, dataManager);
psl = new PubSubListener(this);
getProxy().getScheduler().buildTask(this, psl).schedule();
integrityCheck = getProxy().getScheduler().buildTask(this,new Runnable() {
@Override
public void run() {
integrityCheck = getProxy().getScheduler().buildTask(this, () -> {
try (Jedis tmpRsc = requestJedis()) {
Set<String> players = getLocalPlayersAsUuidStrings();
Set<String> playersInRedis = tmpRsc.smembers("proxy:" + configuration.getServerId() + ":usersOnline");
@ -473,9 +471,8 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
} catch (Throwable e) {
getLogger().error("Unable to fix up stored player data", e);
}
}
}).repeat(1, TimeUnit.MINUTES).schedule();
}
// plugin messages are disabled for now
//getProxy().registerChannel("legacy:redisbungee");
//getProxy().registerChannel("RedisBungee");
@ -527,20 +524,18 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
@Override
public void loadConfig() throws IOException {
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
if (!getDataFolder().exists() && getDataFolder().mkdir()) {
getLogger().info("data folder was created");
}
File file = new File(getDataFolder(), "config.yml");
if (!file.exists()) {
file.createNewFile();
if (!file.exists() && file.createNewFile()) {
getLogger().info("config file was created");
try (InputStream in = getResourceAsStream("example_config.yml");
OutputStream out = Files.newOutputStream(file.toPath())) {
ByteStreams.copy(in, out);
}
}
final YAMLConfigurationLoader yamlConfiguration = YAMLConfigurationLoader.builder().setFile(file).build();
ConfigurationNode node = yamlConfiguration.load();
final String redisServer = node.getNode("redis-server").getString();
final int redisPort = node.getNode("redis-port").getInt();
@ -591,8 +586,8 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
rsc.ping();
// If that worked, now we can check for an existing, alive Bungee:
File crashFile = new File(getDataFolder(), "restarted_from_crash.txt");
if (crashFile.exists()) {
crashFile.delete();
if (crashFile.exists() && crashFile.delete()) {
getLogger().info("crash file was deleted");
} else if (rsc.hexists("heartbeats", serverId)) {
try {
long value = Long.parseLong(rsc.hget("heartbeats", serverId));
@ -606,14 +601,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
} catch (NumberFormatException ignored) {
}
}
httpClient = new OkHttpClient();
Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(6));
httpClient.setDispatcher(dispatcher);
NameFetcher.setHttpClient(httpClient);
UUIDFetcher.setHttpClient(httpClient);
getLogger().info("Successfully connected to Redis.");
} catch (JedisConnectionException e) {
this.jedisSummoner.close();