コード例 #1
0
ファイル: RedisBungee.java プロジェクト: Civcraft/RedisBungee
  @Override
  public void onDisable() {
    if (pool != null) {
      // Poison the PubSub listener
      psl.poison();
      getProxy().getScheduler().cancel(this);
      integrityCheck.cancel();
      heartbeatTask.cancel();
      getProxy().getPluginManager().unregisterListeners(this);

      try (Jedis tmpRsc = pool.getResource()) {
        tmpRsc.hdel("heartbeats", configuration.getServerId());
        if (tmpRsc.scard("proxy:" + configuration.getServerId() + ":usersOnline") > 0) {
          Set<String> players =
              tmpRsc.smembers("proxy:" + configuration.getServerId() + ":usersOnline");
          for (String member : players) RedisUtil.cleanUpPlayer(member, tmpRsc);
        }
      }

      pool.destroy();
    }
  }
コード例 #2
0
ファイル: RedisBungee.java プロジェクト: Civcraft/RedisBungee
  @Override
  public void onEnable() {
    try {
      loadConfig();
    } catch (IOException e) {
      throw new RuntimeException("Unable to load/save config", e);
    } catch (JedisConnectionException e) {
      throw new RuntimeException("Unable to connect to your Redis server!", e);
    }
    if (pool != null) {
      try (Jedis tmpRsc = pool.getResource()) {
        // This is more portable than INFO <section>
        String info = tmpRsc.info();
        for (String s : info.split("\r\n")) {
          if (s.startsWith("redis_version:")) {
            String version = s.split(":")[1];
            if (!(usingLua = RedisUtil.canUseLua(version))) {
              getLogger()
                  .warning(
                      "Your version of Redis ("
                          + version
                          + ") is not at least version 2.6. RedisBungee requires a newer version of Redis.");
              throw new RuntimeException("Unsupported Redis version detected");
            } else {
              LuaManager manager = new LuaManager(this);
              serverToPlayersScript =
                  manager.createScript(
                      IOUtil.readInputStreamAsString(
                          getResourceAsStream("lua/server_to_players.lua")));
              getPlayerCountScript =
                  manager.createScript(
                      IOUtil.readInputStreamAsString(
                          getResourceAsStream("lua/get_player_count.lua")));
              getServerPlayersScript =
                  manager.createScript(
                      IOUtil.readInputStreamAsString(
                          getResourceAsStream("lua/get_server_players.lua")));
            }
            break;
          }
        }

        tmpRsc.hset(
            "heartbeats", configuration.getServerId(), String.valueOf(System.currentTimeMillis()));

        long uuidCacheSize = tmpRsc.hlen("uuid-cache");
        if (uuidCacheSize > 750000) {
          getLogger()
              .info(
                  "Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
        }
      }
      serverIds = getCurrentServerIds(true, false);
      uuidTranslator = new UUIDTranslator(this);
      heartbeatTask =
          getProxy()
              .getScheduler()
              .schedule(
                  this,
                  new Runnable() {
                    @Override
                    public void run() {
                      try (Jedis rsc = pool.getResource()) {
                        long redisTime = getRedisTime(rsc.time());
                        rsc.hset(
                            "heartbeats", configuration.getServerId(), String.valueOf(redisTime));
                      } catch (JedisConnectionException e) {
                        // Redis server has disappeared!
                        getLogger()
                            .log(
                                Level.SEVERE,
                                "Unable to update heartbeat - did your Redis server go away?",
                                e);
                      }
                      serverIds = getCurrentServerIds(true, false);
                      globalPlayerCount.set(getCurrentCount());
                    }
                  },
                  0,
                  3,
                  TimeUnit.SECONDS);
      dataManager = new DataManager(this);
      if (configuration.isRegisterBungeeCommands()) {
        getProxy()
            .getPluginManager()
            .registerCommand(this, new RedisBungeeCommands.GlistCommand(this));
        getProxy()
            .getPluginManager()
            .registerCommand(this, new RedisBungeeCommands.FindCommand(this));
        getProxy()
            .getPluginManager()
            .registerCommand(this, new RedisBungeeCommands.LastSeenCommand(this));
        getProxy()
            .getPluginManager()
            .registerCommand(this, new RedisBungeeCommands.IpCommand(this));
      }
      getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.SendToAll(this));
      getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.ServerId(this));
      getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.ServerIds());
      getProxy()
          .getPluginManager()
          .registerCommand(this, new RedisBungeeCommands.PlayerProxyCommand(this));
      getProxy()
          .getPluginManager()
          .registerCommand(this, new RedisBungeeCommands.PlistCommand(this));
      getProxy()
          .getPluginManager()
          .registerCommand(this, new RedisBungeeCommands.DebugCommand(this));
      api = new RedisBungeeAPI(this);
      getProxy()
          .getPluginManager()
          .registerListener(
              this, new RedisBungeeListener(this, configuration.getExemptAddresses()));
      getProxy().getPluginManager().registerListener(this, dataManager);
      psl = new PubSubListener();
      getProxy().getScheduler().runAsync(this, psl);
      integrityCheck =
          getProxy()
              .getScheduler()
              .schedule(
                  this,
                  new Runnable() {
                    @Override
                    public void run() {
                      try (Jedis tmpRsc = pool.getResource()) {
                        Set<String> players = getLocalPlayersAsUuidStrings();
                        Set<String> playersInRedis =
                            tmpRsc.smembers(
                                "proxy:" + configuration.getServerId() + ":usersOnline");
                        List<String> lagged = getCurrentServerIds(false, true);

                        // Clean up lagged players.
                        for (String s : lagged) {
                          Set<String> laggedPlayers =
                              tmpRsc.smembers("proxy:" + s + ":usersOnline");
                          tmpRsc.del("proxy:" + s + ":usersOnline");
                          if (!laggedPlayers.isEmpty()) {
                            getLogger()
                                .info(
                                    "Cleaning up lagged proxy "
                                        + s
                                        + " ("
                                        + laggedPlayers.size()
                                        + " players)...");
                            for (String laggedPlayer : laggedPlayers) {
                              RedisUtil.cleanUpPlayer(laggedPlayer, tmpRsc);
                            }
                          }
                        }

                        for (Iterator<String> it = playersInRedis.iterator(); it.hasNext(); ) {
                          String member = it.next();
                          if (!players.contains(member)) {
                            // Are they simply on a different proxy?
                            String found = null;
                            for (String proxyId : getServerIds()) {
                              if (proxyId.equals(configuration.getServerId())) continue;
                              if (tmpRsc.sismember("proxy:" + proxyId + ":usersOnline", member)) {
                                // Just clean up the set.
                                found = proxyId;
                                break;
                              }
                            }
                            if (found == null) {
                              RedisUtil.cleanUpPlayer(member, tmpRsc);
                              getLogger()
                                  .warning(
                                      "Player found in set that was not found locally and globally: "
                                          + member);
                            } else {
                              tmpRsc.srem(
                                  "proxy:" + configuration.getServerId() + ":usersOnline", member);
                              getLogger()
                                  .warning(
                                      "Player found in set that was not found locally, but is on another proxy: "
                                          + member);
                            }

                            it.remove();
                          }
                        }

                        Pipeline pipeline = tmpRsc.pipelined();

                        for (String player : players) {
                          if (playersInRedis.contains(player)) continue;

                          // Player not online according to Redis but not BungeeCord.
                          getLogger()
                              .warning("Player " + player + " is on the proxy but not in Redis.");

                          ProxiedPlayer proxiedPlayer =
                              ProxyServer.getInstance().getPlayer(UUID.fromString(player));
                          if (proxiedPlayer == null) continue; // We'll deal with it later.

                          RedisUtil.createPlayer(proxiedPlayer, pipeline, true);
                        }

                        pipeline.sync();
                      }
                    }
                  },
                  0,
                  1,
                  TimeUnit.MINUTES);
    }
    getProxy().registerChannel("RedisBungee");
  }