private void warmUpConnectionCache(Connection connection, TableName tn) throws IOException { try (RegionLocator locator = connection.getRegionLocator(tn)) { LOG.info( "Warmed up region location cache for " + tn + " got " + locator.getAllRegionLocations().size()); } }
/** * Count regions in <code>hbase:meta</code> for passed table. * @param connection Connection object * @param tableName table name to count regions for * @return Count or regions in table <code>tableName</code> */ public static int getRegionCount(final Connection connection, final TableName tableName) throws IOException { try (RegionLocator locator = connection.getRegionLocator(tableName)) { List<HRegionLocation> locations = locator.getAllRegionLocations(); return locations == null ? 0 : locations.size(); } }
private Set<ServerName> getRegionServersOfTable(RegionLocator regionLocator) throws IOException { Set<ServerName> tableServers = Sets.newHashSet(); for (HRegionLocation regionLocation : regionLocator.getAllRegionLocations()) { tableServers.add(regionLocation.getServerName()); } return tableServers; }
@VisibleForTesting void initializeWorkQueues() throws IOException { if (storesToCompact.isEmpty()) { connection.getTable(tableName).getDescriptor().getColumnFamilyNames() .forEach(a -> storesToCompact.add(Bytes.toString(a))); LOG.info("No family specified, will execute for all families"); } LOG.info( "Initializing compaction queues for table: " + tableName + " with cf: " + storesToCompact); List<HRegionLocation> regionLocations = connection.getRegionLocator(tableName).getAllRegionLocations(); for (HRegionLocation location : regionLocations) { Optional<MajorCompactionRequest> request = MajorCompactionRequest .newRequest(connection.getConfiguration(), location.getRegion(), storesToCompact, timestamp); request.ifPresent(majorCompactionRequest -> clusterCompactionQueues .addToCompactionQueue(location.getServerName(), majorCompactionRequest)); } }
@Override public List<THRegionLocation> getAllRegionLocations(ByteBuffer table) throws TIOError, TException { RegionLocator locator = null; try { locator = getLocator(table); return ThriftUtilities.regionLocationsFromHBase(locator.getAllRegionLocations()); } catch (IOException e) { throw getTIOError(e); } finally { if (locator != null) { try { locator.close(); } catch (IOException e) { LOG.warn("Couldn't close the locator.", e); } } } }
/** * Makes some table with given region names. * */ private RegionLocator mockRegionLocator(String... regionNames) throws IOException { RegionLocator mockedTable = Mockito.mock(RegionLocator.class); when(mockedTable.getName()).thenReturn(TableName.valueOf("sizeTestTable")); List<HRegionLocation> regionLocations = new ArrayList<>(regionNames.length); when(mockedTable.getAllRegionLocations()).thenReturn(regionLocations); for (String regionName : regionNames) { HRegionInfo info = Mockito.mock(HRegionInfo.class); when(info.getRegionName()).thenReturn(regionName.getBytes()); regionLocations.add(new HRegionLocation(info, sn)); } return mockedTable; }
private List<HRegionLocation> waitOnSplit(final Table t) throws IOException { try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(t.getName())) { List<HRegionLocation> regions = locator.getAllRegionLocations(); int originalCount = regions.size(); for (int i = 0; i < TEST_UTIL.getConfiguration().getInt("hbase.test.retries", 30); i++) { Thread.currentThread(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } regions = locator.getAllRegionLocations(); if (regions.size() > originalCount) break; } return regions; } }
private static void waitOnSplit(Connection c, final Table t, int originalCount) throws Exception { for (int i = 0; i < 200; i++) { Threads.sleepWithoutInterrupt(500); try (RegionLocator locator = c.getRegionLocator(t.getName())) { if (locator.getAllRegionLocations().size() > originalCount) { return; } } } throw new Exception("Split did not increase the number of regions"); }
@Override protected List<HRegionLocation> getAllRegionLocations(TableName tableName) throws IOException { try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) { return locator.getAllRegionLocations(); } }
private void addNewRegions() { try { List<HRegionLocation> locations = connection.getRegionLocator(tableName).getAllRegionLocations(); for (HRegionLocation location : locations) { if (location.getRegion().getRegionId() > timestamp) { Optional<MajorCompactionRequest> compactionRequest = MajorCompactionRequest .newRequest(connection.getConfiguration(), location.getRegion(), storesToCompact, timestamp); compactionRequest.ifPresent(request -> clusterCompactionQueues .addToCompactionQueue(location.getServerName(), request)); } } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void perform() throws Exception { LOG.info("Performing action: Restart random RS holding table " + this.locator.getName()); List<HRegionLocation> locations = locator.getAllRegionLocations(); restartRs(locations.get(RandomUtils.nextInt(0, locations.size())).getServerName(), sleepTime); } }
/** verify region boundaries obtained from HTable.getStartEndKeys() */ void verifyRegionsUsingHTable() throws IOException { Table table = null; try { //HTable.getStartEndKeys() table = connection.getTable(tableName); try(RegionLocator rl = connection.getRegionLocator(tableName)) { Pair<byte[][], byte[][]> keys = rl.getStartEndKeys(); verifyStartEndKeys(keys); Set<RegionInfo> regions = new TreeSet<>(RegionInfo.COMPARATOR); for (HRegionLocation loc : rl.getAllRegionLocations()) { regions.add(loc.getRegionInfo()); } verifyTableRegions(regions); } } finally { IOUtils.closeQuietly(table); } }
@Test public void testOpeningReadOnlyRegionBasic() throws Exception { String snapshotDir = client.createSnapshot(baseDir, SNAPSHOT_NAME); RegionInfo firstRegion = TEST_UTIL.getConnection().getRegionLocator( table.getName()).getAllRegionLocations().stream().findFirst().get().getRegion(); Path tableDir = FSUtils.getTableDir(new Path(snapshotDir), TABLE_NAME); HRegion snapshottedRegion = openSnapshotRegion(firstRegion, tableDir); Assert.assertNotNull(snapshottedRegion); snapshottedRegion.close(); }
@Test public void testAssign() throws Exception { List<HRegionLocation> regions; try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) { regions = locator.getAllRegionLocations(); } HRegionLocation location = regions.get(0); final HRegionInfo hri = location.getRegionInfo(); AccessTestAction action = new AccessTestAction() { @Override public Object run() throws Exception { ACCESS_CONTROLLER.preAssign(ObserverContextImpl.createAndPrepare(CP_ENV), hri); return null; } }; verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); }
@Test public void testUnassign() throws Exception { List<HRegionLocation> regions; try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) { regions = locator.getAllRegionLocations(); } HRegionLocation location = regions.get(0); final HRegionInfo hri = location.getRegionInfo(); AccessTestAction action = new AccessTestAction() { @Override public Object run() throws Exception { ACCESS_CONTROLLER.preUnassign(ObserverContextImpl.createAndPrepare(CP_ENV), hri, false); return null; } }; verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); }
@Test public void testRegionOffline() throws Exception { List<HRegionLocation> regions; try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) { regions = locator.getAllRegionLocations(); } HRegionLocation location = regions.get(0); final HRegionInfo hri = location.getRegionInfo(); AccessTestAction action = new AccessTestAction() { @Override public Object run() throws Exception { ACCESS_CONTROLLER.preRegionOffline(ObserverContextImpl.createAndPrepare(CP_ENV), hri); return null; } }; verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN); verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE); }
private HRegionFileSystem getHRegionFS(HTable table, Configuration conf) throws IOException { FileSystem fs = TEST_UTIL.getDFSCluster().getFileSystem(); Path tableDir = FSUtils.getTableDir(TEST_UTIL.getDefaultRootDirPath(), table.getName()); List<Path> regionDirs = FSUtils.getRegionDirs(fs, tableDir); assertEquals(1, regionDirs.size()); List<Path> familyDirs = FSUtils.getFamilyDirs(fs, regionDirs.get(0)); assertEquals(2, familyDirs.size()); RegionInfo hri = table.getRegionLocator().getAllRegionLocations().get(0).getRegionInfo(); HRegionFileSystem regionFs = new HRegionFileSystem(conf, new HFileSystem(fs), tableDir, hri); return regionFs; }
private void assertAllOnLine(final Table t) throws IOException { List<HRegionLocation> regions; try(RegionLocator rl = TEST_UTIL.getConnection().getRegionLocator(t.getName())) { regions = rl.getAllRegionLocations(); } for (HRegionLocation e: regions) { byte [] startkey = e.getRegionInfo().getStartKey(); Scan s = new Scan(startkey); ResultScanner scanner = t.getScanner(s); Result r = scanner.next(); org.junit.Assert.assertTrue(r != null && r.size() > 0); scanner.close(); } } }
@BeforeClass public static void setUp() throws Exception { conf = hbaseTestingUtility.getConfiguration(); hbaseTestingUtility.startMiniCluster(); t1 = hbaseTestingUtility.createTable(TABLE1, COLUMN_FAMILY1); try (RegionLocator locator = hbaseTestingUtility.getConnection().getRegionLocator(TABLE1)) { HRegionInfo firstHRI = locator.getAllRegionLocations().get(0).getRegionInfo(); r1name = firstHRI.getRegionName(); rs1 = hbaseTestingUtility.getHBaseCluster().getRegionServer( hbaseTestingUtility.getHBaseCluster().getServerWith(r1name)); r1 = rs1.getRegion(r1name); } }
public void assertRegionMetrics(String metric, long expectedValue) throws Exception { try (RegionLocator locator = connection.getRegionLocator(tableName)) { for ( HRegionLocation location: locator.getAllRegionLocations()) { HRegionInfo hri = location.getRegionInfo(); MetricsRegionAggregateSource agg = rs.getRegion(hri.getRegionName()).getMetrics().getSource().getAggregateSource(); String prefix = "namespace_" + NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR + "_table_" + tableName.getNameAsString() + "_region_" + hri.getEncodedName()+ "_metric_"; metricsHelper.assertCounter(prefix + metric, expectedValue, agg); } } }