Java Code Examples for org.apache.hadoop.hbase.HRegionLocation#getHostname()

The following examples show how to use org.apache.hadoop.hbase.HRegionLocation#getHostname() . 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: HFileGenerationFunction.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
private static InetSocketAddress getFavoredNode(BulkImportPartition partition) throws IOException {
    InetSocketAddress favoredNode = null;
    SConfiguration configuration = HConfiguration.getConfiguration();
    Connection connection = HBaseConnectionFactory.getInstance(configuration).getConnection();

    String regionName = partition.getRegionName();
    HRegionLocation regionLocation = MetaTableAccessor.getRegionLocation(connection,
            com.splicemachine.primitives.Bytes.toBytesBinary(regionName));

    if (regionLocation != null) {
        String hostname = regionLocation.getHostname();
        int port = regionLocation.getPort();

        InetSocketAddress address = new InetSocketAddress(hostname, port);
        if (!address.isUnresolved()) {
            favoredNode = address;
        } else {
            SpliceLogUtils.info(LOG, "Cannot resolve host %s to achieve better data locality.", hostname);
        }

    }
    else {
        SpliceLogUtils.info(LOG, "Cannot to get region location %s to achieve better data locality.", regionName);
    }
    return favoredNode;
}
 
Example 2
Source File: PhoenixSplitManager.java    From presto with Apache License 2.0 5 votes vote down vote up
private List<InputSplit> generateSplits(QueryPlan queryPlan, List<KeyRange> splits)
        throws IOException
{
    requireNonNull(queryPlan, "queryPlan is null");
    requireNonNull(splits, "splits is null");

    try (org.apache.hadoop.hbase.client.Connection connection = phoenixClient.getHConnection()) {
        RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(queryPlan.getTableRef().getTable().getPhysicalName().toString()));
        long regionSize = -1;
        List<InputSplit> inputSplits = new ArrayList<>(splits.size());
        for (List<Scan> scans : queryPlan.getScans()) {
            HRegionLocation location = regionLocator.getRegionLocation(scans.get(0).getStartRow(), false);
            String regionLocation = location.getHostname();

            if (log.isDebugEnabled()) {
                log.debug(
                        "Scan count[%d] : %s ~ %s",
                        scans.size(),
                        Bytes.toStringBinary(scans.get(0).getStartRow()),
                        Bytes.toStringBinary(scans.get(scans.size() - 1).getStopRow()));
                log.debug("First scan : %swith scanAttribute : %s [scanCache, cacheBlock, scanBatch] : [%d, %s, %d] and  regionLocation : %s",
                        scans.get(0), scans.get(0).getAttributesMap(), scans.get(0).getCaching(), scans.get(0).getCacheBlocks(), scans.get(0).getBatch(), regionLocation);
                for (int i = 0, limit = scans.size(); i < limit; i++) {
                    log.debug("EXPECTED_UPPER_REGION_KEY[%d] : %s", i, Bytes.toStringBinary(scans.get(i).getAttribute(EXPECTED_UPPER_REGION_KEY)));
                }
            }
            inputSplits.add(new PhoenixInputSplit(scans, regionSize, regionLocation));
        }
        return inputSplits;
    }
}
 
Example 3
Source File: RegionChecker.java    From splicer with Apache License 2.0 5 votes vote down vote up
public String getBestRegionHost(byte[] startRowKey, byte[] endRowKey) {
	try {
		List<HRegionLocation> regions = table.getRegionsInRange(startRowKey, endRowKey);
		if (regions != null && regions.size() > 0) {
			HRegionLocation reg = regions.get(0);
			LOG.debug("Found region hostname: " + reg.getHostname());
			return reg.getHostname();
		} else {
			LOG.info("Regions is null");
			throw new RegionCheckException("Could not find a host");
		}
	} catch (IOException e) {
		throw new RegionCheckException("Could not handle region server lookup", e);
	}
}
 
Example 4
Source File: HBaseFsck.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Record the location of the hbase:meta region as found in ZooKeeper.
 */
