コード例 #1
0
 /** Ping the jedis instance, return true if the result is PONG. */
 private boolean ping(ConnectionInfo connectionInfo) {
   Jedis jedis = new Jedis(connectionInfo.getHost(), connectionInfo.getPort());
   try {
     String result = jedis.ping();
     return (result != null) && result.equals("PONG");
   } catch (JedisException e) {
     return false;
   } finally {
     JedisUtils.closeJedis(jedis);
   }
 }
コード例 #2
0
    /** Interrupt the thread and stop the blocking subscription. */
    private void shutdown() {
      running.getAndSet(false);
      this.interrupt();

      try {
        if (subscriber != null) {
          subscriber.unsubscribe();
        }
      } finally {
        JedisUtils.closeJedis(sentinelJedis);
      }
    }
コード例 #3
0
    @Override
    public void run() {
      while (running.get()) {
        try {
          boolean avalibleSentinelExist = selectSentinel();
          if (avalibleSentinelExist) {
            try {
              ConnectionInfo masterConnectionInfo = queryMasterAddress();
              if ((internalPool != null) && isMasterAddressChanged(masterConnectionInfo)) {
                logger.info(
                    "The internalPool {} had changed, destroy it now.",
                    previousMasterConnectionInfo.getHostAndPort());
                destroyInternalPool();
              }

              if ((internalPool == null) || isMasterAddressChanged(masterConnectionInfo)) {
                logger.info(
                    "The internalPool {} is not init or the address had changed, init it now.",
                    masterConnectionInfo.getHostAndPort());
                initInternalPool(masterConnectionInfo);
              }

              previousMasterConnectionInfo = masterConnectionInfo;

              // blocking listen master switch message until exception happen.
              subscriber = new MasterSwitchSubscriber();
              sentinelJedis.subscribe(subscriber, "+switch-master", "+redirect-to-master");
            } catch (JedisConnectionException e) {
              JedisUtils.closeJedis(sentinelJedis);

              if (running.get()) {
                logger.error(
                    "Lost connection with Sentinel "
                        + sentinelInfo.getHostAndPort()
                        + ", sleep 1000ms and try to connect another one.");
                sleep(RETRY_WAIT_TIME_MILLS);
              }
            } catch (Exception e) {
              JedisUtils.closeJedis(sentinelJedis);

              if (running.get()) {
                logger.error(
                    "Unexpected Exception happen, current Sentinel is"
                        + sentinelInfo.getHostAndPort()
                        + ", sleep 1000ms and try again.",
                    e);
                sleep(RETRY_WAIT_TIME_MILLS);
              }
            }
          } else {
            logger.info("All sentinels down, sleep 1000ms and try to select again.");
            // when the system startup but the sentinels not yet, init an ugly address to prevent
            // null point
            // exception.
            if (internalPool == null) {
              ConnectionInfo masterConnectionInfo = new ConnectionInfo(UNAVAILABLE_MASTER_ADDRESS);
              initInternalPool(masterConnectionInfo);
              previousMasterConnectionInfo = masterConnectionInfo;
            }
            sleep(RETRY_WAIT_TIME_MILLS);
          }
        } catch (Exception e) {
          logger.error("Unexpected exception happen", e);
          sleep(RETRY_WAIT_TIME_MILLS);
        }
      }
    }