Search in sources :

Example 31 with RegionInfo

use of org.apache.hadoop.hbase.client.RegionInfo in project hbase by apache.

the class FavoredNodeLoadBalancer method balanceTable.

@Override
protected List<RegionPlan> balanceTable(TableName tableName, Map<ServerName, List<RegionInfo>> loadOfOneTable) {
    // TODO. Look at is whether Stochastic loadbalancer can be integrated with this
    List<RegionPlan> plans = new ArrayList<>();
    Map<ServerName, ServerName> serverNameWithoutCodeToServerName = new HashMap<>();
    for (ServerName sn : provider.getOnlineServersList()) {
        ServerName s = ServerName.valueOf(sn.getHostname(), sn.getPort(), ServerName.NON_STARTCODE);
        // FindBugs complains about useless store! serverNameToServerNameWithoutCode.put(sn, s);
        serverNameWithoutCodeToServerName.put(s, sn);
    }
    for (Map.Entry<ServerName, List<RegionInfo>> entry : loadOfOneTable.entrySet()) {
        ServerName currentServer = entry.getKey();
        // get a server without the startcode for the currentServer
        ServerName currentServerWithoutStartCode = ServerName.valueOf(currentServer.getHostname(), currentServer.getPort(), ServerName.NON_STARTCODE);
        List<RegionInfo> list = entry.getValue();
        for (RegionInfo region : list) {
            if (!FavoredNodesManager.isFavoredNodeApplicable(region)) {
                continue;
            }
            List<ServerName> favoredNodes = fnm.getFavoredNodes(region);
            if (favoredNodes == null || favoredNodes.get(0).equals(currentServerWithoutStartCode)) {
                // either favorednodes does not exist or we are already on the primary node
                continue;
            }
            // check whether the primary is available
            ServerName destination = serverNameWithoutCodeToServerName.get(favoredNodes.get(0));
            if (destination == null) {
                // check whether the region is on secondary/tertiary
                if (currentServerWithoutStartCode.equals(favoredNodes.get(1)) || currentServerWithoutStartCode.equals(favoredNodes.get(2))) {
                    continue;
                }
                // the region is currently on none of the favored nodes
                // get it on one of them if possible
                ServerMetrics l1 = provider.getLoad(serverNameWithoutCodeToServerName.get(favoredNodes.get(1)));
                ServerMetrics l2 = provider.getLoad(serverNameWithoutCodeToServerName.get(favoredNodes.get(2)));
                if (l1 != null && l2 != null) {
                    if (l1.getRegionMetrics().size() > l2.getRegionMetrics().size()) {
                        destination = serverNameWithoutCodeToServerName.get(favoredNodes.get(2));
                    } else {
                        destination = serverNameWithoutCodeToServerName.get(favoredNodes.get(1));
                    }
                } else if (l1 != null) {
                    destination = serverNameWithoutCodeToServerName.get(favoredNodes.get(1));
                } else if (l2 != null) {
                    destination = serverNameWithoutCodeToServerName.get(favoredNodes.get(2));
                }
            }
            if (destination != null) {
                RegionPlan plan = new RegionPlan(region, currentServer, destination);
                plans.add(plan);
            }
        }
    }
    return plans;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) RegionPlan(org.apache.hadoop.hbase.master.RegionPlan) ServerName(org.apache.hadoop.hbase.ServerName) ArrayList(java.util.ArrayList) List(java.util.List) ServerMetrics(org.apache.hadoop.hbase.ServerMetrics) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with RegionInfo

use of org.apache.hadoop.hbase.client.RegionInfo in project hbase by apache.

the class FavoredNodeLoadBalancer method segregateRegionsAndAssignRegionsWithFavoredNodes.

