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 javax.annotation.Nullable;
22  import java.util.List;
23  import java.util.Map;
24  
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    /**
52     * Set the current cluster status.  This allows a LoadBalancer to map host name to a server
53     * @param st
54     */
55    void setClusterStatus(ClusterStatus st);
56  
57  
58    /**
59     * Set the master service.
60     * @param masterServices
61     */
62    void setMasterServices(MasterServices masterServices);
63  
64    /**
65     * Perform the major balance operation
66     * @param tableName
67     * @param clusterState
68     * @return List of plans
69     */
70    List<RegionPlan> balanceCluster(TableName tableName, Map<ServerName,
71        List<HRegionInfo>> clusterState) throws HBaseIOException;
72  
73    /**
74     * Perform the major balance operation
75     * @param clusterState
76     * @return List of plans
77     */
78    List<RegionPlan> balanceCluster(Map<ServerName,
79        List<HRegionInfo>> clusterState) throws HBaseIOException;
80  
81    /**
82     * Perform a Round Robin assignment of regions.
83     * @param regions
84     * @param servers
85     * @return Map of servername to regioninfos
86     */
87    Map<ServerName, List<HRegionInfo>> roundRobinAssignment(
88      List<HRegionInfo> regions,
89      List<ServerName> servers
90    ) throws HBaseIOException;
91  
92    /**
93     * Assign regions to the previously hosting region server
94     * @param regions
95     * @param servers
96     * @return List of plans
97     */
98    @Nullable
99    Map<ServerName, List<HRegionInfo>> retainAssignment(
100     Map<HRegionInfo, ServerName> regions,
101     List<ServerName> servers
102   ) throws HBaseIOException;
103 
104   /**
105    * Get a random region server from the list
106    * @param regionInfo Region for which this selection is being done.
107    * @param servers
108    * @return Servername
109    */
110   ServerName randomAssignment(
111     HRegionInfo regionInfo, List<ServerName> servers
112   ) throws HBaseIOException;
113 
114   /**
115    * Initialize the load balancer. Must be called after setters.
116    * @throws HBaseIOException
117    */
118   void initialize() throws HBaseIOException;
119 
120   /**
121    * Marks the region as online at balancer.
122    * @param regionInfo
123    * @param sn
124    */
125   void regionOnline(HRegionInfo regionInfo, ServerName sn);
126 
127   /**
128    * Marks the region as offline at balancer.
129    * @param regionInfo
130    */
131   void regionOffline(HRegionInfo regionInfo);
132 
133   /*
134    * Notification that config has changed
135    * @param conf
136    */
137   void onConfigurationChange(Configuration conf);
138 }