1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rsgroup;
21
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Maps;
24 import com.google.common.collect.Sets;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.concurrent.ConcurrentMap;
38
39 import org.apache.commons.lang.StringUtils;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.apache.hadoop.hbase.HRegionInfo;
43 import org.apache.hadoop.hbase.HTableDescriptor;
44 import org.apache.hadoop.hbase.NamespaceDescriptor;
45 import org.apache.hadoop.hbase.ServerName;
46 import org.apache.hadoop.hbase.TableName;
47 import org.apache.hadoop.hbase.classification.InterfaceAudience;
48 import org.apache.hadoop.hbase.constraint.ConstraintException;
49 import org.apache.hadoop.hbase.master.AssignmentManager;
50 import org.apache.hadoop.hbase.master.LoadBalancer;
51 import org.apache.hadoop.hbase.master.MasterServices;
52 import org.apache.hadoop.hbase.master.RegionPlan;
53 import org.apache.hadoop.hbase.master.RegionState;
54 import org.apache.hadoop.hbase.master.ServerManager;
55 import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
56 import org.apache.hadoop.hbase.net.Address;
57 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
58
59
60
61
62 @InterfaceAudience.Private
63 public class RSGroupAdminServer implements RSGroupAdmin {
64 private static final Log LOG = LogFactory.getLog(RSGroupAdminServer.class);
65
66 private MasterServices master;
67
68
69 private ConcurrentMap<Address,String> serversInTransition =
70 new ConcurrentHashMap<Address, String>();
71 private RSGroupInfoManager rsGroupInfoManager;
72
73 public RSGroupAdminServer(MasterServices master,
74 RSGroupInfoManager RSGroupInfoManager) throws IOException {
75 this.master = master;
76 this.rsGroupInfoManager = RSGroupInfoManager;
77 }
78
79 @Override
80 public RSGroupInfo getRSGroupInfo(String groupName) throws IOException{
81 return getRSGroupInfoManager().getRSGroup(groupName);
82 }
83
84
85 @Override
86 public RSGroupInfo getRSGroupInfoOfTable(TableName tableName) throws IOException {
87 String groupName = getRSGroupInfoManager().getRSGroupOfTable(tableName);
88 if (groupName == null) {
89 return null;
90 }
91 return getRSGroupInfoManager().getRSGroup(groupName);
92 }
93
94 @Override
95 public void moveServers(Set<Address> servers, String targetGroupName)
96 throws IOException {
97 if (servers == null) {
98 throw new ConstraintException(
99 "The list of servers cannot be null.");
100 }
101 if (StringUtils.isEmpty(targetGroupName)) {
102 throw new ConstraintException("The target group cannot be null.");
103 }
104 if (servers.size() < 1) {
105 return;
106 }
107
108 RSGroupInfo targetGrp = getRSGroupInfo(targetGroupName);
109 if (targetGrp == null) {
110 throw new ConstraintException("Group does not exist: "+targetGroupName);
111 }
112
113 RSGroupInfoManager manager = getRSGroupInfoManager();
114 synchronized (manager) {
115 Address firstServer = servers.iterator().next();
116
117
118 RSGroupInfo srcGrp = manager.getRSGroupOfServer(firstServer);
119
120
121
122 if (srcGrp == null) {
123 throw new ConstraintException(
124 "Server "+firstServer+" does not have a group.");
125 }
126 if (RSGroupInfo.DEFAULT_GROUP.equals(srcGrp.getName())) {
127 Set<Address> onlineServers = new HashSet<Address>();
128 for(ServerName server: master.getServerManager().getOnlineServers().keySet()) {
129 onlineServers.add(server.getAddress());
130 }
131 for(Address el: servers) {
132 if(!onlineServers.contains(el)) {
133 throw new ConstraintException(
134 "Server "+el+" is not an online server in default group.");
135 }
136 }
137 }
138
139 if (RSGroupInfo.DEFAULT_GROUP.equals(srcGrp.getName()) && srcGrp.getServers().size() <=
140 servers.size()) {
141 throw new ConstraintException("Should keep at least one server in 'default' RSGroup ");
142 }
143
144 if(srcGrp.getServers().size() <= servers.size() && srcGrp.getTables().size() > 0) {
145 throw new ConstraintException("Cannot leave a group "+srcGrp.getName()+
146 " that contains tables " +"without servers.");
147 }
148
149 String sourceGroupName = getRSGroupInfoManager()
150 .getRSGroupOfServer(srcGrp.getServers().iterator().next()).getName();
151 if(getRSGroupInfo(targetGroupName) == null) {
152 throw new ConstraintException("Target group does not exist: "+targetGroupName);
153 }
154
155 for(Address server: servers) {
156 if (serversInTransition.containsKey(server)) {
157 throw new ConstraintException(
158 "Server list contains a server that is already being moved: "+server);
159 }
160 String tmpGroup = getRSGroupInfoManager().getRSGroupOfServer(server).getName();
161 if (sourceGroupName != null && !tmpGroup.equals(sourceGroupName)) {
162 throw new ConstraintException(
163 "Move server request should only come from one source group. "+
164 "Expecting only "+sourceGroupName+" but contains "+tmpGroup);
165 }
166 }
167
168 if(sourceGroupName.equals(targetGroupName)) {
169 throw new ConstraintException(
170 "Target group is the same as source group: "+targetGroupName);
171 }
172
173 try {
174
175 for (Address server : servers) {
176 serversInTransition.put(server, targetGroupName);
177 }
178
179 getRSGroupInfoManager().moveServers(servers, sourceGroupName, targetGroupName);
180 boolean found;
181 List<Address> tmpServers = Lists.newArrayList(servers);
182 do {
183 found = false;
184 for (Iterator<Address> iter = tmpServers.iterator();
185 iter.hasNext(); ) {
186 Address rs = iter.next();
187
188 List<HRegionInfo> regions = new LinkedList<HRegionInfo>();
189 for (Map.Entry<HRegionInfo, ServerName> el :
190 master.getAssignmentManager().getRegionStates().getRegionAssignments().entrySet()) {
191 if (el.getValue().getAddress().equals(rs)) {
192 regions.add(el.getKey());
193 }
194 }
195 Iterator<RegionState> i =
196 master.getAssignmentManager().getRegionStates().getRegionsInTransition().iterator();
197 while (i.hasNext()) {
198 RegionState state = i.next();
199 if (state.getServerName().getAddress().equals(rs)) {
200 regions.add(state.getRegion());
201 }
202 }
203
204
205 LOG.info("Unassigning " + regions.size() +
206 " regions from server " + rs + " for move to " + targetGroupName);
207 if (regions.size() > 0) {
208
209 for (HRegionInfo region : regions) {
210
211
212 if (!targetGrp.containsTable(region.getTable())) {
213 master.getAssignmentManager().unassign(region);
214 found = true;
215 }
216 }
217 }
218 if (!found) {
219 iter.remove();
220 }
221 }
222 try {
223 manager.wait(1000);
224 } catch (InterruptedException e) {
225 LOG.warn("Sleep interrupted", e);
226 Thread.currentThread().interrupt();
227 }
228 } while (found);
229 } finally {
230
231 for (Address server : servers) {
232 serversInTransition.remove(server);
233 }
234 }
235 LOG.info("Move server done: "+sourceGroupName+"->"+targetGroupName);
236 }
237 }
238
239 @Override
240 public void moveTables(Set<TableName> tables, String targetGroup) throws IOException {
241 if (tables == null) {
242 throw new ConstraintException(
243 "The list of servers cannot be null.");
244 }
245 if(tables.size() < 1) {
246 LOG.debug("moveTables() passed an empty set. Ignoring.");
247 return;
248 }
249 RSGroupInfoManager manager = getRSGroupInfoManager();
250 synchronized (manager) {
251 if(targetGroup != null) {
252 RSGroupInfo destGroup = manager.getRSGroup(targetGroup);
253 if(destGroup == null) {
254 throw new ConstraintException("Target group does not exist: "+targetGroup);
255 }
256 if(destGroup.getServers().size() < 1) {
257 throw new ConstraintException("Target group must have at least one server.");
258 }
259 }
260
261 for(TableName table : tables) {
262 String srcGroup = manager.getRSGroupOfTable(table);
263 if(srcGroup != null && srcGroup.equals(targetGroup)) {
264 throw new ConstraintException(
265 "Source group is the same as target group for table "+table+" :"+srcGroup);
266 }
267 }
268 manager.moveTables(tables, targetGroup);
269 }
270 for(TableName table: tables) {
271 if (master.getAssignmentManager().getTableStateManager().isTableState(table,
272 ZooKeeperProtos.Table.State.DISABLED,
273 ZooKeeperProtos.Table.State.DISABLING)) {
274 LOG.debug("Skipping move regions because the table" + table + " is disabled.");
275 continue;
276 }
277 TableLock lock = master.getTableLockManager().writeLock(table, "Group: table move");
278 try {
279 lock.acquire();
280 for (HRegionInfo region :
281 master.getAssignmentManager().getRegionStates().getRegionsOfTable(table)) {
282 master.getAssignmentManager().unassign(region);
283 }
284 } finally {
285 lock.release();
286 }
287 }
288 }
289
290 @Override
291 public void addRSGroup(String name) throws IOException {
292 getRSGroupInfoManager().addRSGroup(new RSGroupInfo(name));
293 }
294
295 @Override
296 public void removeRSGroup(String name) throws IOException {
297 RSGroupInfoManager manager = getRSGroupInfoManager();
298 synchronized (manager) {
299 RSGroupInfo groupInfo = getRSGroupInfoManager().getRSGroup(name);
300 if(groupInfo == null) {
301 throw new ConstraintException("Group "+name+" does not exist");
302 }
303 int tableCount = groupInfo.getTables().size();
304 if (tableCount > 0) {
305 throw new ConstraintException("Group "+name+" must have no associated tables: "+tableCount);
306 }
307 int serverCount = groupInfo.getServers().size();
308 if(serverCount > 0) {
309 throw new ConstraintException(
310 "Group "+name+" must have no associated servers: "+serverCount);
311 }
312 for(NamespaceDescriptor ns: master.getTableNamespaceManager().list()) {
313 String nsGroup = ns.getConfigurationValue(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP);
314 if(nsGroup != null && nsGroup.equals(name)) {
315 throw new ConstraintException("Group "+name+" is referenced by namespace: "+ns.getName());
316 }
317 }
318 manager.removeRSGroup(name);
319 }
320 }
321
322 @Override
323 public boolean balanceRSGroup(String groupName) throws IOException {
324 ServerManager serverManager = master.getServerManager();
325 AssignmentManager assignmentManager = master.getAssignmentManager();
326 LoadBalancer balancer = master.getLoadBalancer();
327
328 boolean balancerRan;
329 synchronized (balancer) {
330 if (getRSGroupInfo(groupName) == null) {
331 throw new ConstraintException("Group does not exist: "+groupName);
332 }
333
334 Map<String, RegionState> groupRIT = rsGroupGetRegionsInTransition(groupName);
335 if (groupRIT.size() > 0) {
336 LOG.debug("Not running balancer because " +
337 groupRIT.size() +
338 " region(s) in transition: " +
339 StringUtils.abbreviate(
340 master.getAssignmentManager().getRegionStates().getRegionsInTransition().toString(),
341 256));
342 return false;
343 }
344 if (serverManager.areDeadServersInProgress()) {
345 LOG.debug("Not running balancer because processing dead regionserver(s): " +
346 serverManager.getDeadServers());
347 return false;
348 }
349
350
351 List<RegionPlan> plans = new ArrayList<RegionPlan>();
352 for(Map.Entry<TableName, Map<ServerName, List<HRegionInfo>>> tableMap:
353 getRSGroupAssignmentsByTable(groupName).entrySet()) {
354 LOG.info("Creating partial plan for table "+tableMap.getKey()+": "+tableMap.getValue());
355 List<RegionPlan> partialPlans = balancer.balanceCluster(tableMap.getValue());
356 LOG.info("Partial plan for table "+tableMap.getKey()+": "+partialPlans);
357 if (partialPlans != null) {
358 plans.addAll(partialPlans);
359 }
360 }
361 long startTime = System.currentTimeMillis();
362 balancerRan = plans != null;
363 if (plans != null && !plans.isEmpty()) {
364 LOG.info("Group balance "+groupName+" starting with plan count: "+plans.size());
365 for (RegionPlan plan: plans) {
366 LOG.info("balance " + plan);
367 assignmentManager.balance(plan);
368 }
369 LOG.info("Group balance "+groupName+" completed after "+
370 (System.currentTimeMillis()-startTime)+" seconds");
371 }
372 }
373 return balancerRan;
374 }
375
376 @Override
377 public List<RSGroupInfo> listRSGroups() throws IOException {
378 return getRSGroupInfoManager().listRSGroups();
379 }
380
381 @Override
382 public RSGroupInfo getRSGroupOfServer(Address server) throws IOException {
383 return getRSGroupInfoManager().getRSGroupOfServer(server);
384 }
385
386 @InterfaceAudience.Private
387 public RSGroupInfoManager getRSGroupInfoManager() throws IOException {
388 return rsGroupInfoManager;
389 }
390
391 @Override
392 public void removeServers(Set<Address> servers) throws IOException {
393 {
394 if (servers == null || servers.isEmpty()) {
395 throw new ConstraintException("The set of servers to remove cannot be null or empty.");
396 }
397
398
399 synchronized (rsGroupInfoManager) {
400
401 checkForDeadOrOnlineServers(servers);
402 rsGroupInfoManager.removeServers(servers);
403 LOG.info("Remove decommissioned servers " + servers + " from rsgroup done.");
404 }
405 }
406 }
407
408 private Map<String, RegionState> rsGroupGetRegionsInTransition(String groupName)
409 throws IOException {
410 Map<String, RegionState> rit = Maps.newTreeMap();
411 AssignmentManager am = master.getAssignmentManager();
412 RSGroupInfo RSGroupInfo = getRSGroupInfo(groupName);
413 for(TableName tableName : RSGroupInfo.getTables()) {
414 for(HRegionInfo regionInfo: am.getRegionStates().getRegionsOfTable(tableName)) {
415 RegionState state =
416 master.getAssignmentManager().getRegionStates().getRegionTransitionState(regionInfo);
417 if(state != null) {
418 rit.put(regionInfo.getEncodedName(), state);
419 }
420 }
421 }
422 return rit;
423 }
424
425 private Map<TableName, Map<ServerName, List<HRegionInfo>>>
426 getRSGroupAssignmentsByTable(String groupName) throws IOException {
427 Map<TableName, Map<ServerName, List<HRegionInfo>>> result = Maps.newHashMap();
428 RSGroupInfo RSGroupInfo = getRSGroupInfo(groupName);
429 Map<TableName, Map<ServerName, List<HRegionInfo>>> assignments = Maps.newHashMap();
430 for(Map.Entry<HRegionInfo, ServerName> entry:
431 master.getAssignmentManager().getRegionStates().getRegionAssignments().entrySet()) {
432 TableName currTable = entry.getKey().getTable();
433 ServerName currServer = entry.getValue();
434 HRegionInfo currRegion = entry.getKey();
435 if(RSGroupInfo.getTables().contains(currTable)) {
436 if(!assignments.containsKey(entry.getKey().getTable())) {
437 assignments.put(currTable, new HashMap<ServerName, List<HRegionInfo>>());
438 }
439 if(!assignments.get(currTable).containsKey(currServer)) {
440 assignments.get(currTable).put(currServer, new ArrayList<HRegionInfo>());
441 }
442 assignments.get(currTable).get(currServer).add(currRegion);
443 }
444 }
445
446 Map<ServerName, List<HRegionInfo>> serverMap = Maps.newHashMap();
447 for(ServerName serverName: master.getServerManager().getOnlineServers().keySet()) {
448 if(RSGroupInfo.getServers().contains(serverName.getAddress())) {
449 serverMap.put(serverName, Collections.<HRegionInfo> emptyList());
450 }
451 }
452
453
454 for(TableName tableName : RSGroupInfo.getTables()) {
455 if(assignments.containsKey(tableName)) {
456 result.put(tableName, new HashMap<ServerName, List<HRegionInfo>>());
457 result.get(tableName).putAll(serverMap);
458 result.get(tableName).putAll(assignments.get(tableName));
459 LOG.debug("Adding assignments for "+tableName+": "+assignments.get(tableName));
460 }
461 }
462
463 return result;
464 }
465
466 public void prepareRSGroupForTable(HTableDescriptor desc) throws IOException {
467 String groupName =
468 master.getTableNamespaceManager().get(desc.getTableName().getNamespaceAsString())
469 .getConfigurationValue(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP);
470 if (groupName == null) {
471 groupName = RSGroupInfo.DEFAULT_GROUP;
472 }
473 RSGroupInfo RSGroupInfo = getRSGroupInfo(groupName);
474 if (RSGroupInfo == null) {
475 throw new ConstraintException("RSGroup " + groupName + " does not exist.");
476 }
477 if (!RSGroupInfo.containsTable(desc.getTableName())) {
478 LOG.debug("Pre-moving table " + desc.getTableName() + " to rsgroup " + groupName);
479 moveTables(Sets.newHashSet(desc.getTableName()), groupName);
480 }
481 }
482
483 public void cleanupRSGroupForTable(TableName tableName) throws IOException {
484 try {
485 RSGroupInfo group = getRSGroupInfoOfTable(tableName);
486 if (group != null) {
487 LOG.debug("Removing deleted table from table rsgroup " + group.getName());
488 moveTables(Sets.newHashSet(tableName), null);
489 }
490 } catch (ConstraintException ex) {
491 LOG.debug("Failed to perform rsgroup information cleanup for table: " + tableName, ex);
492 } catch (IOException ex) {
493 LOG.debug("Failed to perform rsgroup information cleanup for table: " + tableName, ex);
494 }
495 }
496
497 @Override
498 public void moveServersAndTables(Set<Address> servers, Set<TableName> tables,
499 String targetGroup) throws IOException {
500 if (servers == null || servers.isEmpty() ) {
501 throw new ConstraintException("The list of servers to move cannot be null or empty.");
502 }
503 if (tables == null || tables.isEmpty()) {
504 throw new ConstraintException("The list of tables to move cannot be null or empty.");
505 }
506 moveServers(servers, targetGroup);
507 moveTables(tables, targetGroup);
508 }
509
510 @Override
511 public void close() throws IOException {
512 }
513
514
515
516
517
518 private void checkForDeadOrOnlineServers(Set<Address> servers) throws ConstraintException {
519
520 Set<Address> onlineServers = new HashSet<>();
521 for(ServerName server: master.getServerManager().getOnlineServers().keySet()) {
522 onlineServers.add(server.getAddress());
523 }
524
525 Set<Address> deadServers = new HashSet<>();
526 for(ServerName server: master.getServerManager().getDeadServers().copyServerNames()) {
527 deadServers.add(server.getAddress());
528 }
529
530 for (Address address: servers) {
531 if (onlineServers.contains(address)) {
532 throw new ConstraintException(
533 "Server " + address + " is an online server, not allowed to remove.");
534 }
535 if (deadServers.contains(address)) {
536 throw new ConstraintException(
537 "Server " + address + " is on the dead servers list,"
538 + " Maybe it will come back again, not allowed to remove.");
539 }
540 }
541 }
542 }