示例#1
0
    private boolean isMasterAddressChanged(ConnectionInfo currentMasterConnectionInfo) {
      if (previousMasterConnectionInfo == null) {
        return true;
      }

      return !previousMasterConnectionInfo
          .getHostAndPort()
          .equals(currentMasterConnectionInfo.getHostAndPort());
    }
示例#2
0
  public JedisPool(ConnectionInfo connectionInfo, JedisPoolConfig config) {
    this.hostAndPort = connectionInfo.getHostAndPort();

    JedisFactory factory =
        new JedisFactory(
            connectionInfo.getHost(),
            connectionInfo.getPort(),
            connectionInfo.getTimeout(),
            connectionInfo.getPassword(),
            connectionInfo.getDatabase(),
            connectionInfo.getClientName());

    internalPool = new GenericObjectPool(factory, config);
  }
示例#3
0
      @Override
      public void onMessage(String channel, String message) {
        // message example: +switch-master: mymaster 127.0.0.1 6379 127.0.0.1 6380
        // +redirect-to-master mymaster 127.0.0.1 6380 127.0.0.1 6381 (if slave-master fail-over
        // quick enough)
        logger.info("Sentinel " + sentinelInfo.getHostAndPort() + " published: " + message);
        String[] switchMasterMsg = message.split(" ");
        // if the switeched master name equals my master name, destroy the old pool and init a new
        // pool
        if (masterName.equals(switchMasterMsg[0])) {
          destroyInternalPool();

          ConnectionInfo masterConnectionInfo =
              buildMasterConnectionInfo(switchMasterMsg[3], switchMasterMsg[4]);
          logger.info("Switch master to " + masterConnectionInfo.getHostAndPort());

          initInternalPool(masterConnectionInfo);

          previousMasterConnectionInfo = masterConnectionInfo;
        }
      }
示例#4
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);
        }
      }
    }