Search in sources :

Example 46 with HRegionLocation

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

the class TestMasterObserver method testTableOperations.

@Test(timeout = 180000)
public void testTableOperations() throws Exception {
    MiniHBaseCluster cluster = UTIL.getHBaseCluster();
    final TableName tableName = TableName.valueOf(name.getMethodName());
    HMaster master = cluster.getMaster();
    MasterCoprocessorHost host = master.getMasterCoprocessorHost();
    CPMasterObserver cp = (CPMasterObserver) host.findCoprocessor(CPMasterObserver.class.getName());
    cp.enableBypass(true);
    cp.resetStates();
    assertFalse("No table created yet", cp.wasCreateTableCalled());
    // create a table
    HTableDescriptor htd = new HTableDescriptor(tableName);
    htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
    try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
        Admin admin = connection.getAdmin()) {
        tableCreationLatch = new CountDownLatch(1);
        admin.createTable(htd, Arrays.copyOfRange(HBaseTestingUtility.KEYS, 1, HBaseTestingUtility.KEYS.length));
        // preCreateTable can't bypass default action.
        assertTrue("Test table should be created", cp.wasCreateTableCalled());
        tableCreationLatch.await();
        assertTrue("Table pre create handler called.", cp.wasPreCreateTableActionCalled());
        assertTrue("Table create handler should be called.", cp.wasCreateTableActionCalled());
        RegionLocator regionLocator = connection.getRegionLocator(htd.getTableName());
        List<HRegionLocation> regions = regionLocator.getAllRegionLocations();
        admin.mergeRegionsAsync(regions.get(0).getRegionInfo().getEncodedNameAsBytes(), regions.get(1).getRegionInfo().getEncodedNameAsBytes(), true);
        assertTrue("Coprocessor should have been called on region merge", cp.wasMergeRegionsCalled());
        tableCreationLatch = new CountDownLatch(1);
        admin.disableTable(tableName);
        assertTrue(admin.isTableDisabled(tableName));
        // preDisableTable can't bypass default action.
        assertTrue("Coprocessor should have been called on table disable", cp.wasDisableTableCalled());
        assertTrue("Disable table handler should be called.", cp.wasDisableTableActionCalled());
        // enable
        assertFalse(cp.wasEnableTableCalled());
        admin.enableTable(tableName);
        assertTrue(admin.isTableEnabled(tableName));
        // preEnableTable can't bypass default action.
        assertTrue("Coprocessor should have been called on table enable", cp.wasEnableTableCalled());
        assertTrue("Enable table handler should be called.", cp.wasEnableTableActionCalled());
        admin.disableTable(tableName);
        assertTrue(admin.isTableDisabled(tableName));
        // modify table
        htd.setMaxFileSize(512 * 1024 * 1024);
        modifyTableSync(admin, tableName, htd);
        // preModifyTable can't bypass default action.
        assertTrue("Test table should have been modified", cp.wasModifyTableCalled());
        // add a column family
        admin.addColumnFamily(tableName, new HColumnDescriptor(TEST_FAMILY2));
        assertTrue("New column family shouldn't have been added to test table", cp.preAddColumnCalledOnly());
        // modify a column family
        HColumnDescriptor hcd1 = new HColumnDescriptor(TEST_FAMILY2);
        hcd1.setMaxVersions(25);
        admin.modifyColumnFamily(tableName, hcd1);
        assertTrue("Second column family should be modified", cp.preModifyColumnCalledOnly());
        // truncate table
        admin.truncateTable(tableName, false);
        // delete table
        admin.disableTable(tableName);
        assertTrue(admin.isTableDisabled(tableName));
        deleteTable(admin, tableName);
        assertFalse("Test table should have been deleted", admin.tableExists(tableName));
        // preDeleteTable can't bypass default action.
        assertTrue("Coprocessor should have been called on table delete", cp.wasDeleteTableCalled());
        assertTrue("Delete table handler should be called.", cp.wasDeleteTableActionCalled());
        // turn off bypass, run the tests again
        cp.enableBypass(false);
        cp.resetStates();
        admin.createTable(htd);
        assertTrue("Test table should be created", cp.wasCreateTableCalled());
        tableCreationLatch.await();
        assertTrue("Table pre create handler called.", cp.wasPreCreateTableActionCalled());
        assertTrue("Table create handler should be called.", cp.wasCreateTableActionCalled());
        // disable
        assertFalse(cp.wasDisableTableCalled());
        assertFalse(cp.wasDisableTableActionCalled());
        admin.disableTable(tableName);
        assertTrue(admin.isTableDisabled(tableName));
        assertTrue("Coprocessor should have been called on table disable", cp.wasDisableTableCalled());
        assertTrue("Disable table handler should be called.", cp.wasDisableTableActionCalled());
        // modify table
        htd.setMaxFileSize(512 * 1024 * 1024);
        modifyTableSync(admin, tableName, htd);
        assertTrue("Test table should have been modified", cp.wasModifyTableCalled());
        // add a column family
        admin.addColumnFamily(tableName, new HColumnDescriptor(TEST_FAMILY2));
        assertTrue("New column family should have been added to test table", cp.wasAddColumnCalled());
        assertTrue("Add column handler should be called.", cp.wasAddColumnFamilyActionCalled());
        // modify a column family
        HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAMILY2);
        hcd.setMaxVersions(25);
        admin.modifyColumnFamily(tableName, hcd);
        assertTrue("Second column family should be modified", cp.wasModifyColumnCalled());
        assertTrue("Modify table handler should be called.", cp.wasModifyColumnFamilyActionCalled());
        // enable
        assertFalse(cp.wasEnableTableCalled());
        assertFalse(cp.wasEnableTableActionCalled());
        admin.enableTable(tableName);
        assertTrue(admin.isTableEnabled(tableName));
        assertTrue("Coprocessor should have been called on table enable", cp.wasEnableTableCalled());
        assertTrue("Enable table handler should be called.", cp.wasEnableTableActionCalled());
        // disable again
        admin.disableTable(tableName);
        assertTrue(admin.isTableDisabled(tableName));
        // delete column
        assertFalse("No column family deleted yet", cp.wasDeleteColumnCalled());
        assertFalse("Delete table column handler should not be called.", cp.wasDeleteColumnFamilyActionCalled());
        admin.deleteColumnFamily(tableName, TEST_FAMILY2);
        HTableDescriptor tableDesc = admin.getTableDescriptor(tableName);
        assertNull("'" + Bytes.toString(TEST_FAMILY2) + "' should have been removed", tableDesc.getFamily(TEST_FAMILY2));
        assertTrue("Coprocessor should have been called on column delete", cp.wasDeleteColumnCalled());
        assertTrue("Delete table column handler should be called.", cp.wasDeleteColumnFamilyActionCalled());
        // delete table
        assertFalse("No table deleted yet", cp.wasDeleteTableCalled());
        assertFalse("Delete table handler should not be called.", cp.wasDeleteTableActionCalled());
        deleteTable(admin, tableName);
        assertFalse("Test table should have been deleted", admin.tableExists(tableName));
        assertTrue("Coprocessor should have been called on table delete", cp.wasDeleteTableCalled());
        assertTrue("Delete table handler should be called.", cp.wasDeleteTableActionCalled());
    }
}
Also used : RegionLocator(org.apache.hadoop.hbase.client.RegionLocator) MasterCoprocessorHost(org.apache.hadoop.hbase.master.MasterCoprocessorHost) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) Connection(org.apache.hadoop.hbase.client.Connection) MiniHBaseCluster(org.apache.hadoop.hbase.MiniHBaseCluster) Admin(org.apache.hadoop.hbase.client.Admin) CountDownLatch(java.util.concurrent.CountDownLatch) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) TableName(org.apache.hadoop.hbase.TableName) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) HMaster(org.apache.hadoop.hbase.master.HMaster) Test(org.junit.Test)

