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.conf.Configurable;
26 import org.apache.hadoop.hbase.ClusterStatus;
27 import org.apache.hadoop.hbase.HBaseIOException;
28 import org.apache.hadoop.hbase.HRegionInfo;
29 import org.apache.hadoop.hbase.ServerName;
30 import org.apache.hadoop.hbase.Stoppable;
31
32 /**
33 * Makes decisions about the placement and movement of Regions across
34 * RegionServers.
35 *
36 * <p>Cluster-wide load balancing will occur only when there are no regions in
37 * transition and according to a fixed period of a time using {@link #balanceCluster(Map)}.
38 *
39 * <p>Inline region placement with {@link #immediateAssignment} can be used when
40 * the Master needs to handle closed regions that it currently does not have
41 * a destination set for. This can happen during master failover.
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 {
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 clusterState
67 * @return List of plans
68 */
69 List<RegionPlan> balanceCluster(Map<ServerName,
70 List<HRegionInfo>> clusterState) throws HBaseIOException;
71
72 /**
73 * Perform a Round Robin assignment of regions.
74 * @param regions
75 * @param servers
76 * @return Map of servername to regioninfos
77 */
78 Map<ServerName, List<HRegionInfo>> roundRobinAssignment(
79 List<HRegionInfo> regions,
80 List<ServerName> servers
81 ) throws HBaseIOException;
82
83 /**
84 * Assign regions to the previously hosting region server
85 * @param regions
86 * @param servers
87 * @return List of plans
88 */
89 Map<ServerName, List<HRegionInfo>> retainAssignment(
90 Map<HRegionInfo, ServerName> regions,
91 List<ServerName> servers
92 ) throws HBaseIOException;
93
94 /**
95 * Sync assign a region
96 * @param regions
97 * @param servers
98 * @return Map regioninfos to servernames
99 */
100 Map<HRegionInfo, ServerName> immediateAssignment(
101 List<HRegionInfo> regions,
102 List<ServerName> servers
103 ) throws HBaseIOException;
104
105 /**
106 * Get a random region server from the list
107 * @param regionInfo Region for which this selection is being done.
108 * @param servers
109 * @return Servername
110 */
111 ServerName randomAssignment(
112 HRegionInfo regionInfo, List<ServerName> servers
113 ) throws HBaseIOException;
114
115 /**
116 * Initialize the load balancer. Must be called after setters.
117 * @throws HBaseIOException
118 */
119 void initialize() throws HBaseIOException;
120
121 /**
122 * Marks the region as online at balancer.
123 * @param regionInfo
124 * @param sn
125 */
126 void regionOnline(HRegionInfo regionInfo, ServerName sn);
127
128 /**
129 * Marks the region as offline at balancer.
130 * @param regionInfo
131 */
132 void regionOffline(HRegionInfo regionInfo);
133 }