Search in sources :

Example 1 with RegionPlan

use of org.apache.hadoop.hbase.master.RegionPlan in project hbase by apache.

the class TestRSGroupBasedLoadBalancer method reconcile.

private ArrayListMultimap<String, ServerAndLoad> reconcile(ArrayListMultimap<String, ServerAndLoad> previousLoad, List<RegionPlan> plans) {
    ArrayListMultimap<String, ServerAndLoad> result = ArrayListMultimap.create();
    result.putAll(previousLoad);
    if (plans != null) {
        for (RegionPlan plan : plans) {
            ServerName source = plan.getSource();
            updateLoad(result, source, -1);
            ServerName destination = plan.getDestination();
            updateLoad(result, destination, +1);
        }
    }
    return result;
}
Also used : RegionPlan(org.apache.hadoop.hbase.master.RegionPlan) ServerName(org.apache.hadoop.hbase.ServerName)

Example 2 with RegionPlan

use of org.apache.hadoop.hbase.master.RegionPlan in project hbase by apache.

the class MergeTableRegionsProcedure method MoveRegionsToSameRS.

/**
   * Move all regions to the same region server
   * @param env MasterProcedureEnv
   * @return whether target regions hosted by the same RS
   * @throws IOException
   */
private boolean MoveRegionsToSameRS(final MasterProcedureEnv env) throws IOException {
    // Make sure regions are on the same regionserver before send merge
    // regions request to region server.
    //
    boolean onSameRS = isRegionsOnTheSameServer(env);
    if (!onSameRS) {
        // Note: the following logic assumes that we only have 2 regions to merge.  In the future,
        // if we want to extend to more than 2 regions, the code needs to modify a little bit.
        //
        RegionStates regionStates = getAssignmentManager(env).getRegionStates();
        ServerName regionLocation2 = regionStates.getRegionServerOfRegion(regionsToMerge[1]);
        RegionLoad loadOfRegionA = getRegionLoad(env, regionLocation, regionsToMerge[0]);
        RegionLoad loadOfRegionB = getRegionLoad(env, regionLocation2, regionsToMerge[1]);
        if (loadOfRegionA != null && loadOfRegionB != null && loadOfRegionA.getRequestsCount() < loadOfRegionB.getRequestsCount()) {
            // switch regionsToMerge[0] and regionsToMerge[1]
            HRegionInfo tmpRegion = this.regionsToMerge[0];
            this.regionsToMerge[0] = this.regionsToMerge[1];
            this.regionsToMerge[1] = tmpRegion;
            ServerName tmpLocation = regionLocation;
            regionLocation = regionLocation2;
            regionLocation2 = tmpLocation;
        }
        long startTime = EnvironmentEdgeManager.currentTime();
        RegionPlan regionPlan = new RegionPlan(regionsToMerge[1], regionLocation2, regionLocation);
        LOG.info("Moving regions to same server for merge: " + regionPlan.toString());
        getAssignmentManager(env).balance(regionPlan);
        do {
            try {
                Thread.sleep(20);
                // Make sure check RIT first, then get region location, otherwise
                // we would make a wrong result if region is online between getting
                // region location and checking RIT
                boolean isRIT = regionStates.isRegionInTransition(regionsToMerge[1]);
                regionLocation2 = regionStates.getRegionServerOfRegion(regionsToMerge[1]);
                onSameRS = regionLocation.equals(regionLocation2);
                if (onSameRS || !isRIT) {
                    // RegionInTransition any more
                    break;
                }
            } catch (InterruptedException e) {
                InterruptedIOException iioe = new InterruptedIOException();
                iioe.initCause(e);
                throw iioe;
            }
        } while ((EnvironmentEdgeManager.currentTime() - startTime) <= getTimeout(env));
    }
    return onSameRS;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) InterruptedIOException(java.io.InterruptedIOException) RegionLoad(org.apache.hadoop.hbase.RegionLoad) RegionStates(org.apache.hadoop.hbase.master.RegionStates) RegionPlan(org.apache.hadoop.hbase.master.RegionPlan) ServerName(org.apache.hadoop.hbase.ServerName)

Example 3 with RegionPlan

use of org.apache.hadoop.hbase.master.RegionPlan in project hbase by apache.

the class BaseLoadBalancer method balanceMasterRegions.

/**
   * Balance the regions that should be on master regionserver.
   */
protected List<RegionPlan> balanceMasterRegions(Map<ServerName, List<HRegionInfo>> clusterMap) {
    if (masterServerName == null || clusterMap == null || clusterMap.size() <= 1)
        return null;
    List<RegionPlan> plans = null;
    List<HRegionInfo> regions = clusterMap.get(masterServerName);
    if (regions != null) {
        Iterator<ServerName> keyIt = null;
        for (HRegionInfo region : regions) {
            if (shouldBeOnMaster(region))
                continue;
            // Find a non-master regionserver to host the region
            if (keyIt == null || !keyIt.hasNext()) {
                keyIt = clusterMap.keySet().iterator();
            }
            ServerName dest = keyIt.next();
            if (masterServerName.equals(dest)) {
                if (!keyIt.hasNext()) {
                    keyIt = clusterMap.keySet().iterator();
                }
                dest = keyIt.next();
            }
            // Move this region away from the master regionserver
            RegionPlan plan = new RegionPlan(region, masterServerName, dest);
            if (plans == null) {
                plans = new ArrayList<>();
            }
            plans.add(plan);
        }
    }
    for (Map.Entry<ServerName, List<HRegionInfo>> server : clusterMap.entrySet()) {
        if (masterServerName.equals(server.getKey()))
            continue;
        for (HRegionInfo region : server.getValue()) {
            if (!shouldBeOnMaster(region))
                continue;
            // Move this region to the master regionserver
            RegionPlan plan = new RegionPlan(region, server.getKey(), masterServerName);
            if (plans == null) {
                plans = new ArrayList<>();
            }
            plans.add(plan);
        }
    }
    return plans;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) 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) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap)

Example 4 with RegionPlan

use of org.apache.hadoop.hbase.master.RegionPlan 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 5 with RegionPlan

use of org.apache.hadoop.hbase.master.RegionPlan 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)

Aggregations

RegionPlan (org.apache.hadoop.hbase.master.RegionPlan)38 ServerName (org.apache.hadoop.hbase.ServerName)32 RegionInfo (org.apache.hadoop.hbase.client.RegionInfo)24 ArrayList (java.util.ArrayList)21 List (java.util.List)19 HashMap (java.util.HashMap)17 TableName (org.apache.hadoop.hbase.TableName)16 Map (java.util.Map)14 Test (org.junit.Test)14 IOException (java.io.IOException)11 TreeMap (java.util.TreeMap)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 HMaster (org.apache.hadoop.hbase.master.HMaster)7 MasterServices (org.apache.hadoop.hbase.master.MasterServices)7 Configuration (org.apache.hadoop.conf.Configuration)6 HBaseClassTestRule (org.apache.hadoop.hbase.HBaseClassTestRule)6 HBaseTestingUtil (org.apache.hadoop.hbase.HBaseTestingUtil)6 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)6 MasterRegion (org.apache.hadoop.hbase.master.region.MasterRegion)6 MasterTests (org.apache.hadoop.hbase.testclassification.MasterTests)6