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 org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.conf.ConfigurationObserver;
26  import org.apache.hadoop.conf.Configurable;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.ClusterStatus;
29  import org.apache.hadoop.hbase.HBaseIOException;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.Stoppable;
33  import org.apache.hadoop.hbase.TableName;
34  
35  /**
36   * Makes decisions about the placement and movement of Regions across
37   * RegionServers.
38   *
39   * <p>Cluster-wide load balancing will occur only when there are no regions in
40   * transition and according to a fixed period of a time using {@link #balanceCluster(Map)}.
41   *
42   * <p>Inline region placement with {@link #immediateAssignment} can be used when
43   * the Master needs to handle closed regions that it currently does not have
44   * a destination set for.  This can happen during master failover.
45   *
46   * <p>On cluster startup, bulk assignment can be used to determine
47   * locations for all Regions in a cluster.
48   *
49   * <p>This classes produces plans for the {@link AssignmentManager} to execute.
50   */
51  @InterfaceAudience.Private
52  public interface LoadBalancer extends Configurable, Stoppable, ConfigurationObserver {
53  
54    //used to signal to the caller that the region(s) cannot be assigned
55    ServerName BOGUS_SERVER_NAME = ServerName.parseServerName("localhost,1,1");
56  
57    /**
58     * Set the current cluster status.  This allows a LoadBalancer to map host name to a server
59     * @param st
60     */
61    void setClusterStatus(ClusterStatus st);
62  
63  
64    /**
65     * Set the master service.
66     * @param masterServices
67     */
68    void setMasterServices(MasterServices masterServices);
69  
70    /**
71     * Perform the major balance operation
72     * @param tableName
73     * @param clusterState
74     * @return List of plans
75     */
76    List<RegionPlan> balanceCluster(TableName tableName, Map<ServerName,
77        List<HRegionInfo>> clusterState) throws HBaseIOException;
78  
79    /**
80     * Perform the major balance operation
81     * @param clusterState
82     * @return List of plans
83     */
84    List<RegionPlan> balanceCluster(Map<ServerName,
85        List<HRegionInfo>> clusterState) throws HBaseIOException;
86  
87    /**
88     * Perform a Round Robin assignment of regions.
89     * @param regions
90     * @param servers
91     * @return Map of servername to regioninfos
92     */
93    Map<ServerName, List<HRegionInfo>> roundRobinAssignment(
94      List<HRegionInfo> regions,
95      List<ServerName> servers
96    ) throws HBaseIOException;
97  
98    /**
99     * Assign regions to the previously hosting region server
100    * @param regions
101    * @param servers
102    * @return List of plans
103    */
104   Map<ServerName, List<HRegionInfo>> retainAssignment(
105     Map<HRegionInfo, ServerName> regions,
106     List<ServerName> servers
107   ) throws HBaseIOException;
108 
109   /**
110    * Sync assign a region
111    * @param regions
112    * @param servers
113     * @return Map regioninfos to servernames
114    */
115   Map<HRegionInfo, ServerName> immediateAssignment(
116     List<HRegionInfo> regions,
117     List<ServerName> servers
118   ) throws HBaseIOException;
119 
120   /**
121    * Get a random region server from the list
122    * @param regionInfo Region for which this selection is being done.
123    * @param servers
124    * @return Servername
125    */
126   ServerName randomAssignment(
127     HRegionInfo regionInfo, List<ServerName> servers
128   ) throws HBaseIOException;
129 
130   /**
131    * Initialize the load balancer. Must be called after setters.
132    * @throws HBaseIOException
133    */
134   void initialize() throws HBaseIOException;
135 
136   /**
137    * Marks the region as online at balancer.
138    * @param regionInfo
139    * @param sn
140    */
141   void regionOnline(HRegionInfo regionInfo, ServerName sn);
142 
143   /**
144    * Marks the region as offline at balancer.
145    * @param regionInfo
146    */
147   void regionOffline(HRegionInfo regionInfo);
148 
149   /*
150    * Notification that config has changed
151    * @param conf
152    */
153   void onConfigurationChange(Configuration conf);
154 }