@Override public void initialize() throws HBaseIOException { Class<? extends LoadBalancer> delegatorKlass = conf.getClass(Constants.INDEX_BALANCER_DELEGATOR_CLASS, StochasticLoadBalancer.class, LoadBalancer.class); this.delegator = ReflectionUtils.newInstance(delegatorKlass, conf); this.delegator.setClusterStatus(clusterStatus); this.delegator.setMasterServices(this.master); try { HTableDescriptor desc = null; Map<String, HTableDescriptor> tableDescriptors = this.master.getTableDescriptors().getAll(); for (Entry<String, HTableDescriptor> entry : tableDescriptors.entrySet()) { desc = entry.getValue(); if (desc.getValue(Constants.INDEX_SPEC_KEY) != null) { addIndexedTable(desc.getTableName()); } } } catch (IOException e) { throw new HBaseIOException(e); } }
public static List<RegionPlan> makePlan(HBaseAdmin admin, List<RegionPlan> newRegionPlan) throws IOException { // snapshot current region assignment Map<HRegionInfo, ServerName> regionAssignmentMap = createRegionAssignmentMap(admin); // update with new plan for (RegionPlan regionPlan : newRegionPlan) { regionAssignmentMap.put(regionPlan.getRegionInfo(), regionPlan.getDestination()); } Map<ServerName, List<HRegionInfo>> clusterState = initializeRegionMap(admin); for (Map.Entry<HRegionInfo, ServerName> entry : regionAssignmentMap.entrySet()) clusterState.get(entry.getValue()).add(entry.getKey()); StochasticLoadBalancer balancer = new StochasticLoadBalancer(); Configuration conf = admin.getConfiguration(); conf.setFloat("hbase.regions.slop", 0.2f); balancer.setConf(conf); return balancer.balanceCluster(clusterState); }
/** * In Ensemble mode, there should be only one ensemble table */ @Test (timeout=60000) public void testJmxMetrics_EnsembleMode() throws Exception { loadBalancer = new StochasticLoadBalancer(); conf.setBoolean(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE, false); loadBalancer.setConf(conf); TableName tableName = HConstants.ENSEMBLE_TABLE_NAME; Map<ServerName, List<RegionInfo>> clusterState = mockClusterServers(mockCluster_ensemble); loadBalancer.balanceCluster(tableName, clusterState); String[] tableNames = new String[] { tableName.getNameAsString() }; String[] functionNames = loadBalancer.getCostFunctionNames(); Set<String> jmxMetrics = readJmxMetricsWithRetry(); Set<String> expectedMetrics = getExpectedJmxMetrics(tableNames, functionNames); // printMetrics(jmxMetrics, "existing metrics in ensemble mode"); // printMetrics(expectedMetrics, "expected metrics in ensemble mode"); // assert that every expected is in the JMX for (String expected : expectedMetrics) { assertTrue("Metric " + expected + " can not be found in JMX in ensemble mode.", jmxMetrics.contains(expected)); } }
@Override public void initialize() throws HBaseIOException { try { if (rsGroupInfoManager == null) { List<RSGroupAdminEndpoint> cps = masterServices.getMasterCoprocessorHost().findCoprocessors(RSGroupAdminEndpoint.class); if (cps.size() != 1) { String msg = "Expected one implementation of GroupAdminEndpoint but found " + cps.size(); LOG.error(msg); throw new HBaseIOException(msg); } rsGroupInfoManager = cps.get(0).getGroupInfoManager(); } } catch (IOException e) { throw new HBaseIOException("Failed to initialize GroupInfoManagerImpl", e); } // Create the balancer Class<? extends LoadBalancer> balancerKlass = config.getClass(HBASE_RSGROUP_LOADBALANCER_CLASS, StochasticLoadBalancer.class, LoadBalancer.class); internalBalancer = ReflectionUtils.newInstance(balancerKlass, config); internalBalancer.setMasterServices(masterServices); internalBalancer.setClusterMetrics(clusterStatus); internalBalancer.setConf(config); internalBalancer.initialize(); }
public static List<RegionPlan> makePlan(HBaseAdmin admin, Map<ServerName, List<HRegionInfo>> clusterState, Configuration conf) throws IOException { StochasticLoadBalancer balancer = new StochasticLoadBalancer() { @Override protected boolean needsBalance(ClusterLoadState cs) { return true; } }; balancer.setConf(conf); balancer.setClusterStatus(admin.getClusterStatus()); List<RegionPlan> regionPlanList = balancer.balanceCluster(clusterState); return regionPlanList == null ? new ArrayList<RegionPlan>() : regionPlanList; }
public static List<RegionPlan> makePlan(HBaseAdmin admin, Map<ServerName, List<HRegionInfo>> clusterState, Configuration conf) throws IOException { StochasticLoadBalancer balancer = new StochasticLoadBalancer() { @Override protected boolean needsBalance(Cluster c) { return true; } }; balancer.setConf(conf); balancer.setClusterStatus(admin.getClusterStatus()); List<RegionPlan> regionPlanList = balancer.balanceCluster(clusterState); return regionPlanList == null ? new ArrayList<RegionPlan>() : regionPlanList; }
/** * Given the tables and functions, return metrics names that should exist in JMX */ private Set<String> getExpectedJmxMetrics(String[] tableNames, String[] functionNames) { Set<String> ret = new HashSet<>(); for (String tableName : tableNames) { ret.add(StochasticLoadBalancer.composeAttributeName(tableName, "Overall")); for (String functionName : functionNames) { String metricsName = StochasticLoadBalancer.composeAttributeName(tableName, functionName); ret.add(metricsName); } } return ret; }
/** * In per-table mode, each table has a set of metrics */ @Test (timeout=60000) public void testJmxMetrics_PerTableMode() throws Exception { loadBalancer = new StochasticLoadBalancer(); conf.setBoolean(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE, true); loadBalancer.setConf(conf); // NOTE the size is normally set in setClusterMetrics, for test purpose, we set it manually // Tables: hbase:namespace, table1, table2 // Functions: costFunctions, overall String[] functionNames = loadBalancer.getCostFunctionNames(); loadBalancer.updateMetricsSize(3 * (functionNames.length + 1)); // table 1 TableName tableName = TableName.valueOf(TABLE_NAME_1); Map<ServerName, List<RegionInfo>> clusterState = mockClusterServers(mockCluster_pertable_1); loadBalancer.balanceCluster(tableName, clusterState); // table 2 tableName = TableName.valueOf(TABLE_NAME_2); clusterState = mockClusterServers(mockCluster_pertable_2); loadBalancer.balanceCluster(tableName, clusterState); // table hbase:namespace tableName = TableName.valueOf(TABLE_NAME_NAMESPACE); clusterState = mockClusterServers(mockCluster_pertable_namespace); loadBalancer.balanceCluster(tableName, clusterState); String[] tableNames = new String[] { TABLE_NAME_1, TABLE_NAME_2, TABLE_NAME_NAMESPACE }; Set<String> jmxMetrics = readJmxMetricsWithRetry(); Set<String> expectedMetrics = getExpectedJmxMetrics(tableNames, functionNames); // printMetrics(jmxMetrics, "existing metrics in per-table mode"); // printMetrics(expectedMetrics, "expected metrics in per-table mode"); // assert that every expected is in the JMX for (String expected : expectedMetrics) { assertTrue("Metric " + expected + " can not be found in JMX in per-table mode.", jmxMetrics.contains(expected)); } }