Search in sources :

Example 6 with RegionLocations

use of org.apache.hadoop.hbase.RegionLocations 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)

Example 7 with RegionLocations

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

the class HRegionLocator method listRegionLocations.

@VisibleForTesting
List<RegionLocations> listRegionLocations() throws IOException {
    final List<RegionLocations> regions = new ArrayList<>();
    MetaTableAccessor.Visitor visitor = new MetaTableAccessor.TableVisitorBase(tableName) {

        @Override
        public boolean visitInternal(Result result) throws IOException {
            RegionLocations locations = MetaTableAccessor.getRegionLocations(result);
            if (locations == null)
                return true;
            regions.add(locations);
            return true;
        }
    };
    MetaTableAccessor.scanMetaForTableRegions(connection, visitor, tableName);
    return regions;
}
Also used : RegionLocations(org.apache.hadoop.hbase.RegionLocations) ArrayList(java.util.ArrayList) MetaTableAccessor(org.apache.hadoop.hbase.MetaTableAccessor) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with RegionLocations

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

the class HRegionLocator method getAllRegionLocations.

@Override
public List<HRegionLocation> getAllRegionLocations() throws IOException {
    TableName tableName = getName();
    List<Pair<HRegionInfo, ServerName>> locations = MetaTableAccessor.getTableRegionsAndLocations(this.connection, tableName);
    ArrayList<HRegionLocation> regions = new ArrayList<>(locations.size());
    for (Pair<HRegionInfo, ServerName> entry : locations) {
        regions.add(new HRegionLocation(entry.getFirst(), entry.getSecond()));
    }
    if (regions.size() > 0) {
        connection.cacheLocation(tableName, new RegionLocations(regions));
    }
    return regions;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) TableName(org.apache.hadoop.hbase.TableName) RegionLocations(org.apache.hadoop.hbase.RegionLocations) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ServerName(org.apache.hadoop.hbase.ServerName) ArrayList(java.util.ArrayList) Pair(org.apache.hadoop.hbase.util.Pair)

Example 9 with RegionLocations

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

the class MetaCache method clearCache.

/**
   * Deletes the cached location of the region if necessary, based on some error from source.
   * @param hri The region in question.
   */
public void clearCache(HRegionInfo hri) {
    ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(hri.getTable());
    RegionLocations regionLocations = tableLocations.get(hri.getStartKey());
    if (regionLocations != null) {
        HRegionLocation oldLocation = regionLocations.getRegionLocation(hri.getReplicaId());
        if (oldLocation == null)
            return;
        RegionLocations updatedLocations = regionLocations.remove(oldLocation);
        boolean removed;
        if (updatedLocations != regionLocations) {
            if (updatedLocations.isEmpty()) {
                removed = tableLocations.remove(hri.getStartKey(), regionLocations);
            } else {
                removed = tableLocations.replace(hri.getStartKey(), regionLocations, updatedLocations);
            }
            if (removed) {
                if (metrics != null) {
                    metrics.incrMetaCacheNumClearRegion();
                }
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Removed " + oldLocation + " from cache");
                }
            }
        }
    }
}
Also used : RegionLocations(org.apache.hadoop.hbase.RegionLocations) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation)

Example 10 with RegionLocations

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

the class MetaCache method clearCache.

/**
   * Delete a cached location for a table, row and server
   */
public void clearCache(final TableName tableName, final byte[] row, ServerName serverName) {
    ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName);
    RegionLocations regionLocations = getCachedLocation(tableName, row);
    if (regionLocations != null) {
        RegionLocations updatedLocations = regionLocations.removeByServer(serverName);
        if (updatedLocations != regionLocations) {
            byte[] startKey = regionLocations.getRegionLocation().getRegionInfo().getStartKey();
            boolean removed = false;
            if (updatedLocations.isEmpty()) {
                removed = tableLocations.remove(startKey, regionLocations);
            } else {
                removed = tableLocations.replace(startKey, regionLocations, updatedLocations);
            }
            if (removed) {
                if (metrics != null) {
                    metrics.incrMetaCacheNumClearRegion();
                }
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Removed locations of table: " + tableName + " ,row: " + Bytes.toString(row) + " mapping to server: " + serverName + " from cache");
                }
            }
        }
    }
}
Also used : RegionLocations(org.apache.hadoop.hbase.RegionLocations)

Aggregations

RegionLocations (org.apache.hadoop.hbase.RegionLocations)47 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)28 ServerName (org.apache.hadoop.hbase.ServerName)18 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)13 Test (org.junit.Test)9 IOException (java.io.IOException)8 InterruptedIOException (java.io.InterruptedIOException)7 ArrayList (java.util.ArrayList)6 Result (org.apache.hadoop.hbase.client.Result)6 TableName (org.apache.hadoop.hbase.TableName)5 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)4 MetaTableAccessor (org.apache.hadoop.hbase.MetaTableAccessor)4 Pair (org.apache.hadoop.hbase.util.Pair)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HBaseIOException (org.apache.hadoop.hbase.HBaseIOException)2 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)2