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  
34  /**
35   * Makes decisions about the placement and movement of Regions across
36   * RegionServers.
37   *
38   * <p>Cluster-wide load balancing will occur only when there are no regions in
39   * transition and according to a fixed period of a time using {@link #balanceCluster(Map)}.
40   *
41   * <p>Inline region placement with {@link #immediateAssignment} can be used when
42   * the Master needs to handle closed regions that it currently does not have
43   * a destination set for.  This can happen during master failover.
44   *
45   * <p>On cluster startup, bulk assignment can be used to determine
46   * locations for all Regions in a cluster.
47   *
48   * <p>This classes produces plans for the {@link AssignmentManager} to execute.
49   */
50  @InterfaceAudience.Private
51  public interface LoadBalancer extends Configurable, Stoppable, ConfigurationObserver {
52  
53    /**
54     * Set the current cluster status.  This allows a LoadBalancer to map host name to a server
55     * @param st
56     */
57    void setClusterStatus(ClusterStatus st);
58  
59  
60    /**
61     * Set the master service.
62     * @param masterServices
63     */
64    void setMasterServices(MasterServices masterServices);
65  
66    /**
67     * Perform the major balance operation
68     * @param clusterState
69     * @return List of plans
70     */
71    List<RegionPlan> balanceCluster(Map<ServerName,
72        List<HRegionInfo>> clusterState) throws HBaseIOException;
73  
74    /**
75     * Perform a Round Robin assignment of regions.
76     * @param regions
77     * @param servers
78     * @return Map of servername to regioninfos
79     */
80    Map<ServerName, List<HRegionInfo>> roundRobinAssignment(
81      List<HRegionInfo> regions,
82      List<ServerName> servers
83    ) throws HBaseIOException;
84  
85    /**
86     * Assign regions to the previously hosting region server
87     * @param regions
88     * @param servers
89     * @return List of plans
90     */
91    Map<ServerName, List<HRegionInfo>> retainAssignment(
92      Map<HRegionInfo, ServerName> regions,
93      List<ServerName> servers
94    ) throws HBaseIOException;
95  
96    /**
97     * Sync assign a region
98     * @param regions
99     * @param servers
100     * @return Map regioninfos to servernames
101    */
102   Map<HRegionInfo, ServerName> immediateAssignment(
103     List<HRegionInfo> 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 }