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  
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    /**
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 clusterState
70     * @return List of plans
71     */
72    List<RegionPlan> balanceCluster(Map<ServerName,
73        List<HRegionInfo>> clusterState) throws HBaseIOException;
74  
75    /**
76     * Perform a Round Robin assignment of regions.
77     * @param regions
78     * @param servers
79     * @return Map of servername to regioninfos
80     */
81    Map<ServerName, List<HRegionInfo>> roundRobinAssignment(
82      List<HRegionInfo> regions,
83      List<ServerName> servers
84    ) throws HBaseIOException;
85  
86    /**
87     * Assign regions to the previously hosting region server
88     * @param regions
89     * @param servers
90     * @return List of plans
91     */
92    @Nullable
93    Map<ServerName, List<HRegionInfo>> retainAssignment(
94      Map<HRegionInfo, ServerName> regions,
95      List<ServerName> servers
96    ) throws HBaseIOException;
97  
98    /**
99     * Sync assign a region
100    * @param regions
101    * @param servers
102     * @return Map regioninfos to servernames
103    */
104   Map<HRegionInfo, ServerName> immediateAssignment(
105     List<HRegionInfo> regions,
106     List<ServerName> servers
107   ) throws HBaseIOException;
108 
109   /**
110    * Get a random region server from the list
111    * @param regionInfo Region for which this selection is being done.
112    * @param servers
113    * @return Servername
114    */
115   ServerName randomAssignment(
116     HRegionInfo regionInfo, List<ServerName> servers
117   ) throws HBaseIOException;
118 
119   /**
120    * Initialize the load balancer. Must be called after setters.
121    * @throws HBaseIOException
122    */
123   void initialize() throws HBaseIOException;
124 
125   /**
126    * Marks the region as online at balancer.
127    * @param regionInfo
128    * @param sn
129    */
130   void regionOnline(HRegionInfo regionInfo, ServerName sn);
131 
132   /**
133    * Marks the region as offline at balancer.
134    * @param regionInfo
135    */
136   void regionOffline(HRegionInfo regionInfo);
137 
138   /*
139    * Notification that config has changed
140    * @param conf
141    */
142   void onConfigurationChange(Configuration conf);
143 }