org.apache.hadoop.hbase.util.Addressing Java Examples

The following examples show how to use org.apache.hadoop.hbase.util.Addressing. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HMaster.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
  String redirectHost = regionServerHostname;
  if(redirectHost == null) {
    redirectHost = request.getServerName();
    if(!Addressing.isLocalAddress(InetAddress.getByName(redirectHost))) {
      LOG.warn("Couldn't resolve '" + redirectHost + "' as an address local to this node and '" +
          MASTER_HOSTNAME_KEY + "' is not set; client will get an HTTP 400 response. If " +
          "your HBase deployment relies on client accessible names that the region server process " +
          "can't resolve locally, then you should set the previously mentioned configuration variable " +
          "to an appropriate hostname.");
      // no sending client provided input back to the client, so the goal host is just in the logs.
      response.sendError(400, "Request was to a host that I can't resolve for any of the network interfaces on " +
          "this node. If this is due to an intermediary such as an HTTP load balancer or other proxy, your HBase " +
          "administrator can set '" + MASTER_HOSTNAME_KEY + "' to point to the correct hostname.");
      return;
    }
  }
  // TODO this scheme should come from looking at the scheme registered in the infoserver's http server for the
  // host and port we're using, but it's buried way too deep to do that ATM.
  String redirectUrl = request.getScheme() + "://"
    + redirectHost + ":" + regionServerInfoPort
    + request.getRequestURI();
  response.sendRedirect(redirectUrl);
}
 
Example #2
Source File: QueryUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static String getConnectionUrl(Properties props, Configuration conf)
        throws ClassNotFoundException, SQLException {
    // make sure we load the phoenix driver
    Class.forName(PhoenixDriver.class.getName());

    // read the hbase properties from the configuration
    String server = ZKConfig.getZKQuorumServersString(conf);
    // could be a comma-separated list
    String[] rawServers = server.split(",");
    List<String> servers = new ArrayList<String>(rawServers.length);
    boolean first = true;
    int port = -1;
    for (String serverPort : rawServers) {
        try {
            server = Addressing.parseHostname(serverPort);
            int specifiedPort = Addressing.parsePort(serverPort);
            // there was a previously specified port and it doesn't match this server
            if (port > 0 && specifiedPort != port) {
                throw new IllegalStateException("Phoenix/HBase only supports connecting to a " +
                        "single zookeeper client port. Specify servers only as host names in " +
                        "HBase configuration");
            }
            // set the port to the specified port
            port = specifiedPort;
            servers.add(server);
        } catch (IllegalArgumentException e) {
        }
    }
    // port wasn't set, shouldn't ever happen from HBase, but just in case
    if (port == -1) {
        port = conf.getInt(QueryServices.ZOOKEEPER_PORT_ATTRIB, -1);
        if (port == -1) {
            throw new RuntimeException("Client zk port was not set!");
        }
    }
    server = Joiner.on(',').join(servers);

    return getUrl(server, port);
}
 