Example 47 with HRegionLocation

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

the class TestMasterObserver method testRegionTransitionOperations.

@Test(timeout = 180000)
public void testRegionTransitionOperations() throws Exception {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    MiniHBaseCluster cluster = UTIL.getHBaseCluster();
    HMaster master = cluster.getMaster();
    MasterCoprocessorHost host = master.getMasterCoprocessorHost();
    CPMasterObserver cp = (CPMasterObserver) host.findCoprocessor(CPMasterObserver.class.getName());
    cp.enableBypass(false);
    cp.resetStates();
    Table table = UTIL.createMultiRegionTable(tableName, TEST_FAMILY);
    try (RegionLocator r = UTIL.getConnection().getRegionLocator(tableName)) {
        UTIL.waitUntilAllRegionsAssigned(tableName);
        List<HRegionLocation> regions = r.getAllRegionLocations();
        HRegionLocation firstGoodPair = null;
        for (HRegionLocation e : regions) {
            if (e.getServerName() != null) {
                firstGoodPair = e;
                break;
            }
        }
        assertNotNull("Found a non-null entry", firstGoodPair);
        LOG.info("Found " + firstGoodPair.toString());
        // Try to force a move
        Collection<ServerName> servers = master.getClusterStatus().getServers();
        String destName = null;
        String serverNameForFirstRegion = firstGoodPair.getServerName().toString();
        LOG.info("serverNameForFirstRegion=" + serverNameForFirstRegion);
        ServerName masterServerName = master.getServerName();
        boolean found = false;
        // Find server that is NOT carrying the first region
        for (ServerName info : servers) {
            LOG.info("ServerName=" + info);
            if (!serverNameForFirstRegion.equals(info.getServerName()) && !masterServerName.equals(info)) {
                destName = info.toString();
                found = true;
                break;
            }
        }
        assertTrue("Found server", found);
        LOG.info("Found " + destName);
        master.getMasterRpcServices().moveRegion(null, RequestConverter.buildMoveRegionRequest(firstGoodPair.getRegionInfo().getEncodedNameAsBytes(), Bytes.toBytes(destName)));
        assertTrue("Coprocessor should have been called on region move", cp.wasMoveCalled());
        // make sure balancer is on
        master.balanceSwitch(true);
        assertTrue("Coprocessor should have been called on balance switch", cp.wasBalanceSwitchCalled());
        // turn balancer off
        master.balanceSwitch(false);
        // wait for assignments to finish, if any
        UTIL.waitUntilNoRegionsInTransition();
        // move half the open regions from RS 0 to RS 1
        HRegionServer rs = cluster.getRegionServer(0);
        byte[] destRS = Bytes.toBytes(cluster.getRegionServer(1).getServerName().toString());
        //Make sure no regions are in transition now
        UTIL.waitUntilNoRegionsInTransition();
        List<HRegionInfo> openRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
        int moveCnt = openRegions.size() / 2;
        for (int i = 0; i < moveCnt; i++) {
            HRegionInfo info = openRegions.get(i);
            if (!info.isMetaTable()) {
                master.getMasterRpcServices().moveRegion(null, RequestConverter.buildMoveRegionRequest(openRegions.get(i).getEncodedNameAsBytes(), destRS));
            }
        }
        //Make sure no regions are in transition now
        UTIL.waitUntilNoRegionsInTransition();
        // now trigger a balance
        master.balanceSwitch(true);
        boolean balanceRun = master.balance();
        assertTrue("Coprocessor should be called on region rebalancing", cp.wasBalanceCalled());
    } finally {
        Admin admin = UTIL.getAdmin();
        admin.disableTable(tableName);
        deleteTable(admin, tableName);
    }
}
Also used : RegionLocator(org.apache.hadoop.hbase.client.RegionLocator) MasterCoprocessorHost(org.apache.hadoop.hbase.master.MasterCoprocessorHost) Table(org.apache.hadoop.hbase.client.Table) MiniHBaseCluster(org.apache.hadoop.hbase.MiniHBaseCluster) Admin(org.apache.hadoop.hbase.client.Admin) HRegionServer(org.apache.hadoop.hbase.regionserver.HRegionServer) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) TableName(org.apache.hadoop.hbase.TableName) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ServerName(org.apache.hadoop.hbase.ServerName) HMaster(org.apache.hadoop.hbase.master.HMaster) Test(org.junit.Test)