private boolean recordMetaRegion() throws IOException {
  List<HRegionLocation> locs;
  try (RegionLocator locator = connection.getRegionLocator(TableName.META_TABLE_NAME)) {
    locs = locator.getRegionLocations(HConstants.EMPTY_START_ROW, true);
  }
  if (locs == null || locs.isEmpty()) {
    errors.reportError(ERROR_CODE.NULL_META_REGION, "META region was not found in ZooKeeper");
    return false;
  }
  for (HRegionLocation metaLocation : locs) {
    // Check if Meta region is valid and existing
    if (metaLocation == null) {
      errors.reportError(ERROR_CODE.NULL_META_REGION, "META region location is null");
      return false;
    }
    if (metaLocation.getRegion() == null) {
      errors.reportError(ERROR_CODE.NULL_META_REGION, "META location regionInfo is null");
      return false;
    }
    if (metaLocation.getHostname() == null) {
      errors.reportError(ERROR_CODE.NULL_META_REGION, "META location hostName is null");
      return false;
    }
    ServerName sn = metaLocation.getServerName();
    HbckRegionInfo.MetaEntry m = new HbckRegionInfo.MetaEntry(metaLocation.getRegion(), sn,
        EnvironmentEdgeManager.currentTime());
    HbckRegionInfo hbckRegionInfo = regionInfoMap.get(metaLocation.getRegion().getEncodedName());
    if (hbckRegionInfo == null) {
      regionInfoMap.put(metaLocation.getRegion().getEncodedName(), new HbckRegionInfo(m));
    } else {
      hbckRegionInfo.setMetaEntry(m);
    }
  }
  return true;
}
 
Example 5
Source File: LazyPartitionServer.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public String getHostname(){
    try(RegionLocator rl = connection.getRegionLocator(tableName)){
        HRegionLocation hrl =rl.getRegionLocation(regionInfo.getStartKey());
        return hrl.getHostname();
    }catch(IOException e){
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: PhoenixInputFormat.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private List<InputSplit> generateSplits(final QueryPlan qplan, Configuration config) throws IOException {
    // We must call this in order to initialize the scans and splits from the query plan
    setupParallelScansFromQueryPlan(qplan);
    final List<KeyRange> splits = qplan.getSplits();
    Preconditions.checkNotNull(splits);

    // Get the RegionSizeCalculator
    try(org.apache.hadoop.hbase.client.Connection connection =
                HBaseFactoryProvider.getHConnectionFactory().createConnection(config)) {
    RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(qplan
            .getTableRef().getTable().getPhysicalName().toString()));
    RegionSizeCalculator sizeCalculator = new RegionSizeCalculator(regionLocator, connection
            .getAdmin());

    final List<InputSplit> psplits = Lists.newArrayListWithExpectedSize(splits.size());
    for (List<Scan> scans : qplan.getScans()) {
        // Get the region location
        HRegionLocation location = regionLocator.getRegionLocation(
                scans.get(0).getStartRow(),
                false
        );

        String regionLocation = location.getHostname();

        // Get the region size
        long regionSize = sizeCalculator.getRegionSize(
                location.getRegion().getRegionName()
        );

        // Generate splits based off statistics, or just region splits?
        boolean splitByStats = PhoenixConfigurationUtil.getSplitByStats(config);

        if(splitByStats) {
            for(Scan aScan: scans) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Split for  scan : " + aScan + "with scanAttribute : " + aScan
                            .getAttributesMap() + " [scanCache, cacheBlock, scanBatch] : [" +
                            aScan.getCaching() + ", " + aScan.getCacheBlocks() + ", " + aScan
                            .getBatch() + "] and  regionLocation : " + regionLocation);
                }

                psplits.add(new PhoenixInputSplit(Collections.singletonList(aScan), regionSize, regionLocation));
            }
            } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scan count[" + scans.size() + "] : " + Bytes.toStringBinary(scans
                        .get(0).getStartRow()) + " ~ " + Bytes.toStringBinary(scans.get(scans
                        .size() - 1).getStopRow()));
                LOGGER.debug("First scan : " + scans.get(0) + "with scanAttribute : " + scans
                        .get(0).getAttributesMap() + " [scanCache, cacheBlock, scanBatch] : " +
                        "[" + scans.get(0).getCaching() + ", " + scans.get(0).getCacheBlocks()
                        + ", " + scans.get(0).getBatch() + "] and  regionLocation : " +
                        regionLocation);

                for (int i = 0, limit = scans.size(); i < limit; i++) {
                    LOGGER.debug("EXPECTED_UPPER_REGION_KEY[" + i + "] : " + Bytes
                            .toStringBinary(scans.get(i).getAttribute
                                    (BaseScannerRegionObserver.EXPECTED_UPPER_REGION_KEY)));
                }
            }

            psplits.add(new PhoenixInputSplit(scans, regionSize, regionLocation));
        }
    }
    return psplits;
}
}