Search in sources :

Example 6 with HRegionInfo

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

the class TestRSGroupBasedLoadBalancer method randomRegions.

/**
   * Generate a list of regions evenly distributed between the tables.
   *
   * @param numRegions The number of regions to be generated.
   * @return List of HRegionInfo.
   */
private List<HRegionInfo> randomRegions(int numRegions) {
    List<HRegionInfo> regions = new ArrayList<>(numRegions);
    byte[] start = new byte[16];
    byte[] end = new byte[16];
    rand.nextBytes(start);
    rand.nextBytes(end);
    int regionIdx = rand.nextInt(tables.length);
    for (int i = 0; i < numRegions; i++) {
        Bytes.putInt(start, 0, numRegions << 1);
        Bytes.putInt(end, 0, (numRegions << 1) + 1);
        int tableIndex = (i + regionIdx) % tables.length;
        HRegionInfo hri = new HRegionInfo(tables[tableIndex], start, end, false, regionId++);
        regions.add(hri);
    }
    return regions;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) ArrayList(java.util.ArrayList)

Example 7 with HRegionInfo

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

the class TestRSGroupBasedLoadBalancer method assertRetainedAssignment.

/**
   * Asserts a valid retained assignment plan.
   * <p>
   * Must meet the following conditions:
   * <ul>
   * <li>Every input region has an assignment, and to an online server
   * <li>If a region had an existing assignment to a server with the same
   * address a a currently online server, it will be assigned to it
   * </ul>
   *
   * @param existing
   * @param assignment
   * @throws java.io.IOException
   * @throws java.io.FileNotFoundException
   */
private void assertRetainedAssignment(Map<HRegionInfo, ServerName> existing, List<ServerName> servers, Map<ServerName, List<HRegionInfo>> assignment) throws FileNotFoundException, IOException {
    // Verify condition 1, every region assigned, and to online server
    Set<ServerName> onlineServerSet = new TreeSet<>(servers);
    Set<HRegionInfo> assignedRegions = new TreeSet<>();
    for (Map.Entry<ServerName, List<HRegionInfo>> a : assignment.entrySet()) {
        assertTrue("Region assigned to server that was not listed as online", onlineServerSet.contains(a.getKey()));
        for (HRegionInfo r : a.getValue()) assignedRegions.add(r);
    }
    assertEquals(existing.size(), assignedRegions.size());
    // Verify condition 2, every region must be assigned to correct server.
    Set<String> onlineHostNames = new TreeSet<>();
    for (ServerName s : servers) {
        onlineHostNames.add(s.getHostname());
    }
    for (Map.Entry<ServerName, List<HRegionInfo>> a : assignment.entrySet()) {
        ServerName currentServer = a.getKey();
        for (HRegionInfo r : a.getValue()) {
            ServerName oldAssignedServer = existing.get(r);
            TableName tableName = r.getTable();
            String groupName = getMockedGroupInfoManager().getRSGroupOfTable(tableName);
            assertTrue(StringUtils.isNotEmpty(groupName));
            RSGroupInfo gInfo = getMockedGroupInfoManager().getRSGroup(groupName);
            assertTrue("Region is not correctly assigned to group servers.", gInfo.containsServer(currentServer.getAddress()));
            if (oldAssignedServer != null && onlineHostNames.contains(oldAssignedServer.getHostname())) {
                // different group.
                if (!oldAssignedServer.getAddress().equals(currentServer.getAddress())) {
                    assertFalse(gInfo.containsServer(oldAssignedServer.getAddress()));
                }
            }
        }
    }
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) TableName(org.apache.hadoop.hbase.TableName) RSGroupInfo(org.apache.hadoop.hbase.rsgroup.RSGroupInfo) TreeSet(java.util.TreeSet) ServerName(org.apache.hadoop.hbase.ServerName) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 8 with HRegionInfo

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

the class TestRSGroupsBase method testKillRS.