Example 48 with HRegionLocation

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

the class TestCoprocessorMetrics method testRegionObserverAfterRegionClosed.

@Test
public void testRegionObserverAfterRegionClosed() 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)).addCoprocessor(CustomRegionObserver.class.getName()), // create with 2 regions
        new byte[][] { foo });
        try (Table table = connection.getTable(tableName)) {
            table.get(new Get(foo));
            // 2 gets
            table.get(new Get(foo));
        }
        assertPreGetRequestsCounter(CustomRegionObserver.class);
        // close one of the regions
        try (RegionLocator locator = connection.getRegionLocator(tableName)) {
            HRegionLocation loc = locator.getRegionLocation(foo);
            admin.closeRegion(loc.getServerName(), loc.getRegionInfo());
            HRegionServer server = UTIL.getMiniHBaseCluster().getRegionServer(loc.getServerName());
            UTIL.waitFor(30000, () -> server.getOnlineRegion(loc.getRegionInfo().getRegionName()) == null);
            assertNull(server.getOnlineRegion(loc.getRegionInfo().getRegionName()));
        }
        // with only 1 region remaining, we should still be able to find the Counter
        assertPreGetRequestsCounter(CustomRegionObserver.class);
        // close the table
        admin.disableTable(tableName);
        MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForRegionCoprocessor(CustomRegionObserver.class.getName());
        // ensure that MetricRegistry is deleted
        Optional<MetricRegistry> registry = MetricRegistries.global().get(info);
        assertFalse(registry.isPresent());
    }
}
Also used : RegionLocator(org.apache.hadoop.hbase.client.RegionLocator) Table(org.apache.hadoop.hbase.client.Table) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) MetricRegistry(org.apache.hadoop.hbase.metrics.MetricRegistry) Connection(org.apache.hadoop.hbase.client.Connection) Admin(org.apache.hadoop.hbase.client.Admin) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HRegionServer(org.apache.hadoop.hbase.regionserver.HRegionServer) TableName(org.apache.hadoop.hbase.TableName) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) Get(org.apache.hadoop.hbase.client.Get) MetricRegistryInfo(org.apache.hadoop.hbase.metrics.MetricRegistryInfo) Test(org.junit.Test)

