Esempio n. 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);
   }
 }
Esempio n. 2
0
    /** Pickup the first available sentinel, if all sentinel down, return false. */
    private boolean selectSentinel() {
      for (ConnectionInfo info : sentinelInfos) {
        if (ping(info)) {
          sentinelInfo = info;
          sentinelJedis = new Jedis(sentinelInfo.getHost(), sentinelInfo.getPort());
          return true;
        }
      }

      return false;
    }
Esempio n. 3
0
  private void initInternalPool(ConnectionInfo masterConnectionInfo) {
    JedisFactory factory =
        new JedisFactory(
            masterConnectionInfo.getHost(),
            masterConnectionInfo.getPort(),
            masterConnectionInfo.getTimeout(),
            masterConnectionInfo.getPassword(),
            masterConnectionInfo.getDatabase(),
            masterConnectionInfo.getClientName());

    internalPool = new GenericObjectPool(factory, masterPoolConfig);
  }
Esempio n. 4
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);
  }
Esempio n. 5
0
  @Override
  protected ConnectionQueryServices getConnectionQueryServices(String url, Properties info)
      throws SQLException {
    ConnectionInfo connInfo = getConnectionInfo(url);
    String zookeeperQuorum = connInfo.getZookeeperQuorum();
    Integer port = connInfo.getPort();
    String rootNode = connInfo.getRootNode();
    boolean isConnectionless = false;
    // Normalize connInfo so that a url explicitly specifying versus implicitly inheriting
    // the default values will both share the same ConnectionQueryServices.
    Configuration globalConfig = getQueryServices().getConfig();
    if (zookeeperQuorum == null) {
      zookeeperQuorum = globalConfig.get(ZOOKEEPER_QUARUM_ATTRIB);
      if (zookeeperQuorum == null) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL)
            .setMessage(url)
            .build()
            .buildException();
      }
    }
    isConnectionless = PhoenixRuntime.CONNECTIONLESS.equals(zookeeperQuorum);

    if (port == null) {
      if (!isConnectionless) {
        String portStr = globalConfig.get(ZOOKEEPER_PORT_ATTRIB);
        if (portStr != null) {
          try {
            port = Integer.parseInt(portStr);
          } catch (NumberFormatException e) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL)
                .setMessage(url)
                .build()
                .buildException();
          }
        }
      }
    } else if (isConnectionless) {
      throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL)
          .setMessage("Port may not be specified when using the connectionless url \"" + url + "\"")
          .build()
          .buildException();
    }
    if (rootNode == null) {
      if (!isConnectionless) {
        rootNode = globalConfig.get(ZOOKEEPER_ROOT_NODE_ATTRIB);
      }
    } else if (isConnectionless) {
      throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL)
          .setMessage(
              "Root node may not be specified when using the connectionless url \"" + url + "\"")
          .build()
          .buildException();
    }
    ConnectionInfo normalizedConnInfo = new ConnectionInfo(zookeeperQuorum, port, rootNode);
    ConnectionQueryServices connectionQueryServices =
        connectionQueryServicesMap.get(normalizedConnInfo);
    if (connectionQueryServices == null) {
      if (isConnectionless) {
        connectionQueryServices = new ConnectionlessQueryServicesImpl(getQueryServices());
      } else {
        Configuration childConfig = HBaseConfiguration.create(globalConfig);
        if (connInfo.getZookeeperQuorum() != null) {
          childConfig.set(ZOOKEEPER_QUARUM_ATTRIB, connInfo.getZookeeperQuorum());
        } else if (childConfig.get(ZOOKEEPER_QUARUM_ATTRIB) == null) {
          throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL)
              .setMessage(url)
              .build()
              .buildException();
        }
        if (connInfo.getPort() != null) {
          childConfig.setInt(ZOOKEEPER_PORT_ATTRIB, connInfo.getPort());
        }
        if (connInfo.getRootNode() != null) {
          childConfig.set(ZOOKEEPER_ROOT_NODE_ATTRIB, connInfo.getRootNode());
        }
        connectionQueryServices = new ConnectionQueryServicesImpl(getQueryServices(), childConfig);
      }
      connectionQueryServices.init(url, info);
      ConnectionQueryServices prevValue =
          connectionQueryServicesMap.putIfAbsent(normalizedConnInfo, connectionQueryServices);
      if (prevValue != null) {
        connectionQueryServices = prevValue;
      }
    }
    return connectionQueryServices;
  }