View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master;
20  
21  import java.util.List;
22  import java.util.Map;
23
24  import edu.umd.cs.findbugs.annotations.Nullable;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.conf.ConfigurationObserver;
27  import org.apache.hadoop.conf.Configurable;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.ClusterStatus;
30  import org.apache.hadoop.hbase.HBaseIOException;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.Stoppable;
34  import org.apache.hadoop.hbase.TableName;
35  
36  /**
37   * Makes decisions about the placement and movement of Regions across
38   * RegionServers.
39   *
40   * <p>Cluster-wide load balancing will occur only when there are no regions in
41   * transition and according to a fixed period of a time using {@link #balanceCluster(Map)}.
42   *
43   * <p>On cluster startup, bulk assignment can be used to determine
44   * locations for all Regions in a cluster.
45   *
46   * <p>This classes produces plans for the {@link AssignmentManager} to execute.
47   */
48  @InterfaceAudience.Private
49  public interface LoadBalancer extends Configurable, Stoppable, ConfigurationObserver {
50  
51    //used to signal to the caller that the region(s) cannot be assigned
52    ServerName BOGUS_SERVER_NAME = ServerName.parseServerName("localhost,1,1");
53  
54    /**
55     * Set the current cluster status.  This allows a LoadBalancer to map host name to a server
56     * @param st
57     */
58    void setClusterStatus(ClusterStatus st);
59  
60  
61    /**
62     * Set the master service.
63     * @param masterServices
64     */
65    void setMasterServices(MasterServices masterServices);
66  
67    /**
68     * Perform the major balance operation
69     * @param tableName
70     * @param clusterState
71     * @return List of plans
72     */
73    List<RegionPlan> balanceCluster(TableName tableName, Map<ServerName,
74        List<HRegionInfo>> clusterState) throws HBaseIOException;
75  
76    /**
77     * Perform the major balance operation
78     * @param clusterState
79     * @return List of plans
80     */
81    List<RegionPlan> balanceCluster(Map<ServerName,
82        List<HRegionInfo>> clusterState) throws HBaseIOException;
83  
84    /**
85     * Perform a Round Robin assignment of regions.
86     * @param regions
87     * @param servers
88     * @return Map of servername to regioninfos
89     */
90    Map<ServerName, List<HRegionInfo>> roundRobinAssignment(
91      List<HRegionInfo> regions,
92      List<ServerName> servers
93    ) throws HBaseIOException;
94  
95    /**
96     * Assign regions to the previously hosting region server
97     * @param regions
98     * @param servers
99     * @return List of plans
100    */
101   @Nullable
102   Map<ServerName, List<HRegionInfo>> retainAssignment(
103     Map<HRegionInfo, ServerName> regions,
104     List<ServerName> servers
105   ) throws HBaseIOException;
106 
107   /**
108    * Get a random region server from the list
109    * @param regionInfo Region for which this selection is being done.
110    * @param servers
111    * @return Servername
112    */
113   ServerName randomAssignment(
114     HRegionInfo regionInfo, List<ServerName> servers
115   ) throws HBaseIOException;
116 
117   /**
118    * Initialize the load balancer. Must be called after setters.
119    * @throws HBaseIOException
120    */
121   void initialize() throws HBaseIOException;
122 
123   /**
124    * Marks the region as online at balancer.
125    * @param regionInfo
126    * @param sn
127    */
128   void regionOnline(HRegionInfo regionInfo, ServerName sn);
129 
130   /**
131    * Marks the region as offline at balancer.
132    * @param regionInfo
133    */
134   void regionOffline(HRegionInfo regionInfo);
135 
136   /*
137    * Notification that config has changed
138    * @param conf
139    */
140   void onConfigurationChange(Configuration conf);
141 }