Example 49 with HRegionLocation

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

the class TestZKAsyncRegistry method test.

@Test
public void test() throws InterruptedException, ExecutionException, IOException {
    assertEquals(TEST_UTIL.getHBaseCluster().getClusterStatus().getClusterId(), REGISTRY.getClusterId().get());
    assertEquals(TEST_UTIL.getHBaseCluster().getClusterStatus().getServersSize(), REGISTRY.getCurrentNrHRS().get().intValue());
    assertEquals(TEST_UTIL.getHBaseCluster().getMaster().getServerName(), REGISTRY.getMasterAddress().get());
    assertEquals(-1, REGISTRY.getMasterInfoPort().get().intValue());
    waitUntilAllReplicasHavingRegionLocation(TableName.META_TABLE_NAME);
    RegionLocations locs = REGISTRY.getMetaRegionLocation().get();
    assertEquals(3, locs.getRegionLocations().length);
    IntStream.range(0, 3).forEach(i -> {
        HRegionLocation loc = locs.getRegionLocation(i);
        assertNotNull("Replica " + i + " doesn't have location", loc);
        assertTrue(loc.getRegionInfo().getTable().equals(TableName.META_TABLE_NAME));
        assertEquals(i, loc.getRegionInfo().getReplicaId());
    });
}
Also used : RegionLocations(org.apache.hadoop.hbase.RegionLocations) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) Test(org.junit.Test)

Example 50 with HRegionLocation

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

the class TestTableFavoredNodes method checkIfFavoredNodeInformationIsCorrect.

/*
   * This checks the following:
   *
   * 1. Do all regions of the table have favored nodes updated in master?
   * 2. Is the number of favored nodes correct for a region? Is the start code -1?
   * 3. Is the FN information consistent between Master and the respective RegionServer?
   */
