/** * Finds the region on which the given row is being served. Does not reload the cache. * @param row Row to find. * @return Location of the row. * @throws IOException if a remote or network exception occurs */ default HRegionLocation getRegionLocation(byte[] row) throws IOException { return getRegionLocation(row, false); }
/** * Finds the region on which the given row is being served. * @param row Row to find. * @param reload true to reload information or false to use cached information * @return Location of the row. * @throws IOException if a remote or network exception occurs */ default HRegionLocation getRegionLocation(byte[] row, boolean reload) throws IOException { return getRegionLocation(row, RegionInfo.DEFAULT_REPLICA_ID, reload); }
/** * Finds the region with the given replica id on which the given row is being served. * @param row Row to find. * @param replicaId the replica id * @return Location of the row. * @throws IOException if a remote or network exception occurs */ default HRegionLocation getRegionLocation(byte[] row, int replicaId) throws IOException { return getRegionLocation(row, replicaId, false); }
public static int getMetaRSPort(Connection connection) throws IOException { try (RegionLocator locator = connection.getRegionLocator(TableName.META_TABLE_NAME)) { return locator.getRegionLocation(Bytes.toBytes("")).getPort(); } }
@Override public THRegionLocation getRegionLocation(ByteBuffer table, ByteBuffer row, boolean reload) throws TIOError, TException { RegionLocator locator = null; try { locator = getLocator(table); byte[] rowBytes = byteBufferToByteArray(row); HRegionLocation hrl = locator.getRegionLocation(rowBytes, reload); return ThriftUtilities.regionLocationFromHBase(hrl); } catch (IOException e) { throw getTIOError(e); } finally { if (locator != null) { try { locator.close(); } catch (IOException e) { LOG.warn("Couldn't close the locator.", e); } } } }
@Override protected HRegionLocation getRegionLocation(TableName tableName, byte[] row, int replicaId) throws IOException { try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) { return locator.getRegionLocation(row, replicaId); } }
@Override public void prepare(final boolean reload) throws IOException { // check table state if this is a retry if (reload && tableName != null && !tableName.equals(TableName.META_TABLE_NAME) && getConnection().isTableDisabled(tableName)) { throw new TableNotEnabledException(tableName.getNameAsString() + " is disabled."); } try (RegionLocator regionLocator = connection.getRegionLocator(tableName)) { this.location = regionLocator.getRegionLocation(row); } if (this.location == null) { throw new IOException("Failed to find location, tableName=" + tableName + ", row=" + Bytes.toString(row) + ", reload=" + reload); } setStubByServiceName(this.location.getServerName()); }
private void verifyRegionResults(RegionLocator regionLocator, Map<byte[], String> results, String expected, byte[] row) throws Exception { for (Map.Entry<byte [], String> e: results.entrySet()) { LOG.info("row=" + Bytes.toString(row) + ", expected=" + expected + ", result key=" + Bytes.toString(e.getKey()) + ", value=" + e.getValue()); } HRegionLocation loc = regionLocator.getRegionLocation(row, true); byte[] region = loc.getRegionInfo().getRegionName(); assertTrue("Results should contain region " + Bytes.toStringBinary(region) + " for row '" + Bytes.toStringBinary(row)+ "'", results.containsKey(region)); assertEquals("Invalid result for row '"+Bytes.toStringBinary(row)+"'", expected, results.get(region)); } }
/** * Closes the region containing the given row. * * @param row The row to find the containing region. * @param table The table to find the region. * @throws IOException */ public void unassignRegionByRow(byte[] row, RegionLocator table) throws IOException { HRegionLocation hrl = table.getRegionLocation(row); unassignRegion(hrl.getRegionInfo().getRegionName()); }
@Override public ServerName getServerHoldingRegion(TableName tn, byte[] regionName) throws IOException { byte[] startKey = RegionInfo.getStartKey(regionName); HRegionLocation regionLoc = null; try (RegionLocator locator = connection.getRegionLocator(tn)) { regionLoc = locator.getRegionLocation(startKey, true); } if (regionLoc == null) { LOG.warn("Cannot find region server holding region " + Bytes.toStringBinary(regionName)); return null; } return regionLoc.getServerName(); }
protected void handleFailure(Counters counters) throws IOException { Configuration conf = job.getConfiguration(); TableName tableName = getTableName(conf); try (Connection conn = ConnectionFactory.createConnection(conf)) { try (RegionLocator rl = conn.getRegionLocator(tableName)) { CounterGroup g = counters.getGroup("undef"); Iterator<Counter> it = g.iterator(); while (it.hasNext()) { String keyString = it.next().getName(); byte[] key = Bytes.toBytes(keyString); HRegionLocation loc = rl.getRegionLocation(key, true); LOG.error("undefined row " + keyString + ", " + loc); } g = counters.getGroup("unref"); it = g.iterator(); while (it.hasNext()) { String keyString = it.next().getName(); byte[] key = Bytes.toBytes(keyString); HRegionLocation loc = rl.getRegionLocation(key, true); LOG.error("unreferred row " + keyString + ", " + loc); } } } } }
/** * Get servername that is up in hbase:meta hosting the given region. this is hostname + port + * startcode comma-delimited. Can return null * @return regionServer hosting the given region */ private ServerName getServerNameForRegion(RegionInfo region) throws IOException { if (!admin.isTableEnabled(region.getTable())) { return null; } HRegionLocation loc = conn.getRegionLocator(region.getTable()).getRegionLocation(region.getStartKey(), true); if (loc != null) { return loc.getServerName(); } else { return null; } }
private List<HRegionLocation> getRegionsInRange(TableName tableName, byte[] startKey, byte[] endKey) throws IOException { List<HRegionLocation> regionsInRange = new ArrayList<>(); byte[] currentKey = startKey; final boolean endKeyIsEndOfTable = Bytes.equals(endKey, HConstants.EMPTY_END_ROW); try (RegionLocator r = TEST_UTIL.getConnection().getRegionLocator(tableName)) { do { HRegionLocation regionLocation = r.getRegionLocation(currentKey); regionsInRange.add(regionLocation); currentKey = regionLocation.getRegionInfo().getEndKey(); } while (!Bytes.equals(currentKey, HConstants.EMPTY_END_ROW) && (endKeyIsEndOfTable || Bytes.compareTo(currentKey, endKey) < 0)); return regionsInRange; } }
@Test public void testSplit() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (Table table = createTable(false)) { table.put(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, Bytes.toBytes(0))); table.put(new Put(Bytes.toBytes(1)).addColumn(CF, CQ, Bytes.toBytes(0))); } UTIL.flush(NAME); HRegionServer rs = UTIL.getRSForFirstRegionInTable(NAME); RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo(); UTIL.getAdmin().splitRegionAsync(region.getRegionName(), Bytes.toBytes(1)).get(1, TimeUnit.MINUTES); long maxSeqId = getMaxSeqId(rs, region); RegionLocator locator = UTIL.getConnection().getRegionLocator(NAME); HRegionLocation locA = locator.getRegionLocation(Bytes.toBytes(0), true); HRegionLocation locB = locator.getRegionLocation(Bytes.toBytes(1), true); assertEquals(maxSeqId + 1, locA.getSeqNum()); assertEquals(maxSeqId + 1, locB.getSeqNum()); }
private List<HRegionLocation> splitRegion(final RegionInfo r) throws IOException, InterruptedException { List<HRegionLocation> locations = new ArrayList<>(); // Split this table in two. Admin admin = TEST_UTIL.getAdmin(); Connection connection = TEST_UTIL.getConnection(); admin.splitRegion(r.getEncodedNameAsBytes()); admin.close(); PairOfSameType<RegionInfo> regions = waitOnDaughters(r); if (regions != null) { try (RegionLocator rl = connection.getRegionLocator(r.getTable())) { locations.add(rl.getRegionLocation(regions.getFirst().getEncodedNameAsBytes())); locations.add(rl.getRegionLocation(regions.getSecond().getEncodedNameAsBytes())); } return locations; } return locations; }
@Test public void testRegionObserverMultiRegion() throws IOException { final TableName tableName = TableName.valueOf(name.getMethodName()); try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration()); Admin admin = connection.getAdmin()) { admin.createTable( new HTableDescriptor(tableName) .addFamily(new HColumnDescriptor(foo)) // add the coprocessor for the region .addCoprocessor(CustomRegionObserver.class.getName()) , new byte[][]{foo}); // create with 2 regions try (Table table = connection.getTable(tableName); RegionLocator locator = connection.getRegionLocator(tableName)) { table.get(new Get(bar)); table.get(new Get(foo)); // 2 gets to 2 separate regions assertEquals(2, locator.getAllRegionLocations().size()); assertNotEquals(locator.getRegionLocation(bar).getRegionInfo(), locator.getRegionLocation(foo).getRegionInfo()); } } assertPreGetRequestsCounter(CustomRegionObserver.class); }
@BeforeClass public static void before() throws Exception { HTU.startMiniCluster(NB_SERVERS); final TableName tableName = TableName.valueOf(TestRegionServerNoMaster.class.getSimpleName()); // Create table then get the single region for our new table. table = HTU.createTable(tableName,HConstants.CATALOG_FAMILY); Put p = new Put(row); p.addColumn(HConstants.CATALOG_FAMILY, row, row); table.put(p); try (RegionLocator locator = HTU.getConnection().getRegionLocator(tableName)) { hri = locator.getRegionLocation(row, false).getRegionInfo(); } regionName = hri.getRegionName(); stopMasterAndAssignMeta(HTU); }
@BeforeClass public static void before() throws Exception { // Reduce the hdfs block size and prefetch to trigger the file-link reopen // when the file is moved to archive (e.g. compaction) HTU.getConfiguration().setInt(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 8192); HTU.getConfiguration().setInt(DFSConfigKeys.DFS_CLIENT_READ_PREFETCH_SIZE_KEY, 1); HTU.getConfiguration().setInt(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, 128 * 1024 * 1024); HTU.startMiniCluster(NB_SERVERS); final TableName tableName = TableName.valueOf(TestRegionReplicas.class.getSimpleName()); // Create table then get the single region for our new table. table = HTU.createTable(tableName, f); try (RegionLocator locator = HTU.getConnection().getRegionLocator(tableName)) { hriPrimary = locator.getRegionLocation(row, false).getRegionInfo(); } // mock a secondary region info to open hriSecondary = new HRegionInfo(hriPrimary.getTable(), hriPrimary.getStartKey(), hriPrimary.getEndKey(), hriPrimary.isSplit(), hriPrimary.getRegionId(), 1); // No master TestRegionServerNoMaster.stopMasterAndAssignMeta(HTU); }
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()); } } }
@Test 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")); RegionInfo region = regionLocation.getRegionInfo(); byte[] regionName = region.getRegionName(); Pair<RegionInfo, ServerName> pair = rawAdmin.getRegion(regionName); assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName())); pair = rawAdmin.getRegion(region.getEncodedNameAsBytes()); assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName())); } }