private Pair<Map<ServerName, List<RegionInfo>>, List<RegionInfo>> segregateRegionsAndAssignRegionsWithFavoredNodes(List<RegionInfo> regions, List<ServerName> availableServers) {
    Map<ServerName, List<RegionInfo>> assignmentMapForFavoredNodes = new HashMap<>(regions.size() / 2);
    List<RegionInfo> regionsWithNoFavoredNodes = new ArrayList<>(regions.size() / 2);
    for (RegionInfo region : regions) {
        List<ServerName> favoredNodes = fnm.getFavoredNodes(region);
        ServerName primaryHost = null;
        ServerName secondaryHost = null;
        ServerName tertiaryHost = null;
        if (favoredNodes != null) {
            for (ServerName s : favoredNodes) {
                ServerName serverWithLegitStartCode = availableServersContains(availableServers, s);
                if (serverWithLegitStartCode != null) {
                    FavoredNodesPlan.Position position = FavoredNodesPlan.getFavoredServerPosition(favoredNodes, s);
                    if (Position.PRIMARY.equals(position)) {
                        primaryHost = serverWithLegitStartCode;
                    } else if (Position.SECONDARY.equals(position)) {
                        secondaryHost = serverWithLegitStartCode;
                    } else if (Position.TERTIARY.equals(position)) {
                        tertiaryHost = serverWithLegitStartCode;
                    }
                }
            }
            assignRegionToAvailableFavoredNode(assignmentMapForFavoredNodes, region, primaryHost, secondaryHost, tertiaryHost);
        }
        if (primaryHost == null && secondaryHost == null && tertiaryHost == null) {
            // all favored nodes unavailable
            regionsWithNoFavoredNodes.add(region);
        }
    }
    return new Pair<>(assignmentMapForFavoredNodes, regionsWithNoFavoredNodes);
}
Also used : HashMap(java.util.HashMap) ServerName(org.apache.hadoop.hbase.ServerName) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) Position(org.apache.hadoop.hbase.favored.FavoredNodesPlan.Position) Pair(org.apache.hadoop.hbase.util.Pair)

Example 33 with RegionInfo

use of org.apache.hadoop.hbase.client.RegionInfo in project hbase by apache.

the class FavoredStochasticBalancer method generateFavoredNodesForDaughter.

/**
 * Generate Favored Nodes for daughters during region split.
 * <p/>
 * If the parent does not have FN, regenerates them for the daughters.
 * <p/>
 * If the parent has FN, inherit two FN from parent for each daughter and generate the remaining.
 * The primary FN for both the daughters should be the same as parent. Inherit the secondary FN
 * from the parent but keep it different for each daughter. Choose the remaining FN randomly. This
 * would give us better distribution over a period of time after enough splits.
 */
@Override
public void generateFavoredNodesForDaughter(List<ServerName> servers, RegionInfo parent, RegionInfo regionA, RegionInfo regionB) throws IOException {
    Map<RegionInfo, List<ServerName>> result = new HashMap<>();
    FavoredNodeAssignmentHelper helper = new FavoredNodeAssignmentHelper(servers, rackManager);
    helper.initialize();
    List<ServerName> parentFavoredNodes = fnm.getFavoredNodes(parent);
    if (parentFavoredNodes == null) {
        LOG.debug("Unable to find favored nodes for parent, " + parent + " generating new favored nodes for daughter");
        result.put(regionA, helper.generateFavoredNodes(regionA));
        result.put(regionB, helper.generateFavoredNodes(regionB));
    } else {
        // Lets get the primary and secondary from parent for regionA
        Set<ServerName> regionAFN = getInheritedFNForDaughter(helper, parentFavoredNodes, PRIMARY, SECONDARY);
        result.put(regionA, Lists.newArrayList(regionAFN));
        // Lets get the primary and tertiary from parent for regionB
        Set<ServerName> regionBFN = getInheritedFNForDaughter(helper, parentFavoredNodes, PRIMARY, TERTIARY);
        result.put(regionB, Lists.newArrayList(regionBFN));
    }
    fnm.updateFavoredNodes(result);
}
Also used : HashMap(java.util.HashMap) FavoredNodeAssignmentHelper(org.apache.hadoop.hbase.favored.FavoredNodeAssignmentHelper) ServerName(org.apache.hadoop.hbase.ServerName) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 34 with RegionInfo

use of org.apache.hadoop.hbase.client.RegionInfo in project hbase by apache.

the class FavoredStochasticBalancer method balanceTable.

/**
 * For all regions correctly assigned to favored nodes, we just use the stochastic balancer
 * implementation. For the misplaced regions, we assign a bogus server to it and AM takes care.
 */