private void checkIfFavoredNodeInformationIsCorrect(TableName tableName) throws Exception {
    /*
     * Since we need HRegionServer to check for consistency of FN between Master and RS,
     * lets construct a map for each serverName lookup. Makes it easy later.
     */
    Map<ServerName, HRegionServer> snRSMap = Maps.newHashMap();
    for (JVMClusterUtil.RegionServerThread rst : TEST_UTIL.getMiniHBaseCluster().getLiveRegionServerThreads()) {
        snRSMap.put(rst.getRegionServer().getServerName(), rst.getRegionServer());
    }
    // Also include master, since it can also host user regions.
    for (JVMClusterUtil.MasterThread rst : TEST_UTIL.getMiniHBaseCluster().getLiveMasterThreads()) {
        snRSMap.put(rst.getMaster().getServerName(), rst.getMaster());
    }
    int dnPort = fnm.getDataNodePort();
    RegionLocator regionLocator = admin.getConnection().getRegionLocator(tableName);
    for (HRegionLocation regionLocation : regionLocator.getAllRegionLocations()) {
        HRegionInfo regionInfo = regionLocation.getRegionInfo();
        List<ServerName> fnList = fnm.getFavoredNodes(regionInfo);
        // 1. Does each region have favored node?
        assertNotNull("Favored nodes should not be null for region:" + regionInfo, fnList);
        // 2. Do we have the right number of favored nodes? Is start code -1?
        assertEquals("Incorrect favored nodes for region:" + regionInfo + " fnlist: " + fnList, FavoredNodeAssignmentHelper.FAVORED_NODES_NUM, fnList.size());
        for (ServerName sn : fnList) {
            assertEquals("FN should not have startCode, fnlist:" + fnList, -1, sn.getStartcode());
        }
        // 3. Check if the regionServers have all the FN updated and in sync with Master
        HRegionServer regionServer = snRSMap.get(regionLocation.getServerName());
        assertNotNull("RS should not be null for regionLocation: " + regionLocation, regionServer);
        InetSocketAddress[] rsFavNodes = regionServer.getFavoredNodesForRegion(regionInfo.getEncodedName());
        assertNotNull("RS " + regionLocation.getServerName() + " does not have FN for region: " + regionInfo, rsFavNodes);
        assertEquals("Incorrect FN for region:" + regionInfo.getEncodedName() + " on server:" + regionLocation.getServerName(), FavoredNodeAssignmentHelper.FAVORED_NODES_NUM, rsFavNodes.length);
        // 4. Does DN port match all FN node list?
        for (ServerName sn : fnm.getFavoredNodesWithDNPort(regionInfo)) {
            assertEquals("FN should not have startCode, fnlist:" + fnList, -1, sn.getStartcode());
            assertEquals("FN port should belong to DN port, fnlist:" + fnList, dnPort, sn.getPort());
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HRegionServer(org.apache.hadoop.hbase.regionserver.HRegionServer) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) JVMClusterUtil(org.apache.hadoop.hbase.util.JVMClusterUtil) ServerName(org.apache.hadoop.hbase.ServerName)

Aggregations

HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)132 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)52 Test (org.junit.Test)50 ServerName (org.apache.hadoop.hbase.ServerName)44 TableName (org.apache.hadoop.hbase.TableName)39 IOException (java.io.IOException)31 RegionLocator (org.apache.hadoop.hbase.client.RegionLocator)30 RegionLocations (org.apache.hadoop.hbase.RegionLocations)29 ArrayList (java.util.ArrayList)25 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)18 Table (org.apache.hadoop.hbase.client.Table)18 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)16 List (java.util.List)12 HashMap (java.util.HashMap)11 Map (java.util.Map)11 Result (org.apache.hadoop.hbase.client.Result)10 MultiRowMutationEndpoint (org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint)10 Connection (org.apache.hadoop.hbase.client.Connection)9 HRegionServer (org.apache.hadoop.hbase.regionserver.HRegionServer)9 Admin (org.apache.hadoop.hbase.client.Admin)8