Search in sources :

Example 26 with HRegionLocation

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

the class TestFromClientSide3 method performMultiplePutAndFlush.

private void performMultiplePutAndFlush(HBaseAdmin admin, Table table, byte[] row, byte[] family, int nFlushes, int nPuts) throws Exception {
    try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(table.getName())) {
        // connection needed for poll-wait
        HRegionLocation loc = locator.getRegionLocation(row, true);
        AdminProtos.AdminService.BlockingInterface server = ((ClusterConnection) admin.getConnection()).getAdmin(loc.getServerName());
        byte[] regName = loc.getRegionInfo().getRegionName();
        for (int i = 0; i < nFlushes; i++) {
            randomCFPuts(table, row, family, nPuts);
            List<String> sf = ProtobufUtil.getStoreFiles(server, regName, FAMILY);
            int sfCount = sf.size();
            admin.flush(table.getName());
        }
    }
}
Also used : HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) MultiRowMutationEndpoint(org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint)

Example 27 with HRegionLocation

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

the class TestAdmin2 method testGetRegion.

@Test(timeout = 300000)
public void testGetRegion() throws Exception {
    // We use actual HBaseAdmin instance instead of going via Admin interface in
    // here because makes use of an internal HBA method (TODO: Fix.).
    HBaseAdmin rawAdmin = TEST_UTIL.getHBaseAdmin();
    final TableName tableName = TableName.valueOf(name.getMethodName());
    LOG.info("Started " + tableName);
    Table t = TEST_UTIL.createMultiRegionTable(tableName, HConstants.CATALOG_FAMILY);
    try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
        HRegionLocation regionLocation = locator.getRegionLocation(Bytes.toBytes("mmm"));
        HRegionInfo region = regionLocation.getRegionInfo();
        byte[] regionName = region.getRegionName();
        Pair<HRegionInfo, ServerName> pair = rawAdmin.getRegion(regionName);
        assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName()));
        pair = rawAdmin.getRegion(region.getEncodedNameAsBytes());
        assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName()));
    }
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) TableName(org.apache.hadoop.hbase.TableName) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ServerName(org.apache.hadoop.hbase.ServerName) Test(org.junit.Test)

Example 28 with HRegionLocation

use of org.apache.hadoop.hbase.HRegionLocation 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 29 with HRegionLocation

use of org.apache.hadoop.hbase.HRegionLocation 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 30 with HRegionLocation

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

the class HTable method getKeysAndRegionsInRange.

/**
   * Get the corresponding start keys and regions for an arbitrary range of
   * keys.
   * <p>
   * @param startKey Starting row in range, inclusive
   * @param endKey Ending row in range
   * @param includeEndKey true if endRow is inclusive, false if exclusive
   * @param reload true to reload information or false to use cached information
   * @return A pair of list of start keys and list of HRegionLocations that
   *         contain the specified range
   * @throws IOException if a remote or network exception occurs
   */
private Pair<List<byte[]>, List<HRegionLocation>> getKeysAndRegionsInRange(final byte[] startKey, final byte[] endKey, final boolean includeEndKey, final boolean reload) throws IOException {
    final boolean endKeyIsEndOfTable = Bytes.equals(endKey, HConstants.EMPTY_END_ROW);
    if ((Bytes.compareTo(startKey, endKey) > 0) && !endKeyIsEndOfTable) {
        throw new IllegalArgumentException("Invalid range: " + Bytes.toStringBinary(startKey) + " > " + Bytes.toStringBinary(endKey));
    }
    List<byte[]> keysInRange = new ArrayList<>();
    List<HRegionLocation> regionsInRange = new ArrayList<>();
    byte[] currentKey = startKey;
    do {
        HRegionLocation regionLocation = getRegionLocator().getRegionLocation(currentKey, reload);
        keysInRange.add(currentKey);
        regionsInRange.add(regionLocation);
        currentKey = regionLocation.getRegionInfo().getEndKey();
    } while (!Bytes.equals(currentKey, HConstants.EMPTY_END_ROW) && (endKeyIsEndOfTable || Bytes.compareTo(currentKey, endKey) < 0 || (includeEndKey && Bytes.compareTo(currentKey, endKey) == 0)));
    return new Pair<>(keysInRange, regionsInRange);
}
Also used : HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ArrayList(java.util.ArrayList) Pair(org.apache.hadoop.hbase.util.Pair)

Aggregations

HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)213 Test (org.junit.Test)74 ServerName (org.apache.hadoop.hbase.ServerName)67 TableName (org.apache.hadoop.hbase.TableName)58 IOException (java.io.IOException)53 RegionLocations (org.apache.hadoop.hbase.RegionLocations)50 RegionLocator (org.apache.hadoop.hbase.client.RegionLocator)46 ArrayList (java.util.ArrayList)44 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)35 RegionInfo (org.apache.hadoop.hbase.client.RegionInfo)27 Table (org.apache.hadoop.hbase.client.Table)21 List (java.util.List)19 Map (java.util.Map)15 Connection (org.apache.hadoop.hbase.client.Connection)15 Configuration (org.apache.hadoop.conf.Configuration)14 HashMap (java.util.HashMap)13 TableNotFoundException (org.apache.hadoop.hbase.TableNotFoundException)13 MultiRowMutationEndpoint (org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint)13 CompletableFuture (java.util.concurrent.CompletableFuture)12 Admin (org.apache.hadoop.hbase.client.Admin)12