@Override
protected List<RegionPlan> balanceTable(TableName tableName, Map<ServerName, List<RegionInfo>> loadOfOneTable) {
    List<RegionPlan> regionPlans = Lists.newArrayList();
    Map<ServerName, List<RegionInfo>> correctAssignments = new HashMap<>();
    int misplacedRegions = 0;
    for (Map.Entry<ServerName, List<RegionInfo>> entry : loadOfOneTable.entrySet()) {
        ServerName current = entry.getKey();
        List<RegionInfo> regions = Lists.newArrayList();
        correctAssignments.put(current, regions);
        for (RegionInfo hri : entry.getValue()) {
            List<ServerName> favoredNodes = fnm.getFavoredNodes(hri);
            if (FavoredNodesPlan.getFavoredServerPosition(favoredNodes, current) != null || !FavoredNodesManager.isFavoredNodeApplicable(hri)) {
                regions.add(hri);
            } else {
                // No favored nodes, lets unassign.
                LOG.warn("Region not on favored nodes, unassign. Region: " + hri + " current: " + current + " favored nodes: " + favoredNodes);
                try {
                    provider.unassign(hri);
                } catch (IOException e) {
                    LOG.warn("Failed unassign", e);
                    continue;
                }
                RegionPlan rp = new RegionPlan(hri, null, null);
                regionPlans.add(rp);
                misplacedRegions++;
            }
        }
    }
    LOG.debug("Found misplaced regions: " + misplacedRegions + ", not on favored nodes.");
    List<RegionPlan> regionPlansFromBalance = super.balanceTable(tableName, correctAssignments);
    if (regionPlansFromBalance != null) {
        regionPlans.addAll(regionPlansFromBalance);
    }
    return regionPlans;
}
Also used : HashMap(java.util.HashMap) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) IOException(java.io.IOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) RegionPlan(org.apache.hadoop.hbase.master.RegionPlan) ServerName(org.apache.hadoop.hbase.ServerName) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with RegionInfo

use of org.apache.hadoop.hbase.client.RegionInfo in project hbase by apache.

the class BalancerClusterState method wouldLowerAvailability.

/**
 * Return true if the placement of region on server would lower the availability of the region in
 * question
 * @return true or false
 */
boolean wouldLowerAvailability(RegionInfo regionInfo, ServerName serverName) {
    if (!serversToIndex.containsKey(serverName.getAddress())) {
        // safeguard against race between cluster.servers and servers from LB method
        return false;
    // args
    }
    int server = serversToIndex.get(serverName.getAddress());
    int region = regionsToIndex.get(regionInfo);
    // Region replicas for same region should better assign to different servers
    for (int i : regionsPerServer[server]) {
        RegionInfo otherRegionInfo = regions[i];
        if (RegionReplicaUtil.isReplicasForSameRegion(regionInfo, otherRegionInfo)) {
            return true;
        }
    }
    int primary = regionIndexToPrimaryIndex[region];
    if (primary == -1) {
        return false;
    }
    // there is a subset relation for server < host < rack
    // check server first
    int result = checkLocationForPrimary(server, colocatedReplicaCountsPerServer, primary);
    if (result != 0) {
        return result > 0;
    }
    // check host
    if (multiServersPerHost) {
        result = checkLocationForPrimary(serverIndexToHostIndex[server], colocatedReplicaCountsPerHost, primary);
        if (result != 0) {
            return result > 0;
        }
    }
    // check rack
    if (numRacks > 1) {
        result = checkLocationForPrimary(serverIndexToRackIndex[server], colocatedReplicaCountsPerRack, primary);
        if (result != 0) {
            return result > 0;
        }
    }
    return false;
}
Also used : RegionInfo(org.apache.hadoop.hbase.client.RegionInfo)

Aggregations

RegionInfo (org.apache.hadoop.hbase.client.RegionInfo)824 Test (org.junit.Test)416 TableName (org.apache.hadoop.hbase.TableName)311 ServerName (org.apache.hadoop.hbase.ServerName)191 ArrayList (java.util.ArrayList)175 IOException (java.io.IOException)174 TableDescriptor (org.apache.hadoop.hbase.client.TableDescriptor)174 Path (org.apache.hadoop.fs.Path)141 List (java.util.List)118 HashMap (java.util.HashMap)90 Table (org.apache.hadoop.hbase.client.Table)90 Map (java.util.Map)81 Put (org.apache.hadoop.hbase.client.Put)81 Configuration (org.apache.hadoop.conf.Configuration)80 HRegion (org.apache.hadoop.hbase.regionserver.HRegion)67 TreeMap (java.util.TreeMap)66 Result (org.apache.hadoop.hbase.client.Result)59 FileSystem (org.apache.hadoop.fs.FileSystem)58 Cell (org.apache.hadoop.hbase.Cell)50 Scan (org.apache.hadoop.hbase.client.Scan)46