Example #3
Source File: MemcachedBlockCache.java    From hbase with Apache License 2.0 5 votes vote down vote up
public MemcachedBlockCache(Configuration c) throws IOException {
  LOG.info("Creating MemcachedBlockCache");

  long opTimeout = c.getLong(MEMCACHED_OPTIMEOUT_KEY, MEMCACHED_DEFAULT_TIMEOUT);
  long queueTimeout = c.getLong(MEMCACHED_TIMEOUT_KEY, opTimeout + MEMCACHED_DEFAULT_TIMEOUT);
  boolean optimize = c.getBoolean(MEMCACHED_OPTIMIZE_KEY, MEMCACHED_OPTIMIZE_DEFAULT);

  ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder()
      .setOpTimeout(opTimeout)
      .setOpQueueMaxBlockTime(queueTimeout) // Cap the max time before anything times out
      .setFailureMode(FailureMode.Redistribute)
      .setShouldOptimize(optimize)
      .setDaemon(true)                      // Don't keep threads around past the end of days.
      .setUseNagleAlgorithm(false)          // Ain't nobody got time for that
      .setReadBufferSize(HConstants.DEFAULT_BLOCKSIZE * 4 * 1024); // Much larger just in case

  // Assume only the localhost is serving memecached.
  // A la mcrouter or co-locating memcached with split regionservers.
  //
  // If this config is a pool of memecached servers they will all be used according to the
  // default hashing scheme defined by the memcache client. Spy Memecache client in this
  // case.
  String serverListString = c.get(MEMCACHED_CONFIG_KEY,"localhost:11211");
  String[] servers = serverListString.split(",");
  List<InetSocketAddress> serverAddresses = new ArrayList<>(servers.length);
  for (String s:servers) {
    serverAddresses.add(Addressing.createInetSocketAddressFromHostAndPortStr(s));
  }

  client = new MemcachedClient(builder.build(), serverAddresses);
}
 
Example #4
Source File: ProtobufUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Get a ServerName from the passed in data bytes.
 * @param data Data with a serialize server name in it; can handle the old style servername where
 *          servername was host and port. Works too with data that begins w/ the pb 'PBUF' magic
 *          and that is then followed by a protobuf that has a serialized {@link ServerName} in
 *          it.
 * @return Returns null if <code>data</code> is null else converts passed data to a ServerName
 *         instance.
 */
public static ServerName toServerName(final byte[] data) throws DeserializationException {
  if (data == null || data.length <= 0) {
    return null;
  }
  if (ProtobufMagic.isPBMagicPrefix(data)) {
    int prefixLen = ProtobufMagic.lengthOfPBMagic();
    try {
      ZooKeeperProtos.Master rss =
        ZooKeeperProtos.Master.parser().parseFrom(data, prefixLen, data.length - prefixLen);
      HBaseProtos.ServerName sn = rss.getMaster();
      return ServerName.valueOf(sn.getHostName(), sn.getPort(), sn.getStartCode());
    } catch (/* InvalidProtocolBufferException */IOException e) {
      // A failed parse of the znode is pretty catastrophic. Rather than loop
      // retrying hoping the bad bytes will changes, and rather than change
      // the signature on this method to add an IOE which will send ripples all
      // over the code base, throw a RuntimeException. This should "never" happen.
      // Fail fast if it does.
      throw new DeserializationException(e);
    }
  }
  // The str returned could be old style -- pre hbase-1502 -- which was
  // hostname and port seperated by a colon rather than hostname, port and
  // startcode delimited by a ','.
  String str = Bytes.toString(data);
  int index = str.indexOf(ServerName.SERVERNAME_SEPARATOR);
  if (index != -1) {
    // Presume its ServerName serialized with versioned bytes.
    return ServerName.parseVersionedServerName(data);
  }
  // Presume it a hostname:port format.
  String hostname = Addressing.parseHostname(str);
  int port = Addressing.parsePort(str);
  return ServerName.valueOf(hostname, port, -1L);
}
 
Example #5
Source File: ProtobufUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Get a ServerName from the passed in data bytes.
 * @param data Data with a serialize server name in it; can handle the old style
 * servername where servername was host and port.  Works too with data that
 * begins w/ the pb 'PBUF' magic and that is then followed by a protobuf that
 * has a serialized {@link ServerName} in it.
 * @return Returns null if <code>data</code> is null else converts passed data
 * to a ServerName instance.
 * @throws DeserializationException
 */
