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.payload.PayloadUtils;
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.JedisCluster;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
import java.net.InetAddress;
@ -67,7 +70,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
@Override
public Long clusterJedisTask(JedisCluster jedisCluster) {
long total = 0;
long redisTime = getRedisTime();
long redisTime = getRedisTime(jedisCluster);
Map<String, String> heartBeats = jedisCluster.hgetAll("heartbeats");
for (Map.Entry<String, String> stringStringEntry : heartBeats.entrySet()) {
String k = stringStringEntry.getKey();
@ -250,7 +253,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
@Override
public List<String> clusterJedisTask(JedisCluster jedisCluster) {
try {
long time = getRedisTime();
long time = getRedisTime(jedisCluster);
ImmutableList.Builder<String> servers = ImmutableList.builder();
Map<String, String> heartbeats = jedisCluster.hgetAll("heartbeats");
for (Map.Entry<String, String> entry : heartbeats.entrySet()) {
@ -337,6 +340,12 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
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) {
return Long.parseLong(timeRes.get(0));
}
@ -368,8 +377,6 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
RedisBungeeMode getRedisBungeeMode();
Long getRedisTime();
void updateProxyIds();
}

View File

@ -43,7 +43,7 @@ public class HeartbeatTask extends RedisTask<Void>{
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
long redisTime = plugin.getRedisTime();
long redisTime = plugin.getRedisTime(jedisCluster);
jedisCluster.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) {
// 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.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.JedisCluster;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.Protocol;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
public class InitialUtils {
public static LuaManager.Script getTimeScript(RedisBungeePlugin<?> plugin, LuaManager luaManager) {
return new RedisTask<LuaManager.Script>(plugin) {
public static void checkRedisVersion(RedisBungeePlugin<?> plugin) {
new RedisTask<Void>(plugin) {
@Override
public LuaManager.Script jedisTask(Jedis jedis) {
public Void jedisTask(Jedis jedis) {
// This is more portable than INFO <section>
String info = jedis.info();
for (String s : info.split("\r\n")) {
@ -41,15 +37,10 @@ public class InitialUtils {
}
@Override
public LuaManager.Script clusterJedisTask(JedisCluster jedisCluster) {
public Void clusterJedisTask(JedisCluster jedisCluster) {
// This is more portable than INFO <section>
LuaManager.Script getRedisClusterTimeScript;
try {
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());
String info = new String((byte[]) jedisCluster.sendCommand(Protocol.Command.INFO));
for (String s : info.split("\r\n")) {
if (s.startsWith("redis_version:")) {
String version = s.split(":")[1];
@ -65,7 +56,7 @@ public class InitialUtils {
break;
}
}
return getRedisClusterTimeScript;
return null;
}
}.execute();
}
@ -110,7 +101,7 @@ public class InitialUtils {
} else if (jedisCluster.hexists("heartbeats", plugin.getConfiguration().getProxyId())) {
try {
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) {
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.");
}
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.api.*;
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.util.uuid.NameFetcher;
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 Future<?> integrityCheck;
private Future<?> heartbeatTask;
private LuaManager.Script getRedisClusterTimeScript;
private static final Object SERVER_TO_PLAYERS_KEY = new Object();
private final Cache<Object, Multimap<String, UUID>> serverToPlayersCache = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.SECONDS)
@ -209,10 +207,7 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
httpClient.setDispatcher(dispatcher);
NameFetcher.setHttpClient(httpClient);
UUIDFetcher.setHttpClient(httpClient);
// init lua manager
LuaManager luaManager = new LuaManager(this);
this.getRedisClusterTimeScript = InitialUtils.getTimeScript(this, luaManager);
getLogger().info("lua manager was loaded");
InitialUtils.checkRedisVersion(this);
// check if this proxy is recovering from a crash and start heart the beat.
InitialUtils.checkIfRecovering(this, getDataFolder().toPath());
updateProxyIds();
@ -297,10 +292,6 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
return this.redisBungeeMode;
}
@Override
public Long getRedisTime() {
return getRedisTime((List<String>) this.getRedisClusterTimeScript.eval(Collections.singletonList("0"), Collections.emptyList()));
}
@Override
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.summoners.Summoner;
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.UUIDFetcher;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator;
@ -61,8 +60,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
private ScheduledTask integrityCheck;
private ScheduledTask heartbeatTask;
private final LuaManager.Script getRedisTimeScript;
private static final Object SERVER_TO_PLAYERS_KEY = new Object();
public static final List<ChannelIdentifier> IDENTIFIERS = List.of(
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);
}
this.api = new RedisBungeeAPI(this);
LuaManager luaManager = new LuaManager(this);
this.getRedisTimeScript = InitialUtils.getTimeScript(this, luaManager);
getLogger().info("lua manager was loaded");
InitialUtils.checkRedisVersion(this);
// check if this proxy is recovering from a crash and start heart the beat.
InitialUtils.checkIfRecovering(this, getDataFolder());
uuidTranslator = new UUIDTranslator(this);
@ -327,11 +322,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
return this.redisBungeeMode;
}
@Override
public Long getRedisTime() {
return getRedisTime((List<String>) this.getRedisTimeScript.eval(Collections.singletonList("0"), Collections.emptyList()));
}
@Override
public void updateProxyIds() {
this.proxiesIds = this.getCurrentProxiesIds(false);