@Test
public void testKillRS() throws Exception {
    RSGroupInfo appInfo = addGroup("appInfo", 1);
    final TableName tableName = TableName.valueOf(tablePrefix + "_ns", name.getMethodName());
    admin.createNamespace(NamespaceDescriptor.create(tableName.getNamespaceAsString()).addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, appInfo.getName()).build());
    final HTableDescriptor desc = new HTableDescriptor(tableName);
    desc.addFamily(new HColumnDescriptor("f"));
    admin.createTable(desc);
    //wait for created table to be assigned
    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {

        @Override
        public boolean evaluate() throws Exception {
            return getTableRegionMap().get(desc.getTableName()) != null;
        }
    });
    ServerName targetServer = ServerName.parseServerName(appInfo.getServers().iterator().next().toString());
    AdminProtos.AdminService.BlockingInterface targetRS = ((ClusterConnection) admin.getConnection()).getAdmin(targetServer);
    HRegionInfo targetRegion = ProtobufUtil.getOnlineRegions(targetRS).get(0);
    Assert.assertEquals(1, ProtobufUtil.getOnlineRegions(targetRS).size());
    try {
        //stopping may cause an exception
        //due to the connection loss
        targetRS.stopServer(null, AdminProtos.StopServerRequest.newBuilder().setReason("Die").build());
    } catch (Exception e) {
    }
    assertFalse(cluster.getClusterStatus().getServers().contains(targetServer));
    //wait for created table to be assigned
    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {

        @Override
        public boolean evaluate() throws Exception {
            return cluster.getClusterStatus().getRegionsInTransition().isEmpty();
        }
    });
    Set<Address> newServers = Sets.newHashSet();
    newServers.add(rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP).getServers().iterator().next());
    rsGroupAdmin.moveServers(newServers, appInfo.getName());
    //Make sure all the table's regions get reassigned
    //disabling the table guarantees no conflicting assign/unassign (ie SSH) happens
    admin.disableTable(tableName);
    admin.enableTable(tableName);
    //wait for region to be assigned
    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {

        @Override
        public boolean evaluate() throws Exception {
            return cluster.getClusterStatus().getRegionsInTransition().isEmpty();
        }
    });
    targetServer = ServerName.parseServerName(newServers.iterator().next().toString());
    targetRS = ((ClusterConnection) admin.getConnection()).getAdmin(targetServer);
    Assert.assertEquals(1, ProtobufUtil.getOnlineRegions(targetRS).size());
    Assert.assertEquals(tableName, ProtobufUtil.getOnlineRegions(targetRS).get(0).getTable());
}
Also used : Address(org.apache.hadoop.hbase.net.Address) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) IOException(java.io.IOException) ConstraintException(org.apache.hadoop.hbase.constraint.ConstraintException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) TableName(org.apache.hadoop.hbase.TableName) ClusterConnection(org.apache.hadoop.hbase.client.ClusterConnection) ServerName(org.apache.hadoop.hbase.ServerName) Waiter(org.apache.hadoop.hbase.Waiter) Test(org.junit.Test)

Example 9 with HRegionInfo

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

the class RSGroupAdminServer method getRSGroupAssignmentsByTable.