public static ServerName parseServerNameFrom(final byte [] data) throws DeserializationException {
  if (data == null || data.length <= 0) return null;
  if (ProtobufMagic.isPBMagicPrefix(data)) {
    int prefixLen = ProtobufMagic.lengthOfPBMagic();
    try {
      ZooKeeperProtos.Master rss =
        ZooKeeperProtos.Master.PARSER.parseFrom(data, prefixLen, data.length - prefixLen);
      org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.ServerName sn =
          rss.getMaster();
      return ServerName.valueOf(sn.getHostName(), sn.getPort(), sn.getStartCode());
    } catch (/*InvalidProtocolBufferException*/IOException e) {
      // A failed parse of the znode is pretty catastrophic. Rather than loop
      // retrying hoping the bad bytes will changes, and rather than change
      // the signature on this method to add an IOE which will send ripples all
      // over the code base, throw a RuntimeException.  This should "never" happen.
      // Fail fast if it does.
      throw new DeserializationException(e);
    }
  }
  // The str returned could be old style -- pre hbase-1502 -- which was
  // hostname and port seperated by a colon rather than hostname, port and
  // startcode delimited by a ','.
  String str = Bytes.toString(data);
  int index = str.indexOf(ServerName.SERVERNAME_SEPARATOR);
  if (index != -1) {
    // Presume its ServerName serialized with versioned bytes.
    return ServerName.parseVersionedServerName(data);
  }
  // Presume it a hostname:port format.
  String hostname = Addressing.parseHostname(str);
  int port = Addressing.parsePort(str);
  return ServerName.valueOf(hostname, port, -1L);
}
 
Example #6
Source File: ClientIdGenerator.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @return Some IPv4/IPv6 address available on the current machine that is up, not virtual
 *         and not a loopback address. Empty array if none can be found or error occurred.
 */
public static byte[] getIpAddressBytes() {
  try {
    return Addressing.getIpAddress().getAddress();
  } catch (IOException ex) {
    LOG.warn("Failed to get IP address bytes", ex);
  }
  return new byte[0];
}
 
Example #7
Source File: TestServerName.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testRegexPatterns() {
  assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123"));
  assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, ""));
  assertTrue(ServerName.SERVERNAME_PATTERN.matcher("www1.example.org,1234,567").matches());
  ServerName.parseServerName("a.b.c,58102,1319771740322");
  ServerName.parseServerName("192.168.1.199,58102,1319771740322");
  ServerName.parseServerName("a.b.c:58102");
  ServerName.parseServerName("192.168.1.199:58102");
}
 
Example #8
Source File: StartcodeAgnosticServerName.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static StartcodeAgnosticServerName valueOf(final String hostnameAndPort, long startcode) {
  return new StartcodeAgnosticServerName(Addressing.parseHostname(hostnameAndPort),
      Addressing.parsePort(hostnameAndPort), startcode);
}
 
Example #9
Source File: HMaster.java    From hbase with Apache License 2.0 4 votes vote down vote up
private int putUpJettyServer() throws IOException {
  if (!conf.getBoolean("hbase.master.infoserver.redirect", true)) {
    return -1;
  }
  final int infoPort = conf.getInt("hbase.master.info.port.orig",
    HConstants.DEFAULT_MASTER_INFOPORT);
  // -1 is for disabling info server, so no redirecting
  if (infoPort < 0 || infoServer == null) {
    return -1;
  }
  if(infoPort == infoServer.getPort()) {
    return infoPort;
  }
  final String addr = conf.get("hbase.master.info.bindAddress", "0.0.0.0");
  if (!Addressing.isLocalAddress(InetAddress.getByName(addr))) {
    String msg =
        "Failed to start redirecting jetty server. Address " + addr
            + " does not belong to this host. Correct configuration parameter: "
            + "hbase.master.info.bindAddress";
    LOG.error(msg);
    throw new IOException(msg);
  }

  // TODO I'm pretty sure we could just add another binding to the InfoServer run by
  // the RegionServer and have it run the RedirectServlet instead of standing up
  // a second entire stack here.
  masterJettyServer = new Server();
  final ServerConnector connector = new ServerConnector(masterJettyServer);
  connector.setHost(addr);
  connector.setPort(infoPort);
  masterJettyServer.addConnector(connector);
  masterJettyServer.setStopAtShutdown(true);

  final String redirectHostname =
      StringUtils.isBlank(useThisHostnameInstead) ? null : useThisHostnameInstead;

  final RedirectServlet redirect = new RedirectServlet(infoServer, redirectHostname);
  final WebAppContext context = new WebAppContext(null, "/", null, null, null, null, WebAppContext.NO_SESSIONS);
  context.addServlet(new ServletHolder(redirect), "/*");
  context.setServer(masterJettyServer);

  try {
    masterJettyServer.start();
  } catch (Exception e) {
    throw new IOException("Failed to start redirecting jetty server", e);
  }
  return connector.getLocalPort();
}
 
