Search in sources :

Example 1 with MetaTableAccessor

use of org.apache.hadoop.hbase.MetaTableAccessor in project hbase by apache.

the class HBaseAdmin method getRegion.

/**
   * @param regionName Name of a region.
   * @return a pair of HRegionInfo and ServerName if <code>regionName</code> is
   *  a verified region name (we call {@link
   *  MetaTableAccessor#getRegionLocation(Connection, byte[])}
   *  else null.
   * Throw IllegalArgumentException if <code>regionName</code> is null.
   * @throws IOException
   */
Pair<HRegionInfo, ServerName> getRegion(final byte[] regionName) throws IOException {
    if (regionName == null) {
        throw new IllegalArgumentException("Pass a table name or region name");
    }
    Pair<HRegionInfo, ServerName> pair = MetaTableAccessor.getRegion(connection, regionName);
    if (pair == null) {
        final AtomicReference<Pair<HRegionInfo, ServerName>> result = new AtomicReference<>(null);
        final String encodedName = Bytes.toString(regionName);
        MetaTableAccessor.Visitor visitor = new MetaTableAccessor.Visitor() {

            @Override
            public boolean visit(Result data) throws IOException {
                HRegionInfo info = MetaTableAccessor.getHRegionInfo(data);
                if (info == null) {
                    LOG.warn("No serialized HRegionInfo in " + data);
                    return true;
                }
                RegionLocations rl = MetaTableAccessor.getRegionLocations(data);
                boolean matched = false;
                ServerName sn = null;
                if (rl != null) {
                    for (HRegionLocation h : rl.getRegionLocations()) {
                        if (h != null && encodedName.equals(h.getRegionInfo().getEncodedName())) {
                            sn = h.getServerName();
                            info = h.getRegionInfo();
                            matched = true;
                        }
                    }
                }
                if (!matched)
                    return true;
                result.set(new Pair<>(info, sn));
                // found the region, stop
                return false;
            }
        };
        MetaTableAccessor.fullScanRegions(connection, visitor);
        pair = result.get();
    }
    return pair;
}
Also used : RegionLocations(org.apache.hadoop.hbase.RegionLocations) MetaTableAccessor(org.apache.hadoop.hbase.MetaTableAccessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ServerName(org.apache.hadoop.hbase.ServerName) Pair(org.apache.hadoop.hbase.util.Pair) NameStringPair(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameStringPair)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)1 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)1 MetaTableAccessor (org.apache.hadoop.hbase.MetaTableAccessor)1 RegionLocations (org.apache.hadoop.hbase.RegionLocations)1 ServerName (org.apache.hadoop.hbase.ServerName)1 NameStringPair (org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameStringPair)1 Pair (org.apache.hadoop.hbase.util.Pair)1