private Map<TableName, Map<ServerName, List<HRegionInfo>>> getRSGroupAssignmentsByTable(String groupName) throws IOException {
    Map<TableName, Map<ServerName, List<HRegionInfo>>> result = Maps.newHashMap();
    RSGroupInfo rsGroupInfo = getRSGroupInfo(groupName);
    Map<TableName, Map<ServerName, List<HRegionInfo>>> assignments = Maps.newHashMap();
    for (Map.Entry<HRegionInfo, ServerName> entry : master.getAssignmentManager().getRegionStates().getRegionAssignments().entrySet()) {
        TableName currTable = entry.getKey().getTable();
        ServerName currServer = entry.getValue();
        HRegionInfo currRegion = entry.getKey();
        if (rsGroupInfo.getTables().contains(currTable)) {
            assignments.putIfAbsent(currTable, new HashMap<>());
            assignments.get(currTable).putIfAbsent(currServer, new ArrayList<>());
            assignments.get(currTable).get(currServer).add(currRegion);
        }
    }
    Map<ServerName, List<HRegionInfo>> serverMap = Maps.newHashMap();
    for (ServerName serverName : master.getServerManager().getOnlineServers().keySet()) {
        if (rsGroupInfo.getServers().contains(serverName.getAddress())) {
            serverMap.put(serverName, Collections.emptyList());
        }
    }
    // add all tables that are members of the group
    for (TableName tableName : rsGroupInfo.getTables()) {
        if (assignments.containsKey(tableName)) {
            result.put(tableName, new HashMap<>());
            result.get(tableName).putAll(serverMap);
            result.get(tableName).putAll(assignments.get(tableName));
            LOG.debug("Adding assignments for " + tableName + ": " + assignments.get(tableName));
        }
    }
    return result;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) TableName(org.apache.hadoop.hbase.TableName) ServerName(org.apache.hadoop.hbase.ServerName) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with HRegionInfo

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

the class RSGroupAdminServer method unassignRegionFromServers.

/**
   * @param servers the servers that will move to new group
   * @param targetGroupName the target group name
   * @param tables The regions of tables assigned to these servers will not unassign
   * @throws IOException
   */
private void unassignRegionFromServers(Set<Address> servers, String targetGroupName, Set<TableName> tables) throws IOException {
    boolean foundRegionsToUnassign;
    RSGroupInfo targetGrp = getRSGroupInfo(targetGroupName);
    Set<Address> allSevers = new HashSet<>(servers);
    do {
        foundRegionsToUnassign = false;
        for (Iterator<Address> iter = allSevers.iterator(); iter.hasNext(); ) {
            Address rs = iter.next();
            // Get regions that are associated with this server and filter regions by tables.
            List<HRegionInfo> regions = new ArrayList<>();
            for (HRegionInfo region : getRegions(rs)) {
                if (!tables.contains(region.getTable())) {
                    regions.add(region);
                }
            }
            LOG.info("Unassigning " + regions.size() + " region(s) from " + rs + " for server move to " + targetGroupName);
            if (!regions.isEmpty()) {
                for (HRegionInfo region : regions) {
                    // Regions might get assigned from tables of target group so we need to filter
                    if (!targetGrp.containsTable(region.getTable())) {
                        this.master.getAssignmentManager().unassign(region);
                        if (master.getAssignmentManager().getRegionStates().getRegionState(region).isFailedOpen()) {
                            continue;
                        }
                        foundRegionsToUnassign = true;
                    }
                }
            }
            if (!foundRegionsToUnassign) {
                iter.remove();
            }
        }
        try {
            rsGroupInfoManager.wait(1000);
        } catch (InterruptedException e) {
            LOG.warn("Sleep interrupted", e);
            Thread.currentThread().interrupt();
        }
    } while (foundRegionsToUnassign);
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) Address(org.apache.hadoop.hbase.net.Address) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)408 ServerName (org.apache.hadoop.hbase.ServerName)153 Test (org.junit.Test)141 TableName (org.apache.hadoop.hbase.TableName)118 ArrayList (java.util.ArrayList)86 IOException (java.io.IOException)83 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)75 Path (org.apache.hadoop.fs.Path)63 List (java.util.List)59 HashMap (java.util.HashMap)57 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)49 Table (org.apache.hadoop.hbase.client.Table)47 Map (java.util.Map)43 HRegionServer (org.apache.hadoop.hbase.regionserver.HRegionServer)41 FileSystem (org.apache.hadoop.fs.FileSystem)40 Configuration (org.apache.hadoop.conf.Configuration)38 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)35 TreeMap (java.util.TreeMap)26 HashSet (java.util.HashSet)23 HBaseIOException (org.apache.hadoop.hbase.HBaseIOException)22