Example #10
Source File: HRegionServer.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Puts up the webui.
 */
private void putUpWebUI() throws IOException {
  int port = this.conf.getInt(HConstants.REGIONSERVER_INFO_PORT,
    HConstants.DEFAULT_REGIONSERVER_INFOPORT);
  String addr = this.conf.get("hbase.regionserver.info.bindAddress", "0.0.0.0");

  if(this instanceof HMaster) {
    port = conf.getInt(HConstants.MASTER_INFO_PORT,
        HConstants.DEFAULT_MASTER_INFOPORT);
    addr = this.conf.get("hbase.master.info.bindAddress", "0.0.0.0");
  }
  // -1 is for disabling info server
  if (port < 0) {
    return;
  }

  if (!Addressing.isLocalAddress(InetAddress.getByName(addr))) {
    String msg =
        "Failed to start http info server. Address " + addr
            + " does not belong to this host. Correct configuration parameter: "
            + "hbase.regionserver.info.bindAddress";
    LOG.error(msg);
    throw new IOException(msg);
  }
  // check if auto port bind enabled
  boolean auto = this.conf.getBoolean(HConstants.REGIONSERVER_INFO_PORT_AUTO, false);
  while (true) {
    try {
      this.infoServer = new InfoServer(getProcessName(), addr, port, false, this.conf);
      infoServer.addPrivilegedServlet("dump", "/dump", getDumpServlet());
      configureInfoServer();
      this.infoServer.start();
      break;
    } catch (BindException e) {
      if (!auto) {
        // auto bind disabled throw BindException
        LOG.error("Failed binding http info server to port: " + port);
        throw e;
      }
      // auto bind enabled, try to use another port
      LOG.info("Failed binding http info server to port: " + port);
      port++;
    }
  }
  port = this.infoServer.getPort();
  conf.setInt(HConstants.REGIONSERVER_INFO_PORT, port);
  int masterInfoPort = conf.getInt(HConstants.MASTER_INFO_PORT,
    HConstants.DEFAULT_MASTER_INFOPORT);
  conf.setInt("hbase.master.info.port.orig", masterInfoPort);
  conf.setInt(HConstants.MASTER_INFO_PORT, port);
}
 
Example #11
Source File: HBaseInputFormatGranular.java    From SpyGlass with Apache License 2.0 4 votes vote down vote up
private String toHostname(String regionServerHostnamePort) {
	return Addressing.parseHostname(regionServerHostnamePort);
}
 
Example #12
Source File: HRegionLocation.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * @return String made of hostname and port formatted as
 * per {@link Addressing#createHostAndPortStr(String, int)}
 */
public String getHostnamePort() {
  return Addressing.createHostAndPortStr(this.getHostname(), this.getPort());
}
 
Example #13
Source File: ServerName.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * @return Return a SHORT version of {@link #toString()}, one that has the host only,
 *   minus the domain, and the port only -- no start code; the String is for us internally mostly
 *   tying threads to their server.  Not for external use.  It is lossy and will not work in
 *   in compares, etc.
 */
public String toShortString() {
  return Addressing.createHostAndPortStr(
    getHostNameMinusDomain(this.address.getHostname()),
    this.address.getPort());
}