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
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 Map<ServerName, List<HRegionInfo>> retainAssignment(
93 Map<HRegionInfo, ServerName> regions,
94 List<ServerName> servers
95 ) throws HBaseIOException;
96
97 /**
98 * Sync assign a region
99 * @param regions
100 * @param servers
101 * @return Map regioninfos to servernames
102 */
103 Map<HRegionInfo, ServerName> immediateAssignment(
104 List<HRegionInfo> regions,
105 List<ServerName> servers
106 ) throws HBaseIOException;
107
108 /**
109 * Get a random region server from the list
110 * @param regionInfo Region for which this selection is being done.
111 * @param servers
112 * @return Servername
113 */
114 ServerName randomAssignment(
115 HRegionInfo regionInfo, List<ServerName> servers
116 ) throws HBaseIOException;
117
118 /**
119 * Initialize the load balancer. Must be called after setters.
120 * @throws HBaseIOException
121 */
122 void initialize() throws HBaseIOException;
123
124 /**
125 * Marks the region as online at balancer.
126 * @param regionInfo
127 * @param sn
128 */
129 void regionOnline(HRegionInfo regionInfo, ServerName sn);
130
131 /**
132 * Marks the region as offline at balancer.
133 * @param regionInfo
134 */
135 void regionOffline(HRegionInfo regionInfo);
136
137 /*
138 * Notification that config has changed
139 * @param conf
140 */
141 void onConfigurationChange(Configuration conf);
142 }