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

Change Jedis -> JedisPooled, and make tasks use UnifiedJedis

since JedisCluster, JedisPooled are Childern of UnifiedJedis
This commit is contained in:
mohammed jasem alaajel 2022-07-26 17:47:18 +04:00
parent f303f2c202
commit ee76fa0b3d
22 changed files with 184 additions and 804 deletions

View File

@ -6,7 +6,7 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -365,10 +365,11 @@ public class RedisBungeeAPI {
* @return {@link Jedis} * @return {@link Jedis}
* @since 0.7.0 * @since 0.7.0
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE} * @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
* @see #getJedisPool()
*/ */
public Jedis requestJedis() { public Jedis requestJedis() {
if (getMode() == RedisBungeeMode.SINGLE) { if (getMode() == RedisBungeeMode.SINGLE) {
return ((JedisSummoner) this.plugin.getSummoner()).obtainResource(); return getJedisPool().getResource();
} else { } else {
throw new IllegalStateException("Mode is not " + RedisBungeeMode.SINGLE); throw new IllegalStateException("Mode is not " + RedisBungeeMode.SINGLE);
} }
@ -380,17 +381,22 @@ public class RedisBungeeAPI {
* @return {@link JedisPool} * @return {@link JedisPool}
* @since 0.6.5 * @since 0.6.5
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE} * @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
* @throws IllegalStateException if JedisPool compatibility mode is disabled in the config
*/ */
public JedisPool getJedisPool() { public JedisPool getJedisPool() {
if (getMode() == RedisBungeeMode.SINGLE) { if (getMode() == RedisBungeeMode.SINGLE) {
return ((JedisSummoner) this.plugin.getSummoner()).getJedisPool(); JedisPool jedisPool = ((JedisPooledSummoner) this.plugin.getSummoner()).getCompatibilityJedisPool();
if (jedisPool == null) {
throw new IllegalStateException("JedisPool compatibility mode is disabled");
}
return jedisPool;
} else { } else {
throw new IllegalStateException("Mode is not " + RedisBungeeMode.SINGLE); throw new IllegalStateException("Mode is not " + RedisBungeeMode.SINGLE);
} }
} }
/** /**
* This gives you instance of Jedis Cluster * This gives you instance of JedisCluster
* WARNING DO NOT USE {@link JedisCluster#close()} it will break the functionally * WARNING DO NOT USE {@link JedisCluster#close()} it will break the functionally
* *
* @return {@link redis.clients.jedis.JedisCluster} * @return {@link redis.clients.jedis.JedisCluster}
@ -406,7 +412,23 @@ public class RedisBungeeAPI {
} }
/** /**
* returns Summoner class responsible for Single Jedis {@link Jedis}, Cluster Jedis {@link redis.clients.jedis.JedisCluster} handling * This gives you instance of JedisPooled
* WARNING: DO NOT USE {@link redis.clients.jedis.JedisPooled#close()} it will break the functionally
*
* @return {@link redis.clients.jedis.JedisPooled}
* @since 0.8.0
* @throws IllegalStateException if the {@link #getMode()} is not equal to {@link RedisBungeeMode#SINGLE}
*/
public JedisCluster requestJedisPooled() {
if (getMode() == RedisBungeeMode.SINGLE) {
return ((JedisClusterSummoner) this.plugin.getSummoner()).obtainResource();
} else {
throw new IllegalStateException("Mode is not " + RedisBungeeMode.SINGLE);
}
}
/**
* returns Summoner class responsible for Single Jedis {@link redis.clients.jedis.JedisPooled} with {@link JedisPool}, Cluster Jedis {@link redis.clients.jedis.JedisCluster} handling
* *
* @return {@link Summoner} * @return {@link Summoner}
* @since 0.8.0 * @since 0.8.0

View File

@ -9,8 +9,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import redis.clients.jedis.Jedis; import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.JedisCluster;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress; import java.net.InetAddress;
@ -53,14 +52,8 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
try { try {
return serverCache.get(uuid, new RedisTask<String>(plugin.getApi()) { return serverCache.get(uuid, new RedisTask<String>(plugin.getApi()) {
@Override @Override
public String jedisTask(Jedis jedis) { public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
return Objects.requireNonNull(jedis.hget("player:" + uuid, "server"), "user not found"); return Objects.requireNonNull(unifiedJedis.hget("player:" + uuid, "server"), "user not found");
}
@Override
public String clusterJedisTask(JedisCluster jedisCluster) {
return Objects.requireNonNull(jedisCluster.hget("player:" + uuid, "server"), "user not found");
} }
}); });
@ -82,13 +75,8 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
try { try {
return proxyCache.get(uuid, new RedisTask<String>(plugin.getApi()) { return proxyCache.get(uuid, new RedisTask<String>(plugin.getApi()) {
@Override @Override
public String jedisTask(Jedis jedis) { public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
return Objects.requireNonNull(jedis.hget("player:" + uuid, "proxy"), "user not found"); return Objects.requireNonNull(unifiedJedis.hget("player:" + uuid, "proxy"), "user not found");
}
@Override
public String clusterJedisTask(JedisCluster jedisCluster) {
return Objects.requireNonNull(jedisCluster.hget("player:" + uuid, "proxy"), "user not found");
} }
}); });
} catch (ExecutionException | UncheckedExecutionException e) { } catch (ExecutionException | UncheckedExecutionException e) {
@ -108,16 +96,8 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
try { try {
return ipCache.get(uuid, new RedisTask<InetAddress>(plugin.getApi()) { return ipCache.get(uuid, new RedisTask<InetAddress>(plugin.getApi()) {
@Override @Override
public InetAddress jedisTask(Jedis jedis) { public InetAddress unifiedJedisTask(UnifiedJedis unifiedJedis) {
String result = jedis.hget("player:" + uuid, "ip"); String result = unifiedJedis.hget("player:" + uuid, "ip");
if (result == null)
throw new NullPointerException("user not found");
return InetAddresses.forString(result);
}
@Override
public InetAddress clusterJedisTask(JedisCluster jedisCluster) {
String result = jedisCluster.hget("player:" + uuid, "ip");
if (result == null) if (result == null)
throw new NullPointerException("user not found"); throw new NullPointerException("user not found");
return InetAddresses.forString(result); return InetAddresses.forString(result);
@ -139,15 +119,10 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
try { try {
return lastOnlineCache.get(uuid, new RedisTask<Long>(plugin.getApi()) { return lastOnlineCache.get(uuid, new RedisTask<Long>(plugin.getApi()) {
@Override
public Long jedisTask(Jedis jedis) {
String result = jedis.hget("player:" + uuid, "online");
return result == null ? -1 : Long.parseLong(result);
}
@Override @Override
public Long clusterJedisTask(JedisCluster jedisCluster) { public Long unifiedJedisTask(UnifiedJedis unifiedJedis) {
String result = jedisCluster.hget("player:" + uuid, "online"); String result = unifiedJedis.hget("player:" + uuid, "online");
return result == null ? -1 : Long.parseLong(result); return result == null ? -1 : Long.parseLong(result);
} }
}); });

View File

@ -3,6 +3,7 @@ package com.imaginarycode.minecraft.redisbungee.api;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
import java.util.Arrays; import java.util.Arrays;
@ -24,22 +25,12 @@ public class PubSubListener implements Runnable {
public void run() { public void run() {
RedisTask<Void> subTask = new RedisTask<Void>(plugin) { RedisTask<Void> subTask = new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
jpsh = new JedisPubSubHandler(plugin); jpsh = new JedisPubSubHandler(plugin);
addedChannels.add("redisbungee-" + plugin.getConfiguration().getProxyId()); addedChannels.add("redisbungee-" + plugin.getConfiguration().getProxyId());
addedChannels.add("redisbungee-allservers"); addedChannels.add("redisbungee-allservers");
addedChannels.add("redisbungee-data"); addedChannels.add("redisbungee-data");
jedis.subscribe(jpsh, addedChannels.toArray(new String[0])); unifiedJedis.subscribe(jpsh, addedChannels.toArray(new String[0]));
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
jpsh = new JedisPubSubHandler(plugin);
addedChannels.add("redisbungee-" + plugin.getConfiguration().getProxyId());
addedChannels.add("redisbungee-allservers");
addedChannels.add("redisbungee-data");
jedisCluster.subscribe(jpsh, addedChannels.toArray(new String[0]));
return null; return null;
} }
}; };

View File

@ -12,8 +12,6 @@ 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 redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Protocol; import redis.clients.jedis.Protocol;
import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
@ -50,34 +48,17 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
default int getCurrentCount() { default int getCurrentCount() {
return new RedisTask<Long>(this) { return new RedisTask<Long>(this) {
@Override @Override
public Long jedisTask(Jedis jedis) { public Long unifiedJedisTask(UnifiedJedis unifiedJedis) {
long total = 0; long total = 0;
long redisTime = getRedisTime(jedis.time()); long redisTime = getRedisTime(unifiedJedis);
Map<String, String> heartBeats = jedis.hgetAll("heartbeats"); Map<String, String> heartBeats = unifiedJedis.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();
String v = stringStringEntry.getValue(); String v = stringStringEntry.getValue();
long heartbeatTime = Long.parseLong(v); long heartbeatTime = Long.parseLong(v);
if (heartbeatTime + 30 >= redisTime) { if (heartbeatTime + 30 >= redisTime) {
total = total + jedis.scard("proxy:" + k + ":usersOnline"); total = total + unifiedJedis.scard("proxy:" + k + ":usersOnline");
}
}
return total;
}
@Override
public Long clusterJedisTask(JedisCluster jedisCluster) {
long total = 0;
long redisTime = getRedisTime(jedisCluster);
Map<String, String> heartBeats = jedisCluster.hgetAll("heartbeats");
for (Map.Entry<String, String> stringStringEntry : heartBeats.entrySet()) {
String k = stringStringEntry.getKey();
String v = stringStringEntry.getValue();
long heartbeatTime = Long.parseLong(v);
if (heartbeatTime + 30 >= redisTime) {
total = total + jedisCluster.scard("proxy:" + k + ":usersOnline");
} }
} }
return total; return total;
@ -92,7 +73,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
default Set<UUID> getPlayers() { default Set<UUID> getPlayers() {
return new RedisTask<Set<UUID>>(this) { return new RedisTask<Set<UUID>>(this) {
@Override @Override
public Set<UUID> jedisTask(Jedis jedis) { public Set<UUID> unifiedJedisTask(UnifiedJedis unifiedJedis) {
ImmutableSet.Builder<UUID> setBuilder = ImmutableSet.builder(); ImmutableSet.Builder<UUID> setBuilder = ImmutableSet.builder();
try { try {
List<String> keys = new ArrayList<>(); List<String> keys = new ArrayList<>();
@ -100,34 +81,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
keys.add("proxy:" + i + ":usersOnline"); keys.add("proxy:" + i + ":usersOnline");
} }
if (!keys.isEmpty()) { if (!keys.isEmpty()) {
Set<String> users = jedis.sunion(keys.toArray(new String[0])); Set<String> users = unifiedJedis.sunion(keys.toArray(new String[0]));
if (users != null && !users.isEmpty()) {
for (String user : users) {
try {
setBuilder = setBuilder.add(UUID.fromString(user));
} catch (IllegalArgumentException ignored) {
}
}
}
}
} catch (JedisConnectionException e) {
// Redis server has disappeared!
logFatal("Unable to get connection from pool - did your Redis server go away?");
throw new RuntimeException("Unable to get all players online", e);
}
return setBuilder.build();
}
@Override
public Set<UUID> clusterJedisTask(JedisCluster jedisCluster) {
ImmutableSet.Builder<UUID> setBuilder = ImmutableSet.builder();
try {
List<String> keys = new ArrayList<>();
for (String i : getProxiesIds()) {
keys.add("proxy:" + i + ":usersOnline");
}
if (!keys.isEmpty()) {
Set<String> users = jedisCluster.sunion(keys.toArray(new String[0]));
if (users != null && !users.isEmpty()) { if (users != null && !users.isEmpty()) {
for (String user : users) { for (String user : users) {
try { try {
@ -156,28 +110,12 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
default Multimap<String, UUID> serversToPlayers() { default Multimap<String, UUID> serversToPlayers() {
return new RedisTask<Multimap<String, UUID>>(this) { return new RedisTask<Multimap<String, UUID>>(this) {
@Override @Override
public Multimap<String, UUID> jedisTask(Jedis jedis) { public Multimap<String, UUID> unifiedJedisTask(UnifiedJedis unifiedJedis) {
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
for (String serverId : getProxiesIds()) { for (String serverId : getProxiesIds()) {
Set<String> players = jedis.smembers("proxy:" + serverId + ":usersOnline"); Set<String> players = unifiedJedis.smembers("proxy:" + serverId + ":usersOnline");
for (String player : players) { for (String player : players) {
String playerServer = jedis.hget("player:" + player, "server"); String playerServer = unifiedJedis.hget("player:" + player, "server");
if (playerServer == null) {
continue;
}
builder.put(playerServer, UUID.fromString(player));
}
}
return builder.build();
}
@Override
public Multimap<String, UUID> clusterJedisTask(JedisCluster jedisCluster) {
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
for (String serverId : getProxiesIds()) {
Set<String> players = jedisCluster.smembers("proxy:" + serverId + ":usersOnline");
for (String player : players) {
String playerServer = jedisCluster.hget("player:" + player, "server");
if (playerServer == null) { if (playerServer == null) {
continue; continue;
} }
@ -193,18 +131,8 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
checkArgument(getProxiesIds().contains(proxyId), proxyId + " is not a valid proxy ID"); checkArgument(getProxiesIds().contains(proxyId), proxyId + " is not a valid proxy ID");
return new RedisTask<Set<UUID>>(this) { return new RedisTask<Set<UUID>>(this) {
@Override @Override
public Set<UUID> jedisTask(Jedis jedis) { public Set<UUID> unifiedJedisTask(UnifiedJedis unifiedJedis) {
Set<String> users = jedis.smembers("proxy:" + proxyId + ":usersOnline"); Set<String> users = unifiedJedis.smembers("proxy:" + proxyId + ":usersOnline");
ImmutableSet.Builder<UUID> builder = ImmutableSet.builder();
for (String user : users) {
builder.add(UUID.fromString(user));
}
return builder.build();
}
@Override
public Set<UUID> clusterJedisTask(JedisCluster jedisCluster) {
Set<String> users = jedisCluster.smembers("proxy:" + proxyId + ":usersOnline");
ImmutableSet.Builder<UUID> builder = ImmutableSet.builder(); ImmutableSet.Builder<UUID> builder = ImmutableSet.builder();
for (String user : users) { for (String user : users) {
builder.add(UUID.fromString(user)); builder.add(UUID.fromString(user));
@ -224,11 +152,11 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
default List<String> getCurrentProxiesIds(boolean lagged) { default List<String> getCurrentProxiesIds(boolean lagged) {
return new RedisTask<List<String>>(this) { return new RedisTask<List<String>>(this) {
@Override @Override
public List<String> jedisTask(Jedis jedis) { public List<String> unifiedJedisTask(UnifiedJedis unifiedJedis) {
try { try {
long time = getRedisTime(jedis.time()); long time = getRedisTime(unifiedJedis);
ImmutableList.Builder<String> servers = ImmutableList.builder(); ImmutableList.Builder<String> servers = ImmutableList.builder();
Map<String, String> heartbeats = jedis.hgetAll("heartbeats"); Map<String, String> heartbeats = unifiedJedis.hgetAll("heartbeats");
for (Map.Entry<String, String> entry : heartbeats.entrySet()) { for (Map.Entry<String, String> entry : heartbeats.entrySet()) {
try { try {
long stamp = Long.parseLong(entry.getValue()); long stamp = Long.parseLong(entry.getValue());
@ -236,33 +164,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
servers.add(entry.getKey()); servers.add(entry.getKey());
} else if (time > stamp + RedisUtil.PROXY_TIMEOUT) { } else if (time > stamp + RedisUtil.PROXY_TIMEOUT) {
logWarn(entry.getKey() + " is " + (time - stamp) + " seconds behind! (Time not synchronized or server down?) and was removed from heartbeat."); logWarn(entry.getKey() + " is " + (time - stamp) + " seconds behind! (Time not synchronized or server down?) and was removed from heartbeat.");
jedis.hdel("heartbeats", entry.getKey()); unifiedJedis.hdel("heartbeats", entry.getKey());
}
} catch (NumberFormatException ignored) {
}
}
return servers.build();
} catch (JedisConnectionException e) {
logFatal("Unable to fetch server IDs");
e.printStackTrace();
return Collections.singletonList(getConfiguration().getProxyId());
}
}
@Override
public List<String> clusterJedisTask(JedisCluster jedisCluster) {
try {
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()) {
try {
long stamp = Long.parseLong(entry.getValue());
if (lagged ? time >= stamp + RedisUtil.PROXY_TIMEOUT : time <= stamp + RedisUtil.PROXY_TIMEOUT) {
servers.add(entry.getKey());
} else if (time > stamp + RedisUtil.PROXY_TIMEOUT) {
logWarn(entry.getKey() + " is " + (time - stamp) + " seconds behind! (Time not synchronized or server down?) and was removed from heartbeat.");
jedisCluster.hdel("heartbeats", entry.getKey());
} }
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
@ -282,21 +184,9 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
default void sendChannelMessage(String channel, String message) { default void sendChannelMessage(String channel, String message) {
new RedisTask<Void>(this) { new RedisTask<Void>(this) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
try { try {
jedis.publish(channel, message); unifiedJedis.publish(channel, message);
} catch (JedisConnectionException e) {
// Redis server has disappeared!
logFatal("Unable to get connection from pool - did your Redis server go away?");
throw new RuntimeException("Unable to publish channel message", e);
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
jedisCluster.publish(channel, message);
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
// Redis server has disappeared! // Redis server has disappeared!
logFatal("Unable to get connection from pool - did your Redis server go away?"); logFatal("Unable to get connection from pool - did your Redis server go away?");
@ -354,14 +244,8 @@ public interface RedisBungeePlugin<P> extends EventsPlatform, ConfigLoader {
if (!getDataManager().handleKick(playerUniqueId, message)) { if (!getDataManager().handleKick(playerUniqueId, message)) {
new RedisTask<Void>(this) { new RedisTask<Void>(this) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
PayloadUtils.kickPlayerPayload(playerUniqueId, message, jedis); PayloadUtils.kickPlayerPayload(playerUniqueId, message, unifiedJedis);
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
PayloadUtils.kickPlayerPayload(playerUniqueId, message, jedisCluster);
return null; return null;
} }
}.execute(); }.execute();

View File

@ -5,7 +5,7 @@ import com.google.common.reflect.TypeToken;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException; import ninja.leaping.configurate.objectmapping.ObjectMappingException;
@ -102,9 +102,15 @@ public interface ConfigLoader {
if (redisServer != null && redisServer.isEmpty()) { if (redisServer != null && redisServer.isEmpty()) {
throw new RuntimeException("No redis server specified"); throw new RuntimeException("No redis server specified");
} }
JedisPoolConfig config = new JedisPoolConfig(); JedisPool jedisPool = null;
config.setMaxTotal(maxConnections); if (node.getNode("enable-jedis-pool-compatibility").getBoolean(true)) {
summoner = new JedisSummoner(new JedisPool(config, redisServer, redisPort, 0, redisPassword, useSSL)); JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxConnections);
jedisPool = new JedisPool(config, redisServer, redisPort, 0, redisPassword, useSSL);
}
GenericObjectPoolConfig<Connection> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxTotal(maxConnections);
summoner = new JedisPooledSummoner(new JedisPooled(poolConfig, redisServer, redisPort, 0, redisPassword, useSSL), jedisPool);
redisBungeeMode = RedisBungeeMode.SINGLE; redisBungeeMode = RedisBungeeMode.SINGLE;
} }
plugin.logInfo("Successfully connected to Redis."); plugin.logInfo("Successfully connected to Redis.");

View File

@ -6,7 +6,6 @@ import java.io.IOException;
public class JedisClusterSummoner implements Summoner<JedisCluster> { public class JedisClusterSummoner implements Summoner<JedisCluster> {
public final JedisCluster jedisCluster; public final JedisCluster jedisCluster;
private boolean closed = false;
public JedisClusterSummoner(JedisCluster jedisCluster) { public JedisClusterSummoner(JedisCluster jedisCluster) {
this.jedisCluster = jedisCluster; this.jedisCluster = jedisCluster;
@ -15,19 +14,14 @@ public class JedisClusterSummoner implements Summoner<JedisCluster> {
jedisCluster.del("random_data"); jedisCluster.del("random_data");
} }
@Override
public JedisCluster obtainResource() {
return jedisCluster;
}
@Override
public boolean isAvailable() {
return !closed;
}
@Override @Override
public void close() throws IOException { public void close() throws IOException {
this.closed = true;
jedisCluster.close(); jedisCluster.close();
} }
@Override
public JedisCluster obtainResource() {
return this.jedisCluster;
}
} }

View File

@ -2,14 +2,17 @@ package com.imaginarycode.minecraft.redisbungee.api.summoners;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPooled;
import java.io.IOException; import java.io.IOException;
public class JedisSummoner implements Summoner<Jedis> { public class JedisPooledSummoner implements Summoner<JedisPooled> {
private final JedisPooled jedisPooled;
private final JedisPool jedisPool; private final JedisPool jedisPool;
public JedisSummoner(JedisPool jedisPool) { public JedisPooledSummoner(JedisPooled jedisPooled, JedisPool jedisPool) {
this.jedisPooled = jedisPooled;
this.jedisPool = jedisPool; this.jedisPool = jedisPool;
try (Jedis jedis = this.jedisPool.getResource()) { try (Jedis jedis = this.jedisPool.getResource()) {
// Test the connection to make sure configuration is right // Test the connection to make sure configuration is right
@ -18,22 +21,18 @@ public class JedisSummoner implements Summoner<Jedis> {
} }
@Override @Override
public Jedis obtainResource() { public JedisPooled obtainResource() {
return jedisPool.getResource(); return this.jedisPooled;
} }
public JedisPool getJedisPool() { public JedisPool getCompatibilityJedisPool() {
return this.jedisPool; return this.jedisPool;
} }
@Override
public boolean isAvailable() {
return !jedisPool.isClosed();
}
@Override @Override
public void close() throws IOException { public void close() throws IOException {
this.jedisPool.close(); this.jedisPool.close();
this.jedisPooled.close();
} }
} }

View File

@ -14,7 +14,5 @@ public interface Summoner<P> extends Closeable {
P obtainResource(); P obtainResource();
boolean isAvailable();
} }

View File

@ -3,6 +3,7 @@ package com.imaginarycode.minecraft.redisbungee.api.tasks;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisConnectionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -19,32 +20,12 @@ public class HeartbeatTask extends RedisTask<Void>{
this.globalPlayerCount = globalPlayerCount; this.globalPlayerCount = globalPlayerCount;
} }
@Override
public Void jedisTask(Jedis jedis) {
try {
long redisTime = plugin.getRedisTime(jedis.time());
jedis.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) {
// Redis server has disappeared!
plugin.logFatal("Unable to update heartbeat - did your Redis server go away?");
e.printStackTrace();
return null;
}
try {
plugin.updateProxiesIds();
globalPlayerCount.set(plugin.getCurrentCount());
} catch (Throwable e) {
plugin.logFatal("Unable to update data - did your Redis server go away?");
e.printStackTrace();
}
return null;
}
@Override @Override
public Void clusterJedisTask(JedisCluster jedisCluster) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
try { try {
long redisTime = plugin.getRedisTime(jedisCluster); long redisTime = plugin.getRedisTime(unifiedJedis);
jedisCluster.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime)); unifiedJedis.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
// Redis server has disappeared! // Redis server has disappeared!
plugin.logFatal("Unable to update heartbeat - did your Redis server go away?"); plugin.logFatal("Unable to update heartbeat - did your Redis server go away?");

View File

@ -5,6 +5,7 @@ import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
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.Protocol;
import redis.clients.jedis.UnifiedJedis;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@ -15,9 +16,10 @@ public class InitialUtils {
public static void checkRedisVersion(RedisBungeePlugin<?> plugin) { public static void checkRedisVersion(RedisBungeePlugin<?> plugin) {
new RedisTask<Void>(plugin) { new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
// This is more portable than INFO <section> // This is more portable than INFO <section>
String info = jedis.info();
String info = new String((byte[]) unifiedJedis.sendCommand(Protocol.Command.INFO));
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];
@ -26,30 +28,7 @@ public class InitialUtils {
plugin.logFatal("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis."); plugin.logFatal("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected"); throw new RuntimeException("Unsupported Redis version detected");
} }
long uuidCacheSize = jedis.hlen("uuid-cache"); long uuidCacheSize = unifiedJedis.hlen("uuid-cache");
if (uuidCacheSize > 750000) {
plugin.logInfo("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
}
break;
}
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
// This is more portable than INFO <section>
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];
plugin.logInfo(version + " <- redis version");
if (!RedisUtil.isRedisVersionRight(version)) {
plugin.logFatal("Your version of Redis (" + version + ") is not at least version 3.0 RedisBungee requires a newer version of Redis.");
throw new RuntimeException("Unsupported Redis version detected");
}
long uuidCacheSize = jedisCluster.hlen("uuid-cache");
if (uuidCacheSize > 750000) { if (uuidCacheSize > 750000) {
plugin.logInfo("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible."); plugin.logInfo("Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
} }
@ -65,7 +44,7 @@ public class InitialUtils {
public static void checkIfRecovering(RedisBungeePlugin<?> plugin, Path dataFolder) { public static void checkIfRecovering(RedisBungeePlugin<?> plugin, Path dataFolder) {
new RedisTask<Void>(plugin) { new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
Path crashFile = dataFolder.resolve("restarted_from_crash.txt"); Path crashFile = dataFolder.resolve("restarted_from_crash.txt");
if (Files.exists(crashFile)) { if (Files.exists(crashFile)) {
try { try {
@ -74,34 +53,10 @@ public class InitialUtils {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
plugin.logInfo("crash file was deleted"); plugin.logInfo("crash file was deleted");
} else if (jedis.hexists("heartbeats", plugin.getConfiguration().getProxyId())) { } else if (unifiedJedis.hexists("heartbeats", plugin.getConfiguration().getProxyId())) {
try { try {
long value = Long.parseLong(jedis.hget("heartbeats", plugin.getConfiguration().getProxyId())); long value = Long.parseLong(unifiedJedis.hget("heartbeats", plugin.getConfiguration().getProxyId()));
long redisTime = plugin.getRedisTime(jedis.time()); long redisTime = plugin.getRedisTime(unifiedJedis);
if (redisTime < value + RedisUtil.PROXY_TIMEOUT) {
logImposter(plugin);
throw new RuntimeException("Possible impostor instance!");
}
} catch (NumberFormatException ignored) {
}
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
Path crashFile = dataFolder.resolve("restarted_from_crash.txt");
if (Files.exists(crashFile)) {
try {
Files.delete(crashFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
plugin.logInfo("crash file was deleted");
} else if (jedisCluster.hexists("heartbeats", plugin.getConfiguration().getProxyId())) {
try {
long value = Long.parseLong(jedisCluster.hget("heartbeats", plugin.getConfiguration().getProxyId()));
long redisTime = plugin.getRedisTime(jedisCluster);
if (redisTime < value + RedisUtil.PROXY_TIMEOUT) { if (redisTime < value + RedisUtil.PROXY_TIMEOUT) {
logImposter(plugin); logImposter(plugin);

View File

@ -2,9 +2,7 @@ package com.imaginarycode.minecraft.redisbungee.api.tasks;
import com.imaginarycode.minecraft.redisbungee.api.util.player.PlayerUtils; import com.imaginarycode.minecraft.redisbungee.api.util.player.PlayerUtils;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import redis.clients.jedis.Jedis; import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -22,20 +20,20 @@ public abstract class IntegrityCheckTask extends RedisTask<Void> {
} }
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
try { try {
Set<String> players = plugin.getLocalPlayersAsUuidStrings(); Set<String> players = plugin.getLocalPlayersAsUuidStrings();
Set<String> playersInRedis = jedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline"); Set<String> playersInRedis = unifiedJedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
List<String> lagged = plugin.getCurrentProxiesIds(true); List<String> lagged = plugin.getCurrentProxiesIds(true);
// Clean up lagged players. // Clean up lagged players.
for (String s : lagged) { for (String s : lagged) {
Set<String> laggedPlayers = jedis.smembers("proxy:" + s + ":usersOnline"); Set<String> laggedPlayers = unifiedJedis.smembers("proxy:" + s + ":usersOnline");
jedis.del("proxy:" + s + ":usersOnline"); unifiedJedis.del("proxy:" + s + ":usersOnline");
if (!laggedPlayers.isEmpty()) { if (!laggedPlayers.isEmpty()) {
plugin.logInfo("Cleaning up lagged proxy " + s + " (" + laggedPlayers.size() + " players)..."); plugin.logInfo("Cleaning up lagged proxy " + s + " (" + laggedPlayers.size() + " players)...");
for (String laggedPlayer : laggedPlayers) { for (String laggedPlayer : laggedPlayers) {
PlayerUtils.cleanUpPlayer(laggedPlayer, jedis, true); PlayerUtils.cleanUpPlayer(laggedPlayer, unifiedJedis, true);
} }
} }
} }
@ -49,85 +47,26 @@ public abstract class IntegrityCheckTask extends RedisTask<Void> {
boolean found = false; boolean found = false;
for (String proxyId : plugin.getProxiesIds()) { for (String proxyId : plugin.getProxiesIds()) {
if (proxyId.equals(plugin.getConfiguration().getProxyId())) continue; if (proxyId.equals(plugin.getConfiguration().getProxyId())) continue;
if (jedis.sismember("proxy:" + proxyId + ":usersOnline", member)) { if (unifiedJedis.sismember("proxy:" + proxyId + ":usersOnline", member)) {
// Just clean up the set. // Just clean up the set.
found = true; found = true;
break; break;
} }
} }
if (!found) { if (!found) {
PlayerUtils.cleanUpPlayer(member, jedis, false); PlayerUtils.cleanUpPlayer(member, unifiedJedis, false);
plugin.logWarn("Player found in set that was not found locally and globally: " + member);
} else {
jedis.srem("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline", member);
plugin.logWarn("Player found in set that was not found locally, but is on another proxy: " + member);
}
}
Pipeline pipeline = jedis.pipelined();
for (String player : absentInRedis) {
// Player not online according to Redis but not BungeeCord.
plugin.logWarn("Player " + player + " is on the proxy but not in Redis.");
handlePlatformPlayer(player, pipeline);
}
pipeline.sync();
} catch (Throwable e) {
plugin.logFatal("Unable to fix up stored player data");
e.printStackTrace();
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
Set<String> players = plugin.getLocalPlayersAsUuidStrings();
Set<String> playersInRedis = jedisCluster.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
List<String> lagged = plugin.getCurrentProxiesIds(true);
// Clean up lagged players.
for (String s : lagged) {
Set<String> laggedPlayers = jedisCluster.smembers("proxy:" + s + ":usersOnline");
jedisCluster.del("proxy:" + s + ":usersOnline");
if (!laggedPlayers.isEmpty()) {
plugin.logInfo("Cleaning up lagged proxy " + s + " (" + laggedPlayers.size() + " players)...");
for (String laggedPlayer : laggedPlayers) {
PlayerUtils.cleanUpPlayer(laggedPlayer, jedisCluster, true);
}
}
}
Set<String> absentLocally = new HashSet<>(playersInRedis);
absentLocally.removeAll(players);
Set<String> absentInRedis = new HashSet<>(players);
absentInRedis.removeAll(playersInRedis);
for (String member : absentLocally) {
boolean found = false;
for (String proxyId : plugin.getProxiesIds()) {
if (proxyId.equals(plugin.getConfiguration().getProxyId())) continue;
if (jedisCluster.sismember("proxy:" + proxyId + ":usersOnline", member)) {
// Just clean up the set.
found = true;
break;
}
}
if (!found) {
PlayerUtils.cleanUpPlayer(member, jedisCluster, false);
plugin.logWarn("Player found in set that was not found locally and globally: " + member); plugin.logWarn("Player found in set that was not found locally and globally: " + member);
} else { } else {
jedisCluster.srem("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline", member); unifiedJedis.srem("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline", member);
plugin.logWarn("Player found in set that was not found locally, but is on another proxy: " + member); plugin.logWarn("Player found in set that was not found locally, but is on another proxy: " + member);
} }
} }
// due JedisCluster does not support pipelined. // due unifiedJedis does not support pipelined.
//Pipeline pipeline = jedis.pipelined(); //Pipeline pipeline = jedis.pipelined();
for (String player : absentInRedis) { for (String player : absentInRedis) {
// Player not online according to Redis but not BungeeCord. // Player not online according to Redis but not BungeeCord.
handlePlatformPlayer(player, jedisCluster); handlePlatformPlayer(player, unifiedJedis);
} }
} catch (Throwable e) { } catch (Throwable e) {
plugin.logFatal("Unable to fix up stored player data"); plugin.logFatal("Unable to fix up stored player data");
@ -137,7 +76,6 @@ public abstract class IntegrityCheckTask extends RedisTask<Void> {
} }
public abstract void handlePlatformPlayer(String player, JedisCluster jedisCluster); public abstract void handlePlatformPlayer(String player, UnifiedJedis unifiedJedis);
public abstract void handlePlatformPlayer(String player, Pipeline pipeline);
} }

View File

@ -3,11 +3,11 @@ package com.imaginarycode.minecraft.redisbungee.api.tasks;
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI; import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.UnifiedJedis;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -15,7 +15,6 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
protected final Summoner<?> summoner; protected final Summoner<?> summoner;
protected final RedisBungeeAPI api; protected final RedisBungeeAPI api;
protected Jedis jedis;
protected RedisBungeePlugin<?> plugin; protected RedisBungeePlugin<?> plugin;
@Override @Override
@ -34,25 +33,7 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
this.summoner = api.getSummoner(); this.summoner = api.getSummoner();
} }
// way to reuse jedis inside another RedisTask object public abstract V unifiedJedisTask(UnifiedJedis unifiedJedis);
public RedisTask(RedisBungeeAPI api, Jedis jedis) {
this.api = api;
this.summoner = api.getSummoner();
this.jedis = jedis;
}
// way to reuse jedis inside another RedisTask object
public RedisTask(RedisBungeePlugin<?> plugin, Jedis jedis) {
this.plugin = plugin;
this.api = plugin.getApi();
this.summoner = api.getSummoner();
this.jedis = jedis;
}
public abstract V jedisTask(Jedis jedis);
public abstract V clusterJedisTask(JedisCluster jedisCluster);
@Override @Override
public void run() { public void run() {
@ -60,19 +41,13 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
} }
public V execute(){ public V execute(){
// JedisCluster, JedisPooled in fact is just UnifiedJedis does not need new instance since its single instance anyway.
if (api.getMode() == RedisBungeeMode.SINGLE) { if (api.getMode() == RedisBungeeMode.SINGLE) {
if (this.jedis != null){ JedisPooledSummoner jedisSummoner = (JedisPooledSummoner) summoner;
return this.jedisTask(this.jedis); return this.unifiedJedisTask(jedisSummoner.obtainResource());
}
JedisSummoner jedisSummoner = (JedisSummoner) summoner;
try (Jedis newJedis = jedisSummoner.obtainResource()) {
return this.jedisTask(newJedis);
}
} else if (api.getMode() == RedisBungeeMode.CLUSTER) { } else if (api.getMode() == RedisBungeeMode.CLUSTER) {
// Jedis cluster does not need new instance since its single instance anyways.
JedisClusterSummoner jedisClusterSummoner = (JedisClusterSummoner) summoner; JedisClusterSummoner jedisClusterSummoner = (JedisClusterSummoner) summoner;
return this.clusterJedisTask(jedisClusterSummoner.obtainResource()); return this.unifiedJedisTask(jedisClusterSummoner.obtainResource());
} }
return null; return null;
} }

View File

@ -4,6 +4,7 @@ import com.imaginarycode.minecraft.redisbungee.api.util.player.PlayerUtils;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.UnifiedJedis;
import java.util.Set; import java.util.Set;
@ -12,23 +13,12 @@ public class ShutdownUtils {
public static void shutdownCleanup(RedisBungeePlugin<?> plugin) { public static void shutdownCleanup(RedisBungeePlugin<?> plugin) {
new RedisTask<Void>(plugin) { new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
jedis.hdel("heartbeats", plugin.getConfiguration().getProxyId()); unifiedJedis.hdel("heartbeats", plugin.getConfiguration().getProxyId());
if (jedis.scard("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline") > 0) { if (unifiedJedis.scard("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline") > 0) {
Set<String> players = jedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline"); Set<String> players = unifiedJedis.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
for (String member : players) for (String member : players)
PlayerUtils.cleanUpPlayer(member, jedis, true); PlayerUtils.cleanUpPlayer(member, unifiedJedis, true);
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
jedisCluster.hdel("heartbeats", plugin.getConfiguration().getProxyId());
if (jedisCluster.scard("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline") > 0) {
Set<String> players = jedisCluster.smembers("proxy:" + plugin.getConfiguration().getProxyId() + ":usersOnline");
for (String member : players)
PlayerUtils.cleanUpPlayer(member, jedisCluster, true);
} }
return null; return null;
} }

View File

@ -6,6 +6,7 @@ import com.imaginarycode.minecraft.redisbungee.api.AbstractDataManager;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
import redis.clients.jedis.UnifiedJedis;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.UUID; import java.util.UUID;
@ -13,76 +14,30 @@ import java.util.UUID;
public class PayloadUtils { public class PayloadUtils {
private static final Gson gson = new Gson(); private static final Gson gson = new Gson();
public static void playerJoinPayload(UUID uuid, Pipeline pipeline, InetAddress inetAddress) { public static void playerJoinPayload(UUID uuid, UnifiedJedis unifiedJedis, InetAddress inetAddress) {
pipeline.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>( unifiedJedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.JOIN,
new AbstractDataManager.LoginPayload(inetAddress))));
}
public static void playerJoinPayload(UUID uuid, JedisCluster jedisCluster, InetAddress inetAddress) {
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.JOIN,
new AbstractDataManager.LoginPayload(inetAddress))));
}
public static void playerJoinPayload(UUID uuid, Jedis jedis, InetAddress inetAddress) {
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.JOIN, uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.JOIN,
new AbstractDataManager.LoginPayload(inetAddress)))); new AbstractDataManager.LoginPayload(inetAddress))));
} }
public static void playerQuitPayload(String uuid, UnifiedJedis unifiedJedis, long timestamp) {
public static void playerQuitPayload(String uuid, Jedis jedis, long timestamp) { unifiedJedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
UUID.fromString(uuid), RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.LEAVE, UUID.fromString(uuid), RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.LEAVE,
new AbstractDataManager.LogoutPayload(timestamp)))); new AbstractDataManager.LogoutPayload(timestamp))));
} }
public static void playerQuitPayload(String uuid, JedisCluster jedisCluster, long timestamp) {
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
UUID.fromString(uuid), RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.LEAVE,
new AbstractDataManager.LogoutPayload(timestamp))));
}
public static void playerQuitPayload(String uuid, Pipeline pipeline, long timestamp) { public static void playerServerChangePayload(UUID uuid, UnifiedJedis unifiedJedis, String newServer, String oldServer) {
pipeline.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>( unifiedJedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
UUID.fromString(uuid), RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.LEAVE,
new AbstractDataManager.LogoutPayload(timestamp))));
}
public static void playerServerChangePayload(UUID uuid, Jedis jedis, String newServer, String oldServer) {
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE,
new AbstractDataManager.ServerChangePayload(newServer, oldServer))));
}
public static void playerServerChangePayload(UUID uuid, Pipeline pipeline, String newServer, String oldServer) {
pipeline.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE,
new AbstractDataManager.ServerChangePayload(newServer, oldServer))));
}
public static void playerServerChangePayload(UUID uuid, JedisCluster jedisCluster, String newServer, String oldServer) {
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE, uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.SERVER_CHANGE,
new AbstractDataManager.ServerChangePayload(newServer, oldServer)))); new AbstractDataManager.ServerChangePayload(newServer, oldServer))));
} }
public static void kickPlayerPayload(UUID uuid, String message, Pipeline pipeline) { public static void kickPlayerPayload(UUID uuid, String message, UnifiedJedis unifiedJedis) {
pipeline.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>( unifiedJedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.KICK,
new AbstractDataManager.KickPayload(message))));
}
public static void kickPlayerPayload(UUID uuid, String message, Jedis jedis) {
jedis.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.KICK,
new AbstractDataManager.KickPayload(message))));
}
public static void kickPlayerPayload(UUID uuid, String message, JedisCluster jedisCluster) {
jedisCluster.publish("redisbungee-data", gson.toJson(new AbstractDataManager.DataManagerMessage<>(
uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.KICK, uuid, RedisBungeeAPI.getRedisBungeeApi().getProxyId(), AbstractDataManager.DataManagerMessage.Action.KICK,
new AbstractDataManager.KickPayload(message)))); new AbstractDataManager.KickPayload(message))));
} }

View File

@ -1,37 +1,14 @@
package com.imaginarycode.minecraft.redisbungee.api.util.player; package com.imaginarycode.minecraft.redisbungee.api.util.player;
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI; import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
import redis.clients.jedis.Jedis; import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline;
import static com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils.playerQuitPayload; import static com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils.playerQuitPayload;
public class PlayerUtils { public class PlayerUtils {
public static void cleanUpPlayer(String uuid, Jedis rsc, boolean firePayload) { public static void cleanUpPlayer(String uuid, UnifiedJedis rsc, boolean firePayload) {
rsc.srem("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", uuid);
rsc.hdel("player:" + uuid, "server", "ip", "proxy");
long timestamp = System.currentTimeMillis();
rsc.hset("player:" + uuid, "online", String.valueOf(timestamp));
if (firePayload) {
playerQuitPayload(uuid, rsc, timestamp);
}
}
public static void cleanUpPlayer(String uuid, Pipeline rsc, boolean firePayload) {
rsc.srem("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", uuid);
rsc.hdel("player:" + uuid, "server", "ip", "proxy");
long timestamp = System.currentTimeMillis();
rsc.hset("player:" + uuid, "online", String.valueOf(timestamp));
if (firePayload) {
playerQuitPayload(uuid, rsc, timestamp);
}
}
public static void cleanUpPlayer(String uuid, JedisCluster rsc, boolean firePayload) {
rsc.srem("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", uuid); rsc.srem("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", uuid);
rsc.hdel("player:" + uuid, "server", "ip", "proxy"); rsc.hdel("player:" + uuid, "server", "ip", "proxy");
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();

View File

@ -11,6 +11,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.exceptions.JedisException;
import java.util.*; import java.util.*;
@ -74,17 +75,17 @@ public final class UUIDTranslator {
} }
RedisTask<UUID> redisTask = new RedisTask<UUID>(plugin.getApi()) { RedisTask<UUID> redisTask = new RedisTask<UUID>(plugin.getApi()) {
@Override @Override
public UUID jedisTask(Jedis jedis) { public UUID unifiedJedisTask(UnifiedJedis unifiedJedis) {
String stored = jedis.hget("uuid-cache", player.toLowerCase()); String stored = unifiedJedis.hget("uuid-cache", player.toLowerCase());
if (stored != null) { if (stored != null) {
// Found an entry value. Deserialize it. // Found an entry value. Deserialize it.
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class); CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
// Check for expiry: // Check for expiry:
if (entry.expired()) { if (entry.expired()) {
jedis.hdel("uuid-cache", player.toLowerCase()); unifiedJedis.hdel("uuid-cache", player.toLowerCase());
// Doesn't hurt to also remove the UUID entry as well. // Doesn't hurt to also remove the UUID entry as well.
jedis.hdel("uuid-cache", entry.getUuid().toString()); unifiedJedis.hdel("uuid-cache", entry.getUuid().toString());
} else { } else {
nameToUuidMap.put(player.toLowerCase(), entry); nameToUuidMap.put(player.toLowerCase(), entry);
uuidToNameMap.put(entry.getUuid(), entry); uuidToNameMap.put(entry.getUuid(), entry);
@ -105,46 +106,7 @@ public final class UUIDTranslator {
} }
for (Map.Entry<String, UUID> entry : uuidMap1.entrySet()) { for (Map.Entry<String, UUID> entry : uuidMap1.entrySet()) {
if (entry.getKey().equalsIgnoreCase(player)) { if (entry.getKey().equalsIgnoreCase(player)) {
persistInfo(entry.getKey(), entry.getValue(), jedis); persistInfo(entry.getKey(), entry.getValue(), unifiedJedis);
return entry.getValue();
}
}
return null;
}
@Override
public UUID clusterJedisTask(JedisCluster jedisCluster) {
String stored = jedisCluster.hget("uuid-cache", player.toLowerCase());
if (stored != null) {
// Found an entry value. Deserialize it.
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
// Check for expiry:
if (entry.expired()) {
jedisCluster.hdel("uuid-cache", player.toLowerCase());
// Doesn't hurt to also remove the UUID entry as well.
jedisCluster.hdel("uuid-cache", entry.getUuid().toString());
} else {
nameToUuidMap.put(player.toLowerCase(), entry);
uuidToNameMap.put(entry.getUuid(), entry);
return entry.getUuid();
}
}
// That didn't work. Let's ask Mojang.
if (!expensiveLookups || !plugin.isOnlineMode())
return null;
Map<String, UUID> uuidMap1;
try {
uuidMap1 = new UUIDFetcher(Collections.singletonList(player)).call();
} catch (Exception e) {
plugin.logFatal("Unable to fetch UUID from Mojang for " + player);
return null;
}
for (Map.Entry<String, UUID> entry : uuidMap1.entrySet()) {
if (entry.getKey().equalsIgnoreCase(player)) {
persistInfo(entry.getKey(), entry.getValue(), jedisCluster);
return entry.getValue(); return entry.getValue();
} }
} }
@ -178,57 +140,18 @@ public final class UUIDTranslator {
RedisTask<String> redisTask = new RedisTask<String>(plugin.getApi()) { RedisTask<String> redisTask = new RedisTask<String>(plugin.getApi()) {
@Override @Override
public String jedisTask(Jedis jedis) { public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
String stored = jedis.hget("uuid-cache", player.toString()); String stored = unifiedJedis.hget("uuid-cache", player.toString());
if (stored != null) { if (stored != null) {
// Found an entry value. Deserialize it. // Found an entry value. Deserialize it.
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class); CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
// Check for expiry: // Check for expiry:
if (entry.expired()) { if (entry.expired()) {
jedis.hdel("uuid-cache", player.toString()); unifiedJedis.hdel("uuid-cache", player.toString());
// Doesn't hurt to also remove the named entry as well. // Doesn't hurt to also remove the named entry as well.
// TODO: Since UUIDs are fixed, we could look up the name and see if the UUID matches. // TODO: Since UUIDs are fixed, we could look up the name and see if the UUID matches.
jedis.hdel("uuid-cache", entry.getName()); unifiedJedis.hdel("uuid-cache", entry.getName());
} else {
nameToUuidMap.put(entry.getName().toLowerCase(), entry);
uuidToNameMap.put(player, entry);
return entry.getName();
}
}
if (!expensiveLookups || !plugin.isOnlineMode())
return null;
// That didn't work. Let's ask Mojang. This call may fail, because Mojang is insane.
String name;
try {
List<String> nameHist = NameFetcher.nameHistoryFromUuid(player);
name = Iterables.getLast(nameHist, null);
} catch (Exception e) {
plugin.logFatal("Unable to fetch name from Mojang for " + player);
return null;
}
if (name != null) {
persistInfo(name, player, jedis);
return name;
}
return null;
}
@Override
public String clusterJedisTask(JedisCluster jedisCluster) {
String stored = jedisCluster.hget("uuid-cache", player.toString());
if (stored != null) {
// Found an entry value. Deserialize it.
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
// Check for expiry:
if (entry.expired()) {
jedisCluster.hdel("uuid-cache", player.toString());
// Doesn't hurt to also remove the named entry as well.
// TODO: Since UUIDs are fixed, we could look up the name and see if the UUID matches.
jedisCluster.hdel("uuid-cache", entry.getName());
} else { } else {
nameToUuidMap.put(entry.getName().toLowerCase(), entry); nameToUuidMap.put(entry.getName().toLowerCase(), entry);
uuidToNameMap.put(player, entry); uuidToNameMap.put(player, entry);
@ -250,7 +173,7 @@ public final class UUIDTranslator {
} }
if (name != null) { if (name != null) {
persistInfo(name, player, jedisCluster); persistInfo(name, player, unifiedJedis);
return name; return name;
} }
@ -268,22 +191,10 @@ public final class UUIDTranslator {
} }
} }
public void persistInfo(String name, UUID uuid, Jedis jedis) { public void persistInfo(String name, UUID uuid, UnifiedJedis unifiedJedis) {
addToMaps(name, uuid); addToMaps(name, uuid);
String json = gson.toJson(uuidToNameMap.get(uuid)); String json = gson.toJson(uuidToNameMap.get(uuid));
jedis.hmset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json)); unifiedJedis.hmset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json));
}
public void persistInfo(String name, UUID uuid, Pipeline pipeline) {
addToMaps(name, uuid);
String json = gson.toJson(uuidToNameMap.get(uuid));
pipeline.hmset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json));
}
public void persistInfo(String name, UUID uuid, JedisCluster jedisCluster) {
addToMaps(name, uuid);
String json = gson.toJson(uuidToNameMap.get(uuid));
jedisCluster.hmset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json));
} }
private static class CachedUUIDEntry { private static class CachedUUIDEntry {

View File

@ -5,6 +5,7 @@ import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
import redis.clients.jedis.UnifiedJedis;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -13,43 +14,23 @@ import static com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUt
public class BungeePlayerUtils { public class BungeePlayerUtils {
public static void createPlayer(ProxiedPlayer player, Pipeline pipeline, boolean fireEvent) { public static void createPlayer(ProxiedPlayer player, UnifiedJedis unifiedJedis, boolean fireEvent) {
createPlayer(player.getPendingConnection(), pipeline, fireEvent); createPlayer(player.getPendingConnection(), unifiedJedis, fireEvent);
if (player.getServer() != null) if (player.getServer() != null)
pipeline.hset("player:" + player.getUniqueId().toString(), "server", player.getServer().getInfo().getName()); unifiedJedis.hset("player:" + player.getUniqueId().toString(), "server", player.getServer().getInfo().getName());
} }
public static void createPlayer(PendingConnection connection, Pipeline pipeline, boolean fireEvent) { public static void createPlayer(PendingConnection connection, UnifiedJedis unifiedJedis, boolean fireEvent) {
Map<String, String> playerData = new HashMap<>(4); Map<String, String> playerData = new HashMap<>(4);
playerData.put("online", "0"); playerData.put("online", "0");
playerData.put("ip", connection.getAddress().getAddress().getHostAddress()); playerData.put("ip", connection.getAddress().getAddress().getHostAddress());
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId()); playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId());
pipeline.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", connection.getUniqueId().toString()); unifiedJedis.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", connection.getUniqueId().toString());
pipeline.hmset("player:" + connection.getUniqueId().toString(), playerData); unifiedJedis.hmset("player:" + connection.getUniqueId().toString(), playerData);
if (fireEvent) { if (fireEvent) {
playerJoinPayload(connection.getUniqueId(), pipeline, connection.getAddress().getAddress()); playerJoinPayload(connection.getUniqueId(), unifiedJedis, connection.getAddress().getAddress());
}
}
public static void createPlayer(ProxiedPlayer player, JedisCluster jedisCluster, boolean fireEvent) {
createPlayer(player.getPendingConnection(), jedisCluster, fireEvent);
if (player.getServer() != null)
jedisCluster.hset("player:" + player.getUniqueId().toString(), "server", player.getServer().getInfo().getName());
}
public static void createPlayer(PendingConnection connection, JedisCluster jedisCluster, boolean fireEvent) {
Map<String, String> playerData = new HashMap<>(4);
playerData.put("online", "0");
playerData.put("ip", connection.getAddress().getAddress().getHostAddress());
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId());
jedisCluster.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", connection.getUniqueId().toString());
jedisCluster.hmset("player:" + connection.getUniqueId().toString(), playerData);
if (fireEvent) {
playerJoinPayload(connection.getUniqueId(), jedisCluster, connection.getAddress().getAddress());
} }
} }

View File

@ -23,6 +23,7 @@ import net.md_5.bungee.event.EventHandler;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
import redis.clients.jedis.UnifiedJedis;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.*; import java.util.*;
@ -40,7 +41,7 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
event.registerIntent((Plugin) plugin); event.registerIntent((Plugin) plugin);
plugin.executeAsync(new RedisTask<Void>(plugin) { plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
try { try {
if (event.isCancelled()) { if (event.isCancelled()) {
return null; return null;
@ -60,41 +61,7 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
} }
for (String s : plugin.getProxiesIds()) { for (String s : plugin.getProxiesIds()) {
if (jedis.sismember("proxy:" + s + ":usersOnline", event.getConnection().getUniqueId().toString())) { if (unifiedJedis.sismember("proxy:" + s + ":usersOnline", event.getConnection().getUniqueId().toString())) {
event.setCancelled(true);
// TODO: Make it accept a BaseComponent[] like everything else.
event.setCancelReason(ALREADY_LOGGED_IN);
return null;
}
}
return null;
} finally {
event.completeIntent((Plugin) plugin);
}
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
if (event.isCancelled()) {
return null;
}
// We make sure they aren't trying to use an existing player's name.
// This is problematic for online-mode servers as they always disconnect old clients.
if (plugin.isOnlineMode()) {
ProxiedPlayer player = (ProxiedPlayer) plugin.getPlayer(event.getConnection().getName());
if (player != null) {
event.setCancelled(true);
// TODO: Make it accept a BaseComponent[] like everything else.
event.setCancelReason(ONLINE_MODE_RECONNECT);
return null;
}
}
for (String s : plugin.getProxiesIds()) {
if (jedisCluster.sismember("proxy:" + s + ":usersOnline", event.getConnection().getUniqueId().toString())) {
event.setCancelled(true); event.setCancelled(true);
// TODO: Make it accept a BaseComponent[] like everything else. // TODO: Make it accept a BaseComponent[] like everything else.
event.setCancelReason(ALREADY_LOGGED_IN); event.setCancelReason(ALREADY_LOGGED_IN);
@ -114,24 +81,9 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
public void onPostLogin(PostLoginEvent event) { public void onPostLogin(PostLoginEvent event) {
plugin.executeAsync(new RedisTask<Void>(plugin) { plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
// this code was moved out from login event due being async.. plugin.getUuidTranslator().persistInfo(event.getPlayer().getName(), event.getPlayer().getUniqueId(), unifiedJedis);
// and it can be cancelled but it will show as false in redis-bungee BungeePlayerUtils.createPlayer(event.getPlayer(), unifiedJedis, true);
// which will register the player into the redis database.
Pipeline pipeline = jedis.pipelined();
plugin.getUuidTranslator().persistInfo(event.getPlayer().getName(), event.getPlayer().getUniqueId(), pipeline);
BungeePlayerUtils.createPlayer(event.getPlayer(), pipeline, true);
pipeline.sync();
// the end of moved code.
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
// due no support to pipeline in jedis cluster just use the instance instead.
plugin.getUuidTranslator().persistInfo(event.getPlayer().getName(), event.getPlayer().getUniqueId(), jedisCluster);
BungeePlayerUtils.createPlayer(event.getPlayer(), jedisCluster, true);
return null; return null;
} }
}); });
@ -142,17 +94,8 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
public void onPlayerDisconnect(PlayerDisconnectEvent event) { public void onPlayerDisconnect(PlayerDisconnectEvent event) {
plugin.executeAsync(new RedisTask<Void>(plugin) { plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
Pipeline pipeline = jedis.pipelined(); PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), unifiedJedis, true);
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), pipeline, true);
pipeline.sync();
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
// Due some reason JedisCluster does not support pipeline, use instance instead
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), jedisCluster, true);
return null; return null;
} }
}); });
@ -166,16 +109,9 @@ public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener<Login
final String oldServer = event.getPlayer().getServer() == null ? null : event.getPlayer().getServer().getInfo().getName(); final String oldServer = event.getPlayer().getServer() == null ? null : event.getPlayer().getServer().getInfo().getName();
plugin.executeAsync(new RedisTask<Void>(plugin) { plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
jedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getInfo().getName()); unifiedJedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getInfo().getName());
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), jedis, currentServer, oldServer); PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), unifiedJedis, currentServer, oldServer);
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
jedisCluster.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", event.getServer().getInfo().getName());
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), jedisCluster, currentServer, oldServer);
return null; return null;
} }
}); });

View File

@ -221,21 +221,12 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
IntegrityCheckTask integrityCheckTask = new IntegrityCheckTask(this) { IntegrityCheckTask integrityCheckTask = new IntegrityCheckTask(this) {
@Override @Override
public void handlePlatformPlayer(String player, JedisCluster jedis) { public void handlePlatformPlayer(String player, UnifiedJedis unifiedJedis) {
ProxiedPlayer proxiedPlayer = ProxyServer.getInstance().getPlayer(UUID.fromString(player)); ProxiedPlayer proxiedPlayer = ProxyServer.getInstance().getPlayer(UUID.fromString(player));
if (proxiedPlayer == null) if (proxiedPlayer == null)
return; // We'll deal with it later. return; // We'll deal with it later.
BungeePlayerUtils.createPlayer(proxiedPlayer, jedis, false); BungeePlayerUtils.createPlayer(proxiedPlayer, unifiedJedis, false);
}
@Override
public void handlePlatformPlayer(String player, Pipeline pipeline) {
ProxiedPlayer proxiedPlayer = ProxyServer.getInstance().getPlayer(UUID.fromString(player));
if (proxiedPlayer == null)
return; // We'll deal with it later.
BungeePlayerUtils.createPlayer(proxiedPlayer, pipeline, false);
} }
}; };

View File

@ -30,6 +30,7 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
import redis.clients.jedis.UnifiedJedis;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.*; import java.util.*;
@ -47,7 +48,7 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
public void onLogin(LoginEvent event, Continuation continuation) { public void onLogin(LoginEvent event, Continuation continuation) {
plugin.executeAsync(new RedisTask<Void>(plugin) { plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
try { try {
if (!event.getResult().isAllowed()) { if (!event.getResult().isAllowed()) {
return null; return null;
@ -65,37 +66,7 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
} }
for (String s : plugin.getProxiesIds()) { for (String s : plugin.getProxiesIds()) {
if (jedis.sismember("proxy:" + s + ":usersOnline", event.getPlayer().getUniqueId().toString())) { if (unifiedJedis.sismember("proxy:" + s + ":usersOnline", event.getPlayer().getUniqueId().toString())) {
event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ALREADY_LOGGED_IN)));
return null;
}
}
return null;
} finally {
continuation.resume();
}
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
if (!event.getResult().isAllowed()) {
return null;
}
// We make sure they aren't trying to use an existing player's name.
// This is problematic for online-mode servers as they always disconnect old clients.
if (plugin.isOnlineMode()) {
Player player = (Player) plugin.getPlayer(event.getPlayer().getUsername());
if (player != null) {
event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ONLINE_MODE_RECONNECT)));
return null;
}
}
for (String s : plugin.getProxiesIds()) {
if (jedisCluster.sismember("proxy:" + s + ":usersOnline", event.getPlayer().getUniqueId().toString())) {
event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ALREADY_LOGGED_IN))); event.setResult(ResultedEvent.ComponentResult.denied(serializer.deserialize(ALREADY_LOGGED_IN)));
return null; return null;
} }
@ -114,18 +85,9 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
public void onPostLogin(PostLoginEvent event) { public void onPostLogin(PostLoginEvent event) {
plugin.executeAsync(new RedisTask<Void>(plugin) { plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
Pipeline pipeline = jedis.pipelined(); plugin.getUuidTranslator().persistInfo(event.getPlayer().getUsername(), event.getPlayer().getUniqueId(), unifiedJedis);
plugin.getUuidTranslator().persistInfo(event.getPlayer().getUsername(), event.getPlayer().getUniqueId(), pipeline); VelocityPlayerUtils.createPlayer(event.getPlayer(), unifiedJedis, true);
VelocityPlayerUtils.createPlayer(event.getPlayer(), pipeline, true);
pipeline.sync();
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
plugin.getUuidTranslator().persistInfo(event.getPlayer().getUsername(), event.getPlayer().getUniqueId(), jedisCluster);
VelocityPlayerUtils.createPlayer(event.getPlayer(), jedisCluster, true);
return null; return null;
} }
}); });
@ -136,16 +98,8 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
public void onPlayerDisconnect(DisconnectEvent event) { public void onPlayerDisconnect(DisconnectEvent event) {
plugin.executeAsync(new RedisTask<Void>(plugin) { plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
Pipeline pipeline = jedis.pipelined(); PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), unifiedJedis, true);
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), pipeline, true);
pipeline.sync();
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
PlayerUtils.cleanUpPlayer(event.getPlayer().getUniqueId().toString(), jedisCluster, true);
return null; return null;
} }
@ -159,18 +113,10 @@ public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener<Log
final String currentServer = event.getServer().getServerInfo().getName(); final String currentServer = event.getServer().getServerInfo().getName();
final String oldServer = event.getPreviousServer().map(serverConnection -> serverConnection.getServerInfo().getName()).orElse(null); final String oldServer = event.getPreviousServer().map(serverConnection -> serverConnection.getServerInfo().getName()).orElse(null);
plugin.executeAsync(new RedisTask<Void>(plugin) { plugin.executeAsync(new RedisTask<Void>(plugin) {
@Override @Override
public Void jedisTask(Jedis jedis) { public Void unifiedJedisTask(UnifiedJedis unifiedJedis) {
jedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", currentServer); unifiedJedis.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", currentServer);
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), jedis, currentServer, oldServer); PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), unifiedJedis, currentServer, oldServer);
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
jedisCluster.hset("player:" + event.getPlayer().getUniqueId().toString(), "server", currentServer);
PayloadUtils.playerServerChangePayload(event.getPlayer().getUniqueId(), jedisCluster, currentServer, oldServer);
return null; return null;
} }
}); });

View File

@ -244,19 +244,11 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
IntegrityCheckTask integrityCheckTask = new IntegrityCheckTask(this) { IntegrityCheckTask integrityCheckTask = new IntegrityCheckTask(this) {
@Override @Override
public void handlePlatformPlayer(String player, JedisCluster jedisCluster) { public void handlePlatformPlayer(String player, UnifiedJedis unifiedJedis) {
Player playerProxied = getProxy().getPlayer(UUID.fromString(player)).orElse(null); Player playerProxied = getProxy().getPlayer(UUID.fromString(player)).orElse(null);
if (playerProxied == null) if (playerProxied == null)
return; // We'll deal with it later. return; // We'll deal with it later.
VelocityPlayerUtils.createPlayer(playerProxied, jedisCluster, false); VelocityPlayerUtils.createPlayer(playerProxied, unifiedJedis, false);
}
@Override
public void handlePlatformPlayer(String player, Pipeline pipeline) {
Player playerProxied = getProxy().getPlayer(UUID.fromString(player)).orElse(null);
if (playerProxied == null)
return; // We'll deal with it later.
VelocityPlayerUtils.createPlayer(playerProxied, pipeline, false);
} }
}; };
integrityCheck = getProxy().getScheduler().buildTask(this, integrityCheckTask::execute).repeat(30, TimeUnit.SECONDS).schedule(); integrityCheck = getProxy().getScheduler().buildTask(this, integrityCheckTask::execute).repeat(30, TimeUnit.SECONDS).schedule();

View File

@ -4,6 +4,7 @@ import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.ServerConnection;
import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
import redis.clients.jedis.UnifiedJedis;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -12,38 +13,20 @@ import java.util.Optional;
import static com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils.playerJoinPayload; import static com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils.playerJoinPayload;
public class VelocityPlayerUtils { public class VelocityPlayerUtils {
protected static void createPlayer(Player player, UnifiedJedis unifiedJedis, boolean fireEvent) {
protected static void createPlayer(Player player, Pipeline pipeline, boolean fireEvent) {
Optional<ServerConnection> server = player.getCurrentServer(); Optional<ServerConnection> server = player.getCurrentServer();
server.ifPresent(serverConnection -> pipeline.hset("player:" + player.getUniqueId().toString(), "server", serverConnection.getServerInfo().getName())); server.ifPresent(serverConnection -> unifiedJedis.hset("player:" + player.getUniqueId().toString(), "server", serverConnection.getServerInfo().getName()));
Map<String, String> playerData = new HashMap<>(4); Map<String, String> playerData = new HashMap<>(4);
playerData.put("online", "0"); playerData.put("online", "0");
playerData.put("ip", player.getRemoteAddress().getHostName()); playerData.put("ip", player.getRemoteAddress().getHostName());
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId()); playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId());
pipeline.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", player.getUniqueId().toString()); unifiedJedis.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", player.getUniqueId().toString());
pipeline.hmset("player:" + player.getUniqueId().toString(), playerData); unifiedJedis.hmset("player:" + player.getUniqueId().toString(), playerData);
if (fireEvent) { if (fireEvent) {
playerJoinPayload(player.getUniqueId(), pipeline, player.getRemoteAddress().getAddress()); playerJoinPayload(player.getUniqueId(), unifiedJedis, player.getRemoteAddress().getAddress());
}
}
protected static void createPlayer(Player player, JedisCluster jedisCluster, boolean fireEvent) {
Optional<ServerConnection> server = player.getCurrentServer();
server.ifPresent(serverConnection -> jedisCluster.hset("player:" + player.getUniqueId().toString(), "server", serverConnection.getServerInfo().getName()));
Map<String, String> playerData = new HashMap<>(4);
playerData.put("online", "0");
playerData.put("ip", player.getRemoteAddress().getHostName());
playerData.put("proxy", RedisBungeeAPI.getRedisBungeeApi().getProxyId());
jedisCluster.sadd("proxy:" + RedisBungeeAPI.getRedisBungeeApi().getProxyId() + ":usersOnline", player.getUniqueId().toString());
jedisCluster.hmset("player:" + player.getUniqueId().toString(), playerData);
if (fireEvent) {
playerJoinPayload(player.getUniqueId(), jedisCluster, player.getRemoteAddress().getAddress());
} }
} }
} }