001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.master; 020 021import edu.umd.cs.findbugs.annotations.Nullable; 022import java.io.IOException; 023import java.util.List; 024import java.util.Map; 025import org.apache.hadoop.conf.Configurable; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.ClusterMetrics; 028import org.apache.hadoop.hbase.HBaseIOException; 029import org.apache.hadoop.hbase.ServerName; 030import org.apache.hadoop.hbase.Stoppable; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.RegionInfo; 033import org.apache.hadoop.hbase.conf.ConfigurationObserver; 034import org.apache.yetus.audience.InterfaceAudience; 035 036/** 037 * Makes decisions about the placement and movement of Regions across 038 * RegionServers. 039 * 040 * <p>Cluster-wide load balancing will occur only when there are no regions in 041 * transition and according to a fixed period of a time using {@link #balanceCluster(Map)}. 042 * 043 * <p>On cluster startup, bulk assignment can be used to determine 044 * locations for all Regions in a cluster. 045 * 046 * <p>This class produces plans for the 047 * {@link org.apache.hadoop.hbase.master.assignment.AssignmentManager} 048 * to execute. 049 */ 050@InterfaceAudience.Private 051public interface LoadBalancer extends Configurable, Stoppable, ConfigurationObserver { 052 /** 053 * Master can carry regions as of hbase-2.0.0. 054 * By default, it carries no tables. 055 * TODO: Add any | system as flags to indicate what it can do. 056 */ 057 String TABLES_ON_MASTER = "hbase.balancer.tablesOnMaster"; 058 059 /** 060 * Master carries system tables. 061 */ 062 String SYSTEM_TABLES_ON_MASTER = "hbase.balancer.tablesOnMaster.systemTablesOnly"; 063 064 // Used to signal to the caller that the region(s) cannot be assigned 065 // We deliberately use 'localhost' so the operation will fail fast 066 ServerName BOGUS_SERVER_NAME = ServerName.valueOf("localhost,1,1"); 067 068 /** 069 * Set the current cluster status. This allows a LoadBalancer to map host name to a server 070 * @param st 071 */ 072 void setClusterMetrics(ClusterMetrics st); 073 074 /** 075 * Set the master service. 076 * @param masterServices 077 */ 078 void setMasterServices(MasterServices masterServices); 079 080 /** 081 * Perform the major balance operation for cluster, will invoke {@link #balanceTable} to do 082 * actual balance. Normally not need override this method, except SimpleLoadBalancer and 083 * RSGroupBasedLoadBalancer. 084 * @param loadOfAllTable region load of servers for all table 085 * @return a list of regions to be moved, including source and destination, or null if cluster is 086 * already balanced 087 */ 088 List<RegionPlan> balanceCluster(Map<TableName, 089 Map<ServerName, List<RegionInfo>>> loadOfAllTable) throws IOException; 090 091 /** 092 * Perform the major balance operation for table, all class implement of {@link LoadBalancer} 093 * should override this method 094 * @param tableName the table to be balanced 095 * @param loadOfOneTable region load of servers for the specific one table 096 * @return List of plans 097 */ 098 List<RegionPlan> balanceTable(TableName tableName, 099 Map<ServerName, List<RegionInfo>> loadOfOneTable); 100 /** 101 * Perform a Round Robin assignment of regions. 102 * @param regions 103 * @param servers 104 * @return Map of servername to regioninfos 105 */ 106 Map<ServerName, List<RegionInfo>> roundRobinAssignment( 107 List<RegionInfo> regions, 108 List<ServerName> servers 109 ) throws HBaseIOException; 110 111 /** 112 * Assign regions to the previously hosting region server 113 * @param regions 114 * @param servers 115 * @return List of plans 116 */ 117 @Nullable 118 Map<ServerName, List<RegionInfo>> retainAssignment( 119 Map<RegionInfo, ServerName> regions, 120 List<ServerName> servers 121 ) throws HBaseIOException; 122 123 /** 124 * Get a random region server from the list 125 * @param regionInfo Region for which this selection is being done. 126 * @param servers 127 * @return Servername 128 */ 129 ServerName randomAssignment( 130 RegionInfo regionInfo, List<ServerName> servers 131 ) throws HBaseIOException; 132 133 /** 134 * Initialize the load balancer. Must be called after setters. 135 * @throws HBaseIOException 136 */ 137 void initialize() throws HBaseIOException; 138 139 /** 140 * Marks the region as online at balancer. 141 * @param regionInfo 142 * @param sn 143 */ 144 void regionOnline(RegionInfo regionInfo, ServerName sn); 145 146 /** 147 * Marks the region as offline at balancer. 148 * @param regionInfo 149 */ 150 void regionOffline(RegionInfo regionInfo); 151 152 /* 153 * Notification that config has changed 154 * @param conf 155 */ 156 @Override 157 void onConfigurationChange(Configuration conf); 158 159 /** 160 * If balancer needs to do initialization after Master has started up, lets do that here. 161 */ 162 void postMasterStartupInitialize(); 163 164 /*Updates balancer status tag reported to JMX*/ 165 void updateBalancerStatus(boolean status); 166 167 /** 168 * @return true if Master carries regions 169 */ 170 static boolean isTablesOnMaster(Configuration conf) { 171 return conf.getBoolean(TABLES_ON_MASTER, false); 172 } 173 174 static boolean isSystemTablesOnlyOnMaster(Configuration conf) { 175 return conf.getBoolean(SYSTEM_TABLES_ON_MASTER, false); 176 } 177 178 static boolean isMasterCanHostUserRegions(Configuration conf) { 179 return isTablesOnMaster(conf) && !isSystemTablesOnlyOnMaster(conf); 180 } 181}