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

git rid of lua system

This commit is contained in:
mohammed jasem alaajel 2022-07-26 12:40:14 +04:00
parent 86c6e9464d
commit e7b241edd6
8 changed files with 23 additions and 140 deletions

View File

@ -12,8 +12,11 @@ import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil; import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils; import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator;
import org.checkerframework.checker.units.qual.A;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
import java.net.InetAddress; import java.net.InetAddress;
@ -67,7 +70,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
@Override @Override
public Long clusterJedisTask(JedisCluster jedisCluster) { public Long clusterJedisTask(JedisCluster jedisCluster) {
long total = 0; long total = 0;
long redisTime = getRedisTime(); long redisTime = getRedisTime(jedisCluster);
Map<String, String> heartBeats = jedisCluster.hgetAll("heartbeats"); Map<String, String> heartBeats = jedisCluster.hgetAll("heartbeats");
for (Map.Entry<String, String> stringStringEntry : heartBeats.entrySet()) { for (Map.Entry<String, String> stringStringEntry : heartBeats.entrySet()) {
String k = stringStringEntry.getKey(); String k = stringStringEntry.getKey();
@ -250,7 +253,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
@Override @Override
public List<String> clusterJedisTask(JedisCluster jedisCluster) { public List<String> clusterJedisTask(JedisCluster jedisCluster) {
try { try {
long time = getRedisTime(); long time = getRedisTime(jedisCluster);
ImmutableList.Builder<String> servers = ImmutableList.builder(); ImmutableList.Builder<String> servers = ImmutableList.builder();
Map<String, String> heartbeats = jedisCluster.hgetAll("heartbeats"); Map<String, String> heartbeats = jedisCluster.hgetAll("heartbeats");
for (Map.Entry<String, String> entry : heartbeats.entrySet()) { for (Map.Entry<String, String> entry : heartbeats.entrySet()) {
@ -337,6 +340,12 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
sendProxyCommand(getConfiguration().getProxyId(), cmd); sendProxyCommand(getConfiguration().getProxyId(), cmd);
} }
default Long getRedisTime(UnifiedJedis unifiedJedis) {
List<Object> data = (List<Object>) unifiedJedis.sendCommand(Protocol.Command.TIME);
List<String> times = new ArrayList<>();
data.forEach((o) -> times.add(new String((byte[])o)));
return getRedisTime(times);
}
default long getRedisTime(List<String> timeRes) { default long getRedisTime(List<String> timeRes) {
return Long.parseLong(timeRes.get(0)); return Long.parseLong(timeRes.get(0));
} }
@ -368,8 +377,6 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
RedisBungeeMode getRedisBungeeMode(); RedisBungeeMode getRedisBungeeMode();
Long getRedisTime();
void updateProxyIds(); void updateProxyIds();
} }

View File

@ -43,7 +43,7 @@ public class HeartbeatTask extends RedisTask<Void>{
@Override @Override
public Void clusterJedisTask(JedisCluster jedisCluster) { public Void clusterJedisTask(JedisCluster jedisCluster) {
try { try {
long redisTime = plugin.getRedisTime(); long redisTime = plugin.getRedisTime(jedisCluster);
jedisCluster.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime)); jedisCluster.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
// Redis server has disappeared! // Redis server has disappeared!

View File

@ -2,24 +2,20 @@ package com.imaginarycode.minecraft.redisbungee.api.tasks;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil; import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
import com.imaginarycode.minecraft.redisbungee.api.util.io.IOUtil;
import com.imaginarycode.minecraft.redisbungee.api.util.lua.LuaManager;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.Protocol;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections;
public class InitialUtils { public class InitialUtils {
public static LuaManager.Script getTimeScript(RedisBungeePlugin<?> plugin, LuaManager luaManager) { public static void checkRedisVersion(RedisBungeePlugin<?> plugin) {
return new RedisTask<LuaManager.Script>(plugin) { new RedisTask<Void>(plugin) {
@Override @Override
public LuaManager.Script jedisTask(Jedis jedis) { public Void jedisTask(Jedis jedis) {
// This is more portable than INFO <section> // This is more portable than INFO <section>
String info = jedis.info(); String info = jedis.info();
for (String s : info.split("\r\n")) { for (String s : info.split("\r\n")) {
@ -41,15 +37,10 @@ public class InitialUtils {
} }
@Override @Override
public LuaManager.Script clusterJedisTask(JedisCluster jedisCluster) { public Void clusterJedisTask(JedisCluster jedisCluster) {
// This is more portable than INFO <section> // This is more portable than INFO <section>
LuaManager.Script getRedisClusterTimeScript;
try { String info = new String((byte[]) jedisCluster.sendCommand(Protocol.Command.INFO));
getRedisClusterTimeScript = luaManager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_time.lua")));
} catch (JedisException e) {
throw new RuntimeException("possible not supported redis version", e);
}
String info = (String) luaManager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_info.lua"))).eval(Collections.singletonList("0"), Collections.emptyList());
for (String s : info.split("\r\n")) { for (String s : info.split("\r\n")) {
if (s.startsWith("redis_version:")) { if (s.startsWith("redis_version:")) {
String version = s.split(":")[1]; String version = s.split(":")[1];
@ -65,7 +56,7 @@ public class InitialUtils {
break; break;
} }
} }
return getRedisClusterTimeScript; return null;
} }
}.execute(); }.execute();
} }
@ -110,7 +101,7 @@ public class InitialUtils {
} else if (jedisCluster.hexists("heartbeats", plugin.getConfiguration().getProxyId())) { } else if (jedisCluster.hexists("heartbeats", plugin.getConfiguration().getProxyId())) {
try { try {
long value = Long.parseLong(jedisCluster.hget("heartbeats", plugin.getConfiguration().getProxyId())); long value = Long.parseLong(jedisCluster.hget("heartbeats", plugin.getConfiguration().getProxyId()));
long redisTime = plugin.getRedisTime(); long redisTime = plugin.getRedisTime(jedisCluster);
if (redisTime < value + RedisUtil.PROXY_TIMEOUT) { if (redisTime < value + RedisUtil.PROXY_TIMEOUT) {
logImposter(plugin); logImposter(plugin);
@ -130,7 +121,4 @@ public class InitialUtils {
plugin.logFatal("If this instance is coming up from a crash, create a file in your RedisBungee plugins directory with the name 'restarted_from_crash.txt' and RedisBungee will not perform this check."); plugin.logFatal("If this instance is coming up from a crash, create a file in your RedisBungee plugins directory with the name 'restarted_from_crash.txt' and RedisBungee will not perform this check.");
} }
private static InputStream getResourceAsStream(String resource) {
return InitialUtils.class.getClassLoader().getResourceAsStream(resource);
}
} }

View File

@ -1,91 +0,0 @@
package com.imaginarycode.minecraft.redisbungee.api.util.lua;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.exceptions.JedisDataException;
import java.util.List;
public class LuaManager {
private final RedisBungeePlugin<?> plugin;
public LuaManager(RedisBungeePlugin<?> plugin) {
this.plugin = plugin;
}
public Script createScript(String script) {
RedisTask<Script> scriptRedisTask = new RedisTask<Script>(plugin.getApi()) {
@Override
public Script jedisTask(Jedis jedis) {
String hash = jedis.scriptLoad(script);
return new Script(script, hash);
}
@Override
public Script clusterJedisTask(JedisCluster jedisCluster) {
String hash = jedisCluster.scriptLoad(script, "0");
return new Script(script, hash);
}
};
return scriptRedisTask.execute();
}
public class Script {
private final String script;
private final String hashed;
public Script(String script, String hashed) {
this.script = script;
this.hashed = hashed;
}
public String getScript() {
return script;
}
public String getHashed() {
return hashed;
}
public Object eval(List<String> keys, List<String> args) {
RedisTask<Object> objectRedisTask = new RedisTask<Object>(plugin.getApi()) {
@Override
public Object jedisTask(Jedis jedis) {
Object data;
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;
}
@Override
public Object clusterJedisTask(JedisCluster jedisCluster) {
Object data;
try {
data = jedisCluster.evalsha(hashed, keys, args);
} catch (JedisDataException e) {
if (e.getMessage().startsWith("NOSCRIPT")) {
data = jedisCluster.eval(script, keys, args);
} else {
throw e;
}
}
return data;
}
};
return objectRedisTask.execute();
}
}
}

View File

@ -1 +0,0 @@
return redis.call("INFO")

View File

@ -1 +0,0 @@
return redis.call('TIME')

View File

@ -13,7 +13,6 @@ 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.api.*; import com.imaginarycode.minecraft.redisbungee.api.*;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.util.lua.LuaManager;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.NameFetcher; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.NameFetcher;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDFetcher; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDFetcher;
@ -50,7 +49,6 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
private final AtomicInteger globalPlayerCount = new AtomicInteger(); private final AtomicInteger globalPlayerCount = new AtomicInteger();
private Future<?> integrityCheck; private Future<?> integrityCheck;
private Future<?> heartbeatTask; private Future<?> heartbeatTask;
private LuaManager.Script getRedisClusterTimeScript;
private static final Object SERVER_TO_PLAYERS_KEY = new Object(); private static final Object SERVER_TO_PLAYERS_KEY = new Object();
private final Cache<Object, Multimap<String, UUID>> serverToPlayersCache = CacheBuilder.newBuilder() private final Cache<Object, Multimap<String, UUID>> serverToPlayersCache = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.SECONDS) .expireAfterWrite(5, TimeUnit.SECONDS)
@ -209,10 +207,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
httpClient.setDispatcher(dispatcher); httpClient.setDispatcher(dispatcher);
NameFetcher.setHttpClient(httpClient); NameFetcher.setHttpClient(httpClient);
UUIDFetcher.setHttpClient(httpClient); UUIDFetcher.setHttpClient(httpClient);
// init lua manager InitialUtils.checkRedisVersion(this);
LuaManager luaManager = new LuaManager(this);
this.getRedisClusterTimeScript = InitialUtils.getTimeScript(this, luaManager);
getLogger().info("lua manager was loaded");
// check if this proxy is recovering from a crash and start heart the beat. // check if this proxy is recovering from a crash and start heart the beat.
InitialUtils.checkIfRecovering(this, getDataFolder().toPath()); InitialUtils.checkIfRecovering(this, getDataFolder().toPath());
updateProxyIds(); updateProxyIds();
@ -297,10 +292,6 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
return this.redisBungeeMode; return this.redisBungeeMode;
} }
@Override
public Long getRedisTime() {
return getRedisTime((List<String>) this.getRedisClusterTimeScript.eval(Collections.singletonList("0"), Collections.emptyList()));
}
@Override @Override
public void updateProxyIds() { public void updateProxyIds() {

View File

@ -9,7 +9,6 @@ import com.imaginarycode.minecraft.redisbungee.api.*;
import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.tasks.*; import com.imaginarycode.minecraft.redisbungee.api.tasks.*;
import com.imaginarycode.minecraft.redisbungee.api.util.lua.LuaManager;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.NameFetcher; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.NameFetcher;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDFetcher; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDFetcher;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator; import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator;
@ -61,8 +60,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
private ScheduledTask integrityCheck; private ScheduledTask integrityCheck;
private ScheduledTask heartbeatTask; private ScheduledTask heartbeatTask;
private final LuaManager.Script getRedisTimeScript;
private static final Object SERVER_TO_PLAYERS_KEY = new Object(); private static final Object SERVER_TO_PLAYERS_KEY = new Object();
public static final List<ChannelIdentifier> IDENTIFIERS = List.of( public static final List<ChannelIdentifier> IDENTIFIERS = List.of(
MinecraftChannelIdentifier.create("legacy", "redisbungee"), MinecraftChannelIdentifier.create("legacy", "redisbungee"),
@ -88,9 +85,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
throw new RuntimeException("Unable to connect to your Redis server!", e); throw new RuntimeException("Unable to connect to your Redis server!", e);
} }
this.api = new RedisBungeeAPI(this); this.api = new RedisBungeeAPI(this);
LuaManager luaManager = new LuaManager(this); InitialUtils.checkRedisVersion(this);
this.getRedisTimeScript = InitialUtils.getTimeScript(this, luaManager);
getLogger().info("lua manager was loaded");
// check if this proxy is recovering from a crash and start heart the beat. // check if this proxy is recovering from a crash and start heart the beat.
InitialUtils.checkIfRecovering(this, getDataFolder()); InitialUtils.checkIfRecovering(this, getDataFolder());
uuidTranslator = new UUIDTranslator(this); uuidTranslator = new UUIDTranslator(this);
@ -327,11 +322,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
return this.redisBungeeMode; return this.redisBungeeMode;
} }
@Override
public Long getRedisTime() {
return getRedisTime((List<String>) this.getRedisTimeScript.eval(Collections.singletonList("0"), Collections.emptyList()));
}
@Override @Override
public void updateProxyIds() { public void updateProxyIds() {
this.proxiesIds = this.getCurrentProxiesIds(false); this.proxiesIds = this.getCurrentProxiesIds(false);